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

时间:2019-06-05 15:51:13

标签: python json pandas dataframe latitude-longitude

我有一系列地点事故的清单,想看看离这些事故最近的警察局。目前,事故的latlongs列表使用熊猫分别放在两列中(很抱歉,我对python还是很陌生,所以我可能使用了错误的单词)。警察局的latlongs当前位于单独的json文件中。我当前的目标是创建一个新的列(或文件),其中最近的派出所的拉特朗斯出现。理想情况下,它应该是对应的名称,但这是我遇到的桥梁。

我研究了其他人是如何做到的,但是除了询问该地点只一对拉特朗斯,而且并非同时询问所有拉特朗斯之外,别无他法。

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分为两列并将其放入新列。

我希望有一列能显示最近警察局的拉特朗(latlong)。 有时我遇到问题TypeError: string indices must be integers

1 个答案:

答案 0 :(得分:0)

下面您将看到如何将索引列表转换为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来访问它们。

希望有帮助。