How to use WFS in QGIS
Now and again we get the question of how to use WFS data in QGIS. Here we want to provide an easy all-in-one guidance on the topic for those who are not yet familiar with WFS.
In the article you will find the following:
- What is Web Feature Service (WFS)
- How to connect to WFS from QGIS
- How to make SQL queries to WFS to filter data
- How to add a layer from WFS
- How to save data from WFS locally
What is WFS?
Very simply put Web Feature Service (WFS) is an interface for sharing geographical features over the internet. WFS is a platform-independent format, meaning that it can be used with a wide variety of different software.
The basic WFS allows one to query features and retrieve them from the source, i.e. ask what features exist and load the desired features for one’s own use. With a WFS-T (Transactional Web Feature Service) the user can additionally create, delete and update original features in the source.
Geographical features can be, for instance, lines or polygons together with their related information (e.g. coordinates and different type attributes). Geographical features allow for versatile editing and spatial analysis. This is a notable difference between WFS and services made for transferring map images, like tiled maps or Web Map Service (WMS).
To read more about the WFS as an international standard, visit the OGC website.
How to connect to WFS from QGIS
To open a connection to a WFS server, open a new project and click the “Open Data Source Manager” button . In the Data Source Manager window select “WFS / OGC API Features” from the left-hand menu. Click on “New” to create a new connection.
If you want to add a layer to an existing project, go to Layer > Add Layer > Add WFS layer.
In the window that opens fill in the “Name” (something useful for you to remember what the connection is about) and the “URL” for the WFS server. If no authentication is needed, click “OK”.
The available datasets should appear as a list. Now you can select the dataset you want to add to your project and click “Add”.
In this example, we added a population grid dataset for Finland in 2021, which now appears as a layer in the QGIS project.
By looking at the attribute table of the layer, we see that each 5×5 km cell has its own row with data included: coordinates, municipality code (kunta), population (vaesto), men (miehet), women (naiset) and population according to age groups (ika). This is the magic of WFS: so much data available for every feature!
Querying WFS with SQL from QGIS
Sometimes the datasets are very large and you only need certain parts of them. In these cases you can make a query to the WFS data already before loading it. This can save time and, later if you want to save the data locally, also disk capacity.
Let’s say that – in the Finnish population dataset – we are only interested to know where the hotspots of the population over 65 years of age are. We define a hotspot to be where there are over 1000 elderly people living in a 5×5 km statistical square. To do this, we select the dataset we’re interested to filter (query) and click on “Build query”.
The SQL Query Composer opens and we can use it to filter the data according to our preference. The composer helps by providing e.g. the available columns in the datasets. It is also possible to make joins of different datasets using the composer. However, building a query does require a bit of SQL knowhow.
In our example we select all columns (SELECT *) from table vaki2021_5km and include rows fulfilling the condition that (WHERE) the population over 65 years is greater than 1000 (ika_65 > 1000). When we click OK, a new layer appears in the project with only the wanted data included.
The data on the map does look different from when we loaded the full dataset (a background map was added to make it visually easier to read). If we change our mind about the query, it can be easily modified by clicking on the little funnel symbol which appears next to the filtered layer name in the Layers panel. This re-opens the Query Composer.
Save the WFS layer locally
If you want to make a local copy of the data loaded from the WFS, it can be done by exporting the WFS layer to a GeoPackage. This process requires some attention as you want to be sure not to accidentally export large amounts of useless data. Filtering by query (as described above) is a good way to start, but sometimes we can additionally limit the data by its geographic extent.
To only save the needed data, start by either zooming into the area you are interested in, or by selecting the features you want to save (from the WFS layer). Once you have done one of the two, click on the right mouse button over the layer name and click on “Export” > “Save features as…”.
The best way to save the data is as GeoPackage. Give a name to the file (best done by clicking the button with three dots at the end of the text field and giving the full path). Also give a name to the layer and make sure that the coordinate system (CRS) is what it is supposed to be.
Then, especially when the WFS dataset you’re dealing with is large, the following is important! To filter out anything outside of our map view we tick the box “Extent” and click on “Map Canvas Extent”. This way you only save the relevant selection of the potentially very big amount of data available in the WFS dataset. However, if the dataset is small or if you just happen to need the whole dataset and don’t want to limit anything, then don’t worry about the extent.
Finally, if you want to add the layer to your map, tick the box “Add saved file to map” and then just OK.
Wow! Now you know how to connect to a WFS, how to filter out the data you need and how to save it locally in your own environment for further use. You’re ready to explore different Finnish (list by Gispo) and international (site by Spatineo) WFS sources to find some interesting data to play with.
If you want to dive into e.g. spatial analysis with QGIS or building your own database for geographic data, check our courses here!
This article is written by Linda Talve.