as GIS Analysis of spatial data in R

  • Slides: 43
Download presentation
as GIS Analysis of spatial data in R IMPRS Modular R course: Advanced module

as GIS Analysis of spatial data in R IMPRS Modular R course: Advanced module on spatial data in R Matthias Forkel mforkel@bgc-jena. mpg. de Jena, 23. 05. 2012

Presentation, example scripts, data and materials at: ftp: //ftp. bgc-jena. mpg. de/pub/outgoing/mforkel/Rcourse/

Presentation, example scripts, data and materials at: ftp: //ftp. bgc-jena. mpg. de/pub/outgoing/mforkel/Rcourse/

Agenda Spatial data in R (A) >> Wed, May 23, 2012, 9: 30 -12:

Agenda Spatial data in R (A) >> Wed, May 23, 2012, 9: 30 -12: 30, part 1 1 Introduction to spatial data in R 1. 2 Packages for spatial data 1. 3 Classes for vector and raster datasets 1. 4 Reading and writing of spatial data Spatial data in R (B) >> Wed, May 23, 2012, 9: 30 -12: 30, part 2 2 Mapping 2. 1 Plotting functions for spatial data 2. 2 Graphical representation of spatial information 2. 3 Coordinate systems and reprojection Spatial data in R (C) >> Wed, May 23, 2012, 13: 30 -16: 30, part 1 3 Exchange between R and other spatial software 3. 1 Import of Google. Maps and Open. Street. Map data in R 3. 2 Export data to Google. Earth 3. 3 Linking R with GIS software Spatial data in R (D) >> Wed, May 23, 2012, 13: 30 -16: 30, part 2 4 Spatial data analysis 4. 1 Methods for vector data 4. 2 Methods for raster data 4. 3 Spatial interpolation

Introduction to spatial data in R 1. 2 Packages for spatial data 1. 3

Introduction to spatial data in R 1. 2 Packages for spatial data 1. 3 Classes for vector and raster datasets 1. 4 Reading and writing of spatial data

as GIS? – Why and how is this possible? - Geostatistics as a core

as GIS? – Why and how is this possible? - Geostatistics as a core feature of R: - Spatial autocorrelation, spatial interpolation, geostatistics (package „spatial“ is a core part of S/R for a long time) - visualization capabilities of R: mapping - many packages for geostatistical analyzes (e. g. gstat) - Interfaces to other geoinformation software: - Geospatial basics: - GDAL: reading/writing of spatial data (package rgdal) - Proj 4: definition of coordinate reference systems (package rgdal) - Geographical Information Systems - GRASS GIS (package spgrass 6) - SAGA GIS (package RSAGA) - Arc. GIS (via Python with package RPy. Geo) - Post. GIS (packages rgdal or RODBC) - Google. Earth: - download of Google. Maps data (package Rgoogle. Maps) - writing of KML files for Google. Earth (package maptools, raster)

Literature Bivand, R, E. Pebesma & V. Gómez-Rubio (2008): Applied Spatial Data Analysis with

Literature Bivand, R, E. Pebesma & V. Gómez-Rubio (2008): Applied Spatial Data Analysis with R. New York: Springer. - Classes for spatial data in R Reading/writing of spatial data Mapping Point pattern analysis Spatial autocorrelation Spatial interpolation/Geostatistics Disease mapping Hengl, T. (2009): A Practical Guide to Geostatistical Mapping. Open source book. http: //www. lulu. com/product/file-download/a-practicalguide-to-geostatistical-mapping/14938111 - R as GIS = R + GRASS + SAGA + Google. Earth - Geostatistics, spatial autocorrelation, Applications: - Geomorphometry, digital elevation models - Heavy metal concentrations - Land surface temperature

Links Overview of R packages for spatial data: http: //cran. r-project. org/web/views/Spatial. html Description

Links Overview of R packages for spatial data: http: //cran. r-project. org/web/views/Spatial. html Description of the sp package: http: //cran. r-project. org/web/packages/sp/sp. pdf Description of the raster package: http: //cran. r-project. org/web/packages/raster/vignettes/Raster. pdf Some R code examples for spatial data (University Oregon): http: //geography. uoregon. edu/Geog. R/index. html Spatial-analyst. net – a Wiki for spatial data analysis based on R: http: //spatial-analyst. net/ Some more tips: http: //spatialanalysis. co. uk/r/ Mailinglist of R special interest group on using geographical data and mapping https: //stat. ethz. ch/mailman/listinfo/R-SIG-Geo/

Packages for spatial data in R Classes for spatial data Toolbox Reading/Writing Geostatistics Animal

