0% found this document useful (0 votes)
34 views26 pages

Spatial and Spatio-Temporal Data In: Edzer Pebesma

The document discusses spatial and spatio-temporal data classes and operations in R. It introduces the SpatialPoints, SpatialLines, SpatialPolygons, and SpatialGrid classes for representing spatial data, as well as functions for selecting, aggregating, and plotting spatial data. It then discusses the spacetime package for representing spatio-temporal data and performing operations that combine spatial and temporal selection, aggregation, and analysis.

Uploaded by

Luis AC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views26 pages

Spatial and Spatio-Temporal Data In: Edzer Pebesma

The document discusses spatial and spatio-temporal data classes and operations in R. It introduces the SpatialPoints, SpatialLines, SpatialPolygons, and SpatialGrid classes for representing spatial data, as well as functions for selecting, aggregating, and plotting spatial data. It then discusses the spacetime package for representing spatio-temporal data and performing operations that combine spatial and temporal selection, aggregation, and analysis.

Uploaded by

Luis AC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1.

Das neue IfGI-Logo

1.6 Logovarianten

Spatial and spatio-temporal data in


Logo fr den Einsatz in internationalen bzw.
englischsprachigen Prsentationen.
Einsatzbereiche: Briefbogen, Visitenkarte,
Titelbltter etc.
Mindestgre 45 mm Breite

ifgi
Institute for Geoinformatics
University of Mnster

Edzer Pebesma
Logo fr den Einsatz in nationalen bzw.
deutschsprachigen Prsentationen.
Einsatzbereiche: Briefbogen, Visitenkarte,
Titelbltter etc.
Mindestgre 45 mm Breite

ifgi
Institut fr Geoinformatik
Universitt Mnster

DailyMeteo, Belgrade, 24-27 Jun 2014

http://ifgi.uni-muenster.de/~epebe_01/R/

Dieses Logo kann bei Anwendungen


eingesetzt werden, wo das Logo besonders

1 / 26

Overview

1. vector, matrix, array; lists, data.frame


2. selection on data.frame
3. spatial classes
4. selection on spatial objects
5. aggregation, in general
6. aggregation on spatial classes
7. spatio-temporal classes
8. selection, overlay, aggregation on spatio-temporal objects

2 / 26

All data are spatio-temporal


1. There are no pure-spatial data. Maps reflect either
I

a snapshot in time (remote sensing image)

an aggregate over a time period (e.g., interpolated yearly


average temperature, or yearly aggregated daily interpolations)

something that is constant over a period of time (political


boundary)

a seemingly non-changing phenomenon (geology)

2. There are no pure-temporal data. Time series reflect either


I

spatially aggregated values (global temperature curves)

a single spatial location (air quality sensor DEUB032, at


8.191934E,50.93033N)

vaguely located, or universal aggregates (world market prices,


stock quotes)
3 / 26

Vector, matrix, array


> a = vector(3, mode = "numeric")
> a
[1] 0 0 0
> length(a)
[1] 3
or simply by initialisation

> m = matrix(rnorm(6), 2, 3)
> print(m, digits=3)

> c = 1:3
> c

[,1]
[,2]
[,3]
[1,] 0.415 0.735 -0.171
[2,] -1.115 -0.540 -1.029

[1] 1 2 3

> dim(m)

> typeof(c)

[1] 2 3

[1] "integer"

> a = array(1:(5*7*9), c(5,7,9))


> dim(a)

> d = 1.5:3.5
> d

[1] 5 7 9

[1] 1.5 2.5 3.5


> typeof(c)

4 / 26

lists, data.frame

Lists can contain anything:


> a = list(1:3, x = c("foo", "bar"), c(TRUE, FALSE))
> a
[[1]]
[1] 1 2 3
$x
[1] "foo" "bar"
[[3]]
[1] TRUE FALSE
> a[1]
[[1]]
[1] 1 2 3
> a[[1]]

data.frame is a column store, but


mimics records of mixed type
>
>
>
>

a[[1]] = 1:2
b = as.data.frame(a)
names(b) = c("NR", "what", "cond")
b

1
2

NR what cond
1 foo TRUE
2 bar FALSE

> is.list(b)
[1] TRUE

