省市区(县)行政区划境界线(geojson)获取说明及代码
- 高德地圖行政區(qū)劃查詢接口說明 ? ? ? ?
? ? ? ? 高德地圖WEB服務(wù)API提供了很多實(shí)用的Web接口,申請(qǐng)完高德地圖key(詳見獲取key),就可以使用。本篇博文介紹的行政區(qū)劃境界線下載,使用的是行政區(qū)劃查詢這個(gè)接口,接口訪問url如下:
https://restapi.amap.com/v3/config/district?key=申請(qǐng)的key&keywords=$&subdistrict=0&extensions=all
下面對(duì)這個(gè)接口的關(guān)鍵參數(shù)進(jìn)行簡(jiǎn)單說明:
? ? (1)keywords:查詢關(guān)鍵字,支持 行政區(qū)名稱、citycode、adcode,adcode信息可參考城市編碼表;
? ? (2)subdistrict:子級(jí)行政區(qū),設(shè)置顯示下級(jí)行政區(qū)級(jí)數(shù)(行政區(qū)級(jí)別包括:國(guó)家、省/直轄市、市、區(qū)/縣、鄉(xiāng)鎮(zhèn)/街道多級(jí)數(shù)據(jù)),可選值:0、1、2、3等數(shù)字,并以此類推,含義如下:
? ? ? ? 0:不返回下級(jí)行政區(qū);
? ? ? ? 1:返回下一級(jí)行政區(qū);
? ? ? ? 2:返回下兩級(jí)行政區(qū);
? ? ? ? 3:返回下三級(jí)行政區(qū);
? ? ?特別說明:因需要獲取的是境界線數(shù)據(jù),即使設(shè)置返回下級(jí)行政區(qū),返回的也只是屬性信息,不包含子行政區(qū)的境界線數(shù)據(jù)。
? ? (3)extensions:此項(xiàng)控制行政區(qū)信息中返回行政區(qū)邊界坐標(biāo)點(diǎn); 可選值:base、all;
? ? ? ? base:不返回行政區(qū)邊界坐標(biāo)點(diǎn);
? ? ? ? all:只返回當(dāng)前查詢district的邊界值,不返回子節(jié)點(diǎn)的邊界值;
? ? ? ? 目前不能返回鄉(xiāng)鎮(zhèn)/街道級(jí)別的邊界值
? ? 因此,在本例中需要設(shè)置為?all。
- 抓取省市縣等行政區(qū)劃數(shù)據(jù)思路
? ? ? ? (1)指定抓取的區(qū)域,可為adcode,citycodes或行政區(qū)劃名稱;
? ? ? ? (2)利用高德地圖接口請(qǐng)求數(shù)據(jù);
? ? ? ? (3)對(duì)邊界點(diǎn)數(shù)據(jù)進(jìn)行解析,并處理成geojson格式;
? ? ? ? ? ? ? ? 解析過程中,要注意多部件的問題(即MultiPolygon的情況)。
? ? ? ? (4)將結(jié)果保存成json格式。
- 抓取代碼(真實(shí)可用)
? ? ? ? 下面是抓取全國(guó)省級(jí)行政區(qū)邊界線并將所有數(shù)據(jù)最終處理成geojson格式并保存的代碼,本例親自編寫并測(cè)試有效,數(shù)據(jù)已應(yīng)用在實(shí)際項(xiàng)目中,需要的可直接使用。
''' Created on 2018年12月26日@author: QinChao @description:請(qǐng)求高德地圖行政區(qū)劃數(shù)據(jù) '''adcodes = {'110000':'北京市','120000':'天津市','130000':'河北省','140000':'山西省','150000':'內(nèi)蒙古自治區(qū)','210000':'遼寧省','220000':'吉林省','230000':'黑龍江省','310000':'上海市','320000':'江蘇省','330000':'浙江省','340000':'安徽省','350000':'福建省','360000':'江西省','370000':'山東省','410000':'河南省','420000':'湖北省','430000':'湖南省','440000':'廣東省','450000':'廣西壯族自治區(qū)','460000':'海南省','500000':'重慶市','510000':'四川省','520000':'貴州省','530000':'云南省','540000':'西藏自治區(qū)','610000':'陜西省','620000':'甘肅省','630000':'青海省','640000':'寧夏回族自治區(qū)','650000':'新疆維吾爾自治區(qū)','710000':'臺(tái)灣省','810000':'香港特別行政區(qū)','820000':'澳門特別行政區(qū)','900000':'外國(guó)' }url = 'https://restapi.amap.com/v3/config/district?key=申請(qǐng)的高德地圖key&keywords=$&subdistrict=0&extensions=all'import urllib.request import simplejson import jsondef 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('|')# 由多部分構(gòu)成,為多部件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'):'''文本內(nèi)容寫入file_path--- 寫入文件路徑content--- 寫入內(nèi)容file_type--- 文件格式, 默認(rèn)為txt, 其他包括jsonmode--- 文件打開模式'''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')?
總結(jié)
以上是生活随笔為你收集整理的省市区(县)行政区划境界线(geojson)获取说明及代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果计算机次方怎么按,详细的mac计算器
- 下一篇: Vmware Linux虚拟机硬盘扩容