当前位置:网站首页>省市区三级坐标边界数据csv转JSON
省市区三级坐标边界数据csv转JSON
2022-07-06 17:07:00 【卖香油的少掌柜】
有时候我们绘制如下地图时,要框出某一个城市,或者县城的区域:

这时在编码绘图时,我们需要相应型行政区的json格式的边界坐标集合。
我的上一篇文章写了, 使用python,利用request工具,从高德地图公开API获取中国各个行政区域边界经纬度坐标集。
批量获取中国所有行政区域经边界纬度坐标(到县区级别)_卖香油的少掌柜的博客-CSDN博客本文使用python,利用request工具,从高德地图公开API获取中国各个行政区域边界经纬度坐标集。行政区最细分到县和区的级别。https://blog.csdn.net/qq_58832911/article/details/125617715但是有些同一级同名的地区,会使用相同的边界。这是一个小bug。
本文是,使用了公开坐标数据集转换Json格式,实现获取json格式的边界坐标集合。
csv数据源是可以免费获取的:

你也可以下载我的网盘:
链接:https://pan.baidu.com/s/1AU7sFHoBQBtWT3lMBZAZHg
提取码:ucv9
csv数据的样子:
目的:转化为这个格式的json格式

import pandas as pd
import json
import re
# 读 ok_geo.csv csv文件
def read_csv(file_name):
df = pd.read_csv(file_name, encoding='utf-8')
# 创建国家字典
country_dict = {"name": "China", "border": None, "area": []}
# 实验用的n控制循环次数
n = 0
for index, row in df.iterrows():
# 判断是否是一级地址
if row["deep"] == 0:
# 创建省字典
province_dict = {"name": None, "center": None, "border": None, "area": []}
# 赋值省份字典
province_dict["name"] = row["name"]
# 获取省份坐标中心
province_dict["center"] = [float(i) for i in row["geo"].split(" ")]
# print(province_dict["center"])
province_border = row["polygon"]
# 按","分割,再按照空格分割,转换浮点类型
province_border = [[float(i) for i in re.split(" |;", j)] for j in province_border.split(",")]
province_dict["border"] = province_border
# 将省份字典添加到国家字典的area中
country_dict["area"].append(province_dict)
elif row["deep"] == 1:
# 创建市字典
city_dict = {"name": None, "center": None, "border": None, "countyArea": []}
# 赋值市字典
city_dict["name"] = row["name"]
try:
# 获取市坐标中心
city_dict["center"] = [float(i) for i in row["geo"].split(" ")]
except:
city_dict["center"] = None
# 获取市边界
try:
city_border = row["polygon"]
# 按","分割,再按照空格分割,转换浮点类型
city_border = [[float(i) for i in re.split(' |;', j)] for j in city_border.split(",")]
except:
city_border = None
city_dict["border"] = city_border
# 判断属于哪个省
belong_to_p = row["ext_path"].split(" ")[0]
# print(belong_to_p)
# 查找国家字典中省份列表中的省份字典那个name属性值与belong_to_p相同的那个省份字典所在列表的索引
for i, x in enumerate(country_dict["area"]):
if x["name"] == belong_to_p:
# 将市字典添加到省份字典的area中
p_index = i
break
# print(p_index)
country_dict["area"][p_index]["area"].append(city_dict)
else:
# 创建县区字典
district_dict = {"name": None, "center": None, "border": None}
# 赋值县区字典
district_dict["name"] = row["name"]
# 获取县坐标中心
try:
district_dict["center"] = [float(i) for i in row["geo"].split(" ")]
except:
district_dict["center"] = []
# 获取边界坐标
try:
district_border = row["polygon"]
# 按","分割,再按照空格分割,转换浮点类型
district_border = [[float(i) for i in re.split(' |;', j)] for j in district_border.split(",")]
except:
district_border = []
district_dict["border"] = district_border
# 判断属于哪个省
belong_to_p, belong_to_c= row["ext_path"].split(" ")[0], row["ext_path"].split(" ")[1]
# print(belong_to_p)
# 查找国家字典中省份列表中的省份字典那个name属性值与belong_to_p相同的那个省份字典所在列表的索引
for i, x in enumerate(country_dict["area"]):
if x["name"] == belong_to_p:
# 将县区字典添加到市字典的countyArea中
p_index = i
break
# print(p_index)
# 判断属于那个市
for i, x in enumerate(country_dict["area"][p_index]["area"]):
if x["name"] == belong_to_c:
c_index = i
break
# print(c_index)
country_dict["area"][p_index]["area"][c_index]["countyArea"].append(district_dict)
# print(country_dict)
print("正在进行第" + str(n) + "次循环," + "处理的地址为:" + row["ext_path"])
n += 1
if n==3:
break
# print(country_dict)
# 写入json文件
with open('demo.json', 'w', encoding='utf-8') as f:
json.dump(country_dict, f, ensure_ascii=False)
if __name__ == "__main__":
read_csv('ok_geo.csv')
pass结果:

json文件的下载地址。(不过也希望你可以自己跑代码试一试)
链接:https://pan.baidu.com/s/13RDBSmI0RSQHXllRr3Ke3Q
提取码:homc
仅供交流探讨。不用于商业目的。
边栏推荐
- Notes of training courses selected by Massey school
- 建立自己的网站(17)
- Telerik UI 2022 R2 SP1 Retail-Not Crack
- stm32F407-------SPI通信
- [user defined type] structure, union, enumeration
- 工程师如何对待开源 --- 一个老工程师的肺腑之言
- @TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
- 新手如何入门学习PostgreSQL?
- C Primer Plus Chapter 14 (structure and other data forms)
- 深度学习之环境配置 jupyter notebook
猜你喜欢

Telerik UI 2022 R2 SP1 Retail-Not Crack

48 page digital government smart government all in one solution

The difference between redirectto and navigateto in uniapp
![[software reverse automation] complete collection of reverse tools](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[software reverse automation] complete collection of reverse tools

37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme

@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.

Article management system based on SSM framework

stm32F407-------DAC数模转换

Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing

Deep learning environment configuration jupyter notebook
随机推荐
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
How to judge whether an element in an array contains all attribute values of an object
Js+svg love diffusion animation JS special effects
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
学习使用代码生成美观的接口文档!!!
Model-Free Prediction
深度学习之环境配置 jupyter notebook
做微服务研发工程师的一年来的总结
Learn self 3D representation like ray tracing ego3rt
[force buckle]41 Missing first positive number
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
ZYNQ移植uCOSIII
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Cross-entrpy Method
New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
stm32F407-------DAC数模转换
dynamic programming
什么是时间
Hero League | King | cross the line of fire BGM AI score competition sharing
https://xiangyuecn.gitee.io/areacity-jsspider-statsgov/