阿里云提供的地理信息接口

https://datav.aliyun.com/tools/atlas/

有两个接口, 一个是[行政编码].json, 一个是[行政编码]_full.json, 从接口中可以提取到区县一级的行政区划信息. 提取的过程中遇到的一些问题:

  • 从[行政编码].json中读取的信息中, 可能parent = null, 出现这种情况的大都是一些撤县改区的节点, 要将其设为上一级节点的行政编码
  • 从[行政编码].json中读到的parent的adcode, 可能与[父节点行政编码]_full.json中读到的parent的adcode不一致, 例如从110000_full.json中得到的节点列表, 其parent都是110000, 但是在取其字节点110101.json时会发现, parent变成了110100, 这时候要使用110100这个行政编码
  • 因为从上至下遍历时, 是不会遇到110100这个节点的, 所以在遍历的过程中, 要检查是否出现了未知的行政编码, 如果有, 需要额外读取并入库
  • 有部分节点, 其json无法读取(不存在), 例如密云110118.json, 延庆110119.json, 这时候要用前一步得到的信息入库

使用生成的行政区划数据时, 对于香港澳门的数据, 因为没有level=city的这一级, 所以需要特殊处理一下, 例如在读取province这一级的子节点时, 如果发现没有level=city的节点, 那么就返回一个虚拟的节点, 这个节点各字段值和自己一样, 但是level=city.

#!/usr/bin/python3
# -*- coding: UTF-8 -*-import json
import traceback
import rbcommondef readRegion(adcode, parent_code = None):# https://geo.datav.aliyun.com/areas/bound/140000.jsonurl = 'https://geo.datav.aliyun.com/areas/bound/' + adcode + '.json'print(url)echo = rbcommon.requestGet(url, 'UTF-8', 20, 10)if echo is None:print('URL request failed: ' + url)returnelif echo.find('<?') == 0:print('Not found: ' + url)return# print(echo)json_obj = json.loads(echo)region = {}region['name'] = json_obj['features'][0]['properties']['name']region['adcode'] = json_obj['features'][0]['properties']['adcode']region['telecode'] = json_obj['features'][0]['properties']['telecode']level = json_obj['features'][0]['properties']['level']if (level == 'country'):region['level'] = 0elif (level == 'province'):region['level'] = 1elif (level == 'city'):region['level'] = 2elif (level == 'district'):region['level'] = 3if ('parent' in json_obj['features'][0]['properties']) and (not json_obj['features'][0]['properties']['parent'] is None):region['parent'] = json_obj['features'][0]['properties']['parent']['adcode']else:region['parent'] = parent_code# read sub regionssub_regions = []region['children'] = sub_regions# https://geo.datav.aliyun.com/areas/bound/140000_full.jsonurl = 'https://geo.datav.aliyun.com/areas/bound/' + adcode + '_full.json'print(url)echo = rbcommon.requestGet(url, 'UTF-8', 20, 10)if echo is None:print('URL request failed: ' + url)return regionelif echo.find('<?') == 0:print('Not found: ' + url)return region# print(echo)json_obj = json.loads(echo)sub_objs = json_obj['features']for sub_obj in sub_objs:sub_region = {}sub_region['adcode'] = (str)(sub_obj['properties']['adcode'])if (sub_region['adcode'] == region['adcode']):continuesub_region['name'] = sub_obj['properties']['name']sub_region['telecode'] = Nonelevel = sub_obj['properties']['level']if (level == 'country'):sub_region['level'] = 0elif (level == 'province'):sub_region['level'] = 1elif (level == 'city'):sub_region['level'] = 2elif (level == 'district'):sub_region['level'] = 3sub_region['parent'] = adcodesub_regions.append(sub_region)# further check if the parent adcode is correctif (len(sub_regions) > 0):# https://geo.datav.aliyun.com/areas/bound/140000.jsonurl = 'https://geo.datav.aliyun.com/areas/bound/' + sub_regions[0]['adcode'] + '.json'# print(url)echo = rbcommon.requestGet(url, 'UTF-8', 20, 10)if echo is None:print('URL request failed: ' + url)elif echo.find('<?') == 0:print('Not found: ' + url)else:json_obj = json.loads(echo)if ('parent' in json_obj['features'][0]['properties']) and (not json_obj['features'][0]['properties']['parent'] is None):dummy_parent = json_obj['features'][0]['properties']['parent']['adcode']if (dummy_parent != sub_regions[0]['parent']):print('Update parent from {} to {}', sub_regions[0]['parent'], dummy_parent)for sub_region in sub_regions:sub_region['parent'] = dummy_parentreturn regiondef readAllRegion(parent_region):region = readRegion(parent_region['adcode'], parent_region['parent'])if not region is None:if (not region['parent'] is None) and (not region['parent'] in regions):new_region = readRegion(region['parent'], parent_region['parent'])if not new_region is None:regions.add(new_region['adcode'])insert(new_region)regions.add(region['adcode'])insert(region)for sub_region in region['children']:readAllRegion(sub_region)else:regions.add(parent_region['adcode'])insert(parent_region)def insert(region):try:with rbcommon.mysqlclient.cursor() as cursor:sql = 'INSERT IGNORE INTO `s_region` (`id`, `parent_id`, `level`, `name`, `tele_code`, `short_name`, ' \'`full_name`) VALUES (%s, %s, %s, %s, %s, %s, %s)'cursor.execute(sql, (region['adcode'],None if (not 'parent' in region) else region['parent'],region['level'],region['name'],region['telecode'],region['name'],'{}'))rbcommon.mysqlclient.commit()except Exception as e:print(json.dumps(region))traceback.print_exc()### MAIN ###
regions = set()
region = readRegion('100000')
readAllRegion(region)

