PyQGIS – Loading WFS Layer Using PyQGIS

importpyqgiswfs

I'm trying to load a WFS layer in PyQGIS. My WFS uri is for county boundaries here in England. It's open data and pretty light, if you were wanting to recreate the issue.

Here's my code:

uri = 'http://www.geostore.com/OGC/OGCInterface?SERVICE=WFS&UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&VERSION=2.0.0&LC=0'
layer = QgsVectorLayer(uri, "WFS_Layer", "WFS")
if not layer.isValid():
    print "Layer failed to load!"

Sure enough, the layer is not valid.

I'm following (to the best of my limited ability) the QGIS instructions here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html

I'm losing the thread at this part:

enter image description here

Where in the declaration of vlayer is the uri being passed in? I feel I'm missing something fundamental.

Can anyone post working code? It'd be awesome to be able to get PyQGIS and WFS working nicely!

Best Answer

There can be issues with QGIS but the service is also broken. Test with WFS 1.0.0 because it is the simplest of all and the server supports only WFS 1.0.0 and 1.1.0.

The first test is to read the capabilities:

http://www.geostore.com/OGC/OGCInterface?SERVICE=WFS&UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&VERSION=1.0.0&LC=0&request=GetCapabilities

Response reveals that the server is very old MapServer. Select one feature type for DescribeFeatureType:

http://www.geostore.com/OGC/OGCInterface?SERVICE=WFS&UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&VERSION=1.0.0&LC=0&request=DescribeFeatureType&TypeName=ea-wfs-area_public_face_inspire

Nothing odd in the schema. Because there were problems, download the feature type with curl and save it into disk for further research:

curl "http://www.geostore.com/OGC/OGCInterface?SERVICE=WFS&UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&VERSION=1.0.0&LC=0&request=GetFeature&TypeName=ea-wfs-area_public_face_inspire" -o wfs_test.gml

Open the wfs_test.gml file and notice the message that is printed by MapServer on line 15:

<!-- WARNING: FeatureId item 'ogr_fid' not found in typename 'ea-wfs-area_public_face_inspire'. -->

Later you can see the beginning of the first feature:

<gml:featureMember>
<ms:ea-wfs-area_public_face_inspire>

The feature is missing a compulsory "fid" attribute and that's because the source data doesn not have column "ogr_fid" which should be used for generating the fid. You should tell that to the service provider and ask them to upgrade into MapServer 7.0 at the same.

However, you have now the GML on disk and you can open it into QGIS with Add Vector Layer (tested with QGIS 2.10). You can also try to convert GML data into some other format with ogr2ogr but because of the NULL fids it will fail for example with Spatialite output because the database table is also created with NOT NULL constraint. Conversion into shapefile goes well.

ogr2ogr -f "ESRI Shapefile" wfs_test.shp wfs_test.gml
Related Question