- 高德地圖行政區劃查詢接口說明
高德地圖WEB服務API提供了很多實用的Web接口,申請完高德地圖key(詳見獲取key),就可以使用。本篇博文介紹的行政區劃境界線下載,使用的是行政區劃查詢這個接口,接口訪問url如下:
https://restapi.amap.com/v3/config/district?key=申請的key&keywords=$&subdistrict=0&extensions=all
下面對這個接口的關鍵參數進行簡單說明:
(1)keywords:查詢關鍵字,支持 行政區名稱、citycode、adcode,adcode信息可參考城市編碼表;
(2)subdistrict:子級行政區,設置顯示下級行政區級數(行政區級別包括:國家、省/直轄市、市、區/縣、鄉鎮/街道多級數據),可選值:0、1、2、3等數字,並以此類推,含義如下:
0:不返回下級行政區;
1:返回下一級行政區;
2:返回下兩級行政區;
3:返回下三級行政區;
特別說明:因需要獲取的是境界線數據,即使設置返回下級行政區,返回的也只是屬性信息,不包含子行政區的境界線數據。
(3)extensions:此項控制行政區信息中返回行政區邊界座標點; 可選值:base、all;
base:不返回行政區邊界座標點;
all:只返回當前查詢district的邊界值,不返回子節點的邊界值;
目前不能返回鄉鎮/街道級別的邊界值
因此,在本例中需要設置爲 all。
- 抓取省市縣等行政區劃數據思路
(1)指定抓取的區域,可爲adcode,citycodes或行政區劃名稱;
(2)利用高德地圖接口請求數據;
(3)對邊界點數據進行解析,並處理成geojson格式;
解析過程中,要注意多部件的問題(即MultiPolygon的情況)。
(4)將結果保存成json格式。
- 抓取代碼(真實可用)
下面是抓取全國省級行政區邊界線並將所有數據最終處理成geojson格式並保存的代碼,本例親自編寫並測試有效,數據已應用在實際項目中,需要的可直接使用。
'''
Created on 2018年12月26日
@author: QinChao
@description:請求高德地圖行政區劃數據
'''
adcodes = {
'110000':'北京市',
'120000':'天津市',
'130000':'河北省',
'140000':'山西省',
'150000':'內蒙古自治區',
'210000':'遼寧省',
'220000':'吉林省',
'230000':'黑龍江省',
'310000':'上海市',
'320000':'江蘇省',
'330000':'浙江省',
'340000':'安徽省',
'350000':'福建省',
'360000':'江西省',
'370000':'山東省',
'410000':'河南省',
'420000':'湖北省',
'430000':'湖南省',
'440000':'廣東省',
'450000':'廣西壯族自治區',
'460000':'海南省',
'500000':'重慶市',
'510000':'四川省',
'520000':'貴州省',
'530000':'雲南省',
'540000':'西藏自治區',
'610000':'陝西省',
'620000':'甘肅省',
'630000':'青海省',
'640000':'寧夏回族自治區',
'650000':'新疆維吾爾自治區',
'710000':'臺灣省',
'810000':'香港特別行政區',
'820000':'澳門特別行政區'
,'900000':'外國'
}
url = 'https://restapi.amap.com/v3/config/district?key=申請的高德地圖key&keywords=$&subdistrict=0&extensions=all'
import urllib.request
import simplejson
import json
def request_district(keyword):
print('key:', url.replace('$', keyword))
return simplejson.loads(urllib.request.urlopen(url.replace('$', keyword)).read())
def struct_feature(coords, adcodes, name):
return {
'type': 'Feature',
'geometry': {
'type': coords['type'],
'coordinates': coords['coords']
},
'properties':{
'adcodes': adcodes,
'name': name
}
}
def struct_coordinates(data):
coordinates = []
type = 'Polygon'
districts = data['districts'][0]
polyline_str = districts['polyline']
district = polyline_str.split('|')
# 由多部分構成,爲多部件
if len(district) > 1:
type = 'MultiPolygon'
for part in district:
coordinate = []
coords = part.split(';')
for coord in coords:
x, y = coord.split(',')
coordinate.append([float(x), float(y)])
if len(coordinate) > 200:
coordinates.append([coordinate])
else:
coordinate = []
coords = district[0].split(';')
for coord in coords:
x, y = coord.split(',')
coordinate.append([float(x), float(y)])
coordinates.append(coordinate)
return {
'type': type,
'coords': coordinates
}
def write_content(file_path, content, file_type = 'txt', mode = 'w'):
'''
文本內容寫入
file_path--- 寫入文件路徑
content--- 寫入內容
file_type--- 文件格式, 默認爲txt, 其他包括json
mode--- 文件打開模式
'''
with open(file_path, mode) as f:
if file_type == 'txt':
f.writelines(content)
elif file_type == 'json':
json.dump(content, f)
f.flush()
f.close()
if __name__ == "__main__":
province_geojson = {
'type': 'FeatureCollection',
'features': []
}
for adcode in adcodes.keys():
print('adcode:', adcode)
src_data = request_district(adcode)
coordinates = struct_coordinates(src_data)
feature = struct_feature(coordinates, adcode, adcodes[adcode])
province_geojson['features'].append(feature)
write_content('G:/heze.json', province_geojson, 'json')