Using osgeo's Python API for OGR, I'd like to implement the following process:

  1. Import ESRI Shapefiles from a given directory (call it input_dir)
  2. Import a CSV file that contains lat-longs of interest (call it poicsv)
  3. Join the CSV file long, lats with the Shapefile layer and extract the feature attribute values for that long, lat

I am currently up to the step where I have a single Shapefile loaded, and the CSV file loaded with long/lats in a list of tuples (ie. [(-123.76073, 41.24946), (-123.84451, 41.24946), ...]). Per long lat, I am trying to use SetSpatialFilter on the layer by the following code:

for ll in longlat:
    point = ogr.Geometry(ogr.wkbPoint)
    point.AddPoint_2D(ll[0], ll[1])
    for feat in layer:

However, upon sanity checking I've noticed there is no API for removing the Spatial Filter in my for loop, and I am also concerned about the memory leak created by creating a wkbPoint through the ogr.Geometry function in every iteration. Will the point object be cleaned up every iteration, and how could I reset the Spatial Filter every iteration?

1 Answer 1


Your point geom will get cleaned up automatically by Python's garbage collector each iteration after it goes out of scope. I just tested by running a 10 million iteration loop and creating a point and overwriting the same variable and memory usage stayed the same.

According to the documentation, you can reset your spatial filter by passing a NULL value (None in Python), i.e.


Though I don't think you actually need to as you're overwriting the existing spatial filter with a new one each iteration.

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.