给定纬度/经度,根据纬度/经度

时间:2017-10-09 08:16:10

标签: python json python-3.x

给定一个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个公共汽车站的列表,任何用户使用给定的公式使用lat / long

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个距离。

非常感谢你。

修改

使用Eric 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是用户输入经度纬度的示例。

2 个答案:

答案 0 :(得分:2)

您可以简单地定义一个函数来计算距离,并使用它来使用key参数对公交站点进行排序:

from math import cos, sqrt

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 )

bustops = [{"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}]

print(sorted(bustops, key= lambda d: distance(d["Longitude"], d["Latitude"], 103.5, 1.2)))
# [{'BusStopCode': '01012', 'RoadName': 'Victoria St', 'Description': 'Hotel Grand Pacific', 'Latitude': 1.29684825487647, 'Longitude': 103.85253591654006}, {'BusStopCode': '00481', 'RoadName': 'Woodlands Rd', 'Description': 'BT PANJANG TEMP BUS PK', 'Latitude': 1.383764, 'Longitude': 103.7583}]

对此列表进行排序后,您只需使用[:5]提取最近的5个公交车站。 它应该足够快,即使有5000个公共汽车站。

请注意,如果您不关心特定距离但只想对公交车站进行排序,则可以将此功能用作关键:

def distance2(lon1, lat1, lon2, lat2):
    x = (lon2 - lon1) * cos(0.5*(lat2+lat1))
    y = (lat2 - lat1)
    return x*x + y*y

答案 1 :(得分:0)

对于这样的项目,我做了相同的事情,但是计算大型数据集的所有距离可能要花费很多时间。

我最终得到knn nearest neighbors,它快得多,而且您不需要一直重新计算距离:

import numpy as np
from sklearn.neighbors import NearestNeighbors

buslist = [{ ...., 'latitude':45.5, 'longitude':7.6}, { ...., 'latitude':48.532, 'longitude':7.451}]

buslist_coords = np.array([[x['latitude'], x['longitude']] for x in buslist]) #extracting x,y coordinates

# training the knn with the xy coordinates
knn = NearestNeighbors(n_neighbors=num_connections)
knn.fit(buslist_coords)
distances, indices = knn.kneighbors(xy_coordinates)
# you can pickle these and load them later to determinate the nearest point to an user


# finding the nearest point for a given coordinate
userlocation = [47.456, 6.25]
userlocation = np.array([[userlocation[0], userlocation[1]]])
distances, indices = knn.kneighbors(userlocation)

# get the 5 nearest stations in a list
nearest_stations = buslist[indices[0][:5]] # the order of the buslist must be the same when training the knn and finding the nearest point

# printing the 5 nearest stations
for station in nearest_stations :
    print(station)

此后,我使用networkx用这些数据构建了一个图,但我仍在使用knn.kneighbors(userlocation)来查找用户的最近点。