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
    from osgeo import ogr, gdal
    sys.exit('ERROR: cannot find GDAL/OGR modules')

# Set the driver (optional)
driver = ogr.GetDriverByName('WFS')
url = ''
wfs_ds = driver.Open('WFS:' + url)
if not wfs_ds:
    sys.exit('ERROR: can not open WFS datasource')

# 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?

Tags : python gdal wfs ogr

Related Questions

python GDAL translate to convert and coloring

Updated March 03, 2018 02:22 AM

WMS Tutorial on Python Gdal

Updated February 28, 2019 14:22 PM

Module has no attribute Warp?

Updated July 28, 2017 20:22 PM

gdal.Warp workingType option default

Updated August 24, 2018 15:22 PM