I'm writing a script that is suppose to connect to several WFS, and download them to a disk. Currently, im trying the GDAL/OGR modules in python to do this. OGR is quite new to me, but i have followed Get WFS layers and iterate over features which seems quite thorough. My code is very similar to the one in the link. The lines i have removed from the original, were tested and did not have any effect on the script.
import sys try: from osgeo import ogr, gdal except: sys.exit('ERROR: cannot find GDAL/OGR modules') # Set the driver (optional) driver = ogr.GetDriverByName('WFS') url = 'http://kystatlas.kyst.dk/arcgis/services/ekstern/KDI_Bypass/MapServer/WFSServer?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetCapabilities' wfs_ds = driver.Open('WFS:' + url) if not wfs_ds: sys.exit('ERROR: can not open WFS datasource') else: pass # iterate over available layers for i in range(wfs_ds.GetLayerCount()): layer = wfs_ds.GetLayerByIndex(i) srs = layer.GetSpatialRef() print ('Layer: %s, Features: %s, SR: %s...' % (layer.GetName(), layer.GetFeatureCount(), srs.ExportToWkt()[0:50])) # iterate over features feat = layer.GetNextFeature() while feat is not None: feat = layer.GetNextFeature() # do something more.. feat = None
The code always breaks at "if not wfs_ds", as the error is always "ERROR: can not open WFS datasource". I have tried several WFS urls, including:
All the urls have been tried manually in QGIS 3.2 and work fine there.
Is there some other option that is needed to connect to a wfs through the osgeo.ogr module?