在高德坐标拾取器(坐标拾取器 | 高德地图API)中,如果输入北京,坐标获取结果为:116.407387,39.904179,这是经纬度坐标,在高德公开的全国所有区县编码表(https://a.amap.com/lbs/static/code_resource/AMap_adcode_citycode.zip)中,有3241行数据,字段分别为中文名,adcode,citycode,其中citycode就相当于电话区号,而adcode比较精确,每个区县是不同的,如果adcode最末位为0,表示前三位和它相同的adcode都是它的下辖区县。
中文名 | adcode | citycode |
中华人民共和国 | 100000 | |
北京市 | 110000 | 010 |
东城区 | 110101 | 010 |
不过这张表没有给出经纬度。
可以使用高德开放平台(高德开放平台 | 高德地图API)的API采集经纬度,需要用到的是搜索POI。
搜索POI产品介绍
搜索服务API是一类简单的HTTP接口,提供多种查询POI信息的能力,其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制。
使用API前您需先申请Key,若无高德地图API账号需要先申请账号。
注意:在此接口之中,您可以通过city&citylimit参数指定希望搜索的城市或区县。而city参数能够接收citycode和adcode,citycode仅能精确到城市,而adcode却能够精确到区县。
具体参数详见(搜索POI-高级 API 文档-开发指南-Web服务 API | 高德地图API)
本次请求的url= 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
其中city=adcode,keywords=中文名
为了统一请求,提供一个通用的请求函数
def openUrl(_url):
# 设置请求头 request header
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
}
response = requests.get(_url, headers=headers)
if response.status_code == 200: # 如果状态码为200,寿命服务器已成功处理了请求,则继续处理数据
return response.content
else:
return None
对每个地点都调用获取经纬度,返回POI的第一个
def get_location(city,keywords):
# return str(Lon,Lat)
_url = 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
content = openUrl(_url,False)
if content:
res = json.loads(content)
pois = res.get('pois')
if len(pois) > 0:
return pois[0].get('location')
else:
return None
print('request not return 200')
return None
为了能方便存取,先把从高德下载的excel表转存为csv,分别由两个函数存取
def write_csv(filepath, data, head=None):
if head:
data = [head] + data
with open(filepath, mode='w', encoding='UTF-8', newline='') as f:
writer = csv.writer(f)
for i in data:
writer.writerow(i)
def read_csv(filepath):
data = []
if os.path.exists(filepath):
with open(filepath, mode='r', encoding='utf-8') as f:
lines = csv.reader(f) # #此处读取到的数据是将每行数据当做列表返回的
for line in lines:
data.append(line)
return data
else:
print('filepath is wrong:{}'.format(filepath))
return []
最后,构建一个循环来一行一行地采集
# 处理一个地点经纬度后更新csv
def process_lon_lat():
data = read_csv(CSV_PATH)
header = data[0]
data = data[1:]
data_new = []
done = False
for city in data:
if not done and not city[3]:
loc = get_location(city[1],city[0])
if loc:
loc_split = loc.split(',')
print(city,loc_split)
data_new.append([city[0],city[1],city[2],loc_split[0],loc_split[1]])
done = True
else:
data_new.append([city[0],city[1],city[2],'None','None'])
print(city,'None')
else:
data_new.append(city)
write_csv(CSV_PATH, data_new, header)
# 更新csv经纬度主控
def LonLat_update():
data = read_csv(CSV_PATH)
data = data[1:]
for city in data:
process_lon_lat()
time.sleep(6)
由于API的使用配额是每天100次请求这个接口,我不得不使用了两个key来采集,总共历时十多天才完成,每天半小时,采集好的数据样例如下(完整的已传到我的资源中,https://download.csdn.net/download/cnnews/88938675):
中文名 | adcode | citycode | Lon | Lat |
北京市 | 110000 | 010 | 116.407387 | 39.904179 |
东城区 | 110101 | 010 | 116.416334 | 39.928359 |
西城区 | 110102 | 010 | 116.36585 | 39.9126 |
完整代码:
# coding: utf-8
import os
import requests
import csv
import json
import time
CSV_PATH = 'AMap_adcode_citycode.csv'
def openUrl(_url):
# 设置请求头 request header
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
}
response = requests.get(_url, headers=headers)
if response.status_code == 200: # 如果状态码为200,寿命服务器已成功处理了请求,则继续处理数据
return response.content
else:
return None
def write_csv(filepath, data, head=None):
if head:
data = [head] + data
with open(filepath, mode='w', encoding='UTF-8', newline='') as f:
writer = csv.writer(f)
for i in data:
writer.writerow(i)
def read_csv(filepath):
data = []
if os.path.exists(filepath):
with open(filepath, mode='r', encoding='utf-8') as f:
lines = csv.reader(f) # #此处读取到的数据是将每行数据当做列表返回的
for line in lines:
data.append(line)
return data
else:
print('filepath is wrong:{}'.format(filepath))
return []
##################################################################################
# 获取地点经纬度
# {'status': '0', 'info': 'USER_DAILY_QUERY_OVER_LIMIT', 'infocode': '10044'}
def get_location(city,keywords):
# return str(Lon,Lat)
_url = 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
content = openUrl(_url,False)
if content:
res = json.loads(content)
pois = res.get('pois')
if len(pois) > 0:
return pois[0].get('location')
else:
return None
print('request not return 200')
return None
# 处理一个地点经纬度后更新csv
def process_lon_lat():
data = read_csv(CSV_PATH)
header = data[0]
data = data[1:]
data_new = []
done = False
for city in data:
if not done and not city[3]:
loc = get_location(city[1],city[0])
if loc:
loc_split = loc.split(',')
print(city,loc_split)
data_new.append([city[0],city[1],city[2],loc_split[0],loc_split[1]])
done = True
else:
data_new.append([city[0],city[1],city[2],'None','None'])
print(city,'None')
else:
data_new.append(city)
write_csv(CSV_PATH, data_new, header)
# 更新csv经纬度主控
def LonLat_update():
data = read_csv(CSV_PATH)
data = data[1:]
for city in data:
process_lon_lat()
time.sleep(6)
if __name__ == '__main__':
LonLat_update()
标签:十多天,None,return,经纬度,city,Python,csv,data,loc
From: https://blog.csdn.net/cnnews/article/details/136634259