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

【森气杂谈】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 可视化如下:

在这里插入图片描述

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

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值