|Version 6 (modified by bnordgren, 21 months ago)|
Calculating coefficients for Affine Transformation
Geospatial rasters inherently have two coordinate systems associated with them: pixel indices and real world coordinates. Although some rasters have a very complex relationship between these two coordinate systems, many have a set of simple linear relationships between the two coordinate systems. These simple linear relationships are modular and may be combined in many ways. Regardless of the order in which they are combined, an affine transform results. The transform is then used to convert coordinates between the two coordinate systems of the raster. This page describes a set of ubiquitous individual transformations and demonstrates how they may be combined to produce an affine transformation.
The level of math required to follow along is advanced high school algebra or introductory college algebra. It should be accessible to anyone with a science, math, or engineering background. Lacking this, the nontechnical introduction to matrix multiplication on wikipedia should provide sufficient background.
This page discusses operations in two dimensions only. Each operation is presented as a 2x2 matrix, and each operation performs only one function. These operations were taken from wikipedia. While the matrices presented here contain the bulk of the functionality of a finished affine transform, they are not complete affine transforms themselves. Each transformation will be presented in matrix and equation form: they are equivilent representations.
There are two directions one can rotate in two dimensions: clockwise and counter clockwise. The transformations are different. The counter-clockwise rotation is:
- x' = xcosθ − ysinθ
- y' = xsinθ + ycosθ
To rotate in the clockwise direction:
- x' = xcosθ + ysinθ
- y' = − xsinθ + ycosθ
Scaling is used to set the size of the raster's grid cells in the x and y direction. The transformation is as follows:
- x' = x sx
- y' = y sy
Shearing is visually equivalent to a "slanting" which is parallel to either the x or the y axis. This is a less common operation than rotation and scaling. These are presented as individual operations: one for each axis.
Shearing parallel to the x axis takes the following form.
- x' = x + kxy
- y' = y
While shearing parallel to the y axis has this form:
- x' = x
- y' = kyx + y
Combining individual operations
Whenever more than one of the above operations is required, they may be combined using matrix multiplication. As an example, all of the above matrices will be combined into one. The result of such a combination is still not an affine transform, however. It is just a 2x2 matrix has all the individual functions aggregated into it.
We will be calculating a new matrix, O, which is the aggregate of the following individual operations:
- clockwise rotation around the origin
- shearing parallel to the x axis
- shearing parallel to the y axis
We do this by multiplying the 2x2 matrices of the individual operations together, as follows:
The above matrix equation is shorthand for four equations: one equation each for o11, o12, o21 and o22. We will perform the multiplications on the right hand side one at a time.
- o11 = sx ( (1 + kx ky) cosθ + ky sinθ )
- o12 = sx ( kx cosθ + sinθ )
- o21 = sy ( -(1 + kx ky) sinθ + ky cosθ )
- o22 = sy ( - kx sinθ + cosθ )
Notice that none of the coefficients in the O matrix may be said to represent pure scaling, rotation or shearing. Rather, they all have components of each of these operations factored in. If a particular transformation is not needed (say there is no shearing in either the x or y directions), then the relevant parameters may be set to zero (kx = ky = 0).
Also notice that it is not necessary to compute this matrix every time one wants to convert between pixel indices and geographic location. The coefficients are computed once for the entire raster, and may be reused for every pixel calculation. You would use this aggregate matrix O exactly as you would use any of the individual matrices:
- x' = o11 x + o12 y
- y' = o21 x + o22 y
Constructing the affine transformation
The 2x2 matrix O is the upper-left hand corner of the affine transformation, which is the 3x3 matrix, A. The right hand column contains the offsets, or translation, of the raster in the x and y directions. The bottom row is always filled with the numbers 0, 0, 1. The result of this is that there are six parameters to an affine transformation which can actually change, as shown:
- a11 = o11 = sx ( (1 + kx ky) cosθ + ky sinθ )
- a12 = o12 = sx ( kx cosθ + sinθ )
- a13 = tx
- a21 = o21 = sy ( -(1 + kx ky) sinθ + ky cosθ )
- a22 = o22 = sy ( - kx sinθ + cosθ )
- a23 = ty
- sx : scale factor in x direction
- sy : scale factor in y direction
- tx : offset in x direction
- ty : offset in y direction
- θ : angle of rotation clockwise around origin
- kx : shearing parallel to x axis
- ky : shearing parallel to y axis
It is these six parameters (a11...a23)which are typically stored within a geospatial image file format to record the conversion from pixel index to geolocation. The actual conversion is described as follows:
- E = a11 i + a12 j + a13
- N = a21 i + a22 j + a23
where E is easting, N is northing, i is pixel column and j is pixel row. The last row of the matrix equation is always ignored, as it boils down to 1=1. It is there to make a square matrix used to calculate the inverse operation.
Link to postgis raster
The six parameters of the affine transform are given the following names in postgis raster:
- ScaleX = a11
- SkewX = a12
- OffsetX = a13 = tx
- SkewY = a21
- ScaleY = a22
- OffsetY = a23 = ty
With the exception of OffsetX and OffsetY, the names are somewhat arbitrary for the general case.
Throughout this page, the following symbols have been
- CCW_rotation.png (1.2 KB) - added by bnordgren 21 months ago.
- CW_rotation.png (1.2 KB) - added by bnordgren 21 months ago.
- scaling.png (0.9 KB) - added by bnordgren 21 months ago.
- shear_x.png (0.8 KB) - added by bnordgren 21 months ago.
- shear_y.png (0.8 KB) - added by bnordgren 21 months ago.
- aggregate_usage.png (1.3 KB) - added by bnordgren 21 months ago.
- affinematrix.png (2.2 KB) - added by bnordgren 21 months ago.
- affineusage.png (2.1 KB) - added by bnordgren 21 months ago.
- aggregate_step1.png (3.2 KB) - added by bnordgren 19 months ago.
- aggregate_step2.png (3.0 KB) - added by bnordgren 19 months ago.
- aggregate_step3.png (2.6 KB) - added by bnordgren 19 months ago.
- aggregate_step4.odf (4.8 KB) - added by bnordgren 19 months ago.
- aggregate_step4.png (3.2 KB) - added by bnordgren 19 months ago.
- reflect_x.png (1.0 KB) - added by bnordgren 19 months ago.
- construction-step5.png (6.8 KB) - added by bnordgren 19 months ago.
- basisvector_i.png (2.4 KB) - added by bnordgren 19 months ago.
- basisvector_j.png (2.7 KB) - added by bnordgren 19 months ago.
- basisvectormag_i.png (1.0 KB) - added by bnordgren 19 months ago.
- basisvectormag_j.png (3.8 KB) - added by bnordgren 19 months ago.
- shearing_params.png (11.4 KB) - added by bnordgren 19 months ago.
- scalefactor_j.png (3.9 KB) - added by bnordgren 19 months ago.