gpt4 book ai didi

Python Google 地理编码 API - IndexError : list index out of range?

转载 作者:行者123 更新时间:2023-11-30 23:11:00 30 4
gpt4 key购买 nike

我正在开发一个可视化位置数据的个人项目,在这里我通过Geocoding API反转来自Google的地理编码位置数据。 ,通过向其提供坐标并检索城市名称和国家/地区。

这是一个 CSV 文件,有 2 列:“位置”(纬度和经度)和“时间”(日期+时间)。有 8533 行。

示例数据:

    Location                Time
--------------------------------------------------
| 41.2911084,2.0779035 | 4/15/2015 10:58 |
--------------------------------------------------
| 41.2885014,2.0725591 | 4/15/2015 10:07 |
--------------------------------------------------
| 41.3484125,2.1442487 | 4/15/2015 9:56 |
--------------------------------------------------

我的 API 有问题我不断收到错误的地方。首先让我展示一下代码。

# import necessary modules
import pandas as pd
import json, requests, logging

# configure logging for our tool
lfh = logging.FileHandler('reverseGeocoder.log')
lfh.setFormatter(logging.Formatter('%(levelname)s %(asctime)s %(message)s'))
log = logging.getLogger('reverseGeocoder')
log.setLevel(logging.INFO)
log.addHandler(lfh)

# load the gps coordinate data
df = pd.read_csv('LocationHistory.csv')

# create new columns
df['geocode_data'] = ''
df['city'] = ''
df['country'] = ''


df.head()

# function that handles the geocoding requests
def reverseGeocode(latlng):

result = {}
url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng={0}&key={1}'
apikey = 'API_KEY_GOES_HERE'

request = url.format(latlng, apikey)
log.info(request)
data = json.loads(requests.get(request).text)
log.info(data)
result = data['results'][0]['address_components']
return {
'city': result[3]['long_name'],
'country': result[6]['long_name']
}

# comment out the following line of code to geocode the entire dataframe
#df = df.head()

for i, row in df.iterrows():
# for each row in the dataframe, geocode the lat-long data
revGeocode = reverseGeocode(df['Location'][i])
df['geocode_data'][i] = revGeocode
df['city'] = revGeocode['city']
df['country'] = revGeocode['country']


# once every 100 loops print a counter
#if i % 100 == 0:
print i

df.head()

df.to_csv('LocationHistory2.csv', encoding='utf-8', index=False)

我不断收到的相关错误:

Traceback (most recent call last):
File "D:\...\ReverseGeocoding.py", line 45, in <module>
revGeocode = reverseGeocode(df['Location'][i])
File "D:\...\ReverseGeocoding.py", line 37, in reverseGeocode
'country': result[6]['long_name']
IndexError: list index out of range

我认为部分问题在于我需要进行适当的检查,以防 API 不返回任何位置信息。为什么它不会返回任何内容,我不知道。

我对 API(和 Python)的世界还很陌生,但是我怎样才能让这段代码进入运行状态呢?

最佳答案

您可能想要对所需地址属性的类型键进行检查。所以尝试类似的东西;

    result = data['results'][0]['address_components']
city = ''
country = ''

for item in result:
if 'administrative_area_level_1' in item[types]:
city = item['long_name']
elif 'country' in item[types]:
country = item['long_name']
return {
'city': city,
'country': country
}

关于Python Google 地理编码 API - IndexError : list index out of range?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30399769/

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