Packages for spatial data in R Classes for spatial data Toolbox Reading/Writing Geostatistics Animal tracking raster Classes for raster data raster GIS functions Centrographic statistics Spatial and Space-Time Point Pattern Analysis Functions geo. weighted regression Spatial autocorrelation point patterns clusters of diseases Support vector machine exploring spatial data analysis of outbreaks automated redistricting geological mapping R/GRASS interface analysis of env. data point pattern analysis seismic tomography Spatial sampling similarity/abscence of species generalized linear spatial models analyzing missing values Fig. : R packages depending on package sp (Bivand et al. 2008: 5) + raster package for geographic analysis and modeling with raster data

Exercise 0 Load packages for spatial data in R Aim: install and load packages

Exercise 0 Load packages for spatial data in R Aim: install and load packages to work with spatial data in R - Open and run the script 00_spatial. R_main. R

Types of spatial data Vector data Point + + + Raster data Rasterlayer Line

Types of spatial data Vector data Point + + + Raster data Rasterlayer Line Rasterstack Polygon

Classes for spatial data in R Vector data Data type + + + read/write

Classes for spatial data in R Vector data Data type + + + read/write Basic methods for classes Spatial. Points. Data. Frame read. OGR() write. OGR() Get extent: bbox() Spatial. Lines. Data. Frame Get projection: proj 4 string() Spatial. Polygons. Data. Frame Get coordinates: coordinates() Spatial. Pixels. Data. Frame + + + + ++ + + + ++++ Raster data classes from package sp read. GDAL() write. GDAL() Access data: @data Spatial. Grid. Data. Frame classes from package raster: raster() Raster. Layer Get extent: extent() Get resolution: res() stack() brick() Raster. Stack – multiple files Raster. Brick – one file Get projection: projection() Get data: get. Values()

Classes for vector data: Spatial* classes (package sp) Spatial. Points, Spatial. Lines, Spatial. Polygons,

Classes for vector data: Spatial* classes (package sp) Spatial. Points, Spatial. Lines, Spatial. Polygons, Spatial. Pixels Read ESRI shapefile with country borders:

Supported file formats: package sp and rgdal* Raster data: read. GDAL(), write. GDAL() Vector

Supported file formats: package sp and rgdal* Raster data: read. GDAL(), write. GDAL() Vector data: read. OGR(), write. OGR() he t ta e a d ag r te ack s ra er p … h it ast le w g e r asib n i h fe k t r f o o re w s o r e o m s F las re a c * Package rgdal requires GDAL (geospatial data abstraction library) installed (comes e. g. with GRASS, QGIS, FWtools or other FOSS GIS)

Exercise 1 Classes for vector data Aim: Learn the structure of sp* objects -

Exercise 1 Classes for vector data Aim: Learn the structure of sp* objects - Open the script 01_spatial. R_ex 1_classes-for-vector-data. R - Run the demonstration. - Exercise 1: make a simple world map

Classes for raster data: Raster. Layer, Raster. Stack, Raster. Brick Raster. Layer - raster()

Classes for raster data: Raster. Layer, Raster. Stack, Raster. Brick Raster. Layer - raster() - only one layer - reference to file Raster. Stack - stack() - create from files, Raster. Layers or Spatial. Grid. Data. Frames - mutilayer collection of Raster. Layers (+) data stored on disk, not in memory Raster. Brick - brick() - multilayer - data stored in one multiband file - shortest processing time

Supported file formats: package raster Read raster data: raster(), stack() or brick() Write raster

Supported file formats: package raster Read raster data: raster(), stack() or brick() Write raster data: write. Raster()

Exercise 2 Classes for raster data Aim: learn the structure of raster* objects and

Exercise 2 Classes for raster data Aim: learn the structure of raster* objects and create a Raster. Layer - Open the script 02_spatial. R_ex 2_classes-for-raster-data. R - Run the demonstration. - Exercise 2: Create a Raster. Layer object

Mapping 2. 1 Plotting functions for spatial data 2. 2 Graphical representation of spatial

Mapping 2. 1 Plotting functions for spatial data 2. 2 Graphical representation of spatial information 2. 3 Coordinate systems and reprojection

Graphical representation of vector data (in general, not only R) Tab. : Legend types

Graphical representation of vector data (in general, not only R) Tab. : Legend types for point, line and areal data Legenden-typ Single Symbol Graduated Color Graduated Symbol Unique Value Chart Dot Point -- Line -- Area --

Elements of a map - title / subtitle - main map, secondary maps -

