从阿里云DATAV GeoAtlas接口抽取行政区划数据
阿里云提供的地理信息接口
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接口抽取行政区划数据的更多相关文章
- php与阿里云短信接口接入
使用阿里云短信API,需要在控制台获取以下必要参数,其中需要自己手机验证+官方审核多次,尤其审核需要保持耐心. 1. accessKeyId 相当于你的个人账户密钥: 2. accessKeySec ...
- 阿里云DNS api接口 shell 更改DNS解析
可定时任务检查域名解析,调用alidns.sh更新DNS解析 #!/bin/bash # alidns.sh #https://www.cnblogs.com/elvi/p/11663910.html ...
- 大型可视化项目用什么工具好呢?——不如了解一下阿里云DataV尊享版
随着信息化的发展和进步,可视化大屏开始为社会各行业提供全面应用.目前越来越多的需求显示希望大屏能够更直观的还原出所要展示数据可视化的真实场景,让整个项目更立体.更有科技感,让项目在面对复杂操作时能灵活 ...
- 【阿里云产品公测】大数据下精确快速搜索OpenSearch
[阿里云产品公测]大数据下精确快速搜索OpenSearch 作者:阿里云用户小柒2012 相信做过一两个项目的人都会遇到上级要求做一个类似百度或者谷歌的站内搜索功能.传统的sql查询只能使用like ...
- THINKPHP3.2.3增加阿里云短信接口思路整理
https://help.aliyun.com/document_detail/55359.html?spm=5176.product44282.4.7.O4lc1n 阿里云短信服务地址,感冒的下载看 ...
- 阿里云DataV专业版发布,为可视化创造更多可能!
阿里云数据可视化应用工具DataV正式推出专业版,该版本为可视化领域专业团队和从业者量身打造,定位数据可视分析大屏搭建场景,让使用者可以轻松hold住复杂交互设计和实时数据交互查询需求. 什么是Dat ...
- 阿里云短信接口开发实践(Java
随着互联网的兴起,各行各业的需求都在不断的增加.随着业务的扩大,企业给用户发送短信验证码的业务,也是如火如荼.在这里,calvin给各位开发者推荐阿里云短信平台.原因有二:1.接入较简单,开发成本低 ...
- TP5整合的阿里云短信接口
现阶段,短信的应用主要就是用来验证下手机号是不是正常的手机号.只要涉及到用户手机号的问题的时候,都会做短信验证码来验证下改手机号是否是正常手机号.接下来就是操作步骤. 首先要在阿里云账号上开通短信功能 ...
- thinkphp5.1 阿里云短信接口
1.首先声明,我个人是没有,accessKeyId accessKeySecret SignName TemplateCode这些参数是需要自己去,阿里云注册,生成的. 我用的密钥( ...
随机推荐
- scrapy爬虫中间件-urlLength
浏览器里面能输入的最大url是有限制的 safari 最多 一万多 ie最少 2083 urllength中间件源码 谷歌和火狐正常 八千多 """ Url Lengt ...
- springdata jpa 关于分页@Query问题
关于springdata jpa 分页问题相信很多小伙伴都遇到过,只要表中数量到达分页条件就会报错 废话少说直接上代码: @Query(nativeQuery = true, value = &quo ...
- Strength(HDU6563+2018年吉林站+双指针瞎搞)
题目链接 传送门 题意 你有\(n\)只怪,每只怪的伤害为\(a_i\),对手有\(m\)只怪,每只怪的伤害为\(b_i\),对手的怪有普通状态和防守状态(普通状态:如果你用攻击力为\(a_i(a_i ...
- 管理员权限运行-C#程序
C#程序以管理员权限运行 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也 ...
- delete properties inside object
- dimensionality reduction动机---data compression(使算法提速)
data compression可以使数据占用更少的空间,并且能使算法提速 什么是dimensionality reduction(维数约简) 例1:比如说我们有一些数据,它有很多很多的feat ...
- springboot项目报错Could not resolve placeholder 'datasource.type' in value "${datasource.type}"解决办法
一,首先确认数据库的连接信息是否都正确,数据库能否正常连接(例如用客户端能连接上):二,确认配置文件中datasource.type配置是否正确,此处我们公司用的阿里的是com.alibaba.dru ...
- Mybatis框架-resultMap元素的自动映射级别
resultMap的自动映射级别:分为三种:NONE PARTIAL FULL 其中默认的属性是:PARTIAL:开启自动匹配,会自动匹配数据库中的字段名和实体类中的属性名,如果一致,就能匹配上, ...
- [CodeForces 663E] - Binary Table(FWT)
题目 Codeforces 题目链接 分析 大佬博客,写的很好 本蒟蒻就不赘述了,就是一个看不出来的异或卷积 精髓在于 mask对sta的影响,显然操作后的结果为mask ^ sta AC code ...
- [Debug] How to Debug a NestJs Backend using the Chrome Dev Tools
TO debug NestJS code with Chrome dev tool, we can run: node --inspect-brk dist/rest-api/src/main.js ...