Created
June 29, 2015 11:10
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------------ | |
# 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