sp dan S4

Memvisualisasikan Data Geospasial di R

Charlotte Wickham

Assistant Professor at Oregon State University

Dua tipe objek sp

summary(countries_sp)
Object of class SpatialPolygons
Coordinates:
   min       max
x -180 180.00000
y  -90  83.64513
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
  +towgs84=0,0,0]
summary(countries_spdf)
Object of class SpatialPolygonsDataFrame
Coordinates:
   min       max
x -180 180.00000
y  -90  83.64513
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
  +towgs84=0,0,0]
Data attributes:
     name              iso_a3          
 Length:177         Length:177         
 Class :character   Class :character   
 Mode  :character   Mode  :character
Memvisualisasikan Data Geospasial di R

Objek SpatialPolygons

str(countries_sp, max.level = 2)
Formal class 'SpatialPolygons' [package "sp"] with 4 slots
  ..@ polygons   :List of 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")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
Memvisualisasikan Data Geospasial di R

Objek SpatialPolygonsDataFrame

str(countries_spdf, max.level = 2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame':    177 obs. of  6 variables:
  ..@ polygons   :List of 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")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
Memvisualisasikan Data Geospasial di R

S4

  • Salah satu sistem berorientasi objek (OO) di R
  • Konsep OO utama
    • Class: tipe objek, atributnya, dan relasinya dengan kelas lain
    • Methods: fungsi; perilaku bergantung pada kelas input
  • Objek S4 bisa rekursif; elemennya disebut slot
  • http://adv-r.had.co.nz/OO-essentials.html#s4
Memvisualisasikan Data Geospasial di R

Mengakses slot

# 1. Gunakan metode khusus
proj4string(countries_sp)
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# 2. Gunakan @ diikuti nama slot tanpa kutip
countries_sp@proj4string
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# 3. Gunakan slot() dengan nama slot ber-kutip
slot(countries_sp, "proj4string")
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
Memvisualisasikan Data Geospasial di R

Ayo berlatih!

Memvisualisasikan Data Geospasial di R

Preparing Video For Download...