1 References

3 Coordinate Reference Systems (CRS)

  • unprojected or geographic: Latitude and Longitude for referencing location on the ellipsoid Earth
    (decimal degrees (DD) or degrees, minutes, and seconds (DMS))

  • projected: Easting and Northing for referencing location on 2-dimensional representation of Earth
    Common projection: Universal Transverse Mercator (UTM)
    Location is given by the zone number (60 zones), hemisphere (north or south), and Easting and Northing coordinates in the zone in meters

4 Spatial data

Spatial data can be represented as simple features (sf) or as spatial class (sp).

Packages sf and sp can be used to work with spatial data.

4.1 Simple features (sf)

sf objects are extensions of data frames. Each row is one feature or spatial object that could have associated data.

4.2 Spatial class (sp)

  • SpatialPoints and SpatialPointsDataFrame
  • SpatialLines and SpatialLinesDataFrame
  • SpatialPolygons and SpatialPolygonsDataFrame
  • SpatialGrid and SpatialGridDataFrame
  • SpatialPixel and SpatialPixelDataFrame

5 Shapefile

Geographic data can be represented using a data storage format called shapefile. A shapefile consists of a collection of related files:

  • .shp: contains the geometry data,
  • .shx: is a positional index of the geometry data that allows to seek forwards and backwards the .shp file,
  • .dbf: stores the attributes for each shape.

Other files that can form a shapefile are the following:

  • .prj: plain text file describing the projection,
  • .sbn and .sbx: spatial index of the geometry data,
  • .shp.xml: geospatial metadata in XML format.

We can read shapefiles with rgdal::readOGR() or sf::st_read()

# name of the shapefile of North Carolina of the sf package
nameshp <- system.file("shape/nc.shp", package = "sf")


Exercise: Identify the folder of nc.shp in your computer.

5.1 sf (simple features)

# read shapefile with st_read()
library(sf)
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
map <- st_read(nameshp, quiet = TRUE)
class(map)
## [1] "sf"         "data.frame"
head(map)
plot(map)
## Warning: plotting the first 10 out of 14 attributes; use max.plot = 14 to plot
## all

5.2 sp

# read shapefile with readOGR()
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired during 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-4, (SVN revision 1196)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.5.2, released 2022/09/02
## Path to GDAL shared files: C:/Users/MORAGAPE/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
## Path to PROJ shared files: C:/Users/MORAGAPE/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-1
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
map <- rgdal::readOGR(nameshp, verbose = FALSE)
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
## Warning: OGR support is provided by the sf and terra packages among others
class(map)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
head(map@data)
plot(map)

spplot(map, zcol = "BIR74")

6 Types of spatial data

https://www.paulamoraga.com/book-geospatial/sec-spatialdataandCRS.html#types-of-spatial-data

Exercises:

  • Change theme
  • Add labels
  • Save plot