Fork me on GitHub

Make sure you have the sp and raster libraries installed. [Note that the latest raster(2.5-2) depends on sp (≥ 1.2-0), so make sure that your versions align.]

Mac users

Since there is now a Mac binary package for rgdal available on CRAN a installing sp with the default settings will also install this rgdal as one of its dependencies, which includes a built-in basic GDAL that lacks all the extra GDAL formats can handle. There is an alternative rgdal distributed by kyngchaos that you can use instead. Formats not included in the CRAN distribution but included in the kyngchaos distribution are currently: DODS, Geomedia, Interlis 1, Interlis 2, LIBKML, MSSQLSpatial, NAS, ODBC, OGDI, PGeo, PostgreSQL, OSI, Walk, XLS. (You mostly might care about having the PostgreSQL driver at some point.)

If you DON’T care, simply say:

install.packages(c("sp", "raster"))

If you DO care, follow the instructions below.

  1. Install the raster library:

  2. Don’t install sp with dependencies:

    install.packages("sp", dependencies = F)
  3. Download the latest GDAL complete from this site

  4. Doubleclick and install the downloaded .dmg file as you are used to on a Mac.

  5. Make sure you have R Version 3.2 or later installed – if not update it.

  6. Download a different rgdal from this site.

  7. Doubleclick to open the .dmg file

  8. Move rgdal_*.tgz to your Desktop folder

  9. Install the local package with:

    install.packages("~/Desktop/rgdal_*.tgz", repos = NULL, type = .Platform$pkgType)

Windows users

install.packages(c("sp", "raster"))

Mac and Windows

Test if all went well:

library (rgdal)

1. Spatial objects in R

Conceptualizing a spatial Object

In vector GIS we deal with, points, lines, and polygons:

Exercise 1

Discuss with your neighbor: What do we need to specify in order to define spatial vector data?

  • lat/lon coordinates
  • projection
  • attribute data
  • if polygon, is it a hole or not
  • … ?

In R the sp package provides classes and methods for spatial data types1.

Development of the sp began in the early 2000s in an attempt to standardize how spatial data would be treated in R and to allow for better interoparbility between different analysis packages that use spatial data. The package provides classes and methods to create points, lines, polygons, and grids and to operate on them. It is one of the most important packages that you will need to use if dealing with spatial data in R. Many of the spatial analysis packages now use the spatial data types that are implemented in sp i.e. they “depend” on the sp package.

In sp spatial objects are conceptualized in the following way2:

The foundational structure for any spatial object in sp is the Spatial class. It has two slots (new-style class objects in R have pre-defined components called slots):

  • a bounding box

  • a CRS class object to define the Coordinate Reference System

2. Creating a spatial object

In order to create a spatial object manually the basic steps are:

I. Create a bunch of points, lines, or polygons (details below)

  1. Convert those to a Spatial* object (* stands for Points, Lines, or Polygons). This steps adds the bounding box (automatically) and the slot for the Coordinate Reference System or CRS (which needs to be filled manually).
  1. (Optional:) Add a data frame with attribute data, which will turn your Spatial* object into a Spatial*DataFrame object.

I. Create geometric objects (topology)

Points (which may have 2 or 3 dimensions) as the most basic spatial data object. They are generated out of either a single coordinate or a set of coordinates3, like a two-column matrix or a dataframe with a column for latitude and one for longitude.

Lines are generated out of Line objects. A Line object is a spaghetti collection of 2D coordinates and is generated out of a two-column matrix or a dataframe with a column for latitude and one for longitude. A Lines object is a list of Line objects, for example all the contours at a single elevation.

Polygons are generated out Polygon objects. A Polygon object is a spaghetti collection of 2D coordinates with equal first and last coordinates and is generated out of a two-column matrix or a dataframe with a column for latitude and one for longitude. A Polygons object is a list of Polygon objects, for example islands belonging to the same country.

II. Create spatial objects

Both Line/Lines and Polygon/Polygons objects are part of the R’s basic graphics package. So we need to turn those into spatial, i.e. “geographically aware” objects. SpatialPoints are directly made out of the coordinates. SpatialLines and SpatialPolygons objects are made using lists of Lines or Polygons objects respectively.

III. Add attributes

The points in a SpatialPoints object may be associated with a row of attributes to create a SpatialPointsDataFrame object. The coordinates and attributes may, but do not have to be keyed to each other using ID values.

SpatialLinesDataFrame and SpatialPolygonsDataFrame objects are defined using SpatialLines and SpatialPolygons objects and standard data frames, and the ID fields are here required to match the data frame row names.