gpt4 book ai didi

python - 计算 GeoPandas 中两个 GeoDataFrame(点)之间的所有距离

转载 作者:行者123 更新时间:2023-12-04 00:53:15 28 4
gpt4 key购买 nike

这是一个非常简单的案例,但到目前为止我还没有找到任何简单的方法。这个想法是在 GeoDataFrame 中定义的所有点之间获得一组距离。以及在另一个 GeoDataFrame 中定义的那些.

import geopandas as gpd
import pandas as pd

# random coordinates
gdf_1 = gpd.GeoDataFrame(geometry=gpd.points_from_xy([0, 0, 0], [0, 90, 120]))
gdf_2 = gpd.GeoDataFrame(geometry=gpd.points_from_xy([0, 0], [0, -90]))
print(gdf_1)
print(gdf_2)

#  distances are calculated elementwise
print(gdf_1.distance(gdf_2))
这会产生 gdf_1 中点之间的元素距离和 gdf_2共享相同的索引(还有一个警告,因为两个 GeoSeries 没有相同的索引,这就是我的情况)。
                geometry
0 POINT (0.000 0.000)
1 POINT (0.000 90.000)
2 POINT (0.000 120.000)
geometry
0 POINT (0.00000 0.00000)
1 POINT (0.00000 -90.00000)
/home/seydoux/anaconda3/envs/chelyabinsk/lib/python3.8/site-packages/geopandas/base.py:39: UserWarning: The indices of the two GeoSeries are different.
warn("The indices of the two GeoSeries are different.")
0 0.0
1 180.0
2 NaN
问题是;如何获得一系列所有点到点的距离(或者至少是 gdf_1gdf_2 的索引的唯一组合,因为它是对称的)。
编辑
  • this post ,给出了几个点的解决方案;但我找不到一种简单的方法来组合两个数据集中的所有点。
  • this post仅建议按元素操作。
  • 也有人提出了类似的问题 on the GitHub repo of geopandas .建议的解决方案之一是使用 apply方法,没有任何详细的答案。
  • 最佳答案

    您必须在第一个 gdf ​​中的每个几何图形上应用以获得与第二个 gdf ​​中所有几何图形的距离。

    import geopandas as gpd
    import pandas as pd

    # random coordinates
    gdf_1 = gpd.GeoDataFrame(geometry=gpd.points_from_xy([0, 0, 0], [0, 90, 120]))
    gdf_2 = gpd.GeoDataFrame(geometry=gpd.points_from_xy([0, 0], [0, -90]))

    gdf_1.geometry.apply(lambda g: gdf_2.distance(g))
          0      1
    0 0.0 90.0
    1 90.0 180.0
    2 120.0 210.0

    关于python - 计算 GeoPandas 中两个 GeoDataFrame(点)之间的所有距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64754025/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com