Skip to content

Instantly share code, notes, and snippets.

@rigid
Created June 29, 2015 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rigid/691eddb1e3c3b0aa7d3d to your computer and use it in GitHub Desktop.
Save rigid/691eddb1e3c3b0aa7d3d to your computer and use it in GitHub Desktop.
sequential check if single point is within polygon VS multipoint intersection with polygon
# ------------------------------------------------------------------------------
# this works fine (finds correct amount of objects within an area)
# walk all areas
for area in areas:
polygon = ogr.CreateGeometryFromWkt(area.polygon)
within_count = 0
# walk all objects
for obj in all_objects:
point = ogr.Geometry(ogr.wkbPoint)
point.SetPoint(0, obj.lon, obj.lat)
if point.Within(polygon):
within_count += 1
print "{0}: {1}".format(area.name, within_count)
# ------------------------------------------------------------------------------
# this doesn't work (misses objects, although they are within area)
# create multipoint of all objects
all_objects_multipoint = ogr.Geometry(ogr.wkbMultiPoint)
point = ogr.Geometry(ogr.wkbPoint)
# walk all objects
for obj in all_objects:
point.SetPoint(0, obj.lon, obj.lat)
all_objects_multipoint.AddGeometry(point)
# walk all areas
for area in areas:
polygon = ogr.CreateGeometryFromWkt(area.polygon)
# intersect to get amount of objects within area
intersection = polygon.Intersection(all_objects_multipoint)
within_count = intersection.GetPointCount()
print "{0}: {1}".format(area.name, within_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment