sp e S4

Visualizzare dati geospaziali in R

Charlotte Wickham

Assistant Professor at Oregon State University

Due tipi di oggetti sp

summary(countries_sp)
Oggetto di classe SpatialPolygons
Coordinate:
   min       max
x -180 180.00000
y  -90  83.64513
Proiettato: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
  +towgs84=0,0,0]
summary(countries_spdf)
Oggetto di classe SpatialPolygonsDataFrame
Coordinate:
   min       max
x -180 180.00000
y  -90  83.64513
Proiettato: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 
  +towgs84=0,0,0]
Attributi dei dati:
     name              iso_a3          
 Lunghezza:177      Lunghezza:177      
 Classe :character  Classe :character  
 Modalità:character Modalità:character
Visualizzare dati geospaziali in R

Oggetto SpatialPolygons

str(countries_sp, max.level = 2)
Classe formale 'SpatialPolygons' [pacchetto "sp"] con 4 slot
  ..@ polygons   :Lista di 177
  .. .. [output lista troncato]
  ..@ 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")=Lista di 2
  ..@ proj4string:Classe formale 'CRS' [pacchetto "sp"] con 1 slot
Visualizzare dati geospaziali in R

Oggetto SpatialPolygonsDataFrame

str(countries_spdf, max.level = 2)
Classe formale 'SpatialPolygonsDataFrame' [pacchetto "sp"] con 5 slot
  ..@ data       :'data.frame':    177 oss. di  6 variabili:
  ..@ polygons   :Lista di 177
  .. .. [output lista troncato]
  ..@ 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")=Lista di 2
  ..@ proj4string:Classe formale 'CRS' [pacchetto "sp"] con 1 slot
Visualizzare dati geospaziali in R

S4

  • Uno dei sistemi OO (object-oriented) di R
  • Concetti chiave OO
    • Classe: definisce tipo di oggetto, attributi e relazioni con altre classi
    • Metodi: funzioni, il comportamento dipende dalla classe dell'input
  • Gli oggetti S4 possono essere ricorsivi; gli elementi si chiamano slot
  • http://adv-r.had.co.nz/OO-essentials.html#s4
Visualizzare dati geospaziali in R

Accesso agli slot

# 1. Usa un metodo dedicato
proj4string(countries_sp)
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# 2. Usa @ seguito dal nome dello slot senza virgolette
countries_sp@proj4string
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# 3. Usa slot() con il nome dello slot tra virgolette
slot(countries_sp, "proj4string")
CRS arguments: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
Visualizzare dati geospaziali in R

Ayo berlatih!

Visualizzare dati geospaziali in R

Preparing Video For Download...