GDAL – Convert WFS GetFeature GML to Shapefile Using ogr2ogr in QGIS Server

gdalogrogr2ogrqgis-serverwfs

I'd like to use ogr2ogr with the WFS driver to convert the GML response of my QGIS Server 3.4.2 GetFeature layer into Shapefile. I have to do this from a WebGIS JavaScript application I am developing, via (e.g.) a click event on a "Download" button.

First of all, is there a way to do this with QGIS Server itself without implementing this functionality myself (I cannot see any outputFormat for Shapefile)?

If this Scenario (let's call it Scenario 1) is not possible, I thought about two alternatives, and I'd like to ask which should be the best.

  • Scenario 2: using PHP exec() like explained here
  • Scenario 3: using the node.js ogr2ogr wrapper found in this repo

Here is the GetCapabilities of my WFS: https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?&map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetCapabilities

Best Answer

Check the service with ogrinfo

ogrinfo WFS:"https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?&map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetCapabilities"

Result:

...
Metadata:
  PROVIDER_NAME=Wondermap
  TITLE=Impianti sciistici Lombardia
1: domini_sciabili (Multi Polygon)
2: impianti_risalita (Multi Line String)
3: piste_sci (Multi Line String)

Find summary of one layer

ogrinfo WFS:"https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?&map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetCapabilities" domini_sciabili -so

Result

Metadata:
  PROVIDER_NAME=Wondermap
  TITLE=Impianti sciistici Lombardia

Layer name: domini_sciabili
Metadata:
  TITLE=domini sciabili
Geometry: Multi Polygon
Feature Count: 38
Extent: (526875.694200, 5096641.169516) - (615063.094113, 5155811.7
Layer SRS WKT:
PROJCS["WGS 84 / UTM zone 32N",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",9],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["Easting",EAST],
    AXIS["Northing",NORTH],
    AUTHORITY["EPSG","32632"]]
Geometry Column = geometry
gml_id: String (0.0) NOT NULL
Id: Integer (0.0)
CODICE: String (0.0)
SOSTENIBIL: String (0.0)
COMUNE: String (0.0)
DENOMINAZ: String (0.0)
FONTE: String (0.0)
STATO: String (0.0)

Get features with BBOX and save into shapefile

ogr2ogr -f "ESRI Shapefile" -spat 544138 5098446 564138 5108446 wfs_test.shp WFS:"https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?&map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetCapabilities" domini_sciabili

Check what you got

ogrinfo wfs_test.shp -so

INFO: Open of `wfs_test.shp'
      using driver `ESRI Shapefile' successful.

Layer name: wfs_test
Metadata:
  DBF_DATE_LAST_UPDATE=2018-12-16
Geometry: Polygon
Feature Count: 1
Extent: (543944.462085, 5096641.169516) - (544546.601519, 5098546.684997)
Layer SRS WKT:
PROJCS["WGS 84 / UTM zone 32N",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",9],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["Easting",EAST],
    AXIS["Northing",NORTH],
    AUTHORITY["EPSG","32632"]]
gml_id: String (80.0)
Id: Integer (9.0)
CODICE: String (80.0)
SOSTENIBIL: String (80.0)
COMUNE: String (80.0)
DENOMINAZ: String (80.0)
FONTE: String (80.0)
STATO: String (80.0)

For attribute queries use ogr2ogr with -where or -sql as documented https://www.gdal.org/ogr2ogr.html. I suggest to read also the WFS driver manual https://gdal.org/drivers/vector/wfs.html.

Related Question