Извлечение широты и долготы с использованием Python - PullRequest
0 голосов
/ 16 октября 2019

Я пытаюсь извлечь широту и долготу из фактического адреса.

Я видел несколько видео, которые помогают мне написать следующий скрипт, но у меня появляется ошибка, показывающая

Lat=Geocode_Result[0]["geometry"]["Location"]["Lat"]
KeyError: 'Location'
  import pandas as pd
  import googlemaps code
  df=pd.DataFrame({'Address':['9 RUE DU FOSSE, L-7772, luxembourg', '37 RUE DE LA GARE,
  L-7535, luxembourg']})      # Creating a dataframe with 2 addresses (its just an example)              
  Gmaps_key=googlemaps.Client("xxxxxxxxx")

  df['Latitude'] = None
  df['Longitude'] = None

  for i in range(len(df)):
   Geocode_Result=Gmaps_key.geocode(df.loc[i,'Address'])  # sending a request for each of the addresses

  Lat=Geocode_Result[0]["geometry"]["Location"]["Lat"]  # Extracting the Latitude information
  Long=Geocode_Result[0]["geometry"]["Location"]["Long"] # Extracting the Longitude information
  df.loc[i,'Latitude'] = Lat   # Pass the information into the DataFrame
  df.loc[i,'Longitude'] = Long

 print(df)   

Ответы [ 2 ]

1 голос
/ 16 октября 2019

Сначала необходимо установить geopy (pip install geopy) и импортировать Nominatim. Я сделал несколько изменений в вашем коде, чтобы получить вывод.

import pandas as pd
from geopy.geocoders import Nominatim

df=pd.DataFrame({'Address':['9 RUE DU FOSSE, L-7772, luxembourg', '37 RUE DE LA GARE,L-7535, luxembourg']})         
df['Latitude'] = None
df['Longitude'] = None
locator = Nominatim(user_agent="myGeocoder")
for i in range(len(df)):
    location = locator.geocode(df.loc[i,'Address'])
    df.loc[i,'Latitude'] = location.latitude   
    df.loc[i,'Longitude'] = location.longitude
print(df) 

Выход:

                                 Address Latitude Longitude
0    9 RUE DU FOSSE, L-7772, luxembourg   49.789   6.06569
1  37 RUE DE LA GARE,L-7535, luxembourg  49.7684   5.52118
0 голосов
/ 16 октября 2019

Ошибка говорит, что нет ключа Location в Geocode_Result[0]["geometry"]. Попробуйте напечатать объект?

print(Geocode_Result[0]["geometry"])

Что говорят документы об ответе, который вы получаете?

Также попробуйте использовать location вместо Location?

...