The sp package provides classes and methods for spatial data analysis and utility functions for plotting maps, spatial selection, as well as methods for retrieving coordinates, for subsetting, print, summary, etc. Many other packages have become dependent on the data representation provided by the sp package.

library(sp)

The foundation class of the sp package is the Spatial class, with two slots. The first is a bounding box, a matrix of numerical coordinates. The second is a CRS class object defining the coordinate reference system.

By calling getClass() we may inspect the complete definition of a class, including its slot names and the types of their contents:

getClass("Spatial")
## Class "Spatial" [package "sp"]
##
## Slots:
##
## Name:         bbox proj4string
## Class:      matrix         CRS
##
## Known Subclasses:
## Class "SpatialPoints", directly
## Class "SpatialMultiPoints", directly
## Class "SpatialGrid", directly
## Class "SpatialLines", directly
## Class "SpatialPolygons", directly
## Class "SpatialPointsDataFrame", by class "SpatialPoints", distance 2
## Class "SpatialPixels", by class "SpatialPoints", distance 2
## Class "SpatialMultiPointsDataFrame", by class "SpatialMultiPoints", distance 2
## Class "SpatialGridDataFrame", by class "SpatialGrid", distance 2
## Class "SpatialLinesDataFrame", by class "SpatialLines", distance 2
## Class "SpatialPixelsDataFrame", by class "SpatialPoints", distance 3
## Class "SpatialPolygonsDataFrame", by class "SpatialPolygons", distance 2

#### SpatialPoints class

The SpatialPoints class is the first subclass of Spatial. It extends the Spatial class by adding a coords slot, for a matrix of point coordinates.

getClass("SpatialPoints")
## Class "SpatialPoints" [package "sp"]
##
## Slots:
##
## Name:       coords        bbox proj4string
## Class:      matrix      matrix         CRS
##
## Extends: "Spatial"
##
## Known Subclasses:
## Class "SpatialPointsDataFrame", directly
## Class "SpatialPixels", directly
## Class "SpatialPixelsDataFrame", by class "SpatialPixels", distance 2

Let us create a SpatialPoints class object by ourselves. Therefore we need point coordinates and decide about the coordinate reference system for our data. For the first task we make use of the geocode() function in the ggmap() package. This function allows us to geocode a location (find its latitude and longitude) using either the Data Science Toolkit or Google Maps. Note that when using Google you are agreeing to the Google Maps API Terms of Service.

Let us retrieve the coordinates for the GeoCampus Lankwitz using the geocode() function and the Google Maps API.

Please note that the free access to the Google API is depricated. Hence, before we can start geocoding, we need to obtain an API key from Google. Visit the registration page, and follow the instructions. Once you got your key you have to register it. In order not to share my private key online, I created an environment variable and access that file to retrieve my private API key (Note that there are actually several ways to deal with secrets in R (e.g. here)

library(ggmap)
key = Sys.getenv("my_google_api_key") # retrieve my private API key from disk
register_google(key = key)            # activate the key

Make sure to activate Google’s Geocoding API and Maps Static API. Once you completed those steps you may start with geocoding.

geocampus <- ggmap::geocode("Malteserstrasse 74-100, Berlin")
geocampus
##       lon      lat
## 1 13.3581 52.42512

Nice! In the next step we have to decide on a coordinate reference system (CRS). In general the CRS may be defined in several ways, for example the CRS may be defined as Well-known text (WKT) format, or JSON format, or GML format, or in the Proj4 format, among many others.

The Proj4 format is a generic, string-based description of a CRS, understood by the proj.4 library. It defines projection types and parameter values for particular projections. For instance the Proj4 format string for the European Terrestrial Reference System 1989 (ETRS89) is $$\text {+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no\_defs}$$.

With respect to the enormous amount of existing CRS the International Association of Oil & Gas Producers (IOGP), formerly known as European Petroleum Survey Group (EPSG), built a collection of definitions for global, regional, national and local coordinate reference systems and coordinate transformations, the EPSG Geodetic Parameter Dataset. Within this collection each particular coordinate reference systems gets an unique integer identifier, commonly denoted as EPSG. For instance, the EPSG identifier for the ETRS89 is simply $$\mathtt 4258$$ (for other commonly used representations of ETRS89 go here or here).

In R we may either provide Spatial objects the Proj4 format string (proj4string) or simply the EPSG identifier. In our example we us the EPSG identifier $$4326$$, which corresponds to the latest revision of the World Geodetic System (WGS84).

The CRS() function of the sp package transforms the character string into a coordinate reference system (CRS) object.

latlong = CRS("+init=epsg:4326")
geocampus.sp <- SpatialPoints(geocampus,
proj4string = latlong)
geocampus.sp
## SpatialPoints:
##          lon      lat
## [1,] 13.3581 52.42512
## Coordinate Reference System (CRS) arguments: +init=epsg:4326
## +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0

Without a geographical context geographical coordinates are difficult to understand, thus we plot the location of the GeoCampus Lankwitz by making use of the mapview package.

library(mapview)
img <- "http://www.osa.fu-berlin.de/geographie_bsc/_medien/bilder_geocampus/bild_010/010_546.jpeg"

mapview(geocampus.sp,
map.types = "Esri.WorldImagery",
popup = popupImage(img, src = "remote"),
label = "GeoCampus Lankwitz")