获取全国各级城市坐标

目录

  • 1. 前言
  • 2. 代码
    • 2.1 获取34个省级行政区邮政编码及对应省会坐标
    • 2.2 给provinces 添加省内城市信息
    • 2.3 给provinces的城市添加县级行政区信息
    • 2.4 解析出各级行政区的经纬度并保存

1. 前言

做项目时经常用到全国各个城市的坐标信息,网上很多都是收费的,而且不全,感谢博文《全国地级市坐标、名称、编码获取 / 全球城市坐标位置》提供了全国各个城市坐标的获取方法,在此基础上补充一些说明及文件。

代码保证可用性。

本文代码运行结果为:《下载-gitee-全国各级行政区经纬度坐标(精确到县级).json》。

2. 代码

2.1 获取34个省级行政区邮政编码及对应省会坐标

import datetime,time,json,requests

def get_provinces_info():
    """
    获取省份邮政编码(adcode)和省会坐标
    """
    data = requests.get('https://geo.datav.aliyun.com/areas/bound/100000_full.json')
    data=data.json()
    provinces=[]
    for item in data['features']:
        # print(item)
        if item['properties']['name']:
            province_item = {
                "name":item['properties']['name'],
                "adcode":item['properties']['adcode'],
                "geo":item['properties']['center']
            }
            # print(province_item)
            provinces.append(province_item)
    return provinces
provinces = get_provinces_info()

2.2 给provinces 添加省内城市信息

def add_cities_to_provinces(provinces):
    for item in tqdm(provinces):
        item['city']=[]
        data = requests.get('https://geo.datav.aliyun.com/areas/bound/%s_full.json'%item['adcode'])
        data = data.json()
        for city in data['features']:
            if city['properties']['name']:
                city_item = {
                    "name": city['properties']['name'],
                    "adcode": city['properties']['adcode'],
                    "geo": city['properties']['center']
                }
                # print(city_item)
                item['city'].append(city_item)
    return provinces
provinces = add_cities_to_provinces(provinces)

2.3 给provinces的城市添加县级行政区信息

def add_counties_to_provinces(provinces):
    for province in tqdm(provinces):
        for city in province['city']:
            city['district']=[]
            data = requests.get('https://geo.datav.aliyun.com/areas/bound/%s_full.json'%city['adcode'])
            if data.status_code==200:
                data = data.json()
                if data:
                    for district in data['features']:
                        if district['properties']['name']:
                            district_item={
                                "name": district['properties']['name'],
                                "adcode": district['properties']['adcode'],
                                "geo": district['properties']['center']
                            }
                            # print(district_item)
                            city['district'].append(district_item)
    return provinces
provinces = add_counties_to_provinces(provinces)

2.4 解析出各级行政区的经纬度并保存

# 解析各级行政区经纬度
all_address={}
for province in tqdm(provinces):
    all_address[province['name']]=province['geo']
    for city in province['city']:
        all_address[city['name']]=city['geo']
        for district in city['district']:
            all_address[district['name']]=district['geo']

# 保存
import json
with open("cities_coordinate.json", "w") as f:
    json.dump(all_address, f)

【完】

你可能感兴趣的:(专栏08-工程开发相关,python,全国城市经纬度)