将纬度和经度放入距离矩阵中,使用python的google map API
我有一个熊猫数据框,格式如下: 现在我想把所有的起始纬度和起始经度对放到原点,把所有的结束纬度和结束经度对放到目的地。预期产出:将纬度和经度放入距离矩阵中,使用python的google map API,python,google-maps,numpy,pandas,latitude-longitude,Python,Google Maps,Numpy,Pandas,Latitude Longitude,我有一个熊猫数据框,格式如下: 现在我想把所有的起始纬度和起始经度对放到原点,把所有的结束纬度和结束经度对放到目的地。预期产出: Rental Id | Distance | Duration | Status 0 51649420 0 0 OK 1 51649421 959 214 OK 2 ... 15 我尝试了以下两种方法,但它们都给了我超时错误 方法1: import googlemaps
Rental Id | Distance | Duration | Status
0 51649420 0 0 OK
1 51649421 959 214 OK
2
...
15
我尝试了以下两种方法,但它们都给了我超时错误
方法1:
import googlemaps
from pandas.io.json import json_normalize
gmaps = googlemaps.Client(key='my API key')
for i in range (0,15):
origins = (journeydf['Start Latitude'][i], journeydf['Start Longitude'][i])
destinations = (journeydf['End Latitude'][i], journeydf['End Longitude'][i])
matrix = gmaps.distance_matrix(origins, destinations, mode="bicycling")
matrixdf = json_normalize(matrix,['rows','elements'])
matrixdf['Rental Id']=journeydf['Rental Id']
方法2:
import urllib, json, time
import pandas as pd
def google(lato, lono, latd, lond):
url = """http://maps.googleapis.com/maps/api/distancematrix/json?origins=%s,%s"""%(lato, lono)+ \
"""&destinations=%s,%s&mode=driving&language=en-EN&sensor=false"""% (latd, lond)
#CHANGE THIS FOR PYTHON 3.X TO urllib.request.urlopen(url)...
response = urllib.urlopen(url).read().decode('utf8')
#Wait a second so you don't overwhelm the API if doing lots of calls
time.sleep(1)
obj = json.loads(response)
try:
minutes = obj['rows'][0]['elements'][0]['duration']['value']/60
miles = (obj['rows'][0]['elements'][0]['distance']['value']/100)*.62137 #kilometers per mile
return minutes, miles
except IndexError:
#something went wrong, the result was not found
print (url)
#return the error code
return obj['Status'], obj['Status']
def ApplyGoogle(row):
lato, lono = row['Start Latitude'], row['Start Longitude']
latd, lond = row['End Latitude'], row['End Longitude']
return google(lato, lono, latd, lond)
journeydf['Minutes'], journeydf['Miles'] = zip(*journeydf.apply(ApplyGoogle, axis = 1))
有没有办法解决这个问题?提前感谢。我很惊讶方法1出现超时错误。你能确认输出吗 您是否创建了谷歌地图API密钥?标准使用免费(每天2500次距离计算,每次查询限制100次,每10秒限制100次) 该密钥需要在代码具有“MyAPI密钥”的地方编写 您在for循环上的缩进以及分配给起始点和目的地也可能存在问题。试试这个:
# make sure you can connect to Google's server
import requests
try:
response = requests.get('http://www.google.com')
except:
print 'Can\'t connect to Google\'s server'
raw_input('Press any key to exit.')
quit()
# use the Google Maps API
import googlemaps
gmaps = googlemaps.Client(key='YOUR KEY')
origins = []
destinations = []
for i in range (0,15):
origins.append(str(journeydf['Start Latitude'][i]) + ' ' + str(journeydf['Start Longitude'][i]))
destinations.append(str(journeydf['End Latitude'][i]) + ' ' + str(journeydf['End Longitude'][i]))
matrix = gmaps.distance_matrix(origins, destinations, mode="bicycling")
print matrix
16*16=256>100,因此可能尝试使用更小的矩阵,即10 X 10,然后它应该可以工作。但我仍然收到超时错误。我已经创建了谷歌地图API,并将其放入“我的API密钥”中。当我1岁的时候,它确实适用于特定的情况。我认为在循环中有一个问题。我也有过超时,这实际上帮助了我。我有144种组合,减少到96种,效果很好。