[geotk] transform for image

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Sun Feb 14 10:18:47 EST 2010


Hello

Thanks a lot for your interest in geotoolkit :)


Le 14/02/10 14:45, Milo van der Linden a écrit :
> Given an image with a given dimension say 500px width and 500px height
> and a realworld boundingbox of 1000,1023,2034,2210 and epsg:9999; can I
> use geotoolkit to construct a transform that I can use to calculate the
> pixel values of a real coordinate?

I suggest that you use java.awt.geom.AffineTransform for that. You can create 
one as below. Note that in this code, I'm assuming the the y axis is flipped 
(i.e. y are increasing downward in pixel coordinates, while they are increasing 
upward in "real world" coordinates). If the y axis is not flipped in your case, 
modify the equation in order to have scaleY computed in the same way than 
scaleX, and use ymin as the translateY term.

int width = 500;
int height = 500;
double xmin = 1000;
double xmax = 2034;
double ymin = 1023;
double ymax = 2210;

double scaleX = (xmax - xmin) / width;
double scaleY = (ymin - ymax) / height; // Negative on intend.
double translateX = xmin;
double translateY = ymax;

AffineTransform gridToCRS = new AffineTransform(scaleX, 0, 0, scaleY, 
translateX, translateY);

The following code will converts a pixel coordinates to "real world" coordinate:

Point2D grid = new Point(x, y);
Point2D world = gridToCRS.transform(grid, null);

In order to convert "real world" coordinates to grid coordinates, use:

AffineTransform crsToGrid = gridToCRS.createInverse();

and use the "crsToGrid" in the same way than above.



Alternatively you can also use the following convenience constructor:

http://www.geotoolkit.org/apidocs/org/geotoolkit/coverage/grid/GridGeometry2D.html#GridGeometry2D%28java.awt.Rectangle,%20java.awt.geom.Rectangle2D%29

followed by a call to getGridToCRS2D(), which you can cast to AffineTransform.

	Regards,

		Martin


More information about the Geotoolkit mailing list