如何在python中计算几个地理位置的中点

2024-05-23 16:09:36 发布

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

有没有一个库或一种方法来计算几个地理位置点的中心点? 这是我在纽约的地理位置列表,希望找到大致的中点地理位置

L = [
     (-74.2813611,40.8752222),
     (-73.4134167,40.7287778),
     (-74.3145014,40.9475244),
     (-74.2445833,40.6174444),
     (-74.4148889,40.7993333),
     (-73.7789256,40.6397511)
    ]

Tags: 方法列表地理位置中心点
3条回答

comments之后,我从HERE接收并评论

使用close to each other坐标,可以将地球视为局部平坦,只需像平面坐标一样找到质心。然后你只需要取纬度的平均值和经度的average就可以找到centroidlatitudelongitude

lat = []
long = []
for l in L :
  lat.append(l[0])
  long.append(l[1])

sum(lat)/len(lat)
sum(long)/len(long)

-74.07461283333332, 40.76800886666667

基于:https://gist.github.com/tlhunter/0ea604b77775b3e7d7d25ea0f70a23eb

假设您有一个带有纬度和经度列的pandas数据框,下一个代码将返回一个带有平均坐标的字典。

import math

x = 0.0
y = 0.0
z = 0.0

for i, coord in coords_df.iterrows():
    latitude = math.radians(coord.latitude)
    longitude = math.radians(coord.longitude)

    x += math.cos(latitude) * math.cos(longitude)
    y += math.cos(latitude) * math.sin(longitude)
    z += math.sin(latitude)

total = len(coords_df)

x = x / total
y = y / total
z = z / total

central_longitude = math.atan2(y, x)
central_square_root = math.sqrt(x * x + y * y)
central_latitude = math.atan2(z, central_square_root)

mean_location = {
    'latitude': math.degrees(central_latitude),
    'longitude': math.degrees(central_longitude)
    }

考虑到您使用的是有符号度数格式(more),简单的纬度和经度平均值会因为这条线的经度值的不连续性(在-180到180之间的突然跳跃)而给反经度附近的小区域(即+或-180度经度)带来问题。

假设两个位置的经度是-179和179,它们的平均值是0,这是错误的。

相关问题 更多 >