Skip to content

Instantly share code, notes, and snippets.

@1papaya
Created March 8, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1papaya/52fff6fb5416cb844d53e877e51e79d6 to your computer and use it in GitHub Desktop.
Save 1papaya/52fff6fb5416cb844d53e877e51e79d6 to your computer and use it in GitHub Desktop.
Distance matrix of GeoDataFrames with scipy
from scipy.spatial import distance_matrix
import pandas as pd
def gdf_distance_matrix(gdf1, gdf2, crs={'init':'epsg:3857'}):
"""
Calculate the distance matrix between two GeoDataFrames
Both GeoDataFrames must have the source crs set in order to be projected.
Parameters
----------
gdf1 : geopandas.GeoDataFrame
GeoDataFrame #1
gdf2 : geopandas.GeoDataFrame
GeoDataFrame #2
crs : str or dict
Output projection parameters, passed to geopandas
Returns
-------
pd.DataFrame
Distance matrix dataframe; Distances can be looked up by .loc[]
"""
## Transform to mutual coordinate system to calculate distance
dset1 = gdf1.to_crs(crs)
dset2 = gdf2.to_crs(crs)
## List of coordinate pairs [x,y] for each dataset
dset1_xy = dset1.apply(lambda b: [b.geometry.x, b.geometry.y], axis=1).tolist()
dset2_xy = dset2.apply(lambda b: [b.geometry.x, b.geometry.y], axis=1).tolist()
return pd.DataFrame(distance_matrix(dset1_xy, dset2_xy),
columns=dset2.index,
index=dset1.index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment