pyQGIS fetch WFS manually

Sometimes I only want to fetch a small amount of data from WFS. While this is easily possible from the direct WFS interface in QGIS its more difficult from a python script:
In this example I set a bbox and download the WFS xml file directly from python. The resulting xml file file can be loaded directly as an "OGR" layer
bbox_min_lon=0
bbox_max_lon=10
bbox_min_lat=0
bbox_max_lat=10
xmlconnect=urllib.urlopen('http://wfs.geoserver.com/wfs?service=WFS&version=1.1.0&request=GetFeature&typename=FEATURENAME&filter=<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><ogc:BBOX><ogc:PropertyName>GEOMETRY</ogc:PropertyName><gml:Envelope><gml:lowerCorner>'+str(bbox_min_lat)+' '+str(bbox_min_lon)+'</gml:lowerCorner><gml:upperCorner>'+str(bbox_max_lat)+' '+str(bbox_max_lon)+'</gml:upperCorner></gml:Envelope></ogc:BBOX></ogc:Filter>')
xmlbuffer = xmlconnect.read()
xmlconnect.close()
print "saving temporary xml file"
f = open('geoserver-GetFeature.xml', 'w')
f.write(xmlbuffer)
f.close()

Leave a Reply

Your email address will not be published. Required fields are marked *


four + = 7