gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:47:43 25 4
gpt4 key购买 nike

我有一份事故地点经纬度列表,想了解距离这些事故最近的警察局。目前,事故的经纬度列表位于使用 pandas 的两个单独的列中(抱歉,我对 python 很陌生,所以我可能使用了错误的单词)。警察局的经纬度当前位于单独的 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 成为两列并将其放入新列中。

我希望有一个列显示最近的警察局的经纬度。有时我会遇到问题TypeError:字符串索引必须是整数

最佳答案

下面您可以看到如何将索引列表转换为 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 来访问每个警察局的名称或地址(如果您还存储了这些信息)。

希望有帮助。

关于python - 如何计算整列最接近的纬度经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56463982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com