[1] 1 2 3
> a$x
[1] "foo" "bar"

5 / 26

Selection on data.frame
> b
NR what cond
1 1 foo TRUE
2 2 bar FALSE
> b[[1]]

> b[1,]
1

> b[,1:2]

[1] 1 2
> b[["NR"]]
[1] 1 2
> b$NR
[1] 1 2

1
2

NR
1
2

NR what
1 foo
2 bar

> b[,1]
[1] 1 2
> b[,1,drop=FALSE]

> b[1]
1
2

NR what cond
1 foo TRUE

1
2

NR
1
2

6 / 26

Deletion, negative selection, replacement


> b$cond2 = ! b$cond
> b

> b
1
2

NR what cond
1 foo TRUE
2 bar FALSE

> b$NR = NULL


> b
1
2

what cond
foo TRUE
bar FALSE

> b[-1,]
2

what cond
bar FALSE

1
2

what cond cond2


foo TRUE FALSE
bar FALSE TRUE

> b[1,1] = NA
> b
what cond cond2
1 <NA> TRUE FALSE
2 bar FALSE TRUE
> class(b$what)
[1] "factor"
> b$what
[1] <NA> bar
Levels: bar foo
> as.numeric(b$what)
7 / 26

Spatial classes (sp)


Class Spatial provides a coordinate reference system and a
bounding box.
I Objects deriving from Spatial consist of a geometry:
I
I
I
I
I

>
>
>
>

SpatialPoints
SpatialLines
SpatialPolygons
SpatialPixels
SpatialGrid

from these, Spatial*DataFrame objects derive, and have


attributes (a data slot, of class data.frame)

library(sp)
library(rgdal)
p = SpatialPoints(cbind(lon = 8, lat = 52), CRS("+init=epsg:4326"))
p

SpatialPoints:
lon lat
[1,]
8 52
Coordinate Reference System (CRS) arguments: +init=epsg:4326
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
+towgs84=0,0,0

8 / 26

Meuse data set

>
>
>
>
+

library(sp)
data("meuse")
coordinates(meuse) <- ~x+y
spplot(meuse["zinc"],
col.regions = bpy.colors())

[113,458.2]
(458.2,803.4]
(803.4,1149]
(1149,1494]
(1494,1839]

9 / 26

Getting spatial data in R


Usually, we dont create spatial objects from scratch, but from
external files (readGDAL, readOGR), or from data.frame objects:
> library(sp)
> data(meuse)
> class(meuse)
[1] "data.frame"
> dim(meuse)
[1] 155

14

> names(meuse)[1:6]
[1] "x"
>
>
>
>
>
>

"y"

"cadmium" "copper"

"lead"

"zinc"

coordinates(meuse) = c("x", "y")


# which is short for:
data(meuse)
pts = SpatialPoints(meuse[c("x", "y")])
m = SpatialPointsDataFrame(pts, meuse)
class(m)

