使用高德逆地理編碼批量請求解析經緯度到具體的位置信息
項目中涉及到要根據經緯度解析實際的地理位置,所以就需要搞一把。
縱觀出申請一個企業開發者賬號,每天的api調用就可以達到300w次了,開心。
個人開發者和企業開發者的調用限制:
這是註冊地址:
https://lbs.amap.com/dev/id/choose
web API調用參數
下面是用python寫的一個demo,可以參考一下。
# -*- coding: utf-8 -*-
import uuid
import requests
import json
class Location_query(object):
def __init__(self, key):
# 傳入參數,申請到的高德api
self.key = key
if len(self.key) != 32:
print("你的key好像不對,大兄弟...")
exit(0)
def get_map_address(self, gd_coordinates):
"""
逆地理編碼(高德座標轉地址)
:param gd_coordinates:多個位置"高德經度,高德緯度"的集合(元組或數組都可以)
:return:多個位置地址信息的列表,最多一次查詢20個經緯度點
"""
try:
coordinates = "|".join(gd_coordinates)
batch = "true" if len(gd_coordinates) > 1 else "false"
url = "https://restapi.amap.com/v3/geocode/regeo?key={0}&location={1}&batch={2}&radius=500&extensions=base&output=json".format(
self.key, coordinates, batch)
response = requests.get(url)
result = json.loads(response.text)
if "1" == result["status"]:
address = []
if batch == "true":
address.extend([[add['addressComponent']['district'],add["formatted_address"]]
for add in list(result["regeocodes"])])
else:
fmt_add = result["regeocode"]["formatted_address"]
district = result['regeocode']['addressComponent']['district']
address.append([district,fmt_add])
return address
else:
print("Unknown")
except Exception as e:
raise e
if __name__ == "__main__":
key = "24306d0fc1cd30e49be57305ce824b**" # 填寫你申請到的高德開放平臺api
addr = Location_query(key)
list_loctions = []
# 寫出
fwrite = open('/root/GEO_data/ok/gps01.txt','w+',encoding='utf-8')
# 讀入
# with open("D://model//PaddleSeg//python_Algorithm//loc.csv",'r',encoding='utf-8') as f:
with open('/root/GEO_data/original/gps0.txt', 'r', encoding='utf-8') as f:
flines = f.readlines()
try:
for idx, line in enumerate(flines, start=1):
sp_line = line.split(",")
if(float(sp_line[4])>0.0 and float(sp_line[5])>0.0):
location = ",".join([str(sp_line[4]), str(sp_line[5])])
print(location)
list_loctions.append(location)
if idx % 20 == 0:
try:
res = addr.get_map_address(list_loctions)
if res is not None:
for i in res:
dist, detail = i[0], i[1]
basetime = sp_line[3]
gpstime = ' '.join(['-'.join([basetime[:4], basetime[4:6], basetime[6:8]]),
':'.join([basetime[8:10], basetime[10:12], basetime[12:14]])])
year = basetime[:4]
month = basetime[4:6]
day = basetime[6:8]
hour = basetime[8:10]
suuid = str(uuid.uuid4()).replace("-", "")
fout = ','.join([suuid, dist, detail, year, month, day, hour, gpstime, line])
print(fout)
fwrite.write(fout)
except Exception as e:
print("Exception {} \n input :{}".format(e, list_loctions))
list_loctions = []
except Exception as e:
print(e)
finally: