0

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

  • 1
    What kind of filters do you want to use with GetFeatures? Simple bbox ones or something more advanced? – user30184 Dec 14 '18 at 19:34
  • Well, for now simple bbox will be just fine but ideally I'll make something that will work with advanced filters as well. At present I am building my GetFeature requests with OpenLayers API. Indeed, I didn't think how to pass those to ogr2ogr function to make the conversion. – umbe1987 Dec 14 '18 at 20:03
  • Anyway, to clarify let's say I discover a way to intercept the GetFeature requests I make with OL (I am using readFeatures() method from ol/format/WFS to build them dinamically), I would then pass them to ogr2ogr with one of the scenarios I showed to be able to convert the GML3 response to SHP, using the wfs_driver (gdal.org/drv_wfs.html) – umbe1987 Dec 14 '18 at 20:29
1

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://www.gdal.org/drv_wfs.html.

  • Thanks for the comprehensive answer. Also, really appreciate your examples. Although my question was more about whether to use QGIS SERVER itself (Scenario 1), or ogr2ogr with PHP exec() or nodejs repo (Scenario 2, 3), I think I'll accept it anyway because it gave me good reason to use ogr2ogr. I wonder if I could combine OpenLayers writeGetFeature() with ogr2ogr, but this is another story... Thanks! – umbe1987 Dec 17 '18 at 8:22
  • Do you know if I can use the bbox optional parameter of the WFS standard directly in the WFS URL instead of the -spat ogr option? Something like 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=GetFeature&typename=domini_sciabili&bbox=544138,5098446,564138,5108446 – umbe1987 Dec 17 '18 at 13:21
  • Just tested: yes it does! Wonderful!!! Tried with ogr2ogr -f "ESRI Shapefile" 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=GetFeature&typename=domini_sciabili&bbox=544138,5098446,564138,5108446" – umbe1987 Dec 17 '18 at 16:09
0

An easy way to do this might be to use GeoServer as an intermediary to proxy the remote WFS and then just request SHAPE-ZIP as the output format of the GeoServer WFS request.

  • @IanTutrton Thanks for the feedback, but I'd like to avoid installing and configuring such a component only to convert formats. If there's no real reason not to, I'd like to possibly stick with one of the scenarios I've mentioned. Of course I am open to altenatives, but yours seems a bit too much for me, as I don't really know Geoserver. Thank you for the suggestion though, I really appreciate your help. – umbe1987 Dec 14 '18 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.