Intro SP
Intro SP
the sp Package
Edzer Pebesma∗ Roger S. Bivand†
Feb 2005
Contents
1 Introduction 2
4 Spatial points 4
4.1 Points without attributes . . . . . . . . . . . . . . . . . . . . . . 4
4.2 Points with attributes . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Grids 11
5.1 Creating grids from topology . . . . . . . . . . . . . . . . . . . . 11
5.2 Creating grids from points . . . . . . . . . . . . . . . . . . . . . . 13
5.3 Gridded data with attributes . . . . . . . . . . . . . . . . . . . . 14
5.4 Are grids stored as points or as matrix/array? . . . . . . . . . . . 15
5.5 Row and column selection of a region . . . . . . . . . . . . . . . . 16
6 Lines 17
6.1 Building line objects from scratch . . . . . . . . . . . . . . . . . . 17
6.2 Building line objects with attributes . . . . . . . . . . . . . . . . 19
7 Polygons 19
7.1 Building from scratch . . . . . . . . . . . . . . . . . . . . . . . . 19
7.2 Polygons with attributes . . . . . . . . . . . . . . . . . . . . . . . 20
∗ Institutefor Geoinformatics, University of Muenster, Heisenbergstraße 2, 48149 Münster,
Germany. [email protected]
† Economic Geography Section, Department of Economics, Norwegian School of
1
8 Importing and exporting data 21
1 Introduction
The sp package provides classes and methods for dealing with spatial data in
R1 . The spatial data structures implemented include points, lines, polygons
and grids; each of them with or without attribute data. We have chosen to
use S4 classes and methods style (Chambers, 1998) to allow validation of ob-
jects created. Although we mainly aim at using spatial data in the geographical
(two-dimensional) domain, the data structures that have a straightforward im-
plementation in higher dimensions (points, grids) do allow this.
From the package home page on CRAN, https://cran.r-project.org/
package=sp, links to a graph gallery with R code, and the development source
tree are found.
This vignette describes the classes, methods and functions provided by sp.
Instead of manipulating the class slots (components) directly2 , we provide meth-
ods and functions to create or modify the classes from elementary types such
as matrices, data.frames or lists and convert them back to any of these types.
Also, coercion (type casting) from one class to the other is provided, where
relevant.
Package sp is loaded by
> library(sp)
during DSC 2003. At that time, the advantage of having multiple R packages for spatial
statistics seemed to be hindered by a lack of a uniform interface for handling spatial data.
Each package had its own conventions on how spatial data were stored and returned. With
this package, and packages supporting the classes provided here, we hope that R with its
extension packages becomes more coherent for analyzing different types of spatial data.
2 which is possible, but not recommended because validity of resulting objects is no longer
verified
2
data type class attributes contains
points SpatialPoints No Spatial
points SpatialPointsDataFrame data.frame SpatialPoints
multipoints SpatialMultiPoints No Spatial
multipoints SpatialMultiPointsDataFrame data.frame SpatialMultiPoints
pixels SpatialPixels No SpatialPoints
pixels SpatialPixelsDataFrame data.frame SpatialPixels
SpatialPointsDataFrame
full grid SpatialGrid No SpatialPixels
full grid SpatialGridDataFrame data.frame SpatialGrid
line Line No
lines Lines No Line list
lines SpatialLines No Spatial, Lines list
lines SpatialLinesDataFrame data.frame SpatialLines
polygons Polygon No Line
polygons Polygons No Polygon list
polygons SpatialPolygons No Spatial, Polygons list
polygons SpatialPolygonsDataFrame data.frame SpatialPolygons
The class Spatial only holds metadata common to all derived classes (bound-
ing box, coordinate reference system), and is of convenient for defining methods
that are common to all derived classes. In the following sections we will show
how we can create objects of these classes from scratch or from other objects,
and which methods and functions are available to them.
3
Other methods available are: plot, summary, print, dim and names (operate
on the data.frame part), as.data.frame, as.matrix and image (for gridded
data), lines (for line data), points (for point data), subset (points and grids),
stack (point and grid data.frames), over for spatial joins, spplot, and length
(number of features).
4 Spatial points
4.1 Points without attributes
We can generate a set of 10 points on the unit square [0, 1] × [0, 1] by
> xc = round(runif(10), 2)
> yc = round(runif(10), 2)
> xy = cbind(xc, yc)
> xy
4
xc yc
[1,] 0.67 0.30
[2,] 0.96 0.62
[3,] 0.92 0.91
[4,] 0.77 0.85
[5,] 0.72 0.46
[6,] 0.74 0.39
[7,] 0.63 0.64
[8,] 0.19 0.72
[9,] 0.70 0.20
[10,] 0.37 0.28
SpatialPoints:
xc yc
[1,] 0.67 0.30
[2,] 0.96 0.62
[3,] 0.92 0.91
[4,] 0.77 0.85
[5,] 0.72 0.46
[6,] 0.74 0.39
[7,] 0.63 0.64
[8,] 0.19 0.72
[9,] 0.70 0.20
[10,] 0.37 0.28
Coordinate Reference System (CRS) arguments: NA
> dim(xy.cc)
[1] 10 2
and other methods retrieve the bounding box, the dimensions, select points (not
dimensions or columns), coerce to a data.frame, or print a summary:
> bbox(xy.sp)
5
Figure 1: plot of SpatialPoints object; aspect ratio of x and y axis units is 1
6
min max
xc 0.19 0.96
yc 0.20 0.91
> dimensions(xy.sp)
[1] 2
> xy.sp[1:2]
SpatialPoints:
xc yc
[1,] 0.67 0.30
[2,] 0.96 0.62
Coordinate Reference System (CRS) arguments: NA
[1] "data.frame"
> dim(xy.df)
[1] 10 2
> summary(xy.sp)
z1 z2
1 3.10 20
2 4.15 21
3 3.68 22
4 4.45 23
7
5 6.62 24
6 5.57 25
7 3.66 26
8 3.75 27
9 5.19 28
10 5.02 29
coordinates z1 z2
1 (0.67, 0.3) 3.10 20
2 (0.96, 0.62) 4.15 21
3 (0.92, 0.91) 3.68 22
4 (0.77, 0.85) 4.45 23
5 (0.72, 0.46) 6.62 24
6 (0.74, 0.39) 5.57 25
7 (0.63, 0.64) 3.66 26
8 (0.19, 0.72) 3.75 27
9 (0.7, 0.2) 5.19 28
10 (0.37, 0.28) 5.02 29
> summary(xy.spdf)
> dimensions(xy.spdf)
[1] 2
8
coordinates z1 z2
1 (0.67, 0.3) 3.10 20
2 (0.96, 0.62) 4.15 21
coordinates z1
1 (0.67, 0.3) 3.10
2 (0.96, 0.62) 4.15
3 (0.92, 0.91) 3.68
4 (0.77, 0.85) 4.45
5 (0.72, 0.46) 6.62
6 (0.74, 0.39) 5.57
7 (0.63, 0.64) 3.66
8 (0.19, 0.72) 3.75
9 (0.7, 0.2) 5.19
10 (0.37, 0.28) 5.02
coordinates z2
1 (0.67, 0.3) 20
2 (0.96, 0.62) 21
z1 z2 xc yc
1 3.10 20 0.67 0.30
2 4.15 21 0.96 0.62
> dim(xy.cc)
[1] 10 2
A note on selection with [: the behaviour is as much as possible copied from that
of data.frames, but coordinates are always sticky and a SpatialPointsDataFrame
is always returned; drop=FALSE is not allowed. If coordinates should be dropped,
use the as.data.frame method and select the non-coordinate data, or use [[
to select a single attribute column (example below).
SpatialPointsDataFrame objects can be created directly from data.frames
by specifying which columns contain the coordinates:
9
> df1 = data.frame(xy, df)
> coordinates(df1) = c("xc", "yc")
> df1
coordinates z1 z2
1 (0.67, 0.3) 3.10 20
2 (0.96, 0.62) 4.15 21
3 (0.92, 0.91) 3.68 22
4 (0.77, 0.85) 4.45 23
5 (0.72, 0.46) 6.62 24
6 (0.74, 0.39) 5.57 25
7 (0.63, 0.64) 3.66 26
8 (0.19, 0.72) 3.75 27
9 (0.7, 0.2) 5.19 28
10 (0.37, 0.28) 5.02 29
or
coordinates z1 z2
1 (0.67, 0.3) 3.10 20
2 (0.96, 0.62) 4.15 21
> as.data.frame(df2)[1:2,]
xc yc z1 z2
1 0.67 0.30 3.10 20
2 0.96 0.62 4.15 21
xc yc
1 0.67 0.30
2 0.96 0.62
10
[1] 20 21 22 23 24 25 26 27 28 29
> df2[["z2"]][10] = 20
> df2[["z3"]] = 1:10
> summary(df2)
Plotting attribute data can be done by using either spplot to colour symbols,
or bubble which uses symbol size:
5 Grids
Package sp has two classes for grid topology: SpatialPixels and SpatialGrid.
The pixels form stores coordinates and is for partial grids, or unordered points;
the SpatialGrid form does not store coordinates but holds full grids (i.e.,
SpatialGridDataFrame holds attribute values for each grid cell). Objects can
be coerced from one representation to the other.
11
z1
3.1 [3.1,3.804]
3.698 (3.804,4.508]
4.3 (4.508,5.212]
5.148 (5.212,5.916]
6.62 (5.916,6.62]
12
Object of class SpatialGrid
Coordinates:
min max
[1,] 0.5 3.5
[2,] 0.5 4.5
[3,] 1.5 7.5
Is projected: NA
proj4string : [NA]
Grid attributes:
cellcentre.offset cellsize cells.dim
1 1 1 3
2 1 1 4
3 2 1 6
13
> grd = as(grd.pts, "SpatialGrid")
> summary(grd)
[1] FALSE
[1] TRUE
> summary(grd.attr)
14
Number of points: 9
Grid attributes:
cellcentre.offset cellsize cells.dim
xc 1 1 3
yc 1 1 3
Data attributes:
z1 z2
Min. :1 Min. :1
1st Qu.:3 1st Qu.:3
Median :5 Median :5
Mean :5 Mean :5
3rd Qu.:7 3rd Qu.:7
Max. :9 Max. :9
Package raster provides dedicated methods to deal with raster data, and can
deal with grids that are too large to be stored in memory.
[1] TRUE
> fullgrid(grd.pts)
[1] FALSE
> fullgrid(grd.attr)
[1] FALSE
[1] TRUE
> fullgrid(grd.attr)
[1] TRUE
The advantage of having grids in cell form is that when a large part of the
grid contains missing values, these cells are not stored. In addition, no ordering
of grid cells is required. For plotting by a grid with levelplot, this form is
15
required and spplot (for grids a front-end to levelplot) will convert grids
that are not in this form. In contrast, image requires a slightly altered version
of the the full grid form. A disadvantage of the cell form is that the coordinates
for each point have to be stored, which may be prohibitive for large grids. Grids
in cell form do have an index to allow for fast transformation to the full grid
form.
Besides print, summary, plot, objects of class SpatialGridDataFrame have
methods for
• [ select rows (points) and/or columns (variables)
• [[ extract a column from the attribute table
16
[4,] 1 2
[5,] 2 2
Coordinate Reference System (CRS) arguments: NA
Data summary:
z1
Min. :4.0
1st Qu.:5.0
Median :7.0
Mean :6.6
3rd Qu.:8.0
Max. :9.0
Data summary:
z2 z1
Min. :1.0 Min. :4.0
1st Qu.:2.5 1st Qu.:5.5
Median :3.5 Median :6.5
Mean :3.5 Mean :6.5
3rd Qu.:4.5 3rd Qu.:7.5
Max. :6.0 Max. :9.0
6 Lines
6.1 Building line objects from scratch
In many instances, line coordinates will be retrieved from external sources. The
following example shows how to build an object of class SpatialLines from
scratch. Note that the Lines objects have to be given character ID values, and
17
that these values must be unique for Lines objects combined in a SpatialLines
object.
> l1 = cbind(c(1,2,3),c(3,2,2))
> l1a = cbind(l1[,1]+.05,l1[,2]+.05)
> l2 = cbind(c(1,2,3),c(1,1.5,1))
> Sl1 = Line(l1)
> Sl1a = Line(l1a)
> Sl2 = Line(l2)
> S1 = Lines(list(Sl1, Sl1a), ID="a")
> S2 = Lines(list(Sl2), ID="b")
> Sl = SpatialLines(list(S1,S2))
> summary(Sl)
18
6.2 Building line objects with attributes
The class SpatialLinesDataFrame is designed for holding lines data that have
an attribute table (data.frame) attached to it:
> df = data.frame(z = c(1,2), row.names=sapply(slot(Sl, "lines"), function(x) slot(x, "ID"))
> Sldf = SpatialLinesDataFrame(Sl, data = df)
> summary(Sldf)
Not many useful methods for it are available yet. The plot method only plots
the lines, ignoring attribute table values. Suggestions for useful methods are
welcome.
7 Polygons
7.1 Building from scratch
The following example shows how a set of polygons are built from scratch.
Note that Sr4 has the opposite direction (anti-clockwise) as the other three
(clockwise); it is meant to represent a hole in the Sr3 polygon. The default
value for the hole colour pbg is "transparent, which will not show, but which
often does not matter, because another polygon fills the hole — here it is set
to "white". Note that the Polygons objects have to be given character ID
values, and that these values must be unique for Polygons objects combined in
a SpatialPolygons object.
> Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
> Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
> Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
> Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)
> Srs1 = Polygons(list(Sr1), "s1")
19
> Srs2 = Polygons(list(Sr2), "s2")
> Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
> SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
> plot(SpP, col = 1:3, pbg="white")
> # plot(SpP)
a b
s1 3 1
s2 2 2
s3/4 1 3
20
> spplot(SrDf)
b
3.0
2.5
2.0
a
1.5
1.0
References
Chambers, J.M., 1998, Programming with data, a guide to the S language.
Springer, New York.
21