如何计算整列最近的经纬度?

2024-04-26 13:13:20 发布

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

我有一份现场事故的清单,想看看离这些事故最近的警察局是什么。目前,事故的latlongs列表是在两个单独的列中使用pandas的(对不起,我对python非常陌生,所以可能用错了词)。警察局的latlongs目前在一个单独的json文件中。我目前的目标是创建一个新的列(或文件),其中的latlongs显示最近的警察局。理想情况下,它应该是相应的名字,但这是一座桥,我会通过时,我来到它。你知道吗

我看过其他人是怎么做的,但我只能问一对latlongs的位置,而不是同时问所有的latlongs。你知道吗

from math import cos, asin, sqrt

def distance (lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2
    return 12742 * asin(sqrt(a))

def closest(data, v):
    return min(data, key=lambda p: distance(v['lat'],v['lon'],p['lat'],p['lon']))

#these are the latlons of the police stations
tempDataList = [{"lat": 52.003181, "lon": 4.353068}, 
    {"lat": 52.089416, "lon": 4.377340},
    {"lat": 52.019911, "lon": 4.426602},
    {"lat": 52.054457, "lon": 4.388764},
    {"lat": 52.044536, "lon": 4.332631},
    {"lat": 52.072910, "lon": 4.274784},
    {"lat": 52.066099, "lon": 4.298664},
    {"lat": 52.070030, "lon": 4.317355},
    {"lat": 52.052636, "lon": 4.289576},
    {"lat": 52.060829, "lon": 4.318683},
    {"lat": 52.075680, "lon": 4.306810},
    {"lat": 52.040353, "lon": 4.256946},
    {"lat": 52.089381, "lon": 4.345599},
    {"lat": 52.111719, "lon": 4.283909},
    {"lat": 52.055222, "lon": 4.233827},
    {"lat": 52.046393, "lon": 4.253105},
    {"lat": 52.144177, "lon": 4.405549},
    {"lat": 51.987035, "lon": 4.199314},
    {"lat": 52.061650, "lon": 4.486572}]

v = {'lat': 52.103167, 'lon': 4.317532}
print(closest(tempDataList, v))

这就是我失败的地方,因为我根本不知道如何使v成为两列并将其放入一个新列中。你知道吗

我希望能有一个专栏,里面有最近警察局的详细情况。 有时我会遇到问题TypeError: string indices must be integers。你知道吗


Tags: 文件defsqrtcosdistancelonlat我会
1条回答
网友
1楼 · 发布于 2024-04-26 13:13:20

在下面,您可以看到如何将索引列表转换为numpy数组,之后可以对其进行操作,而不必遍历每个元素。你知道吗

import numpy as np
# numpy allows you to work with arrays; math only works with scalars
from numpy import cos, arcsin as asin, sqrt

def distance (lat1, lon1, lat2, lon2):
    """Return the distances between all accidents (lat1,lon1)
    and all police stations (lat2,lon2) as a 2-dimensional array
    """
    # add dummy dimension to p
    lat2 = lat2[:,None]
    lon2 = lon2[:,None]
    p = 0.017453292519943295
    a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2
    return 12742 * asin(sqrt(a))

def closest(*args):
    """Returns the index (counting from zero) of the closest
    police station for every accident.
    """
    return np.argmin(distance(*args), axis=0)


tempDataList = [{"lat": 52.003181, "lon": 4.353068},
    {"lat": 52.089416, "lon": 4.377340},
    {"lat": 52.019911, "lon": 4.426602},
    {"lat": 52.054457, "lon": 4.388764},
    {"lat": 52.044536, "lon": 4.332631},
    {"lat": 52.072910, "lon": 4.274784},
    {"lat": 52.066099, "lon": 4.298664},
    {"lat": 52.070030, "lon": 4.317355},
    {"lat": 52.052636, "lon": 4.289576},
    {"lat": 52.060829, "lon": 4.318683},
    {"lat": 52.075680, "lon": 4.306810},
    {"lat": 52.040353, "lon": 4.256946},
    {"lat": 52.089381, "lon": 4.345599},
    {"lat": 52.111719, "lon": 4.283909},
    {"lat": 52.055222, "lon": 4.233827},
    {"lat": 52.046393, "lon": 4.253105},
    {"lat": 52.144177, "lon": 4.405549},
    {"lat": 51.987035, "lon": 4.199314},
    {"lat": 52.061650, "lon": 4.486572}]

# first get lat and lon into arrays
lat, lon = np.transpose([[i['lat'],i['lon']] for i in tempDataList])

# I'm making up the police stations here. Say 5 police stations.
# you should load the actual data in your problem. My station locations
# are randomly located near the first 5 accidents
npolice = 5
# for reproducibility
np.random.seed(3)
plat = lat[:npolice] + np.random.normal(0, 0.02, npolice)
plon = lon[:npolice] + np.random.normal(0, 0.02, npolice)

index = closest(lat, lon, plat, plon)
# index = array([3, 1, 2, 0, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 4, 4, 1, 4, 2])

所以最近的警察局地点是

nearest = {'lat': plat[index], 'lon': plon[index]}

你可以使用index来访问,比如说,每个警察局的名字或地址,如果你也存储了这些。你知道吗

希望有帮助。你知道吗

相关问题 更多 >