用Python历时十多天完成的全国所有区县经纬度采集

在高德坐标拾取器(坐标拾取器 | 高德地图API)中,如果输入北京,坐标获取结果为:116.407387,39.904179,这是经纬度坐标,在高德公开的全国所有区县编码表(https://a.amap.com/lbs/static/code_resource/AMap_adcode_citycode.zip)中,有3241行数据,字段分别为中文名,adcode,citycode,其中citycode就相当于电话区号,而adcode比较精确,每个区县是不同的,如果adcode最末位为0,表示前三位和它相同的adcode都是它的下辖区县。

中文名adcodecitycode
中华人民共和国100000
北京市110000010
东城区110101010

不过这张表没有给出经纬度。

可以使用高德开放平台(高德开放平台 | 高德地图API)的API采集经纬度,需要用到的是搜索POI。

搜索POI产品介绍

搜索服务API是一类简单的HTTP接口,提供多种查询POI信息的能力,其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制。

使用API前您需先申请Key,若无高德地图API账号需要先申请账号。

注意:在此接口之中,您可以通过city&citylimit参数指定希望搜索的城市或区县。而city参数能够接收citycode和adcode,citycode仅能精确到城市,而adcode却能够精确到区县。

具体参数详见(搜索POI-高级 API 文档-开发指南-Web服务 API | 高德地图API

本次请求的url= 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)

其中city=adcode,keywords=中文名

为了统一请求,提供一个通用的请求函数

def openUrl(_url):
    # 设置请求头 request header
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
    }
    response = requests.get(_url, headers=headers)
    if response.status_code == 200:  # 如果状态码为200,寿命服务器已成功处理了请求,则继续处理数据
        return response.content
    else:
        return None

对每个地点都调用获取经纬度,返回POI的第一个

def get_location(city,keywords):
    # return str(Lon,Lat)
    _url = 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
    content = openUrl(_url,False)
    if content:
        res = json.loads(content)
        pois = res.get('pois')
        if len(pois) > 0:
            return pois[0].get('location')
        else:
            return None
    print('request not return 200')
    return None

为了能方便存取,先把从高德下载的excel表转存为csv,分别由两个函数存取

def write_csv(filepath, data, head=None):
    if head:
        data = [head] + data
    with open(filepath, mode='w', encoding='UTF-8', newline='') as f:
        writer = csv.writer(f)
        for i in data:
            writer.writerow(i)

def read_csv(filepath):
    data = []
    if os.path.exists(filepath):
        with open(filepath, mode='r', encoding='utf-8') as f:
            lines = csv.reader(f)  # #此处读取到的数据是将每行数据当做列表返回的
            for line in lines:
                data.append(line)
        return data
    else:
        print('filepath is wrong:{}'.format(filepath))
        return []

最后,构建一个循环来一行一行地采集

# 处理一个地点经纬度后更新csv
def process_lon_lat():
    data = read_csv(CSV_PATH)
    header = data[0]
    data = data[1:]
    data_new = []
    done = False
    for city in data:
        if not done and not city[3]:
            loc = get_location(city[1],city[0])
            if loc:
                loc_split = loc.split(',')
                print(city,loc_split)
                data_new.append([city[0],city[1],city[2],loc_split[0],loc_split[1]])
                done = True
            else:
                data_new.append([city[0],city[1],city[2],'None','None'])
                print(city,'None')
        else:
            data_new.append(city)
    write_csv(CSV_PATH, data_new, header)

# 更新csv经纬度主控
def LonLat_update():
    data = read_csv(CSV_PATH)
    data = data[1:]
    for city in data:
        process_lon_lat()
        time.sleep(6)

