R 벡터 형상 변환
2024. 8. 17. 15:01ㆍR Python GIS
이 게시물의 내용 중 일부는 Robin Lovelace 외의 Geocomputation with R을 참고했음을 밝힙니다. |
기하처리라고도 불리는 벡터 객체의 기하학적 도형의 형상 변화를 다룹니다. 이전 포스트에서 다뤘던 단순화 역시 벡터데이터의 형상 변화 과정 중 일부라고 할 수 있습니다.
library(sf)
library(tidyverse)
library(rmapshaper)
library(tmap)
korea = st_read("C:/경로/BND_SIGUNGU_PG.shp", options="ENCODING=EUC-KR")
seoul = korea %>% select(-BASE_DATE) %>% filter(str_sub(SIGUNGU_CD,1,2)=="11")
seoul_s = ms_simplify(seoul, keep = 0.01, keep_shapes = TRUE)
우선 단순화된 서울의 벡터데이터를 준비합니다.
중심점: st_centroid
tm_shape(seoul_s)+tm_borders()+tm_shape(st_centroid(seoul_s))+tm_symbols(size=0.25)
각 벡터 객체의 중심점을 포인트로 반환합니다.
클리핑
여기선 간단하게 겹치는 원 모양의 벡터를 만들어서 알아보겠습니다.
b = st_sfc(st_point(c(0,1)), st_point(c(1,1)))
b = st_buffer(b, dist = 1)
x = b[1]
y = b[2]
plot(b)
tm_shape(st_intersection(x, y))+tm_polygons()
tm_shape(st_difference(x, y))+tm_polygons()
tm_shape(st_difference(y, x))+tm_polygons()
tm_shape(st_sym_difference(x, y))+tm_polygons()
tm_shape(st_union(x, y))+tm_polygons()
![]() |
![]() |
![]() |
st_intersection(x, y) | st_difference(x, y) | st_difference(y, x) |
![]() |
![]() |
|
st_sym_difference(x, y) | st_union(x, y) |
버퍼
서울 중구의 경계 반경 3km를 나타내는 벡터를 따로 만듭니다.
junggu = seoul %>% filter(SIGUNGU_NM=="중구")
junggu_buff_3km = st_buffer(junggu, dist = 3000)
tm_shape(junggu_buff_3km)+tm_polygons()+tm_shape(seoul_s)+tm_borders()
지역 묶기
서울의 구들을 버스 권역별로 8개의 권역으로 묶겠습니다. 여기서는 dplyr의 group_by()함수를 응용합니다.
print(seoul_s$SIGUNGU_NM)
region = c("C", "C", "C", "E", "E", "E", "E", "N", "N", "N", "N", "NW", "NW", "NW", "W", "W", "W", "SW","W", "SW", "SW", "S", "S", "SE", "SE" )
seoul_s=cbind(seoul_s, region)
seoul_r=seoul_s %>% group_by(region) %>% summarize()
head(seoul_r)
tm_shape(seoul_r) + tm_borders() + tm_text("region")
'R Python GIS' 카테고리의 다른 글
R 회귀분석 (3) | 2024.09.05 |
---|---|
R 벡터 단순화 작업 (0) | 2024.08.17 |
R 벡터 속성 작업 (0) | 2024.08.16 |
R 집단별 요약 (0) | 2024.08.13 |
지오코딩으로 주소를 좌표로 바꾸기 (api 3종 비교) (0) | 2024.08.07 |