Elements of a map - title / subtitle - main map, secondary maps - grid - Legend explaining colors and symbols - Info: Projection, geographic reference system - charts - references (data sources) - data type, e. g. information about satellite - text, explanations - author, cartographer - producer, institution, copyright - scalebar - north arrow - map ID (if map belongs to a collection of maps/atlas) - overview map (for regional maps)

Exercise 3 Create nice maps / plotting functions in R Aim: create a map

Exercise 3 Create nice maps / plotting functions in R Aim: create a map including colors, legend and geographical data - Open the script 03_spatial. R_ex 3_mapping 1. R - Run the demonstration. - Exercise 3: Plot a map of the Koeppen-Geiger climate zones.

Map projections Properties: - Equidistant: preserve distances - Equal area: preserve area - Conformal:

Map projections Properties: - Equidistant: preserve distances - Equal area: preserve area - Conformal: preserve angles Not everthing is possible at once (except on a globe)

Proj 4 PROJ. 4 - Cartographic Projections Library http: //trac. osgeo. org/proj/ Standard to

Proj 4 PROJ. 4 - Cartographic Projections Library http: //trac. osgeo. org/proj/ Standard to define map projections in many open source software. Examples for common projections: http: //www. remotesensing. org/geotiff/proj_list/ Example for the usual longitude/latitude maps (°E, °N) +proj=longlat +ellps=WGS 84 +datum=WGS 84 +no_defs +towgs 84=0, 0, 0

Projections in R In R: define the proj 4 string as character string and

Projections in R In R: define the proj 4 string as character string and convert it to a CRS (coordinate reference system) object: proj <- CRS(„+proj=longlat +ellps=WGS 84 +datum=WGS 84 +no_defs +towgs 84=0, 0, 0“) Projections can be assigned: … to sp* objects: proj 4 string(data) <- CRS(…) … to raster* objects: projection(data) <- CRS(…) Reprojection / Spatial transformation: … for sp* objects: sp. Transform(…) … for raster* objects: project. Extent() project. Raster()

