001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.support;
016:
017: import java.awt.geom.AffineTransform;
018:
019: import net.refractions.udig.tools.edit.EditPlugin;
020:
021: import org.opengis.referencing.operation.MathTransform;
022:
023: import com.vividsolutions.jts.geom.Coordinate;
024:
025: /**
026: * Transforms between points and coordinates
027: *
028: * @author Jesse
029: * @since 1.1.0
030: */
031: public class PointCoordCalculator {
032: final AffineTransform toScreen;
033: final AffineTransform toWorld;
034: MathTransform layerToMap;
035: MathTransform mapToLayer;
036:
037: public PointCoordCalculator(AffineTransform toScreen,
038: MathTransform layerToMap) {
039: this .toScreen = new AffineTransform(toScreen);
040: this .layerToMap = layerToMap;
041: AffineTransform temp;
042: try {
043: temp = this .toScreen.createInverse();
044: } catch (Exception e) {
045: temp = toScreen;
046: }
047: toWorld = temp;
048: try {
049: this .mapToLayer = this .layerToMap.inverse();
050: } catch (Exception e) {
051: throw (RuntimeException) new RuntimeException(e);
052: }
053: }
054:
055: public PointCoordCalculator(PointCoordCalculator other) {
056: toScreen = new AffineTransform(other.toScreen);
057: toWorld = new AffineTransform(other.toWorld);
058: layerToMap = other.layerToMap;
059: mapToLayer = other.mapToLayer;
060: }
061:
062: public synchronized Coordinate toCoord(Point point) {
063: double x;
064: double y;
065: x = point.getX() + .5;
066: y = point.getY() + .5;
067:
068: double[] src = new double[] { x, y };
069: double[] dest = new double[2];
070:
071: toWorld.transform(src, 0, src, 0, 1);
072:
073: if (mapToLayer.isIdentity()) {
074: dest = src;
075: } else {
076: try {
077: mapToLayer.transform(src, 0, dest, 0, 1);
078: } catch (Exception e) {
079: EditPlugin.log("", e); //$NON-NLS-1$
080: }
081: }
082: return new Coordinate(dest[0], dest[1]);
083: }
084:
085: /**
086: * Transforms a Coordinate into the point location it would occupy on the screen.
087: *
088: * @param coord coordinate object
089: * @return point coordinate would occupy on the screen.
090: */
091: public synchronized Point toPoint(Coordinate coord) {
092: double[] src = new double[] { coord.x, coord.y };
093: double[] dest = new double[2];
094: if (layerToMap.isIdentity()) {
095: dest = src;
096: } else {
097: try {
098: layerToMap.transform(src, 0, dest, 0, 1);
099: } catch (Exception e) {
100: EditPlugin.log("", e); //$NON-NLS-1$
101: }
102: }
103: toScreen.transform(dest, 0, dest, 0, 1);
104: return Point.valueOf((int) dest[0], (int) dest[1]);
105: }
106:
107: public void setMapToLayer(MathTransform mapToLayer2) {
108: this .mapToLayer = mapToLayer2;
109: try {
110: this .layerToMap = this .mapToLayer.inverse();
111: } catch (Exception e) {
112: throw (RuntimeException) new RuntimeException(e);
113: }
114: }
115:
116: }
|