1

I'm trying to write a Script, which will download an entire WFS layer to Disk. The output format could be GeoJSON, Spatilite or even Shapefile.

I know how to do this using ogr2ogr, but I need to use the Python Bindings, since this is part of a larger Python script.

How do I download the entire layer using the GDAL bindings?

I've managed to write the following code:

import sys
from osgeo import ogr
driver=ogr.GetDriverByName('WFS')
url=sys.argv[3]
layerName=sys.argv[4]
wfs=driver.Open('WFS:'+url)
layer=wfs.GetLayerByName(layerName)

#I'm stuck here
#I need to save this entire Layer to disk, in any format that GDAL understands
4
  • 1
    What did try so far? pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html describes CopyLayer for example to save geodata from one format to another (see: Load data to memory). Aug 25, 2016 at 7:30
  • Be aware that WFS usually has a limit to the number of features you can get in a single call. You might need to "paginate" through results. Aug 25, 2016 at 8:55
  • @bugmenot123: In my tests, using the accepted answer, files were saved with over 5000+ records. Even the Docs for CopyLayer says that all features will be duplicated. Aug 25, 2016 at 9:19
  • Yeah, it depends on the server. Iirc ArcGIS Server is really low, in the FOSS world it is in the tens of thousands. Still easy to reach if you find a nice WFS with interesting data. ;) CopyLayer will copy all you got, but the WFS might have just served you a subset. Aug 25, 2016 at 9:59

1 Answer 1

5

You can save the Data to GeoJSON by using CreateDataSource and CopyLayer function. Here is some sample code:

dr = ogr.GetDriverByName( 'GeoJSON' )
ds = dr.CreateDataSource( 'test.json' )
ds.CopyLayer(layer, 'local_copy')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.