由于API的使用配额是每天100次请求这个接口,我不得不使用了两个key来采集,总共历时十多天才完成,每天半小时,采集好的数据样例如下(完整的已传到我的资源中,https://download.csdn.net/download/cnnews/88938675):

中文名adcodecitycodeLonLat
北京市110000010116.40738739.904179
东城区110101010116.41633439.928359
西城区110102010116.3658539.9126

完整代码:

# coding: utf-8
import os
import requests
import csv
import json
import time

CSV_PATH = 'AMap_adcode_citycode.csv'

def openUrl(_url):
    # 设置请求头 request header
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
    }
    response = requests.get(_url, headers=headers)
    if response.status_code == 200:  # 如果状态码为200,寿命服务器已成功处理了请求,则继续处理数据
        return response.content
    else:
        return None

def write_csv(filepath, data, head=None):
    if head:
        data = [head] + data
    with open(filepath, mode='w', encoding='UTF-8', newline='') as f:
        writer = csv.writer(f)
        for i in data:
            writer.writerow(i)

def read_csv(filepath):
    data = []
    if os.path.exists(filepath):
        with open(filepath, mode='r', encoding='utf-8') as f:
            lines = csv.reader(f)  # #此处读取到的数据是将每行数据当做列表返回的
            for line in lines:
                data.append(line)
        return data
    else:
        print('filepath is wrong:{}'.format(filepath))
        return []

##################################################################################
# 获取地点经纬度
# {'status': '0', 'info': 'USER_DAILY_QUERY_OVER_LIMIT', 'infocode': '10044'}
def get_location(city,keywords):
    # return str(Lon,Lat)
    _url = 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
    content = openUrl(_url,False)
    if content:
        res = json.loads(content)
        pois = res.get('pois')
        if len(pois) > 0:
            return pois[0].get('location')
        else:
            return None
    print('request not return 200')
    return None

# 处理一个地点经纬度后更新csv
def process_lon_lat():
    data = read_csv(CSV_PATH)
    header = data[0]
    data = data[1:]
    data_new = []
    done = False
    for city in data:
        if not done and not city[3]:
            loc = get_location(city[1],city[0])
            if loc:
                loc_split = loc.split(',')
                print(city,loc_split)
                data_new.append([city[0],city[1],city[2],loc_split[0],loc_split[1]])
                done = True
            else:
                data_new.append([city[0],city[1],city[2],'None','None'])
                print(city,'None')
        else:
            data_new.append(city)
    write_csv(CSV_PATH, data_new, header)

# 更新csv经纬度主控
def LonLat_update():
    data = read_csv(CSV_PATH)
    data = data[1:]
    for city in data:
        process_lon_lat()
        time.sleep(6)

if __name__ == '__main__':
    LonLat_update()

  • 36
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要获取中国所有环保局的经纬度坐标,可以通过以下步骤实现: 1. 获取中国所有环保局的地址信息,可以从官方网站或其他公开数据源中获取。 2. 使用Python中的Geopy库,将地址转换为经纬度坐标。可以使用geopy.geocoders.Nominatim类中的方法进行地址解析,返回的结果包括经纬度坐标信息。 3. 将获取到的经纬度坐标保存到一个数据文件中,可以使用Python中的csv或pandas库进行保存。 以下是一个示例代码,仅供参考: ```python import csv import time from geopy.geocoders import Nominatim # 读取环保局地址信息 with open('epa_address.csv', 'r', encoding='utf-8') as f: reader = csv.reader(f) epa_list = [row[0] for row in reader] # 创建Geopy的Nominatim对象 geolocator = Nominatim(user_agent='my_application') # 解析地址并获取经纬度坐标 result = [] for address in epa_list: try: location = geolocator.geocode(address, timeout=10) if location is not None: lat, lon = location.latitude, location.longitude result.append([address, lat, lon]) else: result.append([address, '', '']) except Exception as e: print('Error: ', e) time.sleep(1) # 限制请求频率,避免被限制访问 # 将结果保存到CSV文件中 with open('epa_location.csv', 'w', encoding='utf-8', newline='') as f: writer = csv.writer(f) writer.writerow(['address', 'latitude', 'longitude']) writer.writerows(result) ``` 注意:以上代码仅供参考,实际应用中需要根据具体情况进行修改。在运行之前,需要安装Geopy库并获取访问Nominatim API的权限。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值