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

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 rbcommon def readRegion(adcode, parent_code = None):
# https://geo.datav.aliyun.com/areas/bound/140000.json
url = '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)
return
elif 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'] = 0
elif (level == 'province'):
region['level'] = 1
elif (level == 'city'):
region['level'] = 2
elif (level == 'district'):
region['level'] = 3
if ('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 regions
sub_regions = []
region['children'] = sub_regions
# https://geo.datav.aliyun.com/areas/bound/140000_full.json
url = '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 region
elif 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']):
continue
sub_region['name'] = sub_obj['properties']['name']
sub_region['telecode'] = None
level = sub_obj['properties']['level']
if (level == 'country'):
sub_region['level'] = 0
elif (level == 'province'):
sub_region['level'] = 1
elif (level == 'city'):
sub_region['level'] = 2
elif (level == 'district'):
sub_region['level'] = 3
sub_region['parent'] = adcode
sub_regions.append(sub_region) # further check if the parent adcode is correct
if (len(sub_regions) > 0):
# https://geo.datav.aliyun.com/areas/bound/140000.json
url = '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_parent return region def 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与阿里云短信接口接入

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

  2. 阿里云DNS api接口 shell 更改DNS解析

    可定时任务检查域名解析,调用alidns.sh更新DNS解析 #!/bin/bash # alidns.sh #https://www.cnblogs.com/elvi/p/11663910.html ...

  3. 大型可视化项目用什么工具好呢?——不如了解一下阿里云DataV尊享版

    随着信息化的发展和进步,可视化大屏开始为社会各行业提供全面应用.目前越来越多的需求显示希望大屏能够更直观的还原出所要展示数据可视化的真实场景,让整个项目更立体.更有科技感,让项目在面对复杂操作时能灵活 ...

  4. 【阿里云产品公测】大数据下精确快速搜索OpenSearch

    [阿里云产品公测]大数据下精确快速搜索OpenSearch 作者:阿里云用户小柒2012 相信做过一两个项目的人都会遇到上级要求做一个类似百度或者谷歌的站内搜索功能.传统的sql查询只能使用like ...

  5. THINKPHP3.2.3增加阿里云短信接口思路整理

    https://help.aliyun.com/document_detail/55359.html?spm=5176.product44282.4.7.O4lc1n 阿里云短信服务地址,感冒的下载看 ...

  6. 阿里云DataV专业版发布,为可视化创造更多可能!

    阿里云数据可视化应用工具DataV正式推出专业版,该版本为可视化领域专业团队和从业者量身打造,定位数据可视分析大屏搭建场景,让使用者可以轻松hold住复杂交互设计和实时数据交互查询需求. 什么是Dat ...

  7. 阿里云短信接口开发实践(Java

    随着互联网的兴起,各行各业的需求都在不断的增加.随着业务的扩大,企业给用户发送短信验证码的业务,也是如火如荼.在这里,calvin给各位开发者推荐阿里云短信平台.原因有二:1.接入较简单,开发成本低 ...

  8. TP5整合的阿里云短信接口

    现阶段,短信的应用主要就是用来验证下手机号是不是正常的手机号.只要涉及到用户手机号的问题的时候,都会做短信验证码来验证下改手机号是否是正常手机号.接下来就是操作步骤. 首先要在阿里云账号上开通短信功能 ...

  9. thinkphp5.1 阿里云短信接口

    1.首先声明,我个人是没有,accessKeyId    accessKeySecret   SignName     TemplateCode这些参数是需要自己去,阿里云注册,生成的. 我用的密钥( ...

随机推荐

  1. Xmind8安装和破解(Windows下)

    如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ 如果解决不了,可以在文末进群交流. 一.准备工作:软件.补丁下载及安装  Xmind8官方下载地址:https://www.xmi ...

  2. Linux之RHEL7root密码破解(二)

    破解Linux root密码的第二种方法,如下: 首先开机,进入启动界面,接着找到如下图所示的代码字段,将ro改成rw init=/sysroot/bin/sh ,如下图: 之后按“Ctrl+X”之后 ...

  3. django项目中使用手机号登录

    本文使用聚合数据的短信接口,需要先获取到申请接口的appkey和模板id 项目目录下创建ubtils文件夹,定义返回随机验证码和调取短信接口的函数 function.py文件 import rando ...

  4. poi读写doc和docx

    https://www.cnblogs.com/always-online/p/4800131.html POI是 Apache 旗下一款读写计算机中的 word 以及 excel 文件的工具. po ...

  5. js判断是否第一次访问跳转

    今天分享一套关于Js劫持代码,进行判断第一次访问进行跳转,仅供大家参考学习! 未加密: if (c.indexOf('isfirstvisited=false') != -1) { } else { ...

  6. 4、markdown基本语法

    一.前言 由于有些语法无法在博客园展示,推荐使用Typora解锁全套,下载地址:https://www.typora.io/ 推荐使用jupyter,使用方法:https://www.cnblogs. ...

  7. [Ignatius and the Princess III] 整数的无序拆分(DP + 生成函数)

    整数的有序拆分就是隔板法,无序拆分则有两种处理方法 DP递推 我们假设P(n,m)P(n,m)P(n,m)是正整数nnn无序拆分为mmm个正整数的方案数 对于某一种拆分,不妨将拆分出来的mmm个数从小 ...

  8. robot framework中如何为每个测试用例,测试集准备数据或销毁数据

    Suite Setup:在这个测试集的所有测试用例开始测试之前运行(类似于junit的@BeforeClass) Suite Teardown:在这个测试集的所有测试用例结束之后运行(类似于junit ...

  9. LeetCode 875. Koko Eating Bananas

    原题链接在这里:https://leetcode.com/problems/koko-eating-bananas/ 题目: Koko loves to eat bananas.  There are ...

  10. shell脚本sed的基本用法

    sed 我们首先准备了一个测试文件 1. s 替换  将文件中的This替换cyy 在替换的时候如果加入了 -i 选项就会真的替换,但是只会替换每一行的第一个 -n 和 -p 一起使用表示的是打印那些 ...