计算GeoPandas中两个GeoDataFrame(点数)之间的所有距离 [英] Calculate all distances between two GeoDataFrame (of points) in GeoPandas

查看:620
本文介绍了计算GeoPandas中两个GeoDataFrame(点数)之间的所有距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的案例,但是到目前为止,我还没有找到任何简单的方法来做到这一点.这个想法是获得在 GeoDataFrame 中定义的所有点与在另一个 GeoDataFrame 中定义的点之间的距离的集合.

 将geopandas导入为gpd将熊猫作为pd导入#随机坐标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)打印(gdf_2)#距离是逐元素计算的打印(gdf_1.distance(gdf_2)) 

这会产生共享相同索引的 gdf_1 gdf_2 中的点之间的元素方向距离(同时会发出警告,因为两个GeoSeries的索引不相同,这将是我的情况.

 几何0分(0.000 0.000)1分(0.000 90.000)2分(0.000 120.000)几何学0分(0.00000 0.00000)1分(0.00000 -90.00000)/home/seydoux/anaconda3/envs/chelyabinsk/lib/python3.8/site-packages/geopandas/base.py:39:用户警告:两个GeoSeries的索引不同.警告(两个GeoSeries的索引不同.")0 0.01 180.02 NaN 

问题是;如何获得一系列所有点到点的距离(或至少是 gdf_1 gdf_2 的索引的唯一组合,因为它是对称的)./p>

编辑

  • 此帖子中,给出了解决方案有两点;但是我找不到一种简单的方法来合并两个数据集中的所有点.

  • 此处仅建议按元素操作.

  • 在geopandas的GitHub存储库上也提出了类似的问题 .提出的解决方案之一是使用 apply 方法,而没有任何详细的答案.

解决方案

您必须在第一个gdf中的每个几何体上应用以获取到第二个gdf中的所有几何体的距离.

 将geopandas导入为gpd将熊猫作为pd导入#随机坐标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 10 0.0 90.01 90.0 180.02 120.0 210.0 

This is quite simple case, but I did not find any easy way to do it so far. The idea is to get a set of distances between all the points defined in a GeoDataFrame and the ones defined in another 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))

This produces the element-wise distance between points in gdf_1 and gdf_2 that share the same index (with also a warning because the two GeoSeries do not have the same index, which will be my case).

                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

The question is; how is it possible to get a series of all points to points distances (or at least, the unique combinations of the index of gdf_1 and gdf_2 since it is symmetric).

EDIT

  • In this post, the solution is given for a couple of points; but I cannot find a straightforward way to combine all points in two datasets.

  • In this post only element-wise operations are proposed.

  • An analogous question was also raised on the GitHub repo of geopandas. One of the proposed solution is to use the apply method, without any detailed answer.

解决方案

You have to apply over each geometry in first gdf to get distance to all geometric in second 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

这篇关于计算GeoPandas中两个GeoDataFrame(点数)之间的所有距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