百度地图json_R: 民政部官网市级行政地图的绘制

807ffb160db14ebdcece2269ea7d2a69.png

前面我们爬取民政部官网的行政区域代码,这里我们根据爬取县级地图数据绘制市级地图

1 需要的包

rm(list = ls()); gc() # 清空内存library(tibble)library(dplyr)library(purrr)library(magrittr)library(sf)library(stringr)library(tidyr)library(readr)library(ggplot2)library(rmapshaper) # 拓扑融合行政区域##           used (Mb) gc trigger (Mb) max used (Mb)## Ncells  524652 28.1    1188345 63.5   621320 33.2## Vcells 1006270  7.7    8388608 64.0  1599914 12.3

2 行政区域代码

爬取行政区域代码见笔者前面的文章。

file_pre sheng_shi_xian_code "sheng_shi_xian_code.csv",                                              sep = "/"),                                 header  = TRUE,                                 stringsAsFactors = FALSE)# 动态表格展示DT::datatable(sheng_shi_xian_code)

d6b91d6f9654f99443b7c166f8c2967a.png

3 市内县级数据

这里我们以获取成都市县级地图为例。

3.1 获取json数据

# 筛选成都市行政区域代码code_chengdu %   filter(sheng == "四川省" & shi == "成都市") %>%   mutate(code = as.character(code_x),          code_s2 = as.character(code_s)) %>%   select(shi, code_s = code_s2, xian, code)head(code_chengdu)# API前缀API_pre # 通过API获取成都市县级地图数据mapdata_chengdu                                             unique(code_chengdu$code_s), ".json"),                                stringsAsFactors=FALSE)## Reading layer `510100' from data source `http://xzqh.mca.gov.cn/data/510100.json' using driver `GeoJSON'## Simple feature collection with 28 features and 4 fields## geometry type:  POLYGON## dimension:      XY## bbox:           xmin: 103.0995 ymin: 30.19739 xmax: 105.1024 ymax: 31.60111## epsg (SRID):    NA## proj4string:    NA

3d73bdf050362bea84d6c3789007652e.png

3.2 数据处理

包括筛选数据,去除毗邻行政区域的数据。计算行政区域质心坐标。

# 筛选数据mapdata_chengdu %<>% select(-id, -FillColor) %>% rename(code = QUHUADAIMA, name = NAME) %>%   filter(code %in% code_chengdu$code)# 计算行政区域质心坐标centerPoints_chengdu % select(-name) %>% # 计算行政区域质心坐标  st_centroid() %>%   left_join(code_chengdu, by = "code") %>% select(-shi, -code_s)

4.3 画图

ggplot(mapdata_chengdu) +  geom_sf(aes(fill = code), show.legend = FALSE) + #   geom_sf_text(data = centerPoints_chengdu, aes(label = xian),                color = "black") +   coord_sf()

a78dcbb285890ab70f4e3970c94d973c.png

3.4 业务数据

如果有业务数据,直接将行政区域地图数据(sf格式)与业务数据联联结表,再将中心点坐标数据(sf格式)与业务数据联结表,然后拼接字符串,拼接行政区域名称和业务数据变量,使用\n换行符作为分隔符。这样就能绘制业务数据地图了。如下图,笔者绘制的金华市快递复工复产地图。

68f8199d49b394805ddd8d0183670e04.png4 省内市级数据

这里我们首先获取省内各个市的县级数据,然后熔化掉市内县级行政区域的边界线,就得到省内市级数据。如果需要省内县级数据,则不进行边界线熔化这一步。

4.1 自定义函数

# 筛选四川省行政区域代码code_sichuan %   filter(sheng == "四川省") %>%   mutate(code_s2 = as.character(code_she),         code_s3 = as.character(code_s),         code = as.character(code_x)) %>%   select(sheng, code_she = code_s2, shi, code_s = code_s3, xian, code_x = code) head(code_sichuan) # 自定义一个函数,获取每个市的县级地图数据get_xian   data_sf                          stringsAsFactors=FALSE) %>%     select(xian = NAME, code_x = QUHUADAIMA, geometry) %>%     filter(code_x %in% code_range)return(data_sf)}

5ac16b009a1d3fea5a767ed5a80bcaca.png

4.2 获取json数据

通过map函数遍历所有省内市的json数据。然后进行熔化掉市内县级行政区的边界线.

# 获取四川省县级地图数据code_sichuan2 %select(shi, code_s) %>% distinct(code_s, .keep_all = TRUE)mapdata_sichuan %select(code_s, code_x) %>%   group_nest(code_s) %>%   left_join(code_sichuan2, by = "code_s") %>%   mutate(., code_range = map(.$data, ~.x$code_x)) %>% select(shi, code_s, code_range) %>% # 获取json地图数据    mutate(., xian_sf = map2(.x = .$code_s, .y = .$code_range,                            ~get_xian(.x, .y))) %>%  mutate(., xian_sf2 = map(.$xian_sf, ~select(.x, -xian))) %>% select(shi, code_s, xian_sf2) %>% # 熔化市内县级行政区的边界线  mutate(., data = map(.$xian_sf2, ~ms_dissolve(.x))) %>%   mutate(., shi_sf = map(.$data, ~.x$geometry)) %>% select(shi, code_s, shi_sf)# 合并sfcmapdata_sichuan2 %   purrr::reduce(., c) %>%   st_set_geometry(select(mapdata_sichuan, -shi_sf), .)
ggplot(mapdata_sichuan2) +   geom_sf(aes(fill = shi)) +   coord_sf()

5ac9c2ac25b603b0ff26854d2373a51d.png

上图中,绵阳地区有个漏洞,应该是数据源问题,暂且忽略,这里只关注计算过程。

4.3 计算质心坐标

# 计算行政区域质心坐标centerPoints_sichuan % select(-shi) %>% # 计算行政区域质心坐标  st_centroid() %>%   left_join(code_sichuan2, by = "code_s") %>%   select(shi, code_s, geometry)
ggplot(mapdata_sichuan2) +  geom_sf(aes(fill = code_s), show.legend = FALSE) +   geom_sf_text(data = centerPoints_sichuan, aes(label = shi),                color = "black") +   coord_sf()

a3998d29891211ed4bad2d5a0b4bf313.png

参考来源:

  • 中国县级地图绘制

  • 全国行政区划信息查询平台

  • 浙江行政区域代码表格

  • 2019年12月中华人民共和国县以上行政区划代码

  • 百度地图坐标拾取系统

  • 计算行政区域中心坐标

  • 行合并sf对象

  • 高德地图API接口获取经纬度坐标

  • Geocomputation with R

  • rmapshaper包github

f1f4528df3a74c09098cdab36180666a.png

如需联系EasyShu团队

请加微信:EasyCharts

微信公众号【EasyShu】博文代码集合地址

https://github.com/Easy-Shu/EasyShu-WeChat

《R语言数据可视化之美》增强版

b9c89b2a4c77c45b407c3a8d8d685445.png

增强版配套源代码下载地址

Github

https://github.com/Easy-Shu/Beautiful-Visualization-with-R

百度云下载

https://pan.baidu.com/s/1ZBKQCXW9TDnpM_GKRolZ0w 

提取码:jpou

f5d196a02da3a04d7cfeb0ef98c7fde7.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值