给出一个JSON文件的JSON列表上最近的位置,由于纬度/长,查找基于经/纬
{"BusStopCode": "00481", "RoadName": "Woodlands Rd", "Description": "BT PANJANG TEMP BUS PK", "Latitude": 1.383764, "Longitude": 103.7583},
{"BusStopCode": "01012", "RoadName": "Victoria St", "Description": "Hotel Grand Pacific", "Latitude": 1.29684825487647, "Longitude": 103.85253591654006}
,等等..
的各种巴士站,我我试图基于5000个总线的这个名单上找到最近的巴士站与指定纬度/长使用给定的公式任何用户停止
import math
R = 6371000 #radius of the Earth in m
x = (lon2 - lon1) * cos(0.5*(lat2+lat1))
y = (lat2 - lat1)
d = R * sqrt(x*x + y*y)
我的问题是,对于LAT1和lon1用户输入,我会怎样能够计算所有的距离在lat1 lon1和lat2 lon2之间(其中lat2 lon2将取json文件中所有5000 lat/lon的值),然后打印最低的5个距离?
我想过使用list.sort,但我不知道如何能够使用python计算所有5000个距离。
非常感谢。
编辑:
与埃里克Duminil的代码,下面的代码工作适合我的需要。
from math import cos, sqrt
import sys
import json
busstops = json.loads(open("stops.json").read())
R = 6371000 #radius of the Earth in m
def distance(lon1, lat1, lon2, lat2):
x = (lon2-lon1) * cos(0.5*(lat2+lat1))
y = (lat2-lat1)
return R * sqrt(x*x + y*y)
buslist = sorted(busstops, key= lambda d: distance(d["Longitude"], d["Latitude"], 103.5, 1.2))
print(buslist[:5])
其中来自buslist的103.5,1.2是示例用户输入经度纬度。
只是检查出https://pypi.python.org /的PyPI/geopy。你可能会找到一种方法来获得某种方式。另请查看此评论https://stackoverflow.com/a/19412565/6165783 –
谢谢你的链接。然而,我并不关心2 lat/lon之间距离的计算,而是 - 用户给出纬度/经度,我怎样才能找到最近的5个公交站点,并且有一个5000个巴士站的json文件。 –
道歉,如果我的问题不好措辞 –