= How to calculate "real" parameters from affine transformation = == Introduction == This page discusses how to compute physically significant parameters from an arbitrary linear transformation. The reverse process, calculating the affine parameters from the physically significant parameters, is articulated on [wiki:DevWikiAffineParameters another wiki page]. The model used to describe how the raster maps onto real world coordinates is illustrated in the following figure: [[Image(model.png)]] This model contains all of the parameters except for translation. Translation may be added in after the coordinates have been scaled, rotated and sheared. The above illustration contains the physically significant parameters which will be calculated by this method. These are: θ,,i,,, θ,,ij,,, and the pixel size along the '''i,,b,,''' and '''j,,b,,''' basis vectors. The inputs to this method are the four non-translation (non-offset) parameters of a linear transform. These are shown below: [[Image(geotransform.png)]] Although these parameters have "common names" within the GIS community, the names are both lengthy and misleading. Within the context of this page, the coefficients o,,11,,, o,,12,,, o,,21,,, and o,,22,, will be used. This page is divided into sections. Each section describes the computation of one physically significant parameter. == Pixel size in the '''i''' direction == To calculate the pixel size in the '''i''' direction, the '''i''' unit vector is projected using the given transform. This gives the basis vector '''i,,b,,''', the magnitude of which is the pixel size. The basis vector is expressed as follows: [[Image(basisvector_i.png)]] The pixel size is then calculated as follows: [[Image(basisvectormag_i.png)]] == Pixel size in the '''j''' direction == The pixel size in the '''j''' direction is computed in a manner similar to the pixel size in the '''i''' direction. The only difference is that the '''j''' unit vector is used in place of the '''i''' unit vector. This gives the basis vector '''j,,b,,''', the magnitude of which is the pixel size. The basis vector is expressed as follows: [[Image(basisvector_j.png)]] The pixel size is then calculated as follows: [[Image(basisvectormag_j.png)]] == Rotation == The grid is rotated by the angle θ,,i,,. This is the angle between the '''x''' axis of the reference frame and the '''i,,b,,''' basis vector. The angle θ,,i,, is considered positive in the clockwise direction, for consistency with compass headings. The calculation of θ,,i,, is a two-step process: first the magnitude is calculated, then the sign is determined. Both steps involve using the [http://en.wikipedia.org/wiki/Dot_product dot product] to determine the angle between θ,,i,, and one of the axes of the target coordinate system (either the '''x''' axis or the '''y''' axis. The equations in this section refer to the angles and vectors defined in the following figure: [[Image(calc_theta_i.png)]] The first step is to calculate the magnitude of the angle between '''i,,b,,''' and the x axis. This is the magnitude of θ,,i,,. [[Image(thetamag_i.png)]] The angle θ,,i,, is defined as the angle ''from'' the '''x''' axis ''to'' '''i,,b,,'''. It is positive in the clockwise direction. In the situation described in the above figure, this means that θ,,i,, is negative if '''i,,b,,''' is above the '''x''' axis, and positive if below. We determine whether '''i,,b,,''' is above or below the '''x''' axis by finding the angle between '''i,,b,,''' and the '''y''' axis. If '''i,,b,,''' and the '''y''' axis are separated by less than 90 degrees, '''i,,b,,''' is above the '''x''' axis, otherwise it is below. [[Image(thetatest_i.png)]] So, if θ,,test,, is less than 90, θ,,i,, = - abs(θ,,i,,). Otherwise, θ,,i,, = abs(θ,,i,,). == Basis vector separation angle == In this section, the method to calculate θ,,ij,, is presented. This is similar to the method for the calculation of θ,,i,,, but it is accomplished with respect to the rotated reference frame of '''i,,b,,''' and '''i,,bp,,''' instead of the x and y axes. The figure which represents this setup is as follows: [[Image(calc_theta_ij.png)]] The first step is to calculate the magnitude of θ,,ij,,, the angle between '''i,,b,,''' and '''j,,b,,'''. [[Image(thetamag_ij.png)]] Next, we need to determine the sign of θ,,ij,, in a manner similar to how the sign for θ,,i,, was determined. The angle θ,,ij,, always represents the angle ''from'' '''i,,b,,''' ''to'' '''j,,b,,''', and is positive counterclockwise for consistency with a right-handed coordinate system. To do this, we first need to calculate '''i,,bp,,''', which is perpendicular to '''i,,b,,''' and forms a [http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Orientation_and_handedness right-hand coordinate system] with '''i,,b,,'''. Observe that '''i,,bp,,''' is '''i,,b,,''' after a 90 degree counterclockwise rotation. [[Image(basisvector_ip.png)]] Now we can determine the size of the angle between '''j,,b,,''' and '''i,,bp,,'''. In this situation, any angle less than 90 degrees means that '''j,,b,,''' is on the same side of '''i,,b,,''' as '''i,,bp,,'''. An angle more than 90 degrees means it lies on the opposite side. [[Image(thetatest_ij.png)]] So, if θ,,test,, is more than 90 degrees, θ,,ij,, = - abs(θ,,ij,,). Otherwise, θ,,ij,, = abs(θ,,ij,,). If θ,,ij,, has any value other than +-90 degrees, the basis vectors are not orthogonal and the transformed pixels are diamond shaped. Probably the most common value for θ,,ij,, is -90 degrees, which indicates that the transform "flips" the '''j''' axis. == Summary == The method presented here performs calculations in the coordinate system used by the geocoordinates. All of the parameters calculated by this method are tied to the same SRID referenced by the transform '''O'''. It is assumed that the x and y axes of the geospatial coordinate system are orthogonal. == See also == * Wikipedia article on the [http://en.wikipedia.org/wiki/Dot_product dot product]. * Wikipedia article on [http://en.wikipedia.org/wiki/Cartesian_coordinate_system coordinate systems]. * Wikipedia article on the [http://en.wikipedia.org/wiki/World_file world file]. * [wiki:DevWikiAffineParameters How to calculate a transform based on physically significant parameters].