7

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!

  • I ran your code as is and with the string literal r for your URI. It worked for me both ways (returned True for layer.isValid()). As for the rest of your request, what have you tried? – Branco Aug 19 '15 at 19:38
  • I wonder why it works for you and not me. My only hunch at the moment is router settings. I haven't gotten any further with the rest of the task, first job is to get a connection. – Ben Mayo Aug 19 '15 at 19:48
5
+50

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
3

You should be able to form a 'get capabilities' request using your browser too, something like this:

https://www.geostore.com/OGC/OGCInterface?SERVICE=WFS&UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&VERSION=2.0.0&LC=0&request=getcapabilities

I tested that and it works in the browser, but it doesn't in QGIS. The error message said something about WFS version 1.0, so I removed your version specification, and now it does work:

https://www.geostore.com/OGC/OGCInterface?UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&LC=0

enter image description here

So that then leads me to your code, and I wonder if you actually did just leave the example in. Try something like this:

uri = 'https://www.geostore.com/OGC/OGCInterface?UID=UDATAGOV2011&PASSWORD=datagov2011&INTERFACE=ENVIRONMENTWFS&LC=0&SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=ea-wfs-aqueducts_inspire&SRSNAME=EPSG:27700'
layer = QgsVectorLayer(uri, "WFS_Layer", "WFS")
if not layer.isValid():
    print "Layer failed to load!"

Note the addition of the 'TYPENAME' specifying a single layer.

But these layers do not work in QGIS, it raises an error in the log stating 'describefeaturetype failed for url...'. I would perhaps try this in another GIS (MapInfo? ArcGIS?). In fact, you can add the layer using the URL I specified above in the NationalMap http://nationalmap.gov.au/. So it does work. This probably needs to be referred to the QGIS Developers at http://hub.qgis.org/.

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.