sp と S4

R で地理空間データを可視化する

Charlotte Wickham

Assistant Professor at Oregon State University

2 種類の 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
R で地理空間データを可視化する

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
R で地理空間データを可視化する

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
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 で地理空間データを可視化する

Passons à la pratique !

R で地理空間データを可視化する

Preparing Video For Download...