【森气杂谈】Python批量获取地址的经纬度信息

01 引言:
今天记录分享一下调用高德地图开放平台,根据具体文本地址信息批量获取对应经纬度信息。
02 注册高德开放平台:

03 代码如下:
# -*- encoding: utf-8 -*-
'''
@File : address2location.py
@Time : 2022/06/22 13:44:17
@Author : HMX
@Version : 1.0
@Contact : kdhb8023@163.com
'''
# here put the import lib
import requests
def adr2loc(address):
url = 'https://restapi.amap.com/v3/geocode/geo?address='+address+'&key=xxxxxxxxxxxxxxxxxxxxxxxxx'
response = requests.get(url).json()
geocodes = response['geocodes']
for geocode in geocodes:
location = geocode['location']
# print(location)
return location
loc = adr2loc('江苏省南京市玄武区南京林业大学')
print(loc)
04 实例–获取江苏地级市经纬度:
# here put the import lib
from pyproj import transform
import requests
import matplotlib.pyplot as plt
import matplotlib as mpl
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
import cartopy.feature as cfeature
from cnmaps import get_adm_maps, draw_maps
def adr2loc(address):
url = 'https://restapi.amap.com/v3/geocode/geo?address='+address+'&key=xxxxxxxxxxxxxxxxxxxxxxxxx'
response = requests.get(url).json()
geocodes = response['geocodes']
for geocode in geocodes:
location = geocode['location']
# print(location)
return location
def cm2inch(x,y):
return x/2.54,y/2.54
size1 = 10.5
fontdict = {'weight': 'bold','size':size1,'color':'k','family':'SimHei'}
mpl.rcParams.update(
{
'text.usetex': False,
'font.family': 'stixgeneral',
'mathtext.fontset': 'stix',
"font.family":'serif',
"font.size": size1,
"font.serif": ['Times New Roman'],
}
)
city= ['南京','苏州','无锡','常州','镇江','扬州','泰州','南通','淮安','连云'',','盐城','徐州','宿迁']
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cnmaps import get_adm_maps, draw_map
import numpy as np
fig = plt.figure(figsize=cm2inch(8,6))
proj = ccrs.PlateCarree()
ax = fig.add_subplot(111, projection=proj)
extent = [116,122,30,36]
ax.set_xticks(np.arange(extent[0], extent[1] + 1, 2), crs = proj)
ax.set_yticks(np.arange(extent[-2], extent[-1] + 1, 2), crs = proj)
ax.minorticks_on()
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=False))
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.set_extent(extent)
draw_maps(get_adm_maps(province='江苏省', level='市'), color='k', linewidth=0.8)
ax.stock_img()
for i in city:
loc = adr2loc('江苏省'+i)
print(loc)
loc = loc.split(',')
lon = float(loc[0])
lat = float(loc[-1])
ax.scatter(lon,lat,c = 'k',s = 3,zorder = 10,transform = proj)
ax.imshow(plt.imread(r'E:\Project\World\HYP_LR_SR_OB_DR\HYP_LR_SR_OB_DR.tif'), origin='upper', transform=proj, extent=[-180, 180, -90, 90])
plt.savefig(r'.\address.png',dpi = 600)
plt.show()
05 输出结果如下:
118.796877,32.060255
120.585315,31.298886
120.311910,31.491169
119.973987,31.810689
119.425836,32.187849
119.412966,32.394210
119.923116,32.455778
120.894291,31.980171
119.015285,33.610353
119.338788,34.760249
120.163561,33.347382
117.284124,34.205768
118.275198,33.963232
06 可视化如下:

如果对你有帮助的话,请‘点赞’、‘收藏’,‘关注’,你们的支持是我更新的动力。
欢迎关注公众号【森气笔记】。

该博客介绍了如何利用Python调用高德地图开放平台的API,批量获取文本地址对应的经纬度信息。通过示例代码展示了从江苏省的地级市名称到经纬度的转换过程,并实现了将这些经纬度信息用于地图可视化的功能。
1808

被折叠的 条评论
为什么被折叠?



