5.1 Introduction
The previous three chapters have demonstrated how geographic datasets are structured in R (Chapter 2) and how to manipulate them based on their non-geographic attributes (Chapter 3) and spatial properties (Chapter 4).This chapter extends these skills.After reading it — and attempting the exercises at the end — you should understand and have control over the geometry column in sf
objects and the geographic location of pixels represented in rasters.
Section 5.2 covers transforming vector geometries with ‘unary’ and ‘binary’ operations.Unary operations work on a single geometry in isolation.This includes simplification (of lines and polygons), the creation of buffers and centroids, and shifting/scaling/rotating single geometries using ‘affine transformations’ (Sections 5.2.1 to 5.2.4).Binary transformations modify one geometry based on the shape of another.This includes clipping and geometry unions, covered in Sections 5.2.5 and 5.2.6, respectively.Type transformations (from a polygon to a line, for example) are demonstrated in Section 5.2.7.
Section 5.3 covers geometric transformations on raster objects.This involves changing the size and number of the underlying pixels, and assigning them new values.It teaches how to change the resolution (also called raster aggregation and disaggregation), the extent and the origin of a raster.These operations are especially useful if one would like to align raster datasets from diverse sources.Aligned raster objects share a one-to-one correspondence between pixels, allowing them to be processed using map algebra operations, described in Section 4.3.2. The final Section 5.4 connects vector and raster objects.It shows how raster values can be ‘masked’ and ‘extracted’ by vector geometries.Importantly it shows how to ‘polygonize’ rasters and ‘rasterize’ vector datasets, making the two data models more interchangeable.