通过空间连接计算距离-Geopandas

发布于 2025-01-29 17:21:26 字数 1140 浏览 2 评论 0原文

导入请求,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中的每个pointpolygon中。

import requests, io, json
import geopandas as gpd
import shapely.geometry
import pandas as pd

I have two GeoPandas DataFrames.

gpd1 with POLYGON geometry
gpd2 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2025-02-05 17:21:26
  • 与您的问题一样,模棱两可的或您的示例代码没有开始工作的
  • 固定示例代码使用英国县和英国城镇
  • sjoin()()< / code>是一列 < / code> right_index 这可以用来构建一系列要点
  • ,然后这是一个简单的使用 geopandas distance> distance> distance>获得距离
import pandas as pd
import geopandas as gpd
import requests, io


# some polygons
gpd1 = gpd.read_file(
    requests.get("https://www.geoboundaries.org/api/current/gbOpen/GBR/ADM2").json()[
        "simplifiedGeometryGeoJSON"
    ]
)

# some points
df = pd.read_csv(
    io.StringIO(
        requests.get("https://simplemaps.com/static/data/country-cities/gb/gb.csv").text
    )
)


gpd2 = gpd.GeoDataFrame(
    df, geometry=gpd.points_from_xy(df["lng"], df["lat"]), crs="epsg:4326"
)


gpd_sj = gpd1.sjoin(gpd2, how="inner").reset_index()
gpd_sj["distance"] = gpd_sj.distance(gpd2.loc[gpd_sj["index_right"]])
gpd_sj.loc[:, ["index", "shapeName", "index_right", "city", "distance"]]
索引索引shapenameindex_rightinterdistry
00Hartlepool1606Carew3.29536
10Hartlepool96Hartlepool2.22489
21Middlesbrough2466Stainton1.41667
31Middlesbrough1849Nunthorpe0.766666652
41Middlesbrough1129Marton0.558888144
ClevelandRedcarSeaton2Ormesby3.30945
7dycrbourd克利夫兰1628Middy
635Guisborough2.11976
82Redcar和Cleveland1330Loftus3.68467
92Redcar和Cleveland1167Skelton3.60919
  • as is common with your questions, either ambiguous or your sample code does not begin to work
  • have fixed sample code to use UK counties and UK towns / cities
  • result of an sjoin() is a column right_index This can be used to construct a series of the points
  • then it's a simple case of use geopandas distance() to get the distance
import pandas as pd
import geopandas as gpd
import requests, io


# some polygons
gpd1 = gpd.read_file(
    requests.get("https://www.geoboundaries.org/api/current/gbOpen/GBR/ADM2").json()[
        "simplifiedGeometryGeoJSON"
    ]
)

# some points
df = pd.read_csv(
    io.StringIO(
        requests.get("https://simplemaps.com/static/data/country-cities/gb/gb.csv").text
    )
)


gpd2 = gpd.GeoDataFrame(
    df, geometry=gpd.points_from_xy(df["lng"], df["lat"]), crs="epsg:4326"
)


gpd_sj = gpd1.sjoin(gpd2, how="inner").reset_index()
gpd_sj["distance"] = gpd_sj.distance(gpd2.loc[gpd_sj["index_right"]])
gpd_sj.loc[:, ["index", "shapeName", "index_right", "city", "distance"]]
indexshapeNameindex_rightcitydistance
00Hartlepool1606Seaton Carew3.29536
10Hartlepool96Hartlepool2.22489
21Middlesbrough2466Stainton1.41667
31Middlesbrough1849Nunthorpe0.766652
41Middlesbrough1129Marton0.581443
51Middlesbrough36Middlesbrough1.8294
62Redcar and Cleveland1628Ormesby3.30945
72Redcar and Cleveland635Guisborough2.11976
82Redcar and Cleveland1330Loftus3.68467
92Redcar and Cleveland1167Skelton3.60919
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文