sp와 S4

R로 지리공간 데이터 시각화하기

Charlotte Wickham

Assistant Professor at Oregon State University

두 가지 sp 객체 유형

summary(countries_sp)
SpatialPolygons 클래스 객체
좌표:
   min       max
x -180 180.00000
y  -90  83.64513
투영됨: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
  +towgs84=0,0,0]
summary(countries_spdf)
SpatialPolygonsDataFrame 클래스 객체
좌표:
   min       max
x -180 180.00000
y  -90  83.64513
투영됨: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
  +towgs84=0,0,0]
데이터 속성:
     name              iso_a3          
 길이:177         길이:177         
 클래스: character   클래스: character   
 모드  : character   모드  : character
R로 지리공간 데이터 시각화하기

SpatialPolygons 객체

str(countries_sp, max.level = 2)
'SpatialPolygons' 정식 클래스 [패키지 "sp"] — 슬롯 4개
  ..@ polygons   :길이 177의 리스트
  .. .. [list output truncated]
  ..@ plotOrder  : int [1:177] 7 136 28 169 31 23 9 66 84 5 ...
  ..@ bbox       : num [1:2, 1:2] -180 -90 180 83.6
  .. ..- attr(*, "dimnames")=길이 2의 리스트
  ..@ proj4string:'CRS' 정식 클래스 [패키지 "sp"] — 슬롯 1개
R로 지리공간 데이터 시각화하기

SpatialPolygonsDataFrame 객체

str(countries_spdf, max.level = 2)
'SpatialPolygonsDataFrame' 정식 클래스 [패키지 "sp"] — 슬롯 5개
  ..@ data       :'data.frame':    177 obs. of  6 variables:
  ..@ polygons   :길이 177의 리스트
  .. .. [list output truncated]
  ..@ plotOrder  : int [1:177] 7 136 28 169 31 23 9 66 84 5 ...
  ..@ bbox       : num [1:2, 1:2] -180 -90 180 83.6
  .. ..- attr(*, "dimnames")=길이 2의 리스트
  ..@ proj4string:'CRS' 정식 클래스 [패키지 "sp"] — 슬롯 1개
R로 지리공간 데이터 시각화하기

S4

  • R의 객체지향(OO) 시스템 중 하나
  • 핵심 OO 개념
    • 클래스: 객체 유형, 속성, 다른 클래스와의 관계 정의
    • 메서드: 함수, 입력 클래스에 따라 동작이 달라짐
  • S4 객체는 재귀적 구조를 가질 수 있으며 구성요소를 슬롯이라고 함
  • http://adv-r.had.co.nz/OO-essentials.html#s4
R로 지리공간 데이터 시각화하기

슬롯 접근

# 1. 전용 메서드 사용
proj4string(countries_sp)
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# 2. @ 뒤에 인용하지 않은 슬롯 이름 사용
countries_sp@proj4string
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# 3. slot()에 슬롯 이름을 문자열로 전달
slot(countries_sp, "proj4string")
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
R로 지리공간 데이터 시각화하기

연습해 봅시다!

R로 지리공간 데이터 시각화하기

Preparing Video For Download...