在commandlin中找到最近的一组坐标

2024-04-23 16:12:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个命令行解决方案,从CSV坐标列表中找到最近的点集。在

Here这是Excel的答案,但我需要一个稍微不同的解决方案。在

我不是在寻找每个点的最近点,而是寻找彼此之间距离最小的点对。在

我想匹配许多来自GEO的发电厂,所以a(python?)命令行工具会很棒。在

下面是一个示例数据集:

Chicoasén Dam,16.941064,-93.100828
Tuxpan Oil Power Plant,21.014891,-97.334492
Petacalco Coal Power Plant,17.983575,-102.115252
Angostura Dam,16.401226,-92.778926
Tula Oil Power Plant,20.055825,-99.276857
Carbon II Coal Power Plant,28.467176,-100.698559
Laguna Verde Nuclear Power Plant,19.719095,-96.406347
Carbón I Coal Power Plant,28.485238,-100.69096
Manzanillo I Oil Power Plant,19.027372,-104.319274
Tamazunchale Gas Power Plant,21.311282,-98.756266

该工具应该打印“Carbon II”和“Carbon I”,因为这对有最小的距离。在

代码片段可以是:

^{pr2}$

Tags: 工具csv命令行距离列表here解决方案excel
1条回答
网友
1楼 · 发布于 2024-04-23 16:12:28

一种简单的方法是计算所有对,然后找到最小对,其中一对的“大小”定义为对中两点之间的距离:

from itertools import combinations

closest = min(combinations(data, 2),
              key=lambda p: haversine(float(p[0][1]), float(p[0][2]), float(p[1][1]), float(p[1][2])))

要获得最小的5个,请使用具有相同键的堆。在

^{pr2}$

相关问题 更多 >