Python调用高德地图web服务API实现地址与经纬度批量转换及地图可视化操作
程序员文章站
2022-06-04 16:29:55
...
网上在线经纬度批量转换资源(准确度稍微有偏差):
1、经纬度在线查询,地名(批量)查询经纬度,经纬度(批量)查询地名
高德API官方文档地址:
地理/逆地理编码:https://lbs.amap.com/api/webservice/guide/api/georegeo
坐标转换:http://lbs.amap.com/api/webservice/guide/api/convert
调用高德地图API前,需先申请高德地图key,https://lbs.amap.com/
申请高德地图key详细步骤:https://www.jianshu.com/p/9129a5f45c99
单条地址转经纬度:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:ZSW
@file:long_lat.py
@time:2020/11/07
Python调用高德地图web服务API实现地理/逆地址编码
"""
import requests
import json
def geocode(address):
params = {'address':address,
'key':'你的高德**',
# 'city':'上海市'
}
url = 'https://restapi.amap.com/v3/geocode/geo'
res = requests.get(url, params)
# 输出结果为json,将其转为字典格式
result = json.loads(res.text)
# 或者直接调用json的方法,返回python下的字典数据
# result = res.json()
# print(result)
res = result['geocodes'][0]['location']
return res
if __name__=='__main__':
address = '上海市徐汇区'
res = geocode(address)
print(res)
利用高德地图api实现 地址 到 经纬度 的批量转换
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:ZSW
@file:batch_addr_longlat.py
@time:2020/11/09
利用高德地图api实现 地址 到 经纬度 的批量转换
"""
import requests
import pandas as pd
import json
# 读取csv文件中 organization 的列,并添加在列表中,示例['地址1','地址2',……,'地址n']
def parse_csv_addr():
datas = []
totalListData = pd.read_csv('test.csv')
totalListDict = totalListData.to_dict('index')
for i in range(0,len(totalListDict)):
datas.append(str(totalListDict[i]['organization']))
return datas
# 地址转经纬度 选用的是地理编码的接口
def geocode(address):
params = {
'address': address,
'key': '你的高德**',
'city': '上海市'
}
url = 'https://restapi.amap.com/v3/geocode/geo'
data = requests.get(url, params)
result = data.json()
res = result['geocodes'][0]['location']
print(res)
return res
if __name__ == '__main__':
i = 0
count = 0
df = pd.DataFrame(columns=['long_lat'])
locations = parse_csv_addr()
for location in locations:
long_lat = geocode(location)
df.loc[i] = [long_lat]
i = i + 1
df.to_csv('locdetail1.csv', index=False, encoding='utf-8')
利用高德地图api实现 经纬度 到 地址 的批量转换
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:ZSW
@file:batch_longlat_addr.py
@time:2020/11/09
利用高德地图api实现 经纬度 到 地址 的批量转换
"""
import requests
import pandas as pd
# 读取csv文件,将列longitude 和列latitude 进行拼接 并添加在列表中 示例['123.57293,32.287868',……]
def parse_csv_longlat():
datas = []
totalListData = pd.read_csv('test.csv')
totalListDict = totalListData.to_dict('index')
for i in range(0, len(totalListDict)):
datas.append(str(totalListDict[i]['longitude']) + ',' + str(totalListDict[i]['latitude']))
return datas
# 为了使用高德服务,用户需要将非高德坐标转换为高德坐标。
# 将用户输入的非高德坐标(GPS坐标、mapbar坐标、baidu坐标)转换成高德坐标
def transform(location):
params = {'coordsys': 'gps', 'locations': location, 'key': '你的高德**'}
url = 'http://restapi.amap.com/v3/assistant/coordinate/convert'
result = requests.get(url, params)
res = result.json()
return res['locations']
# 纬度转换为地址,选用的是逆地理编码的接口
def regeocode(location):
params = {'location': location, 'key': '你的高德**'}
url = 'http://restapi.amap.com/v3/geocode/regeo'
result = requests.get(url, params)
res = result.json()
return res['regeocode']['addressComponent']['district'], res['regeocode'][
'formatted_address']
if __name__ == '__main__':
i = 0
count = 0
df = pd.DataFrame(columns=['location', 'detail'])
locations = parse_csv_longlat()
for location in locations:
dist, detail = regeocode(transform(location))
df.loc[i] = [dist, detail]
i = i + 1
df.to_csv('locdetail.csv', index=False, encoding='utf-8')
地图可视化
登录高德开放平台后, 点击头像,即可看到 数据可视化平台,点击进去,如下:
里面有相关的案例教程:
简单实例, 经纬度坐标可视化,导入csv文件,选择需要可视化的经纬度列即可(平台中一些细节配置可以自己去尝试):