[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
10 / 26

Selection on spatial objects


Selecting the data.frame metaphor:
I selection of records (features), attributes (columns):
> meuse[1:3,1:6]
x
y cadmium copper lead zinc
1 181072 333611
11.7
85 299 1022
2 181025 333558
8.6
81 277 1141
3 181165 333537
6.5
68 199 640
I

extraction of variables:
> meuse$zinc[1:3]
[1] 1022 1141

640

replacement:
> meuse$zinc[1:2] = NA
> meuse[1:3,1:6]
x
y cadmium copper lead zinc
1 181072 333611
11.7
85 299
NA
2 181025 333558
8.6
81 277
NA
3 181165 333537
6.5
68 199 640
11 / 26

Aggregation, in general
> d = data.frame(x = 1:6, grp1 = c(rep("A",3), rep("B",3)))
> d$grp2 = rep(c("P","Q","R"), each = 2)
> d
1
2
3
4
5
6

x grp1 grp2
1
A
P
2
A
P
3
A
Q
4
B
Q
5
B
R
6
B
R

> aggregate(d[1], list(d$grp1), mean)


1
2

Group.1 x
A 2
B 5

> aggregate(d[1], list(d$grp1, d$grp2), mean)


1
2
3
4

Group.1 Group.2
x
A
P 1.5
A
Q 3.0
B
Q 4.0
B
R 5.5

12 / 26

Aggregation, needs:
## S3 method for class 'data.frame'
aggregate(x, by, FUN, ..., simplify = TRUE)
I

an object to aggregate (x)

an aggregation predicate (by)

an aggregation function (FUN)

NOTE that
I

we pass functions as arguments R is a functional


programming language

we can write our own function, and pass it to FUN

... is passed on to this function

13 / 26

Aggregation of spatial classes

>
>
>
>
>
+
>
>
>
+
+
+

library(sp)
data("meuse")
coordinates(meuse) <- ~x+y
offset = c(178460, 329620)+20
gt = GridTopology(offset, c(400,400),
c(8,11))
SG = SpatialGrid(gt)
agg = aggregate(meuse["zinc"], SG)
spplot(agg["zinc"],
col.regions=bpy.colors(),
sp.layout = list("sp.points",
meuse, col=3))

1600

1400

1200

1000

800

600

400

200

14 / 26

Aggregating interpolated values

7.5

>
>
>
>
>
>
>
>
+
>

library(sp)
data("meuse")
coordinates(meuse) <- ~x+y
data("meuse.grid")
coordinates(meuse.grid) <- ~x+y
gridded(meuse.grid) <- TRUE
library(gstat)
x = idw(log(zinc)~1, meuse,
meuse.grid, debug.level=0)[1]
spplot(x[1],col.regions=bpy.colors())

7.0

6.5

6.0

5.5

5.0

Aggregating interpolated values, spatially

6.8

6.6

6.4

> agg = aggregate(x["var1.pred"], SG)


> spplot(agg, col.regions=bpy.colors(),
+
sp.layout = list("sp.points",
+
meuse.grid, col=grey(.5), cex=.5))

6.2

6.0

5.8

5.6

5.4

5.2

NOTE: the aggregation predicate (SG) can be of any type: points,


lines, polygons, grid.

16 / 26

Aggregating by attribute value

(7.5,8]

> i = cut(x$var1.pred, seq(4, 8, by=.5),


+ include.lowest = TRUE)
> xa = aggregate(x["var1.pred"],
+ list(i=i))
> spplot(xa[1],col.regions=bpy.colors(8))

(NOTE: this is still in sp on r-forge, not on CRAN)

(7,7.5]
(6.5,7]
(6,6.5]
(5.5,6]
(5,5.5]
(4.5,5]
[4,4.5]

Spatio-temporal classes

Package spacetime tries to combine all cleverness of spatial data


in sp, of temporal data in zoo and xts, and then add some. It
mainly solves:
I

object creation (e.g. from tables, sp and/or xts objects),

some I/O (RasterStack with time z; TGRASS, PostGIS)

selection (space, time, attributes)

aggregation (over space, over time, over space-time)

plotting

18 / 26

meteo: precipitation and stations


> library(meteo)
> data(dprec); head(dprec, 2)
staid
time prec
1
2707 2011-07-01 0.2
2 67240-99999 2011-07-01 0.0
> data(stations); head(stations, 2)
9602
9512

staid
lon
lat elev_1m data_source station_name
1 14.80 56.86667
166
ECA
VAEXJOE
10 18.05 59.35000
44
ECA
STOCKHOLM

> mtch = match(dprec$staid, stations$staid)


> dprec = data.frame(dprec, stations[mtch, c("lon", "lat")])
> head(dprec, 2)
staid
time prec
lon
lat
1
2707 2011-07-01 0.2 7.241389 62.24778
2 67240-99999 2011-07-01 0.0 7.467000 46.30000

19 / 26

meteo: precipitation and stations


>
>
>
>
>

library(spacetime)
m = stConstruct(dprec, c("lon", "lat"), "time")
#, crs = CRS("+init=epsg:4326"))
m2 = as(m, "STFDF")
summary(m2[,,"prec"])

Object of class STFDF


with Dimensions (s, t, attr): (12923, 31, 1)
[[Spatial:]]
Object of class SpatialPoints
Coordinates:
min
max
lon -179.633 179.75
lat -90.000 83.65
Is projected: NA
proj4string : [NA]
Number of points: 12923
[[Temporal:]]
Index
timeIndex
Min.
:2011-07-01
Min.
: 1.0
1st Qu.:2011-07-08
1st Qu.: 8.5
Median :2011-07-16
Median :16.0
Mean
:2011-07-16
Mean
:16.0

20 / 26

plot: map-panel

prec
20110701
20110702
20110703
20110704
20110705
20110706
20110707

20110708
20110709
20110710
20110711
20110712
20110713
20110714

>
>
>
>
+

data(NLpol) # in meteo!
20110716
20110717
20110718
20110719
20110720
20110721
proj4string(m2) = proj4string(NLpol)20110715
m2.NL = m2[NLpol,]
stplot(m2.NL[,,"prec"],
20110722
20110723
20110724
20110725
20110726
20110727
20110728
col.regions = bpy.colors())

[0,5.12]
(5.12,10.24]
(10.24,15.36]
(15.36,20.48]
(20.48,25.6]
(25.6,30.72]
(30.72,35.84]
(35.84,40.96]
(40.96,46.08]
(46.08,51.2]
(51.2,56.32]
(56.32,61.44]
(61.44,66.56]
(66.56,71.68]
(71.68,76.8]

20110729
20110730
20110731

21 / 26

xt: space-time (Hovm


oller)
prec

60
Jul 25
50

40

Jul 18

time

> proj4string(m2) = proj4string(NLpol)


> m2.NL = m2[NLpol,]
> stplot(m2.NL[1:100,,"prec"],
+
col.regions=bpy.colors(),
+
mode="xt")

30
Jul 11
20

10
Jul 04

0
9
113
0
15
17
19
21
27
29
41
45
61
63
65
69
75
83
85
87
91
93
103
109
119
123
133
141
143
151
155
161
163
167
175
193
195
197
205
206
207
213
215
217
219
229
235
241
245
247
255
258
259
267
271
275
289
295
301
305
308
309
310
320
324
326
342
346
350
352
358
374
382
384
386
388
390
392
400
404
406
412
420
422
430
440
446
448
450
464
470
476
482
488
490
492
494
510
518
526
532

sp.ID

22 / 26

ts: time series


prec

60

40

prec

> proj4string(m2) = proj4string(NLpol)


> m2.NL = m2[NLpol,]
> stplot(m2.NL[120:140,,"prec"],
+
mode="ts")

1467
1539
1807
1957
2163
2169
2770
3317
3427
3625
3913
4345
4476
4625
4761
4951
5524
5730
5848
5956
6356

20

Jul 04

Jul 11

Jul 18

Jul 25

Aug 01

time

23 / 26

time-panel
prec
Jul 04 Jul 18

Jul 04 Jul 18

1467

1539

1807

1957

2163

2169

2770

3317

3427

3625

60
40
20
0

prec

> proj4string(m2) = proj4string(NLpol)


> m2.NL = m2[NLpol,]
> stplot(m2.NL[120:140,,"prec"],
+
mode="tp")

60
40
20
0

3913

4345

4476

4625

4761

4951

5524

5730

5848

5956

60
40
20
0
60
40
20
0

6356
60
40
20
0
Jul 04 Jul 18

time

24 / 26

Aggregation on spatio-temporal objects

prec
20110701 20110706 20110711 20110716

> m2.agg = aggregate(m2.NL[,,"prec"],


+
"5 days", sum)
> stplot(m2.agg[,,"prec"],
+
col.regions=bpy.colors())

20110721 20110726 20110731

[0,8.84]
(8.84,17.68]
(17.68,26.52]
(26.52,35.36]
(35.36,44.2]
(44.2,53.04]
(53.04,61.88]
(61.88,70.72]
(70.72,79.56]
(79.56,88.4]
(88.4,97.24]
(97.24,106.1]
(106.1,114.9]
(114.9,123.8]
(123.8,132.6]

25 / 26

Aggregation on spatio-temporal objects

5
0

> m2.aggNL = aggregate(m2.NL[,,"prec"],


+
NLpol, mean, na.rm=TRUE)
> plot(m2.aggNL)

10

15

20

m2.aggNL

Jul 01
2011

Jul 11
2011

Jul 18
2011

Jul 25
2011

Jul 31
2011

26 / 26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy