1.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
R crash course
(for people who pick up new methods/tools quickly
and have a high motivation to learn)
T. Hengl1 & R.S. Bivand2
1Instituut voor Biodiversiteit en Ecosysteem Dynamica
Universiteit van Amsterdam
2Department of Economics
Norwegian School of Economics and Business administration
June 19, 2010
2.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
3.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Programme
Installation and ﬁrst (baby) steps (intro.R).
Building, running and editing models in R.
Working with spatial data.
4.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Crash course
A“crash course”is a compressed version of a training course for
people that already have full agendas. It is really intended for
people who pick up new methods/tools quickly and have a high
motivation to learn (PhD students?). This means no long
questions, no going back, no long discussion, no complains
about the speed/programme of the course. . . and of course: no
promises that you will manage to learn R in such a short time.
5.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
The course programme
Basic concepts (self-study)
R basic and add-on packages;
R syntax; R objects and ’methods’;
R FAQs; getting help and the most important literature;
Scripting in R
data management; creating and debugging scripts
(scripting editors: Tinn-R);
automating analysis — making functions and packages;
publication quality outputs (using R+Sweave);
Advanced topics
GDAL and R; spatial classes and packages;
calling external applications from R (SAGA GIS, FWTools,
Python, Matlab;
export of maps to Google Earth;
6.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
7.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Getting the right motivation (inspiration)
What is R, and why should you invest time to learn it?
What can it do? (and what it can’t do?)
How does the R community works (what are its shared
principles)?
Is R suited for spatio-temporal data analysis?
8.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Some important assumptions
R is (increasingly) sexy.
R is hell.
You are all beginners (who need a gentle intro).
You are non-statisticians / computer scientists.
I learned R myself. If I can do it, you can do it too.
9.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Some important assumptions
R is (increasingly) sexy.
R is hell.
You are all beginners (who need a gentle intro).
You are non-statisticians / computer scientists.
I learned R myself. If I can do it, you can do it too.
10.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Some important assumptions
R is (increasingly) sexy.
R is hell.
You are all beginners (who need a gentle intro).
You are non-statisticians / computer scientists.
I learned R myself. If I can do it, you can do it too.
11.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Some important assumptions
R is (increasingly) sexy.
R is hell.
You are all beginners (who need a gentle intro).
You are non-statisticians / computer scientists.
I learned R myself. If I can do it, you can do it too.
12.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Some important assumptions
R is (increasingly) sexy.
R is hell.
You are all beginners (who need a gentle intro).
You are non-statisticians / computer scientists.
I learned R myself. If I can do it, you can do it too.
13.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What is R?
the open source implementation of the S language for
statistical computing created by created by Ross Ihaka
and Robert Gentleman (now maintained by the R
Development Core Team);
why R?: the name“R”was selected for two reasons: (1)
precedence —“R”is a letter before“S”, and (2) coincidence
— both of the creators’ names start with a letter“R”;
it is a computer language developed to simplify statistical
computing/programming;
it oﬀers many contributed packages, technical documents,
demos and examples;
widely recognized as one of the fastest growing and most
comprehensive statistical computing tools;
it is a movement;
14.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What is R?
the open source implementation of the S language for
statistical computing created by created by Ross Ihaka
and Robert Gentleman (now maintained by the R
Development Core Team);
why R?: the name“R”was selected for two reasons: (1)
precedence —“R”is a letter before“S”, and (2) coincidence
— both of the creators’ names start with a letter“R”;
it is a computer language developed to simplify statistical
computing/programming;
it oﬀers many contributed packages, technical documents,
demos and examples;
widely recognized as one of the fastest growing and most
comprehensive statistical computing tools;
it is a movement;
15.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What is R?
the open source implementation of the S language for
statistical computing created by created by Ross Ihaka
and Robert Gentleman (now maintained by the R
Development Core Team);
why R?: the name“R”was selected for two reasons: (1)
precedence —“R”is a letter before“S”, and (2) coincidence
— both of the creators’ names start with a letter“R”;
it is a computer language developed to simplify statistical
computing/programming;
it oﬀers many contributed packages, technical documents,
demos and examples;
widely recognized as one of the fastest growing and most
comprehensive statistical computing tools;
it is a movement;
16.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What is R?
the open source implementation of the S language for
statistical computing created by created by Ross Ihaka
and Robert Gentleman (now maintained by the R
Development Core Team);
why R?: the name“R”was selected for two reasons: (1)
precedence —“R”is a letter before“S”, and (2) coincidence
— both of the creators’ names start with a letter“R”;
it is a computer language developed to simplify statistical
computing/programming;
it oﬀers many contributed packages, technical documents,
demos and examples;
widely recognized as one of the fastest growing and most
comprehensive statistical computing tools;
it is a movement;
17.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What is R?
the open source implementation of the S language for
statistical computing created by created by Ross Ihaka
and Robert Gentleman (now maintained by the R
Development Core Team);
why R?: the name“R”was selected for two reasons: (1)
precedence —“R”is a letter before“S”, and (2) coincidence
— both of the creators’ names start with a letter“R”;
it is a computer language developed to simplify statistical
computing/programming;
it oﬀers many contributed packages, technical documents,
demos and examples;
widely recognized as one of the fastest growing and most
comprehensive statistical computing tools;
it is a movement;
18.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What is R?
the open source implementation of the S language for
statistical computing created by created by Ross Ihaka
and Robert Gentleman (now maintained by the R
Development Core Team);
why R?: the name“R”was selected for two reasons: (1)
precedence —“R”is a letter before“S”, and (2) coincidence
— both of the creators’ names start with a letter“R”;
it is a computer language developed to simplify statistical
computing/programming;
it oﬀers many contributed packages, technical documents,
demos and examples;
widely recognized as one of the fastest growing and most
comprehensive statistical computing tools;
it is a movement;
19.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
R is“sexy”
20.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Quote
“R has really become the second language for people
coming out of grad school now, and there’s an
amazing amount of code being written for it.”
Max Kuhn
21.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What it is NOT
it’s not a software with GUI and point-and-click control;
there are practically no data analysis wizards;
it’s not an interactive system;
it comes with no warranties (but so does the commercial
software too!!);
there is practically no formal support;
however. . .
22.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What it is NOT
it’s not a software with GUI and point-and-click control;
there are practically no data analysis wizards;
it’s not an interactive system;
it comes with no warranties (but so does the commercial
software too!!);
there is practically no formal support;
however. . .
23.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What it is NOT
it’s not a software with GUI and point-and-click control;
there are practically no data analysis wizards;
it’s not an interactive system;
it comes with no warranties (but so does the commercial
software too!!);
there is practically no formal support;
however. . .
24.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What it is NOT
it’s not a software with GUI and point-and-click control;
there are practically no data analysis wizards;
it’s not an interactive system;
it comes with no warranties (but so does the commercial
software too!!);
there is practically no formal support;
however. . .
25.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What it is NOT
it’s not a software with GUI and point-and-click control;
there are practically no data analysis wizards;
it’s not an interactive system;
it comes with no warranties (but so does the commercial
software too!!);
there is practically no formal support;
however. . .
26.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
What it is NOT
it’s not a software with GUI and point-and-click control;
there are practically no data analysis wizards;
it’s not an interactive system;
it comes with no warranties (but so does the commercial
software too!!);
there is practically no formal support;
however. . .
27.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Why make scripts?
It’s easy to use: “Because S (and its implementation R) is
a well-developed, simple and eﬀective programming
language which includes conditionals, loops, user-deﬁned
recursive functions and input and output facilities, existing
functions can be modiﬁed.” In R we all become
programmers (but much faster than with C++ or Java).
The basic approach to using R is to generate scripts that
deﬁne the data processing steps (workﬂows?).
Documenting the analysis process is a“good thing”, so
programming scripts are not just a burden, certainly for
users doing original research and repetitive work, arguably
for student classes too.
Point-and-click operations are for amateurs.
28.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Why make scripts?
It’s easy to use: “Because S (and its implementation R) is
a well-developed, simple and eﬀective programming
language which includes conditionals, loops, user-deﬁned
recursive functions and input and output facilities, existing
functions can be modiﬁed.” In R we all become
programmers (but much faster than with C++ or Java).
The basic approach to using R is to generate scripts that
deﬁne the data processing steps (workﬂows?).
Documenting the analysis process is a“good thing”, so
programming scripts are not just a burden, certainly for
users doing original research and repetitive work, arguably
for student classes too.
Point-and-click operations are for amateurs.
29.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Why make scripts?
It’s easy to use: “Because S (and its implementation R) is
a well-developed, simple and eﬀective programming
language which includes conditionals, loops, user-deﬁned
recursive functions and input and output facilities, existing
functions can be modiﬁed.” In R we all become
programmers (but much faster than with C++ or Java).
The basic approach to using R is to generate scripts that
deﬁne the data processing steps (workﬂows?).
Documenting the analysis process is a“good thing”, so
programming scripts are not just a burden, certainly for
users doing original research and repetitive work, arguably
for student classes too.
Point-and-click operations are for amateurs.
30.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Why make scripts?
It’s easy to use: “Because S (and its implementation R) is
a well-developed, simple and eﬀective programming
language which includes conditionals, loops, user-deﬁned
recursive functions and input and output facilities, existing
functions can be modiﬁed.” In R we all become
programmers (but much faster than with C++ or Java).
The basic approach to using R is to generate scripts that
deﬁne the data processing steps (workﬂows?).
Documenting the analysis process is a“good thing”, so
programming scripts are not just a burden, certainly for
users doing original research and repetitive work, arguably
for student classes too.
Point-and-click operations are for amateurs.
31.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Do you speak R?
After some time you basically discovered that most of the things
you want to do, you can do in R, the only question is how.
Well, ﬁrst, you have to learn to how to speak’n’write R.
32.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Why NOT Matlab?
R and Matlab are in fact very similar, except. . . :
(almost) everything you do in Matlab, you can do in R;
R can be used to write homework (for Matlab you need a
computer lab with software licences);
“R gradually pushes people towards better programming
habits”(Tam´as K. Papp);
Matlab has a limited support for processing spatial data (R
is better);
Contributed packages in R are growing faster (R is better);
We are in academia — we shouldn’t be developing
toolboxes for some private company but for the people
(and for ourselves);
33.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
A bit of history
R was ﬁrst time released in 1997;
majority of the development is (still) done by prof. Brian
D. Ripley;
a the moment (juni 19 2010 10:01), there are 2387
contributed packages!
according to Google trends, R-project.org has a
community of about 200–350k active users;
in 2003, a group of researchers (International Workshop on
Distributed Statistical Computing) decided to add Spatial
functionality to R;
. . . now is time to use it more broadly (MSc level and PhD
level modules, projects, reports and scientiﬁc documents);
34.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Quote
“Once methodological problems start being perceived
or even deﬁned in terms of what one’s favorite
software does well, then the software has stopped
being a tool, and has become a crutch, and at worse a
shackle.”
Simon Jackman
35.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
The ASDA(R)-book.org
36.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
The ASDA(R) team
37.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
A Practical Guide to Geostatistical Mapping
38.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Quote
“I created Quick-R for one simple reason. I wanted to
learn R and I am a teacher at heart. The easiest way
for me to learn something is to teach it”
Robert I. Kabacoﬀ
39.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
40.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Software
Not all software is required to follow the exercises
R v2.11 (Windows OS) including a list of packages;
Tinn-R v2.3 (code editor);
Optional: FWTools v2.4 — a list of utilities to handle
spatial data; SAGA GIS v2.0.4 — a light GIS excellent for
educational purposes.
41.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Installing the add-on packages
> install.packages("ctv")
> library(ctv)
> install.views("Spatial")
This will install all connected packages listed at R views Spatial.
42.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Check your installation
> Sys.getenv(c("OS", "COMPUTERNAME", "R_HOME", "R_LIBS_USER",
+ "PROCESSOR_IDENTIFIER"))
OS
"Windows_NT"
COMPUTERNAME
"PC-IBED193"
R_HOME
"C:PROGRA~1RR-210~1.1"
R_LIBS_USER
"n:/R/win-library/2.10"
PROCESSOR_IDENTIFIER
"x86 Family 6 Model 15 Stepping 6, GenuineIntel"
43.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
44.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
45.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
46.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
47.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
48.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
49.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
50.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
51.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
52.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
53.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Making friends with R
1 Use script editors such as as TINN-R or JGR (syntax
highlighting, wrapping, search/replace etc.).
2 The best way to learn R is to look at the existing scripts,
then extend:
Graphical Manual/Addicted to R — if you prefer to explore
examples graphically;
Teach-your-self books e.g.“Statistics with R”or“An
Introduction to R”;“Introduction to R for ITC students”
Quick-R and/or R by example;
3 If your R script does not work, do not break your head, try
to get help:
local installation: > help.search("kriging");
R-project: > RSiteSearch("krige dimensions do not
match")
www: Rseek.org
R mailing lists;
54.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Good practice
Put comments in script (after #).
Add some meta-information at the beginning of your
script.
Once you tested your script and saw that it works, tidy-up
the code.
Place the input data on-line (database) so you can
communicate only the script.
55.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
R mailing lists: Do’s and Don’ts!
Do’s:
If you have not done so already, read the R posting guide.
Use the existing pre-installed data sets (come together with
a certain package) to describe your problem; link your
problem to some existing problems.
Acknowledge the work (time spent) other people do to help
you.
You can submit not only the problems you discover but also
the information that you think is interesting for the
community.
Don’ts:
Do not send poorly formulated questions.
Do not send too much.
Complaining to a mailing list about what frustrates you
about R makes no sense.
You are requesting that somebody does a work for you that
you could do yourself!
56.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
57.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Tinn-R
58.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
JaGuaR
59.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
60.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Spatial objects
An advantage of R (as compared to e.g. Matlab) is that you
can create your own formats and structures for data. But if
there are too many formats you can easily get lots. In addition,
we want to have smooth links to external formats (R is open!).
To reduce this problem, Bivand et al. (2008) developed
new-style classes to represent spatial data.
61.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Spatial class
The foundation object is the Spatial class, with just two basic
slots (new-style S classes have pre-deﬁned components called
slots):
a bounding box — mostly used for setting up plots;
a CRS class object — deﬁning the coordinate reference
system, and may be set to CRS(as.character(NA));
Operations on Spatial* objects should update or copy these
values to the new Spatial* objects being created. The most
basic spatial data object is a point, which may have 2 or 3
dimensions.
62.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Spatial classes
for point features: SpatialPoints;
SpatialPointsDataFrame;
for line features: SpatialLines,
SpatialLinesDataFrame;
polygons: SpatialPolygons,
SpatialPolygonsDataFrame;
rasters: SpatialPixels, SpatialPixelsDataFrame,
SpatialGrid, SpatialGridDataFrame;
63.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
SpatialPoints
> library(sp)
> data(meuse)
> coords <- SpatialPoints(meuse[, c("x", "y")])
> summary(coords)
Object of class SpatialPoints
Coordinates:
min max
x 178605 181390
y 329714 333611
Is projected: NA
proj4string : [NA]
Number of points: 155
64.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
SpatialPointsDataFrame
We can add the tabular data to make a
SpatialPointsDataFrame object:
> meuse1 <- SpatialPointsDataFrame(coords, meuse)
> str(meuse1, max.level = 2)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 155 obs. of 14 variables:
..@ coords.nrs : num(0)
..@ coords : num [1:155, 1:2] 181072 181025 181165 ...
.. ..- attr(*, "dimnames")=List of 2
..@ bbox : num [1:2, 1:2] 178605 329714 181390 333611
.. ..- attr(*, "dimnames")=List of 2
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
65.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Point features
ments
Spatial points classes and their slots
coords
Spatial
coords.nrs
data
SpatialPoints bbox
proj4string
SpatialPointsDataFrame
data.frame
Spatial
SpatialPoints
Spatial lines and polygons
66.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Line/polygon features
dge
m a
ns
Spatial Polygons classes and slots
coords
Spatial
lines
plotOrder
Spatial
polygons
bbox
proj4string
Polygon
coords
labpt
area
hole
ringDir
LineLines
ID
Polygons
plotOrder
labpt
ID
area
SpatialLines
Spatial
SpatialPolygons
Lines
Polygons
67.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Grids
e bank
he data
m the
Spatial grid and pixels classes and their slots
data
SpatialPixels
SpatialGrid
data grid
grid.index
SpatialPoints
grid
grid.index
SpatialPoints
cellcentre.offset
cellsize
cells.dim
coords
Spatial
bbox
proj4string
data.frame
Spatial
GridTopology
SpatialPoints
SpatialGridDataFrame
SpatialGrid
SpatialPixels
SpatialPixelsDataFrame
Spatial classes provided by sp
68.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
69.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Basic methods
spplot — plotting of spatial objects (maps);
spsample — sample points from a set of polygons, on a
set of lines or from a gridded area;
bbox — get the bounding box;
proj4string — get or set the projection (coordinate
reference system);
coordinates — set or retrieve coordinates;
spTransform — transform coordinates from one CRS to
another;
overlay — combine two diﬀerent spatial objects;
70.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Plotting a SpatialPoints object
> plot(as(meuse1, "Spatial"), axes = TRUE)
> plot(meuse1, add = TRUE)
> plot(meuse1[meuse1$ffreq == 1, ], col = "green", add = TRUE)
178000 179000 180000 181000 182000
330000331000332000333000
71.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Combining statistical and GIS operations
Because the Spatial*DataFrame family objects behave in
most cases like data frames, most of what we are used to
doing with standard data frames just works (but no merge,
etc., yet).
These objects are very similar to typical representations of
the same kinds of objects in geographical information
systems, so they do not suit spatial data that is not
geographical (like medical imaging) as such.
Because now sp classes for GIS data exits, this opens the
door for fusing GIS and statistical operations (this has not
been possible in e.g. 2002).
72.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Spatial packages
R now oﬀers a range of contributed packages in spatial
statistics and increasing awareness of the importance of spatial
data analysis in the broader community. Current contributed
packages with spatial applications:
point patterns: spatstat, VR:spatial, splancs;
geostatistics: gstat, geoR, geoRglm, ﬁelds, spBayes,
RandomFields, VR:spatial, sgeostat, vardiag;
lattice/area data: spdep, DCluster, spgwr, ade4;
links to GIS: rgdal, spgrass, RPy, RSAGA;
73.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Let’s create spatial objects!
We can create spatial objects from scratch! For example a
DEM:
> dem <- expand.grid(x = seq(100, 600, 100), y = seq(100,
+ 600, 100))
> dem$Z <- as.vector(c(23, 24, 34, 38, 45, 51, 24, 20,
+ 20, 28, 18, 49, 22, 20, 19, 14, 38, 45, 19, 15, 13,
+ 21, 23, 25, 14, 11, 18, 11, 18, 19, 10, 16, 23, 16,
+ 9, 6))
> gridded(dem) <- ~x + y
> dem <- as(dem, "SpatialGridDataFrame")
> str(dem)
74.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
A small DEM
> dem.plt <- spplot(dem[1], main = "DEM", scales = list(draw = FALSE),
+ col.regions = topo.colors(25))
> print(dem.plt)
> writeGDAL(dem, "dem6.sdat", "SAGA")
DEM
10
20
30
40
50
75.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
76.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Controlling SAGA from R
> library(RSAGA)
> rsaga.env()
$workspace
[1] "."
$cmd
[1] "saga_cmd.exe"
$path
[1] "C:/PROGRA~1/R/R-210~1.1/library/RSAGA/saga_vc"
$modules
[1] "C:/PROGRA~1/R/R-210~1.1/library/RSAGA/saga_vc/modules"
77.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Getting list of modules
> rsaga.get.modules("ta_channels")
$ta_channels
code name interactive
1 0 Channel Network FALSE
2 1 Watershed Basins FALSE
3 2 Watershed Basins (extended) FALSE
4 3 Vertical Distance to Channel Network FALSE
5 4 Overland Flow Distance to Channel Network FALSE
6 5 D8 Flow Analysis FALSE
7 6 Strahler Order FALSE
78.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Run stream extraction
> rsaga.geoprocessor(lib = "ta_channels", module = 5, param = list(DEM = "
+ DIRECTION = "channels.sgrd", CONNECTION = "route.sgrd",
+ NETWORK = "channels.shp"))
SAGA CMD 2.0.4
library path: C:/PROGRA~1/R/R-210~1.1/library/RSAGA/...
library name: ta_channels
module name : D8 Flow Analysis
author : (c) 2003 by O.Conrad
Load grid: dem6.sgrd...
ready
Parameters
Grid system: 100; 6x 6y; 100x 100y
DEM: dem6
Flow Direction: Flow Direction
Flow Connectivity: Flow Connectivity
Flow Network: Flow Network
Minimum Connectivity: 0
...
79.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Read back to R
> dem$route <- readGDAL("route.sdat")$band1
route.sdat has GDAL driver SAGA
and has 6 rows and 6 columns
> channels <- readOGR("channels.shp", "channels")
OGR data source with driver: ESRI Shapefile
Source: "channels.shp", layer: "channels"
with 32 features and 2 fields
Feature type: wkbLineString with 2 dimensions
80.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Plot the ﬁnal result
> dem.plt <- spplot(dem[1], main = "DEM", col.regions = topo.colors(25))
> channels.plt <- spplot(dem[2], col.regions = rev(gray(0:20/20)),
+ main = "Flow connectivity", sp.layout = list("sp.lines",
+ channels, col = "red"))
> print(dem.plt, split = c(1, 1, 2, 1), more = T)
> print(channels.plt, split = c(2, 1, 2, 1), more = F)
DEM
10
20
30
40
50
Flow connectivity
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
81.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
82.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Preparing FWTools
There is still no package to control FWTools from R, but we can
simply send command lines using the system command. Before
we can use FWTools from R, we need to locate it on our PC:
> gdalwarp <- gsub("/", "", dir(path="C:/PROGRA~2/FWTOOL~1.7",
+ pattern="gdalwarp.exe", recursive=TRUE, full.names=TRUE))
> gdalwarp
[1] "C:PROGRA~2FWTOOL~1.7bingdalwarp.exe"
> workd <- paste(gsub("/", "", getwd()), "", sep="")
83.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
MODIS data
Now we can download some GIS data from web:
> MOD12Q1 <- "ftp://anonymous:test@e4ftl01u.ecs.nasa.gov/
+ MOLT/MOD12Q1.004/2004.01.01/"
> download.file(paste(MOD12Q1,
+ "MOD12Q1.A2004001.h18v03.004.2006117173748.hdf", sep=""),
+ destfile=paste(getwd(),
+ "MOD12Q1.A2004001.h18v03.004.2006117173748.hdf", sep="/"),
+ mode='wb', method='wget')
Resolving e4ftl01u.ecs.nasa.gov... 152.61.4.83
Connecting to e4ftl01u.ecs.nasa.gov|152.61.4.83|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD /MOLT/MOD12Q1.004/2004.01.01 ... done.
==> SIZE MOD12Q1.A2004001.h18v03.004.2006117173748.hdf ... 23165983
==> PASV ... done. ==> RETR MOD12Q1.A2004... done.
Length: 23165983 (22M)
0K .......... .......... 0% 64.9K 5m48s
...
22550K .......... .......... 99% 501K 0s
22600K .......... 100% 503K=65s
84.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Reprojecting grids
We can reproject/resample the map to our local coordinate
system using the gdalwarp functionality (this combines several
processing steps in one function):
> NL.prj <- "+proj=sterea +lat_0=52.15616055555555
+ +lon_0=5.38763888888889 +k=0.999908 +x_0=155000
+ +y_0=463000 +ellps=bessel +units=m +no_defs
+ +towgs84=565.237,50.0087,465.658,
+ -0.406857,0.350733,-1.87035,4.0812"
> system(paste(gdalwarp, " HDF4_EOS:EOS_GRID:"", workd,
+ "MOD12Q1.A2004001.h18v03.004.2006117173748.hdf"
+ :MOD12Q1:Land_Cover_Type_1 -t_srs "", NL.prj, ""
+ IGBP2004NL.tif -r near -te 0 300000 280000 625000
+ -tr 500 500", sep=""))
Creating output file that is 560P x 650L.
Processing input file HDF4_EOS:EOS_GRID:MOD12Q1.A2004001...
Using internal nodata values (eg. 255) for image HDF4_EOS:EOS_...
0...10...20...30...40...50...60...70...80...90...100 - done.
85.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Plot the ﬁnal result
In this case we have produced a MODIS-based land cover map
for the whole Netherlands in resolution of 500 m (in local
coordinate system).
86.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Outline
1 Overview
Course programme
Why R?
Software installation
2 Scripting in R
Do’s and don’ts
R code editors
3 Working with spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google Earth
4 Literature
87.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Writing spatial data to KML
There are two possibilities to export maps to KML: (a)
using existing packages, and (b) by writing KML ﬁles
“by-hand”.
To export point or line features to KML, use the writeOGR
method that is available in R package rgdal.
More ﬂexible way to writing KML ﬁles is by using loops.
88.
R crash
course
T. Hengl &
R.S. Bivand
Overview
Course programme
Why R?
Software installation
Scripting in R
Do’s and don’ts
R code editors
Working with
spatial data
Spatial classes
Spatial methods
R+SAGA
R+FWTools
Export to Google
Earth
Literature
Literature
Bivand, R., Pebesma, E., Rubio, V., 2008. Applied Spatial
Data Analysis with R. Use R Series, Springer, Heidelberg,
378 p.
Hengl, T., 2009. A Practical Guide to Geostatistical
Mapping, 2nd edition. University of Amsterdam, 291 p.
ISBN 978-90-9024981-0.
Kabacoﬀ, R.I., 2009. Data Analysis and Graphics with R.
Manning publications, 375 p.
Zuur, A. F., Ieno, E. N., Meesters, E. H. W. G., 2009. A
Beginner’s Guide to R. Springer, Use R series, 228 p.
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.
Be the first to comment