调用高德API逆地理编码,并做了QPS控制 - xd21303/Notebook GitHub Wiki
import pandas as pd
import time
import requests
from tqdm import tqdm
def get_first(pois):
if len(pois) > 0:
return pois[0].get('distance')
else:
return ''
pd.DataFrame(columns=data.columns).to_csv(filename, index=False)
for i in tqdm(range(0, data.shape[0], 20)):
if i % 100 == 0:
time.sleep(1)
chunk = data.iloc[i:i + 20].copy()
a = chunk['longcj'].round(6).astype('str').str.cat(
chunk['latgcj'].round(6).astype('str'), sep=',')
b = '|'.join(a.to_list())
payload = {
'key': '',
'location': b,
'roadlevel': '1',
'radius': '100',
'extensions': 'all',
'batch': 'true'
}
r = requests.get("https://restapi.amap.com/v3/geocode/regeo?parameters",
params=payload)
result = r.json()
if result.get('status') == '1':
regeocodes = pd.DataFrame(result.get('regeocodes'))
# loc 是包含头和尾的,所以要 i + 19
chunk.loc[i:i + 19, 'road_distance'] = regeocodes['roads'].apply(
get_first).values
else:
print(result.get('info'))
chunk.to_csv(filename, mode='a', index=False, header=False)