I have the following WFS link:


I want to read and store the data contained in this link using PyQGIS so that it could be automated. Initially to test it

  • I opened QGIS v3.14.15-Pi
  • Went to Layer->Add Layer->Add WFS Layer->New
  • Put in the name with the URL
  • In WFS Options->Version I clicked detect which results in 2.0.0 (have tried all the other options as well)
  • Set 100 (as well as empty) as the page size
  • Clicked Ok and Connect
  • It shows two layers named ms:SNPP_NE and ms:MODIS_NE
  • I added both layers and as it turns out, only the former shows some data

This confirmed that the WFS link does expose some data

After this I decided to use PyQGIS to run a script to add this WFS layer programmatically. Here is what I tried:

import os
from qgis.core import QgsVectorLayer

path_to_airports_layer = "http://118.abc.xyz.ef:90/cgi-bin/mapserv.exe?map=/ms4w/amc/wfs_data.map"
vlayer = QgsVectorLayer(path_to_airports_layer, "ms:SNPP_NE", "WFS")
#if not vlayer.isValid():
#    print("Layer failed to load!")


#import os
#import urllib
#params = {
#    'service': 'WFS',
#    'version': '2.0.0',
#    'request': 'GetFeature',
#    'typename': 'ms:SNPP_NE',
#    'srsname': "EPSG:3857"
#uri2 = 'http://118.abc.xyz.ef:90/cgi-bin/mapserv.exe?map=/ms4w/amc/wfs_data.map'
#vlayer = QgsVectorLayer(uri2, "my wfs layer", "WFS")

Several attempts at trying to add the above WFS layer resulted in the compiler indicating that the layer was invalid (which begs the question of how QGIS was able to load it in the first place). I even tried using the Processing->Graphical Modeler tool in QGIS hoping to generate the PyQGIS script using a model.

In order to do that, under Algorithms, I chose Modeler Tools->Load layer into project, put in the WFS layer URL and clicked ok. But running the model failed as seen below

model run

I even tried to access the GetCapabilities xml/gml document directly from the browser with the following results:

get capabilities fetch

For the above links I referred to the MapServer WFS documentation. What I wish to do is to somehow obtain the data exposed by the WFS service and store it in some form maybe a GeoJSON or even in PostgreSQL. But all my efforts have thus far been fruitless.

  • 1
    msLoadMap(): Regular expression error. MS_DEFAULT_MAPFILE_PATTERN validation failed. you got this error because you used incorrect syntax in the URL; you have ...map?service.. but you already specified the beginning of the query string ? at mapserv.exe?map so you need instead an ampersand ...map&service.. – nmtoken Nov 3 '20 at 9:07
  • 1
    mapserv(): Web application error. Traditional BROWSE mode requires a TEMPLATE in the WEB section, but none was provided. You got this error because your URL is incomplete, you have omitted the service parameter service=WFS&, the request should be – nmtoken Nov 3 '20 at 9:16
  • Thank you so much. I understand now that the problem was with the way I had written the url. – Siddhartha Bhuyan Nov 3 '20 at 9:21

You have to speficy more than just the url in the layer source. This is an example of what could work for you. You should at least specify the typename, url and version:

source = "maxNumFeatures='20000' pageSize='1000' pagingEnabled='true' srsname='EPSG:{crsId}' typename='ms:MODIS_NE' url='' version='2.0.0'"

vlayer = QgsVectorLayer(source, 'layername', 'WFS')

Also, if adding it manually in the interface worked for you then you can check the source in the layer information and just initialize the layer in PyQGIS with the same source.

enter image description here

  • Thank you so much :) It solved the problem. Looks like I was wrongly forming the url while retrieving the GetCapabilities doc from the browser. Additionally the parameters passed to the uri in the PyQGIS code didn't account for the typename (though I did try adding it through the graphical modeler) – Siddhartha Bhuyan Nov 3 '20 at 9:25

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.