Spatial Objects And Points in R

Spatial Analytics
Author

Muhammad Uzair Aslam

Published

March 9, 2023

In my last article, I explained the fundamentals of Spatial Data Analysis and different types of Spatial Data including point, line, polygon and grid. If you want a quick recap, you can find it here :

Hello World: Introducing Spatial Data

I ended the last article with the promise of covering Points in my next article. In this article I will make a deep dive in Spatial Objects and Spatial Points in R.

Points are one of the important data models in R. However, before we dive into Spatial Points, I want to give brief overview of Spatial Objects in R.

Spatial Objects in R

R developers wrote a special package sp which extend base R classes and methods for spatial data. To get things started download sp by following command:

install.packages("sp")

Next, we attach the package by:

library(sp)

I will call the Spatial class using getClass method to get the complete definition of a S4 class in R. This includes the slot names and types of their content:

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

This returns us two important information about this class.

  1. Slots: a named component of the object that is accessed using the specialized sub-setting operator @ (pronounced at). In our case we see that Spatial class has two named components :

    1. Matrix

    2. CRS (Cordinate Reference System)

  2. Known Sub classes: These are classes that inherit one or more language entities from another class/classes. In our case SpatialPoints class inherits language entities from Spatial class. In other words, there is parent-child relationship between Spatial class and SpatialPoints class.

Spatial Points

From our mathematical knowledge, we know that point is a pair of numbers in \((x,y)\) defined over a known region. According to (Herring 2011), it is a 0 - dimensional geometric object and represents a single location in a coordinate space.

The understanding of Earth before the introduction of World Geodetic System 1984 was sphere. But later Geodesists represented the globe more accurately by ellipsoid model (Roger S. Bivand 2013). Today Global Positioning System (GPS) uses the World Geodetic System (WGS84) as its reference coordinate system. More details about about WGS84 can be found here.

We now see how SpatialPoints class extends Spatial class. By using getClass(), we can see the slot names and type of content.

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

We see that SpatialPoints extends Spatial class by adding a coords slot, into which a matrix of point cordinates can be inserted. We also see three other classes extended by SpatialPoints:

  1. SpatialPointsDataFrame

  2. SpatialPixels

  3. SpatialPixelsDataFrame

To understand the methods and functions provided by SpaitalPoints, I read a data file with the positions of CRAN mirrors across the world in 2005. We first pull the data in R:

df <- read.table("http://www.asdar-book.org/datasets/CRAN051001a.txt", header = TRUE)

Then we see its first six rows:

head(df)
           place   north     east       loc      long       lat
1       Brisbane 27d28'S 153d02'E Australia 153.03333 -27.46667
2      Melbourne 37d49'S 144d58'E Australia 144.96667 -37.81667
3           Wien 48d13'N  16d20'E   Austria  16.33333  48.21667
4       Curitiba 25d25'S  49d16'W    Brazil -49.26667 -25.41667
5      Vi\xe7oza 20d45'S  42d52'W    Brazil -42.86667 -20.75000
6 Rio de Janeiro 22d54'S  43d12'W    Brazil -43.20000 -22.90000

We extract the two columns with the longitude and latitude values into a matrix, and use str to view:

# make a latitude and longitude matrix
cran_mat <- cbind(df$long, df$lat) 

# assign row names from 1 to number of rows
row.names(cran_mat) <- 1:nrow(cran_mat)

# view the matrix structure
str(cran_mat)
 num [1:54, 1:2] 153 145 16.3 -49.3 -42.9 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:54] "1" "2" "3" "4" ...
  ..$ : NULL

Before moving on it is necessary to introduce the coordinate reference system (CRS) class briefly. We saw that Spatial class had two slots. One of them was Coordinate Reference System (CRS). Let us see the slot names and components.

getClass("CRS")
Class "CRS" [package "sp"]
Slots:
Name:   projargs
Class: character

We see that the class has a character string as its only slot value. The character string may be a missing value but if it is not missing it should be PROJ.4-format string describing the projection. For geographical coordinates, the simplest of such string is “+proj=longlat”.

We now see the implementation in our CRAN data set.

# make a CRS object in longlat and World Geodetic System(WGS84)
CRS_obj <- CRS(projargs = "+proj=longlat +ellps=WGS84")

# check summary of the object
summary(CRS_obj)
Length  Class   Mode 
     1    CRS     S4 

At this point you must be wondering what is PROJ.4 format?

PROJ is a generic coordinate transformation software that transforms geospatial coordinates from one coordinate reference system (CRS) to another. This includes cartographic projections as well as geodetic transformations.

To define a Coordinate Reference System in sp package we use CRS object which defines the coordinate reference systems we use.

Finally we create SpatialPoints object

# make a spatial points class by long/lat matrix and proj4string
cran_sp <- SpatialPoints(coords = cran_mat, proj4string = CRS_obj)

# summary of the cran_sp object
summary(cran_sp)
Object of class SpatialPoints
Coordinates:
                 min      max
coords.x1 -122.95000 153.0333
coords.x2  -37.81667  57.0500
Is projected: FALSE 
proj4string : [+proj=longlat +ellps=WGS84]
Number of points: 54

SpatialPoints objects may have more than two dimensions, but plot methods for the class use only the first two.

Ending Remarks

In this article, we got an overview of Spaital and SpatialPoints class. But we are only half way done. In next article, I will explain the methods and data frames for spatial points to do some data wrangling.

References

Herring, J. R. 2011. OpenGIS® Implementation Standard for Geographic Information - Simple Feature Access - Part 1: Common Architecture. Technical Report 1.2.1. Open Geospatial Consortium Inc.
Roger S. Bivand, Virgilio Gómez-Rubio, Edzer Pebesma. 2013. Applied Spatial Data Analysis with r. Springer New York, NY.