6.4 Reprojecting vector geometries

Chapter 2 demonstrated how vector geometries are made-up of points, and how points form the basis of more complex objects such as lines and polygons.Reprojecting vectors thus consists of transforming the coordinates of these points.This is illustrated by cycle_hire_osm, an sf object from spData that represents cycle hire locations across London.The previous section showed how the CRS of vector data can be queried with st_crs().Although the output of this function is printed as a single entity, the result is in fact a named list of class crs, with names proj4string (which contains full details of the CRS) and epsg for its code.This is demonstrated below:

  1. crs_lnd = st_crs(cycle_hire_osm)
  2. class(crs_lnd)
  3. #> [1] "crs"
  4. crs_lnd$epsg
  5. #> [1] 4326

This duality of CRS objects means that they can be set either using an EPSG code or a proj4string.This means that st_crs("+proj=longlat +datum=WGS84 +no_defs") is equivalent to st_crs(4326), although not all proj4strings have an associated EPSG code.Both elements of the CRS are changed by transforming the object to a projected CRS:

  1. cycle_hire_osm_projected = st_transform(cycle_hire_osm, 27700)

The resulting object has a new CRS with an EPSG code 27700.But how to find out more details about this EPSG code, or any code?One option is to search for it online.Another option is to use a function from the rgdal package to find the name of the CRS:

  1. crs_codes = rgdal::make_EPSG()[1:2]
  2. dplyr::filter(crs_codes, code == 27700)
  3. #> code note
  4. #> 1 27700 # OSGB 1936 / British National Grid

The result shows that the EPSG code 27700 represents the British National Grid, a result that could have been found by searching online for “EPSG 27700”.But what about the proj4string element?proj4strings are text strings in a particular format the describe the CRS.They can be seen as formulas for converting a projected point into a point on the surface of the Earth and can be accessed from crs objects as follows (see proj4.org for further details of what the output means):

  1. st_crs(27700)$proj4string
  2. #> [1] "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 ...

Printing a spatial object in the console, automatically returns its coordinate reference system.To access and modify it explicitly, use the st_crs function, for example, st_crs(cycle_hire_osm).