python-批量将经纬度的地址逆向解析为实际地址

功能

利用百度地图API提供的全球逆地理编码服务可以实现经纬度批量转地址的功能。

数据的excel

image

申请自己的AK

https://lbsyun.baidu.com/index.php?title=jspopular3.0/guide/getkey
注意一定要选择服务器端!

代码

# -*- coding: utf-8 -*-
import pandas	#读取excel表
import json	#数据格式转换
from urllib.request import urlopen	#获取网页


# 原数据文件格式: 纬度 + 经度
origin_path = r'1.xlsx'   # 原始坐标文件路径
result_path = r'2.xlsx'   # 爬取数据文件保存路径

"""  # 百度地图提供的api服务网址
'https://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194'
"""

dfBase = pandas.read_excel(origin_path)
# dfBase.head()

dataList = []  # 储存获取的路线数据

for i in range(0,28196):	#有多少数据n就写多少
    print(i)
    out_lat = dfBase.at[i, '经度']
    #print(out_lat)
    out_lng = dfBase.at[i, '纬度']
    #print(out_lat,out_lng)
    url_Geocoder = r"https://api.map.baidu.com/reverse_geocoding/v3/?ak=自己申请的AK&output=json&coordtype=bd09llll&location={0},{1}".format(out_lng,out_lat)

    result_Geocoder = json.loads(urlopen(url_Geocoder).read())  # json转dict
    #print(result_Geocoder)
    status_Geocoder = result_Geocoder['status']

    if status_Geocoder == 0:  # 状态码为0:无异常
        loc = result_Geocoder['result']['formatted_address']
        #print(loc)
        dataList.append([loc])
        dfAll = pandas.DataFrame(dataList, columns=['loc'])

    dfAll.to_excel(result_path)
print('Done!')

结果预览

image

参考博客

  1. https://blog.csdn.net/qq_57240976/article/details/120026276
  2. https://blog.csdn.net/this_zq/article/details/124663604

别的方法

https://map.yanue.net/

image

posted @ 2023-02-16 15:09  司砚章  阅读(336)  评论(0编辑  收藏  举报