Session Ready
Exercise

Crop a raster based on another spatial object

As you saw in the previous exercise with mask(), the raster extent is not changed. If the extents of the input raster and the mask itself are different then they will still be different after running mask(). In many cases, however, you will want your raster to share an extent with another layer and this is where crop() comes in handy. With crop() you are cropping the raster so that the extent (the bounding box) of the raster matches the extent of the input crop layer. But within the bounding box no masking is done (no raster cells are set to NA).

In this exercise you will both mask and crop the NYC canopy layer based on the large parks and you'll compare. You should notice that the masked raster includes a lot of NA values (there are the whitespace) and that the extent is the same as the original canopy layer. With the cropped layer you should notice that the extent of the cropped canopy layer matches the extent of the large parks (essentially it's zoomed in).

Instructions
100 XP
  • Convert the parks_big layer (this is preloaded, it has been limited to large parks and projected) to a Spatial object with as() -- call this parks_sp.
  • First mask the canopy layer using mask() and call this canopy_mask as you did in the previous exercise. This may take a couple of seconds.
  • Plot the canopy_mask object.
  • Crop the canopy layer using the parks_sp layer with crop().
  • Plot the cropped layer and see how the edges go right to the axis lines. Toggle between the plots to compare them.