AffineTransformation Tutorial — GeoTools 14-SNAPSHOT User Guide

Welcome to this tutorial about affine transformations which are used to convert coordinates from one domain to another. As an example we will convert world coordinates to pixel (screen or image) coordinates. All of the mathematics required will be introduced along the way and you will soon be making use of the power of these kind of calculations. Christian Mueller is a geotools developer and is working for a big customer to build up a GIS infrastructure. In the GeoTools project he is best known for his work on Image Moasic JDBC We have a rectangle in world coordinates, the origin is at the LLC, x = 2000, y = 3000,width = 8000 units an height = 9000 units. Next, we want to map this rectangle to a screen, using pixel coordinates. The available scree size = 400x300 pixels. Unfortunately, pixel coordinates have their origin in the ULC normally, not in the LLC. The next table shows the mappings.: The challenge is to find a method how to transform each point within the world rectangle to a point in the pixel rectangle. (And the other way around) Since most people I know dislike mathematics I will reduce this section to an absolute minimum. An affine transformation is based on a matrix. The numbers are row and column indices, e.g. m12 is the matrix element in row 1, column 2. The values in the bottom row are always as shown. The world rectangle has a width of 8000 units and a height of 9000 units, the pixel dimension has a width of 400 pixels and a height of 300 pixels. We need to scale with 400/8000.0 and 300 / 9000.0. Remember: The world rectangle has its origin in the LLC and the pixel rectangle has its origin in the ULC ! There is a need for a mirroring operation. After the scale operation, we have already pixel values, but we must mirror the y value. The x value should not change. For mirroring, we must calculate: Until now, most of you will say that it is easier to write this calculations without the use of the AffineTransform class, be patient. We have created 3 AffineTransform objects, now we combine them. There is a method Which we will be introducing in this section. The only important thing to know is that you have to START with the LAST AffineTransform object, NOT with the first. If you set our transform object in your Graphics2D object, you can draw and paint with world coordinates. What about calculating world coordinates from pixel coordinates? This is a commonly asked in terms of “what did the user click on?”. It can happen that a matrix is not invertible. This chapter is for the interested reader, if you dislike mathematics, you can skip it. The only import thing you should now is that for this kind of matrices the exception can never occur. A matrix has a determinant. For creating the inverse matrix, divisions by the determinant are needed. As we know from school, it is not allowed to divide by zero. As a consequence, the determinant with value 0 prevents the creation of an inverse matrix. I hope this tutorial helps to demystify affine transforms, once you are used to working with them you will never return to doing coordinate calculations “by hand”. Take a look at the Java API of the java.awt.geom.AffineTransform class to see further possibilities. (rotate, shear,...) Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: April 18, 2016 @ 7:11 pm