mcpField
D
distance-calculator

distance-calculator

github.com/dkyazzentwatwa数据与数据库12458 次安装29 周前更新
代码仓库

关于 distance-calculator

distance-calculator 是用于计算地理距离和搜索附近地点的 Python 技能,支持多种距离算法与单位,并可批量处理 CSV 数据。

主要功能

  1. 01计算两点间距离
  2. 02生成成对距离矩阵
  3. 03查找最近地点
  4. 04搜索指定半径内地点
  5. 05批量处理 CSV 文件
  6. 06支持多种距离单位

适用场景

  1. 01查找附近商店
  2. 02检查配送覆盖范围
  3. 03计算路线站点间距离

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dkyazzentwatwa/chatgpt-skills --skill distance-calculator

安装后在 Claude Code 中输入/distance-calculator确认已加载。

附带文件scripts/distance_calc.pyscripts/requirements.txt

详细介绍

距离计算器

使用多种方法计算地理距离并查找附近地点。

功能

  • 点对点距离:Haversine、Vincenty、大圆距离
  • 距离矩阵:所有点对之间的距离
  • 最近邻:查找最近的 N 个点
  • 半径搜索:查找指定距离范围内的所有点
  • 批量处理:处理 CSV 文件
  • 多种单位:千米、英里、米、海里

快速入门

from distance_calc import DistanceCalculator

calc = DistanceCalculator()

# Simple distance
dist = calc.distance(
    (40.7128, -74.0060),  # New York
    (34.0522, -118.2437)  # Los Angeles
)
print(f"Distance: {dist:.2f} km")

# Find nearest points
nearest = calc.find_nearest(
    origin=(40.7128, -74.0060),
    points=store_locations,
    n=5
)

命令行用法

# Distance between two points
python distance_calc.py --from "40.7128,-74.0060" --to "34.0522,-118.2437"

# Find nearest from CSV
python distance_calc.py --origin "40.7128,-74.0060" --input stores.csv --nearest 5

# Points within radius
python distance_calc.py --origin "40.7128,-74.0060" --input stores.csv --radius 50

# Distance matrix
python distance_calc.py --input locations.csv --matrix --output distances.csv

# Different units
python distance_calc.py --from "40.7128,-74.0060" --to "34.0522,-118.2437" --unit miles

API 参考

DistanceCalculator 类
class DistanceCalculator:
    def __init__(self, unit: str = "km", method: str = "haversine")

    # Point-to-point
    def distance(self, point1: tuple, point2: tuple) -> float
    def distance_with_details(self, point1: tuple, point2: tuple) -> dict

    # Batch operations
    def distance_matrix(self, points: list) -> list
    def distances_from_origin(self, origin: tuple, points: list) -> list

    # Search
    def find_nearest(self, origin: tuple, points: list, n: int = 1) -> list
    def find_within_radius(self, origin: tuple, points: list, radius: float) -> list

    # File operations
    def from_csv(self, filepath: str, lat_col: str, lon_col: str) -> list
    def matrix_to_csv(self, matrix: list, labels: list, output: str)

距离计算方法

Haversine(默认)
  • 假设地球为球体,计算大圆距离
  • 速度快,适用于大多数用途
  • 误差:最大约 0.5%
Vincenty
  • 精度更高,可考虑地球的椭球形状
  • 速度稍慢
  • 误差:约 0.5 毫米
calc = DistanceCalculator(method="vincenty")

单位

单位描述
km千米(默认)
miles英里
m米
nm海里
ft英尺
calc = DistanceCalculator(unit="miles")
# Or convert after
dist_km = calc.distance(p1, p2)
dist_miles = calc.convert(dist_km, "km", "miles")

示例工作流

查找最近的商店
calc = DistanceCalculator(unit="miles")
stores = calc.from_csv("stores.csv", "lat", "lon")

customer = (40.7128, -74.0060)
nearest = calc.find_nearest(customer, stores, n=3)

for store in nearest:
    print(f"{store['name']}: {store['distance']:.1f} miles")
检查配送范围
calc = DistanceCalculator(unit="km")
warehouse = (40.7128, -74.0060)
delivery_radius = 50  # km

customers = calc.from_csv("customers.csv", "lat", "lon")
in_zone = calc.find_within_radius(warehouse, customers, delivery_radius)

print(f"{len(in_zone)} customers in delivery zone")
用于路线规划的距离矩阵
calc = DistanceCalculator()
stops = [
    (40.7128, -74.0060),
    (40.7589, -73.9851),
    (40.7484, -73.9857),
    (40.7527, -73.9772)
]

matrix = calc.distance_matrix(stops)
calc.matrix_to_csv(matrix, ["HQ", "Store1", "Store2", "Store3"], "distances.csv")

输出格式

距离结果
{
    "distance": 3935.75,
    "unit": "km",
    "from": {"lat": 40.7128, "lon": -74.0060},
    "to": {"lat": 34.0522, "lon": -118.2437},
    "method": "haversine"
}
最近点结果
[
    {"point": (lat, lon), "distance": 5.2, "data": {...}},
    {"point": (lat, lon), "distance": 8.1, "data": {...}},
]

依赖项

  • geopy>=2.4.0

评论

支持 Markdown