You can play it a bit smart and use some numerical calculations to make your code more efficient.
It is better to iterate over polygons. Although it is 100,000, you only need to do it once so your PC can handle it.
lets import the data
crime = read_csv('2020-2021 London Crime.csv')
#drop nan rows from coords
crime2 = crime[crime['Longitude'].notna()]
#geocode crime points
gCrime = GeoDataFrame(crime2, geometry=points_from_xy(crime2['Longitude'], crime2['Latitude']))
In order to simplify the sjoin
operation, we need the bounds of each feature of your buffers:
#import buffer polygon layer
gBuffer = read_file('London Buffer.zip')
gBuffer [['minx','miny','maxx','maxy']] = gBuffer.geometry.bounds
Now we are going to iterate over buffers. We can use the buffer bounds to filter the points and then use the spatial join. This will reduce the cross-match between all of the points and the polygons.
joined_result = gpd.GeoDataFrame()
for i, poly in gBuffer.iterrows():
selected_points = gCrime[
(gCrime.geometry.x < poly["maxx"]) &
(gCrime.geometry.x > poly["minx"]) &
(gCrime.geometry.y < poly["maxy"]) &
(gCrime.geometry.y > poly["miny"])
]
row = pd.DataFrame(poly).T
row = gpd.GeoDataFrame(row, geometry = 'geometry') # converting row to GDF for sjoin
temp_result = gpd.jsoin(selected_points, row, op='intersects', how='inner')
joined_result = joined_result.append(temp_result, ignore_index=True)
The joined_result
is your final result. Since you are using a grid, you can use the selected_points
GDF as the join result if you don't mind the buffer with straight corners:
joined_result = gpd.GeoDataFrame()
for i, poly in gBuffer.iterrows():
selected_points = gCrime[
(gCrime.geometry.x < poly["maxx"]) &
(gCrime.geometry.x > poly["minx"]) &
(gCrime.geometry.y < poly["maxy"]) &
(gCrime.geometry.y > poly["miny"])
]
selected_points['buffer_ID'] = poly[<ID VALUE KEY>]
joined_result = joined_result.append(selected_points)
These solutions might take a bit of time, but they will be much less power- and time-consuming Since we are using numerical operations to optimize the spatial join.
Best Answer
The tool you're looking for is now called "Count points in polygons", and it can be found in the QGIS's Processing Toolbox (Ctrl+Alt+T) under
Vector analysis
tools or can be also launched found inVector > Analysis Tools > Count Points in Polygons
.