I've created a layer in GeoServer. The layer is based on a SQL view and is parametrized as described in the documentation. I can access the WFS in the browser with a URL something like the below:
http://localhost:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=cite:mylayer&maxFeatures=50&outputFormat=application%2Fjson&viewparams=myuuid:2a18bb306af24e29a8f1a40f80229749;myyear:2016
(Note the viewparams
part at the end of the query string.)
But I don't see a way to set these parameters in QGIS when I'm adding the layer. I can connect successfully and find my layer in the list, but there doesn't seem to be a way to configure these parameters:
How can I set these parameters? Does the Build query functionality allow me to via some mechanism I'm not aware of?
1 Answer 1
with python 2.x in Qgis 2.14.x and 2.18.x, you can use params in url like this:
uri='http://path/wfs?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=workspace:layer&SRSNAME=EPSG:xxx&viewParams=param1:valueparam'
layer = QgsVectorLayer(uri, "layer_wfs_with_params", "WFS")
if not layer.isValid():
print "Layer failed to load!"
else:
QgsMapLayerRegistry.instance().addMapLayer(layer)
in qgis 3.x, with python 3.x
import urllib
params ={
'service':'WFS',
'version':'1.1.0',
'request':'GetFeature',
'typename':'somelayer',
'srsname':"EPSG:xxx",
'user':'a',
'password':'',
# 'cql_filter':cql_filter,
'viewParams':"param1:valueparam"
}
uri="http://path/wfs?'+urllib.parse.unquote(urllib.parse.urlencode(params))
layer= QgsVectorLayer(uri, 'layer_wfs_with_params',"WFS")
if not layer.isValid():
print ("Layer failed to load!")
else:
QgsProject.instance().addMapLayer(layer)
bye.
-
Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. A good answer should include not only what to do, but also how to do it. Please edit your answer to include an explanation about how to use your suggested solution, and what it does.2017年05月24日 14:55:05 +00:00Commented May 24, 2017 at 14:55
http://localhost:8080/geoserver/wfs?VIEW_PARAM=my_param&
. This may work if QGIS does not use the GetFeature URL that comes with the GetCapabilities. Even if it happens to work you can't make it dynamic but you must create a new connection for each VIEW_PARAM value that you will need.