使用 rmapshaper 简化 geojson 后,分区统计图反转

Choropleth plot gets inverted after simplifying geojson with rmapshaper

提问人:Luitpold Wienerle 提问时间:10/14/2023 最后编辑:Luitpold Wienerle 更新时间:10/15/2023 访问量:34

问:

我想使 geojson 文件变小/不那么详细,以加快 R Plotly 分区统计图的速度。这是使用原始 geojson 文件的可重现图:

library(rjson)
library(plotly)

geojson <- rjson::fromJSON(file='https://geodata.ucdavis.edu/gadm/gadm4.1/json/gadm41_DEU_0.json')
#geojson <- geojson2
length(geojson$features)

bl <- sapply(1:length(geojson$features),function(i){geojson$features[i][[1]]$properties$COUNTRY})

for(i in 1:length(geojson$features)){
  geojson$features[[i]]$regio <- geojson$features[i][[1]]$properties$COUNTRY
}

Qkreis <- data.frame(regio = bl,
                     zeroy = rep(0,length(geojson$features))
)
names(Qkreis)[1] <- 'regio'

g <- list(
  fitbounds = "locations",
  projection = list(type = 'mercator'),
  visible = FALSE
)

fig <- plot_ly()
fig <- fig %>% add_trace(
  type="choropleth",
  geojson=geojson,
  locations=Qkreis[,'regio'],
  text=Qkreis[,'regio'],
  z=Qkreis[, 'zeroy'],
  colors = c('white','#ed6a12'),
  showscale=T,
  marker = list(line=list(color='grey',width=0.25)),
  featureidkey = 'regio', 
  geo = 'geo'
)
fig <- fig %>% layout(
  geo = g
)

fig

enter image description here

然后我用 rmapshaper 简化它(需要将其转换为 sf 并转换回 geojson),并将新对象放入上述代码的第二行。但现在剧情发生了逆转。

library(geojsonsf)
library(rmapshaper)
library(sf)

geosf <- geojson_sf('https://geodata.ucdavis.edu/gadm/gadm4.1/json/gadm41_DEU_0.json')
substr(geosf, 1, 200)

geosf_simpl <- ms_simplify(geosf, keep = 0.001,
                                keep_shapes = FALSE)

#geoback <- sf_geojson(geosf)
geoback <- sf_geojson(geosf_simpl)
substr(geoback, 1, 200)
geojson2 <- rjson::fromJSON(geoback)
substr(geojson2, 1, 200)

enter image description here

为什么,我该如何解决它?

我已经发现问题不会直接发生在简化过程中,而是在分区统计图或分区统计图之前发生:正常的 R 图不会反转:

plot(geosf['COUNTRY'])

enter image description here

plot(geosf_simpl['COUNTRY'])
geosf2 <- geojson_sf(geoback)
plot(geosf2['COUNTRY'])

enter image description here

但是,在分区统计中或之前发生反转的地点和原因?

R Plotly GeoJSON 分区统计 图整形器

评论


答:

0赞 Luitpold Wienerle 10/15/2023 #1

已解决:问题是 RMAPShater 的ms_simplify顺时针列出了地理坐标,但分区统计线需要逆时针排列。 您需要反转坐标序列,例如:

geosf <- geojson_sf('https://geodata.ucdavis.edu/gadm/gadm4.1/json/gadm41_DEU_0.json')
geosf_simpl <- ms_simplify(geosf, keep = 0.001,
                                keep_shapes = FALSE)
    geosf_simpl$geometry[[1]][1][[1]]
         [,1]    [,2]
 [1,] 10.4548 47.5564
 [2,] 12.1841 47.7011
 [3,] 13.8311 48.7708
 [4,] 12.1996 50.1117
 [5,] 15.0379 51.2440
 [6,] 14.4506 53.2623
 [7,] 13.0325 54.4351
 [8,] 10.0275 54.5504
 [9,]  7.0925 53.5807
[10,]  5.9055 51.0022
[11,]  7.5211 47.6638
[12,] 10.4548 47.5564
    geosf_simpl$geometry[[1]][1][[1]][,1] <- rev(geosf_simpl$geometry[[1]][1][[1]][,1])
    geosf_simpl$geometry[[1]][1][[1]][,2] <- rev(geosf_simpl$geometry[[1]][1][[1]][,2])
    geosf_simpl$geometry[[1]][1][[1]]
        [,1]    [,2]
 [1,] 10.4548 47.5564
 [2,]  7.5211 47.6638
 [3,]  5.9055 51.0022
 [4,]  7.0925 53.5807
 [5,] 10.0275 54.5504
 [6,] 13.0325 54.4351
 [7,] 14.4506 53.2623
 [8,] 15.0379 51.2440
 [9,] 12.1996 50.1117
[10,] 13.8311 48.7708
[11,] 12.1841 47.7011
[12,] 10.4548 47.5564

geoback <- sf_geojson(geosf_simpl)
geojson2 <- rjson::fromJSON(geoback)

然后你得到正确的分区统计图:

enter image description here