I have the following WFS link:
http://118.abc.xyz.ef:90/cgi-bin/mapserv.exe?map=/ms4w/amc/wfs_data.map
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")
#print(vlayer)
#if not vlayer.isValid():
# print("Layer failed to load!")
#
#else:
#QgsProject.instance().addVectorLayer(path_to_airports_layer,"ms:SNPP_NE","ogr")
iface.addVectorLayer(path_to_airports_layer,"ms:SNPP_NE","WFS")
#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")
#QgsProject.instance().addMapLayer(vlayer)
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
I even tried to access the GetCapabilities xml/gml document directly from the browser with the following results:
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.
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?
atmapserv.exe?map
so you need instead an ampersand...map&service..
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 parameterservice=WFS&
, the request should behttp://117.239.115.44:90/cgi-bin/mapserv.exe?map=/ms4w/fsi_fire/wfs_NE.map&request=GetCapabilities&service=WFS&