我想让 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
然后我用 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)
为什么,我该如何解决?
我已经发现问题并不是在简化过程中直接发生,而是在等值区域或之前:正常的 R 图不会反转:
plot(geosf['COUNTRY'])
plot(geosf_simpl['COUNTRY'])
geosf2 <- geojson_sf(geoback)
plot(geosf2['COUNTRY'])
但是等值线之内或之前的反转发生在何处以及为何发生?
已解决:问题是 rmapshaper 的 ms_simplify 顺时针列出地理坐标,但 choropleth 需要逆时针列出它们。 您需要颠倒坐标顺序,例如像这样:
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)
然后你就得到正确的等值线图: