Affine transformations are of type \(f(x) = Ax + b\), where \(x\) is the spatial coordinates (2D in this case), \(A\) is a rotation matrix (it can also include scale/shear parameters, but only rotation is considered here), and \(b\) is the translation (xy shift) parameters.

affineCoords(s, angle = 0, xy_shift = c(0, 0))

Arguments

s

A spatial object where raster::coordinates() can be extracted from. Example classes: Extent, RasterLayer, RasterBrick, RasterStack (from package raster), SpatialPolygon (from package sp).

angle

A numeric value of the angle (in degrees, 0-360) to rotate s. A negative value will change the direction of rotation to clockwise.

xy_shift

A numeric vector of length two with the x and y shift (the translation parameters).

Value

A two-column matrix with the transformed coordinates (xy).

See also

Examples

p <- system.file('exdata', 'soybean.tif', package = 'hyperbrick')
im <- brick(p)
print(im)
#> class      : RasterBrick 
#> dimensions : 264, 364, 96096, 3  (nrow, ncol, ncell, nlayers)
#> resolution : 1, 1  (x, y)
#> extent     : 16, 380, 16, 280  (xmin, xmax, ymin, ymax)
#> crs        : NA 
#> source     : soybean.tif 
#> names      : soybean.1, soybean.2, soybean.3 
#> min values :  45.41772,  12.00000,   0.00000 
#> max values :  236.4372,  239.0000,  218.0000 
#> 

# view band-3
plot(im[[3]], col = gray.colors(20), asp = 0)

# draw a spatial polygon on image
pol <- Polygon(extent(c(40, 85, 50, 150)))
lines(pol)


# rotate and shift the spatial polygon
new_pol <- affineCoords(pol, angle = -3, xy_shift = c(-11, 0))
plot(im[[3]], col = gray.colors(20), asp = 0)
lines(new_pol)


# do some analysis within it, like:
new_pols <- SpatialPolygons(list(Polygons(list(Polygon(new_pol)), "id0")))
plot(mask(im[[3]], new_pols))

mean(extract(im[[3]], new_pols)[[1]])
#> [1] 84.34281