2017-12-11 34 views
4

이것은 my previous similar question의 업데이트입니다. 동일한 작업 만이 sf 프레임 워크 내에서 수행해야합니다.r gis : sf가있는 다각형 사이의 내부 경계 확인


이지도에서 다각형, 빨간색 선 사이의 내부 경계를 식별해야합니다.


enter image description here

sp 틀 안에서 나는 Spacedman의 answer @ 포장 자체 작성 기능을 활용하는 데 사용됩니다. 여기에 :

identify_borders <- function(SPolyDF){ 
    require(rgeos) 
    require(sp) 
    borders <- gDifference(
      as(SPolyDF,"SpatialLines"), 
      as(gUnaryUnion(SPolyDF),"SpatialLines"), 
      byid=TRUE) 

    df <- data.frame(len = sapply(1:length(borders), 
            function(i) gLength(borders[i, ]))) 
    rownames(df) <- sapply(1:length(borders), 
          function(i) [email protected][[i]]@ID) 

    SLDF <- SpatialLinesDataFrame(borders, data = df) 
    return(SLDF) 
} 

또는 raster::boundaries()을 사용할 수 있습니다.


코드는 공간 데이터를 얻을 그것은 rmapshaper 정확하게 원하는 기능을 가지고 있으며, 잘 sf 객체와 함께 작동 밝혀졌다지도

# dev version of ggplot2 for geom_sf() 
devtools::install_github("tidyverse/ggplot2") 

library(tidyverse) 
library(sf) 

load(url("https://ikashnitsky.github.io/misc/171211-so-question-identify-borders/geodata.Rdata")) 

ggplot() + 
     geom_sf(data = gd_nuts0) + 
     geom_sf(data = gd_borders, color = "red") + 
     coord_sf(datum = NA) + 
     theme_void() 

답변

6

rmapshaper 그 바람을 피우고, 자바 스크립트를 사용합니다! 나는 시도 :

i = st_intersection(gd_nuts0, gd_nuts0) 
i2 <- i[i$nuts_id != i$nuts_id.1,] 
plot(gd_nuts0[1]) 
plot(i2, add = TRUE, col ='red', lwd = 2) 

enter image description here

5

복제하기 - ms_innerlines()합니다. 유일한 어려움 (아마도 버그)은 ms_innerlines()sf 개체가 아니라 목록을 반환한다는 것입니다. 그러나이 이상한 행동은 쉽게 고쳐집니다. 솔루션 코드는 다음과 같습니다. 차이를 확인하기 위해 초기 다각형을 단순화합니다. sf 개체에서 생성 된 새로운 내부 테두리는 네이비 색상으로 그려집니다.

# dev version of ggplot2 for geom_sf() 
devtools::install_github("tidyverse/ggplot2") 

library(tidyverse) 
library(sf) 
library(rmapshaper) 

load(url("https://ikashnitsky.github.io/misc/171211-so-question-identify-borders/geodata.Rdata")) 

sf_poly_simp <- gd_nuts0 %>% 
     ms_simplify(keep = .2, keep_shapes = TRUE) 

sf_bord_simp <- sf_poly_simp %>% 
     ms_innerlines() %>% 
     as_tibble() %>% 
     st_as_sf() 

ggplot() + 
     geom_sf(data = sf_poly_simp) + 
     geom_sf(data = sf_bord_simp, color = "navy", size = 1) + 
     geom_sf(data = gd_borders, color = "red", size = .1) + 
     coord_sf(datum = NA) + 
     theme_void() 

enter image description here