通过空间连接计算距离-Geopandas
导入请求,IO,JSON 导入Geopandas作为GPD 导入shapely.几何 导入大熊猫作为pd,
我有两个地理范围的数据范围。
gpd1
带有polygon
几何形状 gpd2
带有点>点
几何
示例数据:
res = requests.get(
"https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson"
)
gpd1 = gpd.GeoDataFrame.from_features(res.json()["features"], crs="CRS84").pipe(
lambda d: d.rename(columns={c: c.lower() for c in d.columns})
).rename(columns={"lad20cd": "areaCode","lad20nm":"areaName"})
# get some public addressess - hospitals. data that can be scattered
dfhos = pd.read_csv(io.StringIO(requests.get("http://media.nhschoices.nhs.uk/data/foi/Hospital.csv").text),
sep="¬",engine="python",)
# create a geo dataframe of hospitals
gpd2 = gpd.GeoDataFrame(
data=dfhos,
geometry=dfhos.apply(lambda r: shapely.geometry.Point(r["Longitude"],r["Latitude"]), axis=1), crs="EPSG:4326"
)
我的空间加入它们:
gpd1.sjoin(gpd2, how='left')
我也想计算每行连接的距离。如何计算距离并将<代码>距离作为列?请注意,gpd2
中的每个point
在polygon
中。
import requests, io, json
import geopandas as gpd
import shapely.geometry
import pandas as pd
I have two GeoPandas DataFrames.
gpd1
with POLYGON
geometrygpd2
with POINT
geometry
Example data:
res = requests.get(
"https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson"
)
gpd1 = gpd.GeoDataFrame.from_features(res.json()["features"], crs="CRS84").pipe(
lambda d: d.rename(columns={c: c.lower() for c in d.columns})
).rename(columns={"lad20cd": "areaCode","lad20nm":"areaName"})
# get some public addressess - hospitals. data that can be scattered
dfhos = pd.read_csv(io.StringIO(requests.get("http://media.nhschoices.nhs.uk/data/foi/Hospital.csv").text),
sep="¬",engine="python",)
# create a geo dataframe of hospitals
gpd2 = gpd.GeoDataFrame(
data=dfhos,
geometry=dfhos.apply(lambda r: shapely.geometry.Point(r["Longitude"],r["Latitude"]), axis=1), crs="EPSG:4326"
)
I spatial join them:
gpd1.sjoin(gpd2, how='left')
I'd like to also calculate the distances for each row joined. How do I calculate the distance and include distance
as a column? Note, that each POINT
in gpd2
is within the POLYGON
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sjoin()()< / code>是一列 < / code> right_index 这可以用来构建一系列要点
distance> distance> distance>
获得距离sjoin()
is a column right_index This can be used to construct a series of the pointsdistance()
to get the distance