日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬取高德地图乡镇区行政区划

發布時間:2023/12/10 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬取高德地图乡镇区行政区划 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

適用于爬取一個省、市、縣、區等各個行政級別的行政區劃數據,數據精確到區
級別。
以湖南省為例:

# -*- coding:utf-8 -*- import arcpy import urllib2 import sys import json import math# 申請:高德API的開發者KEY # 版本要求:ArcGis 10.2 Python 2.7 # 輸入:輸入省的名稱 存儲路徑 文件名稱 # 輸出:該省的行政區劃的shp 字段 city citycode district adcode# 獲取行政區劃范圍內的鄉鎮區劃列表 并插入數據 def get_ad(name):url = "http://restapi.amap.com/v3/config/district?extensions=base&subdistrict=3&key="+key+"&keywords="+name# extensions說明:all只返回當前查詢的district邊界坐標點# subdistrict說明:1表示下一級行政區(到市),2表示下兩級行政區(到鄉鎮區縣),3表示下三級行政區(到街道)try:req = urllib2.Request(url)res_data = urllib2.urlopen(req)res = res_data.read()data = json.loads(res)# 返回查詢字段的行政區列表city_list = data['districts'][0]['districts']for city in city_list:district_list = city['districts']districts = {} # 存儲district列表for district in district_list:districts[district['adcode'].encode('utf-8')] = district['name'].encode('utf-8')# 添加數據set_polyline(districts, city['name'].encode('utf-8'), city['citycode'].encode('utf-8'))except urllib2.HTTPError, e:if hasattr(e, "code"):print e.codeif hasattr(e, "reason"):print e.reasondef get_polyline(adcode):url = "http://restapi.amap.com/v3/config/district?extensions=all&subdistrict=0&key=" + key_url = url + "&keywords=" + adcodetry:req = urllib2.Request(_url)res_data = urllib2.urlopen(req)res = res_data.read()data = json.loads(res)polyline = data['districts'][0]['polyline']return polylineexcept urllib2.HTTPError, e:if hasattr(e, "code"):print e.codeif hasattr(e, "reason"):print e.reasondef prepare_shp():fields_long = ['city', 'district']fields_short = ['citycode', 'adcode']length_long = 20length_short = 10sr = arcpy.SpatialReference(4326)if arcpy.Exists(file):arcpy.Delete_management(file)polygon = arcpy.CreateFeatureclass_management(path, shp_name, 'POLYGON', '', '', '', sr)print 'create:' + shp_namefor field in fields_long:arcpy.AddField_management(polygon, field, 'TEXT', '', '', length_long)print 'add filed:' + fieldfor field in fields_short:arcpy.AddField_management(polygon, field, 'TEXT', '', '', length_short)print 'add filed:' + fielddef set_polyline(districts_map, city, citycode):insert_cursor = arcpy.da.InsertCursor(file, ('SHAPE@', 'city', 'district', 'citycode', 'adcode'))for adcode in districts_map.keys():polyline = get_polyline(adcode)array = arcpy.Array()for j in polyline.split('|'):arr = arcpy.Array()for i in j.split(';'):pt = arcpy.Point()pt.X = gcj2wgs(i)[0]pt.Y = gcj2wgs(i)[1]arr.append(pt)array.append(arr)polygon = arcpy.Polygon(array)insert_cursor.insertRow((polygon, city, districts_map[adcode], citycode, adcode))print "data create:" + citydel insert_cursor# 高德火星坐標系轉化為WGS84地理坐標系 def gcj2wgs(loc):lon = float(loc.split(',')[0])lat = float(loc.split(',')[1])a = 6378245.0 # 克拉索夫斯基橢球參數長半軸aee = 0.00669342162296594323 # 克拉索夫斯基橢球參數第一偏心率平方PI = 3.14159265358979324 # 圓周率# 以下為轉換公式x = lon - 105.0y = lat - 35.0# 經度dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))dLon += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0dLon += (20.0 * math.sin(x * PI) + 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0dLon += (150.0 * math.sin(x / 12.0 * PI) + 300.0 * math.sin(x / 30.0 * PI)) * 2.0 / 3.0# 緯度dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))dLat += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0dLat += (20.0 * math.sin(y * PI) + 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0dLat += (160.0 * math.sin(y / 12.0 * PI) + 320 * math.sin(y * PI / 30.0)) * 2.0 / 3.0radLat = lat / 180.0 * PImagic = math.sin(radLat)magic = 1 - ee * magic * magicsqrtMagic = math.sqrt(magic)dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * PI);wgsLon = lon - dLonwgsLat = lat - dLatreturn wgsLon, wgsLatreload(sys) sys.setdefaultencoding('utf-8')key = "你的KEY" search_name = "湖南省" # 輸入要獲取的行政區劃名稱 path = "C:/ArcgisData/data" //選擇一個存儲路徑 shp_name = search_name + ".shp" file = path + "/" + shp_name prepare_shp() get_ad(search_name) print "create success"

總結

以上是生活随笔為你收集整理的python爬取高德地图乡镇区行政区划的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。