Skip to content

天地图行政区下载

huifer edited this page May 5, 2019 · 1 revision

天地图行政区下载

简单的下载

直接访问 这个网页 就可以获取行政区信息

import requests
import json
import pymongo

def download_region():
    """
    简易的天地图行政区下载
    :return: null
    """

    client = pymongo.MongoClient(host='localhost', port=27017)

    db = client.lvyou
    collection = db.tianditu_zhongguo_region

    r = requests.get(
        'http://api.tianditu.gov.cn/administrative?postStr={"searchWord":"中国","searchType":"1","needSubInfo":"true","needAll":"true","needPolygon":"true","needPre":"true"}&tk=a4ee5c551598a1889adfabff55a5fc27')

    data = json.loads(r.text)
    data = data['data']
    for item in data:
        child_list = item['child']
        for chi in child_list:
            collection.insert(chi)

if __name__ == '__main__':
    download_region()

复杂一些的行政区

  • 对行政区的一些数据加工成geojson的格式主体代码如下
def points_calc(shi, points):
    """
    将文本坐标转换成浮点坐标
    :param shi: 市级行政区名称
    :param points: "116.403 39.972,116.402 39.97,116.405 39.97,116.405 39.964,"
    :return: [[x1,y1],[x2,y2]]
    """
    x_list = []
    y_list = []
    for i in points:
        s = [x for x in i['region'].split(",")]
        s = [x.split(' ') for x in s]
        for j in s:
            x_list.append(eval(j[0]))
            y_list.append(eval(j[1]))

    res = [[x, y] for x, y in zip(x_list, y_list)]

    polygon = {'type': 'Polygon', 'coordinates': [res]}
    return polygon