4

I am having trouble with using GeoDataFrame.buffer function. That is probably because of my lack of understanding crs.

Here is the case:

I have events dataset (points longs & lats) and locations dataset (points longs & lats). I want to check which of the events occurred within 2km radius circle from locations. I know how to perform spatial join in geopandas, but the problem is in buffer(distance, resolution) function. When creating both GeoDataFrames I used crs: 'epsg:4326' and in distance parameter use 0.2 (without any good reason).

How do I adjust crs and distance parameter to make it in kilometers?

Here is my sample code. The intersection should contain only events with id 1 & 2. However it does contain event 3 which is about 10km far from any of locations. Also it does not contain event with id 4, which is good, because that one is hundreds kilometers from any of locations.

events = pd.DataFrame([
 {'id': 1,
 'longitude': 54.606402,
 'latitude': 18.347709},
 {'id': 2,
 'longitude': 54.604681,
 'latitude': 18.346534},
 {'id': 3,
 'longitude': 54.544322,
 'latitude': 18.293896},
 {'id': 4,
 'longitude': 50.843233,
 'latitude': 11.130688}
])
locations = pd.DataFrame([
 {'id': 'a',
 'longitude': 54.604972,
 'latitude': 18.346815},
 {'id': 'b',
 'longitude': 54.605917,
 'latitude': 18.347249}
])
locations_gpd = gpd.GeoDataFrame(locations,
 geometry=gpd.points_from_xy(locations.longitude, locations.latitude).buffer(0.2, 16),
 crs={'init': 'epsg:4326'})
events_gpd = gpd.GeoDataFrame(events,
 geometry=gpd.points_from_xy(events.longitude, events.latitude),
 crs={'init': 'epsg:4326'})
intersection = gpd.sjoin(locations_gpd, events_gpd, how='left')
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 28, 2020 at 8:57

1 Answer 1

5

Reproject to a projected coordinate system in meters, for example EPSG:32634. Then the buffer distance will be in meters:

import geopandas as gpd
import pandas as pd
events = pd.DataFrame([
 {'id': 1,
 'longitude': 54.606402,
 'latitude': 18.347709},
 {'id': 2,
 'longitude': 54.604681,
 'latitude': 18.346534},
 {'id': 3,
 'longitude': 54.544322,
 'latitude': 18.293896},
 {'id': 4,
 'longitude': 50.843233,
 'latitude': 11.130688}
])
locations = pd.DataFrame([
 {'id': 'a',
 'longitude': 54.604972,
 'latitude': 18.346815},
 {'id': 'b',
 'longitude': 54.605917,
 'latitude': 18.347249}
])
locations_gpd = gpd.GeoDataFrame(locations,
 geometry=gpd.points_from_xy(locations.longitude, locations.latitude),
 crs='epsg:4326')
events_gpd = gpd.GeoDataFrame(events,
 geometry=gpd.points_from_xy(events.longitude, events.latitude),
 crs='epsg:4326')
locations_gpd = locations_gpd.to_crs("EPSG:32634")
locations_gpd.geometry = locations_gpd.geometry.buffer(2000, 6)
events_gpd = events_gpd.to_crs("EPSG:32634")
intersection = gpd.sjoin(locations_gpd, events_gpd, how='left')
answered Dec 28, 2020 at 9:16
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.