sp 与 S4

在 R 中可视化地理空间数据

Charlotte Wickham

Assistant Professor at Oregon State University

两种 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 概念
    • 类(Class):定义对象类型、其属性及与其他类的关系
    • 方法(Methods):函数,其行为取决于输入的类
  • S4 对象可递归,元素称为 slots
  • http://adv-r.had.co.nz/OO-essentials.html#s4
在 R 中可视化地理空间数据

访问 slots

# 1. 使用专用方法
proj4string(countries_sp)
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# 2. 使用 @ 后跟未加引号的 slot 名
countries_sp@proj4string
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# 3. 使用 slot() 并传入加引号的 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...