#### Geographic projections and transformations

geocompr rspatial sf crs

# Introduction

This workbook outlines key concepts and functions related to map projections — also referred to as coordinate reference systems (CRSs) — and transformation of geographic data from one projection to another. It is based on the open source book Geocomputation with R, and Chapter 6 in particular.

It was developed for the ‘CASA Summer School’, or the Doctoral Summer School for Advanced Spatial Modelling: Skills Workshop and Hackathon, 21st to 23rd August 2019, for its full name! It should be of use to anyone interested in projections, beyond the summer school, so we posted it on our newly updated website for maximum benefit.

# Prerequisites

Before you get started, make sure you have the packages installed:

library(sf)
library(dplyr)
library(spData)

# Introduction

Before we get started, why use R for geographic data?

Simple answer: because it works, excels at spatial statistics and visualisation and has a huge user community.

It can be used for a wide range of things such as:

Geographic data relies on a frame of reference. There are two main types of CRS:

• Geographic, where the frame of reference is the globe and how many degrees north or east from the position (0, 0) you are
• Projected, where the frame of reference is a flat representation of part of the Earth’s surface

# TLDR

The ‘too long didn’t read’ (TLDR) take away messages from this text are:

• Be aware of projections
• Decide an appropriate CRS for your project and ensure everything is in that CRS
• Use a projected CRS when doing geometric operations
• EPSG codes such as 27700 and 4326 refer to specific coordinate systems
• It is worth checking if there is an officially supported projection for the region — that is often a good option

In R, you can check, set and transform CRS with st_crs() and st_transform() as follows:

zones_london = lnd
st_crs(zones_london)                                         # find out the CRS
#> Coordinate Reference System:
#>   User input: EPSG:4326
#>   wkt:
#> GEOGCS["WGS 84",
#>     DATUM["WGS_1984",
#>         SPHEROID["WGS 84",6378137,298.257223563,
#>             AUTHORITY["EPSG","7030"]],
#>         AUTHORITY["EPSG","6326"]],
#>     PRIMEM["Greenwich",0,
#>         AUTHORITY["EPSG","8901"]],
#>     UNIT["degree",0.0174532925199433,
#>         AUTHORITY["EPSG","9122"]],
#>     AUTHORITY["EPSG","4326"]]
zones_london_projected = st_transform(zones_london, 27700)   # transform CRS
st_crs(zones_london) = NA                                    # set CRS
st_crs(zones_london) = 4326                                  # set CRS

# Why are projections needed?

Without a coordinate system, we have no context:

Which country is it?

Location on Earth is measured in degrees so, even when axes are equal, maps are highly distorted representations of reality far from the equator:

Along which axis is the image over-represented?

Even when we compensate for this, the cylindrical projection is misleading:

By default, most software (including R) plots data with geographic lon/lat coordinates a cylindrical projection, leading to maps like this:

plot(london$geometry, add = TRUE, pch = 9) # not there # Issues with geometric operations london_buff1 = st_buffer(london, 0.1) plot(london_buff1) plot(zones_london$geometry)
#> Warning in plot.sf(london_buff1, add = T): ignoring all but the first attribute

st_crs(london) = 4326
london_projected = st_transform(london, 27700)
london_buff2 = st_buffer(london_projected, 10000)
st_is_longlat(london_projected)
#> [1] FALSE
plot(zones_london_projected\$geometry)
#> Warning in plot.sf(london_buff2, add = TRUE): ignoring all but the first
#> attribute

If you’re interested in learning more on this, check out Geocomputations with R.

More specific resources on projections include:

Check out the questions in the exercises section of Chapter 6 of Geocomputation with R.

#### Recent changes in R spatial and how to be ready for them

geocompr sf raster tmap docker proj covid19

#### Demo of reproducible geographic data analysis: mapping Covid-19 data with R

geocompr rspatial sf benchmarks

#### Installing spatial R packages on Ubuntu

geocompr rspatial packages gdal geos proj linux