Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to figure out how to do get requests in QGIS using a bbox. I can get a bbox to clip a layer when I paste my uri into a browser but the EXACT same uri returns an empty layer in QGIS.

A simple easy to try specific example: The following uri returns 4 features when pasted into my browser.

http://ogi.state.ok.us/geoserver/wfs?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=ogi:quad100&BBOX=-100,37,-99,38,EPSG:4326&SRSNAME=EPSG:4326

However the following lines create an empty layer (either pasted in to the console or run as a script)

sourceUri = "http://ogi.state.ok.us/geoserver/wfs?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=ogi:quad100&BBOX=-100,37,-99,38,EPSG:4326&SRSNAME=EPSG:4326"
vlayer = QgsVectorLayer(sourceUri, "QUADS", "WFS")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

Interesting to note that without the crsurl I need to swap the lat/lon order to correctly clip data in the browser. with or without the crsurl and which ever lat/lon order I use in QGIS I still get an empty layer.

As far as I can tell my uris are correct (They certainly generate a correct result in a browser) So why Doesn't QGIS get any data? It would be a shame to have to write my own HTML/XML post/parse script in python just because I'm missing some small aspect of QGIS

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Depending on how you look at it, the reason is either a "bug" or "expected behaviour" bug report

My workaround was to use only version 1.0.0 and set bbox there. Its important to note that the map extents need to be zoomed in to the desired bbox before the call to the wfs. To do this I created a dummy layer and zoomed to it. It may be faster to zoom directly to the region without a dummy layer but I'm going to be clipping my data with the polygon from that layer layer anyhow. Using "intersects" in the get request may have been faster than that but I couldnt find the syntax for a GET request using it.

Also please note: ALL the CRS need to be the same INCLUDING the native crs of the project.

#Set the bbox values
bbox_min_lon=-100
bbox_min_lat=37
bbox_max_lon=-99
bbox_max_lat=38

#create a dummy bbox layer
bboxlayer= QgsVectorLayer("Polygon?crs=epsg:4326", "bbox_polygon", "memory")
bbl_dp = bboxlayer.dataProvider()
bboxlayer.startEditing()
bbox_feature = QgsFeature()
bbox_feature.setGeometry(QgsGeometry.fromPolygon( [ [ \
QgsPoint(bbox_min_lon,bbox_min_lat),\
QgsPoint(bbox_min_lon,bbox_max_lat), \
QgsPoint(bbox_max_lon,bbox_max_lat),
QgsPoint(bbox_max_lon,bbox_min_lat) ] ] ))
bboxlayer.dataProvider().addFeatures([bbox_feature])
bboxlayer.commitChanges()
QgsMapLayerRegistry.instance().addMapLayer(bboxlayer)

#Zoom to bbox layer
qgis.utils.iface.setActiveLayer(bboxlayer)
qgis.utils.iface.zoomToActiveLayer()

#Finally get WFS layer ubing bbox
sourceUri = "http://ogi.state.ok.us/geoserver/wfs?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=ogi:quad100&SRSNAME=EPSG:4326&BBOX="+str(bbox_min_lon)+","+str(bbox_min_lat)+","+str(bbox_max_lon)+","+str(bbox_max_lat)
vlayer = QgsVectorLayer(sourceUri, "inputlayer", "WFS")
QgsMapLayerRegistry.instance().addMapLayer(vlayer) 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.