一组125个经纬度坐标和另一组137个经纬度坐标对比分布是否相似代码
时间: 2023-06-27 08:01:19 浏览: 92
这里提供一种可能的实现方式,假设经纬度坐标已经存储在两个列表 `coords1` 和 `coords2` 中,每个经纬度坐标以元组 `(longitude, latitude)` 的形式表示。
```python
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r
def compare_coordinate_distribution(coords1, coords2):
"""
Compare the distribution of two sets of coordinates based on the
Haversine distance between each pair of coordinates. Returns a float
between 0 and 1, where 0 indicates completely different distributions
and 1 indicates identical distributions.
"""
distances1 = []
distances2 = []
for coord1 in coords1:
for coord2 in coords2:
distances1.append(haversine(coord1[0], coord1[1], coord2[0], coord2[1]))
distances2.append(haversine(coord2[0], coord2[1], coord1[0], coord1[1]))
avg_distance1 = sum(distances1) / len(distances1)
avg_distance2 = sum(distances2) / len(distances2)
return min(avg_distance1, avg_distance2) / max(avg_distance1, avg_distance2)
```
这里使用了 Haversine 公式计算两个经纬度坐标之间的距离。`compare_coordinate_distribution` 函数计算了两个坐标集合之间的距离分布比较,返回一个 float 值,表示相似度,值越大表示越相似。
阅读全文