Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active September 24, 2024 07:53
Show Gist options
  • Save ThomasG77/aea651a8d8930aa52578f532de9593f5 to your computer and use it in GitHub Desktop.
Save ThomasG77/aea651a8d8930aa52578f532de9593f5 to your computer and use it in GitHub Desktop.
Download data from WFS using GDAL Python bindings
from osgeo import gdal
gdal.UseExceptions()
gdal.SetConfigOption('CPL_DEBUG', 'ON')
with gdal.config_options({
'OGR_WFS_PAGING_ALLOWED': 'ON',
'OGR_WFS_PAGE_SIZE': '250'
}):
ds = gdal.OpenEx('WFS:https://data.geopf.fr/wfs/wfs', gdal.OF_VECTOR)
print(ds)
'''
for i in range(ds.GetLayerCount()):
layer = ds.GetLayerByIndex(i)
srs = layer.GetSpatialRef()
print('Layer: %s, Features: %s, SR: %s...' % (layer.GetName(), layer.GetFeatureCount(), srs.ExportToWkt()[0:50]))
'''
# Full dataset
gdal.VectorTranslate(
'demo.gpkg',
ds,
options='-f GPKG -nln communes ADMINEXPRESS-COG-CARTO.LATEST:commune'
)
# Filtered using bbox with -spat option (escaped negative coordinates)
gdal.VectorTranslate(
'demo.gpkg',
ds,
options='-f GPKG -update -nln com_bbox1 -spat \-0.1 46.6 0.2 46.9 ADMINEXPRESS-COG-CARTO.LATEST:commune'
)
# Filtered using bbox with -spat option (escaped negative coordinates)
# Output CSV to stdout
gdal.VectorTranslate(
'/vsistdout/',
ds,
options='-f CSV -nln com_bbox1 -spat \-0.1 46.6 0.2 46.9 ADMINEXPRESS-COG-CARTO.LATEST:commune'
)
# Filtered using bbox with -spat option (escaped negative coordinates)
# Output CSV to file
gdal.VectorTranslate(
'demo.csv',
ds,
options='-f CSV -nln com_bbox1 -spat \-0.1 46.6 0.2 46.9 ADMINEXPRESS-COG-CARTO.LATEST:commune'
)
# Make http calls that return GeoJSON instead of WFS (lighter but not supported by all WFS services)
ds = gdal.OpenEx('WFS:https://data.geopf.fr/wfs/wfs?outputFormat=json', gdal.OF_VECTOR)
# Filtered using bbox with -spat option (escaped negative coordinates)
# Output GeoJSON to file
gdal.VectorTranslate(
'demo.geojson',
ds,
options='-f GeoJSON -nln com_bbox1 -spat \-0.1 46.6 0.2 46.9 ADMINEXPRESS-COG-CARTO.LATEST:commune'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment