Use GDAL/OGR python (3.6) to connect to a wfs?

by Niels Jonas Enok Olsen   Last Updated July 11, 2018 12:22 PM

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:

http://kystatlas.kyst.dk/arcgis/services/ekstern/KDI_Bypass/MapServer/WFSServer?SERVICE=WFS&VERSION=1.1.0&REQUEST=GetCapabilities

http://wfs2-miljoegis.mim.dk/havbrug/ows?

https://arealinformation.miljoeportal.dk/gis/services/puls/MapServer/WFSServer?request=GetCapabilities&service=WFS

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?

Tags : python gdal wfs ogr


Related Questions


python GDAL translate to convert and coloring

Updated March 03, 2018 02:22 AM

Module has no attribute Warp?

Updated July 28, 2017 20:22 PM

gdal.Warp workingType option default

Updated August 24, 2018 15:22 PM


What resampling method is used in gdal_merge?

Updated June 22, 2015 22:09 PM