Exercise 4 Transform spatial data Aim: transform raster and vector data sets (to Robinson

Exercise 4 Transform spatial data Aim: transform raster and vector data sets (to Robinson projection, Arctic projection), cut a raster with a polygon - Open the script 04_spatial. R_ex 4_transformations-mapping 2. R - Run the demonstration. - Exercise 4: plot a map in an arctic projection

Spatial R and Google. Earth 3 Exchange between R and other spatial software 3.

Spatial R and Google. Earth 3 Exchange between R and other spatial software 3. 1 Import of Google. Maps and Open. Street. Map data in R 3. 2 Export data to Google. Earth 3. 3 Linking R with GIS software

Rgoogle. Maps Download data from Google. Maps and Open. Street. Map with Rgoogle. Maps

Rgoogle. Maps Download data from Google. Maps and Open. Street. Map with Rgoogle. Maps package http: //www. openstreetmap. org

Exercise 5 Import data from Google. Maps and Open. Street. Map to R Aim:

Exercise 5 Import data from Google. Maps and Open. Street. Map to R Aim: Learn how to use spatial data from web services for your own maps - Open the script 05_spatial. R_ex 5_import-Google. Earth-OSM. R - Run the demonstration.

Exercise 6 Export your own data to Google. Earth Aim: Create a kml-file from

Exercise 6 Export your own data to Google. Earth Aim: Create a kml-file from a raster for an overlay in Google. Earth - Open the script 06_spatial. R_ex 6_export-Google. Earth. R - Run the demonstration.

R and GIS How to access functionality of other GIS software from R? GRASS

R and GIS How to access functionality of other GIS software from R? GRASS GIS: package spgrass 6 Arc. GIS: via package RPy. Geo SAGA GIS: package RSAGA (wraps SAGA shell commands in R functions)

Spatial data analysis 4. 1 Methods for vector data 4. 2 Methods for raster

Spatial data analysis 4. 1 Methods for vector data 4. 2 Methods for raster data 4. 3 Spatial interpolation

Methods for vector data Package sp: overlay() and over() functions - Points in polygon

Methods for vector data Package sp: overlay() and over() functions - Points in polygon - Grid over polygon More methods in rgeos package àhttp: //cran. r-project. org/web/packages/rgeos/index. html e. g. area, distance, buffer, crosses, overlap, intersection, union …

Exercise 7 Spatial analysis with vector data Aim: make an analysis with point-in-polygon and

Exercise 7 Spatial analysis with vector data Aim: make an analysis with point-in-polygon and distance computations - Open the script 07_spatial. R_ex 7_spatialanalysis-vector. R - Open. Street. Map data (downloaded from http: //www. geofabrik. de/) - Run the two examples: 1. Which landuse type contains most of the benches? 2. Which is the closest restaurant to the station 'Jena Paradies'?

Methods for raster data raster package Description: http: //cran. r-project. org/web/packages/raster/ How to process

Methods for raster data raster package Description: http: //cran. r-project. org/web/packages/raster/ How to process large rasters (i. e. rasters which cannot be saved in the R memory)? http: //cran. r-project. org/web/packages/raster/vignettes/functions. pdf

GIS methods in the raster package (1) Resampling: aggregate() creates raster with lower resolution

GIS methods in the raster package (1) Resampling: aggregate() creates raster with lower resolution disaggregate() creates raster with higher resolution resample() transfers raster values to different origin and resolution Spatial transformation: project. Raster() transforms Raster* objects to another projection sp. Transform() transform Spatial* vector objects to other projection Translocation: flip() mirrors a raster rotate() rotates a raster shift() shifts a raster Change raster extent: crop() cuts a raster to a smaller extent expand() enlarges a raster merge() merges raster layers to larger extent mosaic() merges raster layers to larger extent but considers overlap

GIS methods in the raster package (2) Calculations: adjacent() identify adjacent cells to a

GIS methods in the raster package (2) Calculations: adjacent() identify adjacent cells to a cell on a raster area() calculates area of raster cells calc() performs calculations per pixel on multilayer raster objects hill. Shade() compute hillshade from slope and aspect overlay() calculations based on specific layers terrain() calculate slope and aspect from elevation model zonal() calculates zonal statistics for a raster Dealing with NA values: approx. NA() estimate values that are NA in a raster cover() replaces NA values with values of a second raster mask() sets values in a raster to NA Image processing: edge() detects edges focal() moving window filters cut() classify values of a raster to classes reclass() reclassify values in a raster

GIS methods in the raster package (3) Raster-vector interfaces: extract() gets raster values based

GIS methods in the raster package (3) Raster-vector interfaces: extract() gets raster values based on other spatial objects rasterize() converts points, lines or polygons to rasters Sampling: sample. Random() take a random sample from a raster sample. Regular() take a systematic sample from a raster Extract values and coordinates: extract() extract cell values cell. From. Col(), cell. From. Line(), cell. From. Polygon(), cell. From. Row. Col(), cell. From. XY(), cell. From. Extent() get cell number from a raster x. From. Cell(), y. From. Cell(), xy. From. Cell() get coordinates from cell number min. Value(), max. Value() extreme values in a raster Plotting/Mapping: plot() plot a map of a Raster* object plot. RGB() plot red-green-blue composite of a raster contour() plot contour lines pairs() plot matrix of scatterplots for layers in a Raster. Brick or Raster. Stack scalebar() add a scalebar to a raster map

Exercise 8 Calculations with raster data Aim: learn statistical and arithmetical functions for raster

Exercise 8 Calculations with raster data Aim: learn statistical and arithmetical functions for raster data, analyse time series of raster data - Open the script 08_spatial. R_ex 8_spatialanalysis-raster. R - Run the demonstration. - Exercise 8: Compute zonal statistics of NDVI by Koeppen-Geiger climate zones

Spatial Interpolation How to transfer information from observed sites to other locations? z y

Spatial Interpolation How to transfer information from observed sites to other locations? z y ? Fig. : Interpolation of measurement points x

Spatial Interpolation Inverse Distance Weighting - Weighting with inverse of distance - Assumption: less

Spatial Interpolation Inverse Distance Weighting - Weighting with inverse of distance - Assumption: less influence of far points Pros/cons: + simple and fast + less assumptions + often used z d β Estimated value Observed value Distance Exponent - Bubble effect

Spatial Interpolation Spatial correlation and Kriging - Estimation of spatial correlation and variance Variance

Spatial Interpolation Spatial correlation and Kriging - Estimation of spatial correlation and variance Variance (squared difference) - Variogram: description of spatial dependance Correlation length Variogram Sill Weights for Kriging interpolation Nugget Estimation Distance Fig. : Elements of a variogram

Exercise 9 Spatial Interpolation Aim: learn how to interpret variograms and how to use

Exercise 9 Spatial Interpolation Aim: learn how to interpret variograms and how to use inverse distance weigthing and ordinary kriging - Open the script 09_spatial. R_ex 9_spatialinterpolation. R - Run the code.

Feedback - What did you like? - What should be improved? - Which parts

Feedback - What did you like? - What should be improved? - Which parts should be extended or shorter?