I'm looking at Selecting those Polygons in One Shapefile which are contained inside the Polygon which is stored in another Shapefile.

I have managed to make some progress, and I have modified the sample given here: https://pcjericks.github.io/py-gdalogr-cookbook/layers.html#spatial-filter

I'm trying to figure out exactly what this function does, but the documentation says that this might give incorrect results.

What the correct way to apply a SpatialFilter on a Layer using GDAL & Python?

1 Answer 1


Using ogr2ogr (gdal.VectorTranslate) as library function:

from osgeo import gdal

ds_in = gdal.OpenEx('/path/to/input.shp')
ds_out = gdal.VectorTranslate('/path/to/output.shp', ds_in,
                             format = 'ESRI Shapefile',
                             spatFilter = [479609, 4764629, 479764, 4764817])

where spatFilter is the spatial filter as (minX, minY, maxX, maxY) bounding box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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