数据来源:高德开放平台

1. 目标:
(1)获取最新中国县级行政区划矢量数据
(2)学习如何使用R语言将.json数据转为shp文件
2. 步骤:
将json数据处理为shapefile
library(pacman)
p_load(sf,tmap,maptools,rgdal)
china<-st_read("https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json")
class(china)
# [1] "sp"
china
qtm()
qtm(china, fill = "name")+
tm_text("name")
crs(china)
# CRS arguments: +proj=longlat +datum=WGS84 +no_defs
china$name
# [1] "北京市" "天津市" "河北省" "山西省" "内蒙古自治区"
# [6] "辽宁省" "吉林省" "黑龙江省" "上海市" "江苏省"
# [11] "浙江省" "安徽省" "福建省" "江西省" "山东省"
# [16] "河南省" "湖北省" "湖南省" "广东省" "广西壮族自治区"
# [21] "海南省" "重庆市" "四川省" "贵州省" "云南省"
# [26] "西藏自治区" "陕西省" "甘肃省" "青海省" "宁夏回族自治区"
# [31] "新疆维吾尔自治区" "台湾省" "香港特别行政区" "澳门特别行政区" ""
导出为shapefile
st_write(china, "G:/Rdata/China/GeoAltas/China.shp", layer_options = "ENCODING=UTF-8",delete_layer = TRUE)
**province <- paste0("https://geo.datav.aliyun.com/areas_v2/bound/",china$adcode,"_full.json")
province1 <- province[1:34]
province1
count=0
for (i in province1){
x <- st_read(i)
count <- count + 1
st_write(x,dsn = "G:/Rdata/China/GeoAltas/1",layer = china$name[count],
layer_options = "ENCODING=UTF-8",delete_layer = TRUE, driver = "ESRI Shapefile")
}
county <- list.files("G:/Rdata/China/GeoAltas/1/省市",
full.names = TRUE,
pattern = ".shp$")
library(plyr)
data <- NULL
for(aFile in county){
data <- rbind.fill(data, st_read(aFile))
}
data <- st_as_sf(data)
class(data)
# [1] "sf" "data.frame"
qtm(data,fill = "name")
data
#https://stackoverflow.com/questions/64569432/st-union-sf-polygons-with-data-table
data=data[1:nrow(data),]
st_write(data,"G:/Rdata/China/GeoAltas/city.shp",
layer_options = "ENCODING=UTF-8",delete_layer = TRUE)
library("dplyr")
data <- data%>%
dplyr::filter(chldrnN >0 )
qtm(data)
city_county <- paste0("https://geo.datav.aliyun.com/areas_v2/bound/",data$adcode,"_full.json")
head(city_county)
# [1] "https://geo.datav.aliyun.com/areas_v2/bound/340100_full.json"
# [2] "https://geo.datav.aliyun.com/areas_v2/bound/340200_full.json"
# [3] "https://geo.datav.aliyun.com/areas_v2/bound/340300_full.json"
# [4] "https://geo.datav.aliyun.com/areas_v2/bound/340400_full.json"
# [5] "https://geo.datav.aliyun.com/areas_v2/bound/340500_full.json"
# [6] "https://geo.datav.aliyun.com/areas_v2/bound/340600_full.json"
count1 = 0
for(i in city_county){
x <- st_read(i)
count1 <- count1 + 1
st_write(x,dsn = "G:/Rdata/China/GeoAltas/1/市县",layer = data$name[count1],
layer_options = "ENCODING=UTF-8", delete_layer = TRUE,driver = "ESRI Shapefile")
}**
3. 结果
数据分享
https://pan.baidu.com/s/1yhLJodusCwx9GTK858Uxuw
提取码:1111
欢迎关注个人公众号GeoSuper