当前位置:网站首页>省市区三级坐标边界数据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
仅供交流探讨。不用于商业目的。
边栏推荐
- Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
- mongodb客户端操作(MongoRepository)
- Advanced learning of MySQL -- basics -- multi table query -- inner join
- Advanced learning of MySQL -- basics -- basic operation of transactions
- 代码克隆的优缺点
- 再聊聊我常用的15个数据源网站
- 【批处理DOS-CMD命令-汇总和小结】-查看或修改文件属性(ATTRIB),查看、修改文件关联类型(assoc、ftype)
- 工程师如何对待开源 --- 一个老工程师的肺腑之言
- MySQL learning notes (mind map)
- Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
猜你喜欢
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
[user defined type] structure, union, enumeration
Deep learning environment configuration jupyter notebook
Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
详解OpenCV的矩阵规范化函数normalize()【范围化矩阵的范数或值范围(归一化处理)】,并附NORM_MINMAX情况下的示例代码
Mujoco finite state machine and trajectory tracking
[force buckle]41 Missing first positive number
做微服务研发工程师的一年来的总结
Equals() and hashcode()
用tkinter做一个简单图形界面
随机推荐
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
Learn self 3D representation like ray tracing ego3rt
【vulnhub】presidential1
Advanced learning of MySQL -- Fundamentals -- four characteristics of transactions
[software reverse automation] complete collection of reverse tools
What is time
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
Leetcode(547)——省份数量
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
Distributed cache
Data analysis course notes (III) array shape and calculation, numpy storage / reading data, indexing, slicing and splicing
Chapter 5 DML data operation
fastDFS数据迁移操作记录
Advanced learning of MySQL -- basics -- multi table query -- external connection
Leetcode (547) - number of provinces
mongodb客户端操作(MongoRepository)