6.2 When to reproject?
The previous section showed how to set the CRS manually, with stset_crs(london, 4326)
.In real world applications, however, CRSs are usually set automatically when data is read-in.The main task involving CRSs is often to _transform objects, from one CRS into another.But when should data be transformed? And into which CRS?There are no clear-cut answers to these questions and CRS selection always involves trade-offs (Maling 1992).However, there are some general principles provided in this section that can help you decide.
First it’s worth considering when to transform.In some cases transformation to a projected CRS is essential, such as when using geometric functions such as st_buffer()
, as Figure 6.1 shows.Conversely, publishing data online with the leaflet package may require a geographic CRS.Another case is when two objects with different CRSs must be compared or combined, as shown when we try to find the distance between two objects with different CRSs:
st_distance(london_geo, london_proj)
# > Error: st_crs(x) == st_crs(y) is not TRUE
To make the london
and london_proj
objects geographically comparable one of them must be transformed into the CRS of the other.But which CRS to use?The answer is usually ‘to the projected CRS’, which in this case is the British National Grid (EPSG:27700):
london2 = st_transform(london_geo, 27700)
Now that a transformed version of london
has been created, using the sf function st_transform()
, the distance between the two representations of London can be found.It may come as a surprise that london
and london2
are just over 2 km apart
st_distance(london2, london_proj)
#> Units: [m]
#> [,1]
#> [1,] 2018