I have 2 datasets:
admin_area.shp - contains polygon district data which has index, names, geometry point_data.shp - contains point data which has index, lat, lon, geometry
I want to loop through each polygon within admin_area, then for each area identify if any of my points intersect with that polygon.
I've tried this:
# initialise an instance of an rtree Index object
idx = Index()
# loop through each row in the district dataset and extract the geometry of each one
for id, district in admin_areas.iterrows():
idx.insert(id, district.geometry.bounds)
# loop through each row in the point dataset and load into the spatial index
for id, point in point_data.iterrows():
idx.insert(id, point.geometry.bounds)
# get the polygon of each district
polygon = admin_areas.geometry.iloc[0]
# how many rows are we starting with?
print(f"boundaries: {len(admin_areas.index)}")
# get the indexes of points that intersect bounds of the admin_areas
possible_matches_index = list(idx.intersection(polygon.bounds))
# use those indexes to extract the possible matches from the GeoDataFrame
possible_matches = point_data.iloc[possible_matches_index]
# how many points intersect the area?
print(f"Filtered points: {len(possible_matches.index)}")
# then search the possible matches for precise matches using the slower but more precise method
precise_matches = possible_matches.loc[possible_matches.within(polygon)]
print(precise_matches.geometry)
# how many points itersect the area now?
print(f"precise points: {len(precise_matches.index)}")
It's resulted in this:
boundaries: 9
Filtered points: 111
647 POINT (371053.697 410002.844)
194 POINT (371053.697 410002.844)
21 POINT (371053.697 410002.844)
189 POINT (371053.697 410002.844)
640 POINT (371053.697 410002.844)
973 POINT (371848.442 409174.713)
982 POINT (371848.442 409174.713)
989 POINT (371848.442 409174.713)
990 POINT (371848.442 409174.713)
995 POINT (371848.442 409174.713)
Name: geometry, Length: 110, dtype: geometry
precise points: 110
boundaries: 9
Filtered points: 222
188 POINT (371848.442 409174.713)
214 POINT (371848.442 409174.713)
245 POINT (371848.442 409174.713)
246 POINT (371848.442 409174.713)
252 POINT (371848.442 409174.713)
973 POINT (371848.442 409174.713)
982 POINT (371848.442 409174.713)
989 POINT (371848.442 409174.713)
990 POINT (371848.442 409174.713)
995 POINT (371848.442 409174.713)
Name: geometry, Length: 220, dtype: geometry
precise points: 220
boundaries: 9
boundaries: 9
Filtered points: 111
647 POINT (371053.697 410002.844)
194 POINT (371053.697 410002.844)
21 POINT (371053.697 410002.844)
189 POINT (371053.697 410002.844)
640 POINT (371053.697 410002.844)
973 POINT (371848.442 409174.713)
982 POINT (371848.442 409174.713)
989 POINT (371848.442 409174.713)
990 POINT (371848.442 409174.713)
995 POINT (371848.442 409174.713)
Name: geometry, Length: 110, dtype: geometry
precise points: 110
boundaries: 9
Filtered points: 222
188 POINT (371848.442 409174.713)
214 POINT (371848.442 409174.713)
245 POINT (371848.442 409174.713)
246 POINT (371848.442 409174.713)
252 POINT (371848.442 409174.713)
973 POINT (371848.442 409174.713)
982 POINT (371848.442 409174.713)
989 POINT (371848.442 409174.713)
990 POINT (371848.442 409174.713)
995 POINT (371848.442 409174.713)
Name: geometry, Length: 220, dtype: geometry
precise points: 220
boundaries: 9
boundaries: 9
Filtered points: 111
647 POINT (371053.697 410002.844)
194 POINT (371053.697 410002.844)
21 POINT (371053.697 410002.844)
189 POINT (371053.697 410002.844)
640 POINT (371053.697 410002.844)
973 POINT (371848.442 409174.713)
982 POINT (371848.442 409174.713)
989 POINT (371848.442 409174.713)
990 POINT (371848.442 409174.713)
995 POINT (371848.442 409174.713)
Name: geometry, Length: 110, dtype: geometry
precise points: 110
boundaries: 9
Filtered points: 222
188 POINT (371848.442 409174.713)
214 POINT (371848.442 409174.713)
245 POINT (371848.442 409174.713)
246 POINT (371848.442 409174.713)
252 POINT (371848.442 409174.713)
973 POINT (371848.442 409174.713)
982 POINT (371848.442 409174.713)
989 POINT (371848.442 409174.713)
990 POINT (371848.442 409174.713)
995 POINT (371848.442 409174.713)
Name: geometry, Length: 220, dtype: geometry
precise points: 220
boundaries: 9
where it seems to be replicated for just one district, but i want it to go through each district. Does anyone know why? Thank you