其中rbcommon.mysqlclient的初始化方法

mysqlclient = pymysql.connect(host=cfg['mysql']['host'],port=cfg['mysql']['port'],user=cfg['mysql']['user'],password=cfg['mysql']['password'],db=cfg['mysql']['db'],charset=cfg['mysql']['charset'],cursorclass=pymysql.cursors.DictCursor)

从阿里云DATAV GeoAtlas接口抽取行政区划数据相关推荐

  1. 云片短信php接口_php与阿里云短信接口接入

    使用阿里云短信API,需要在控制台获取以下必要参数,其中需要自己手机验证+官方审核多次,尤其审核需要保持耐心. 1. accessKeyId 相当于你的个人账户密钥: 2. accessKeySecr ...

  2. C# 阿里云短信接口调用(不使用SDK,单文件完成)

    虽然阿里云短信接口,提供了很多种语言的SKD,但是对应C#而言,如果使用阿里云SDK,你的项目中需要引入一大堆的dll,像这样: 我只需要发个短信,没必要引入这么多dll吧.于是,根据阿里云官方文档, ...

  3. springboot项目调用阿里云短信接口(包括短信上行)

    最近一直在做项目,很久没有写过博客了,最近做的一个项目中遇到了一个业务需要用到短信功能包括了发送短信和短信回复之后要收到回复内容,阿里云短信服务是一个非常好的平台,里面提供了多种短信接口可以供我们使用 ...

  4. TP5调用阿里云短信接口实现发送短信功能

    TP5调用阿里云短信接口实现发送短信功能 工作需要,对接阿里云的短信服务,借此记录一下(网站 www.lelee.top) 主要分为两个部分,一是下载官方的Demo事例,这个不需要多说,自己去下载就行 ...

  5. 微信小程序手机号验证码登录(调阿里云短信接口)

    小程序效果 代码 <view class='content'><form bindsubmit="formSubmit"><view class='p ...

  6. 调用阿里云人脸识别接口示例

    下面我为大家展示一下我调用阿里云人脸识别接口的示例 首先说下开发环境,springboot 开发的 org.apache.commons.codec.binary.Base64; 这个主要是用来进行b ...

  7. 阿里云人脸识别接口--心得分享

    一:对接阿里云人脸识别接口的工具类 注意:如果你的图片已经转换为base64的编码以后参数是content_1,后面要加type请求参数,我这里是通过图片的url对比的 public class Fa ...

  8. 阿里云人脸识别接口调用卡顿,超时

    阿里云人脸识别接口调用卡顿 在服务端通过pom引入阿里云人脸识别sdk的时候,如果生产环境在内网开通了网络策略连接了 cloudauth.aliyuncs.com 这个地址. 但是sdk调用人脸识别服 ...

  9. 阿里云DataV数据可视化快速入门

    阿里云DataV数据可视化 是什么? DataV数据可视化是使用可视化应用的方式来分析并展示庞杂数据的阿里云产品.DataV旨在通过图形化的界面帮助不同专业背景的用户轻松搭建专业水准的可视化应用,满足 ...

  10. Java调用阿里云短信接口,发送短信

    Java调用阿里云短信接口,发送短信 1.短信服务这个很简单,只需要知道accessKeyId(AK).accessKeySecret(SK).短信签名.短信模板即可. 2.域名和产品名称是固定的,使 ...

最新文章

  1. SpringMVC 处理multipart形式数据:java方式配置文件上传
  2. JavaMoney规范(JSR 354)与对应实现解读
  3. 《OpenGL ES应用开发实践指南:Android卷》—— 2.2 不要从头开始
  4. 文件操作工具类FileUtil
  5. 爬虫 | urllib入门+糗事百科实战
  6. Linux下文件加密方法总结
  7. Linux学习总结(67)——shell脚本中$0 $1 $# $@ $* $? $ 等总结
  8. js页面传值php页面,不同页面,php如何js传值?
  9. CSE 5/7350 – Project Course Timeslot and Student Assignment Project
  10. javascript 异步队列
  11. 基于python下django框架 实现校园二手书籍交易系统详细设计
  12. 胜为蓝牙适配器驱动_udc 324b蓝牙驱动下载-胜为udc 324b蓝牙适配器驱动(win7/win10)v6.5.1.2700 官方版 - 极光下载站...
  13. php 货币换算,PHP货币换算程序代码
  14. 中英文双语切换小程序开发制作
  15. 1、串口(UART/COM/TTL/RS232/RS485)
  16. 电机或编码器相关的 CW 与 CCW
  17. UE4 回合游戏项目 20- 添加人物被攻击的动画
  18. 认识和使用热插拔的正确姿势-续
  19. #loj3124. 「CTS2019 | CTSC2019」氪金手游
  20. 2021年危险化学品经营单位主要负责人免费试题及危险化学品经营单位主要负责人考试总结

热门文章

  1. 浏览器刷新和关闭时显示提示信息
  2. 如何区分PCA PCoA NMDS LDA t-SNE?
  3. 树莓派Raspberry Pi 3 安装CentOS7,并扩展内存卡剩余空间---成功的经验
  4. win10升级后 IE 火狐 谷歌浏览器 不能上网 解决办法
  5. 3.5正交试验设计法
  6. cad缩放_CAD中的AL是什么指令? 怎么操作哦
  7. 2022 年 Flutter 适合我吗?Flutter VS Other 量化对比
  8. EXCEL科学计数法转为文本格式
  9. excel 科学计数法导入数据库
  10. 机器学习数据集划分方法