python - 几何形状Python之间的测地距离

标签 python distance geopandas shapely geodesic-sphere

有一个 dfA,其中包含一个名为 geometry 的列,具有以下几何形状:

d = {'id': [1, 2], 'geometry': ['POINT (-70.66000 -33.45000)', 'POINT (-74.08000 4.60000)']}
dfA = pd.DataFrame(data=d)
dfA

|   | id | geometry              |
|---|----|-----------------------|
| 0 | 1  | POINT (-70.66 -33.45) |
| 1 | 2  | POINT (-74.08 4.6)    |

我想用 dfB 的几何列的每个几何形状计算最小测地距离:

d = {'id': [1, 2, 3], 'geometry': ['LINESTRING (-58.66000 -34.58000, -59.66000 -35.58000)', 'LINESTRING (-47.91000 -15.78000, -48.91000 -16.78000)', 'POINT (-66.86000 10.48000)']}
dfB = pd.DataFrame(data=d)
dfB

|   | id | geometry                                  |
|---|----|-------------------------------------------|
| 0 | 1  | LINESTRING (-58.66 -34.58, -59.66 -35.58) |
| 1 | 2  | LINESTRING (-47.91 -15.78, -48.91 -16.78) |
| 2 | 3  | POINT (-66.86 10.48)                      |

我尝试按照以下步骤使用 Python shapely 和 geopandas 库进行此计算:

from shapely import wkt
import geopandas as gpd

dfA['geometry'] = dfA['geometry'].apply(wkt.loads)
dfA = gpd.GeoDataFrame(dfA, geometry='geometry')
dfB['geometry']= dfB['geometry'].apply(wkt.loads)
for i, value in dfB.iterrows():
    e = dfB.iloc[i]['id']
    dfA[str(e)] = dfA['geometry'].distance(dfB.iloc[i]['geometry'])
dfA

|   | id | geometry              | 1           | 2           | 3           |
|---|----|-----------------------|-------------|-------------|-------------|
| 0 | 1  | POINT (-70.66 -33.45) | 11,20432506 | 27,40349248 | 44,09404608 |
| 1 | 2  | POINT (-74.08 4.6)    | 42,10521108 | 33,0247377  | 9,311433832 |

不幸的是,shapely distance 函数计算的是欧氏距离而不是测地线距离。

要遵循的另一种策略是使用一个函数来计算从点 A 到直线 B [B1、B2、B3...] 上所有点的测地线距离并保持最小距离。也就是说: dist_A-B = min(geodist(A, B1), geodist(A, B2), geodist(A, B3), ....)

此解决方案可行,但在计算上它非常昂贵,因为我们讨论的是从数千个点到数千条线的计算。 执行此计算的任何其他更优化的方法都会有很大帮助。

最佳答案

如果您可以将问题简化为计算到一组点的测地线距离,那么 vantage point tree会给你一个有效的解决方案。请参阅我对类似问题的回答 here ;这包括 python 中的解决方案。

关于python - 几何形状Python之间的测地距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61955079/

相关文章:

python - 如何使用行数和列名从 CSV 文件调用组件

python - 如何定位和对齐 matplotlib 图形图例?

java - 确定从给定位置到网格边缘的最短距离

MATLAB:函数返回单个值而不是向量

python - 如何修复 "latin-1 codec can' t 在请求中的位置编码字符

python - 在 Python 中如何让某件事有 20% 的几率发生?

android - 使用 FaceDetector.Face 在 Android 中查找眼睛之间的距离

python-3.x - GeoPandas 在点上设置 CRS

python-3.x - Geopandas 空间连接 - AttributeError : 'NoneType' object has no attribute 'bounds'

python - 在 Anaconda 中安装 geopandas :"A GDAL API version must be specified "时出错