Asset Publisher

Exporting LPIS parcels from WFS to Spatialite with QGIS and ogr2ogr


 

Exporting LPIS parcels from WFS to Spatialite with QGIS and ogr2ogr

This tutorial will guide you through the process of exporting data from a Web Feature Service (WFS) to a SpatiaLite database using QGIS, a popular Geographic Information System (GIS) application, and OGR, a geospatial data conversion library. As an example WFS we will use the WFS service from the Andalusian Autonomous Community, but you can repeat this process with any other WFS service.

Prerequisites

  1. Have QGIS installed on your computer.
  2. Have basic knowledge of GIS and handling of QGIS.
  3. Access to a WFS service, you will need the URL of the Feature service, in our example it is http://www.geoportalagriculturaypesca.es/geoide/sigpac/wms.

Step 1: Setting Up the WFS Connection in QGIS

1.    Open QGIS: Start the QGIS application on your computer.
2.    Add WFS Connection:

  • Go to Layer > Add Layer > Add WFS Layer > New.
  • Enter the URL of the WFS service:  http://www.geoportalagriculturaypesca.es/geoide/sigpac/wms.
  • Name the connection and click OK.

3.    Load WFS Layer:

  • With the WFS connection configured, select the layer you wish to export.
  • Click on Connect and then Add.

After this, you will have an entry for this service in the WFS connections panel and you can expand it to see what layers are available:

You can drag one of them to visualize the geometries of the precincts or polygons of this Community.

Step 2: Exporting the WFS Layer to Spatialite

If the layer you are going to export has a small number of records, you can export it directly, but in our case, we are going to filter it beforehand to keep the precincts of a single municipality, for example, Andújar (province 23 and municipality 5).

1. Filter the layer from QGIS

  • Once the WFS layer is loaded, right-click on it in the layer panel
  • Select "Filter" and enter this expression: "CD_PROV" = 23 and "CD_MUN" 5
  • Press the accept button and QGIS will start downloading the results.

2. Export the layer  with QGIS:

  • With the WFS layer filtered, right-click on it in the layer panel.
  • Select Export > Save As....
  • In the dialog that appears, select Format: Spatialite.
  • Choose the file location and name it.
  • Configure the export options as needed (CRS, export range, etc.).

  • Click OK.

3. Verification:
Once the export is completed, you can open the Spatialite file in QGIS to verify that the data has been exported correctly. For this, you only have to drag the file to the layer panel.

Step 3: Advanced Use with OGR (Optional)

If you need more control over the export or if you are working with scripts, you can use OGR directly from the command line. The ogr command can be found in the /bin directory of the QGIS installation.

In the ogr2ogr command, we can pass directly the query we want to execute to extract the data from the WFS.

select * from SIGPAC20_RECINTOS where CD_PROV = 23 and CD_MUN =5

Keep in mind that the GDAL WFS driver is converting this query to a WFS filter, so not all operations or SQL statements are supported. You can find more information about the GAL WFS driver here: https://gdal.org/drivers/vector/wfs.html.

Open a Windows command prompt and first we are going to activate the gdal debug level to see what the command is doing:

set CPL_DEBUG=ON

In the same console, then execute this:

ogr2ogr -f SQLite -dsco SPATIALITE=YES d:/tmp/andujar.sqlite WFS:"http://www.geoportalagriculturaypesca.es/geoide/sigpac/wms?service=WMS&request=GetCapabilities" -sql " select * from SIGPAC20_RECINTOS where CD_PROV = 23 and CD_MUN = 5" -nlt POLYGON

The client will make requests and download the precincts in a paginated way, you can speed up the download by adding a spatial filter with the coordinates of the area that contains the entities of interest:

-spat -spat <xmin> <ymin> <xmax> <ymax>

https://gdal.org/programs/ogr2ogr.html

The command will make numerous requests to download the information in a paginated manner until it completes the entire area of the spatial filter.

Now you have learned how to export data from a WFS service to a Spatialite database using QGIS and OGR. This process is essential for integrating geospatial data into your GIS projects and for performing more advanced and personalized analysis.

Additional Resources