Created
May 5, 2022 21:38
-
-
Save ivopbernardo/89acfafa66060d209ee67dbb9189d472 to your computer and use it in GitHub Desktop.
GeoData DareData Blog
This file contains hidden or 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
# Read the metro data | |
metro = pd.read_csv(Path("data", "metro_stations.csv")) | |
# Convert to a GeoDataFrame | |
metro = gpd.GeoDataFrame( | |
metro, | |
geometry=gpd.points_from_xy(metro.longitude, metro.latitude), | |
crs="epsg:4326" | |
).to_crs(epsg=3857) | |
# Get the closest metro station to each house | |
closest_metros = gpd.sjoin_nearest( | |
house_data_gdf, | |
metro, | |
how="left" | |
).loc[:, ["latitude_right", "longitude_right"]] | |
# Convert the closest metro location to a geometry | |
house_data_gdf["closest_metro"] = gpd.points_from_xy( | |
closest_metros.longitude_right, | |
closest_metros.latitude_right, | |
crs="epsg:4326" | |
).to_crs(epsg=3857) | |
# Calculate the distance to the closest metro station | |
house_data_gdf.assign( | |
metro_distance=house_data_gdf.distance( | |
house_data_gdf.closest_metro, align=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment