View Javadoc
1   package au.gov.amsa.geo.projection;
2   
3   import java.awt.Point;
4   import java.awt.geom.Point2D;
5   
6   import org.geotools.geometry.jts.JTS;
7   import org.geotools.referencing.CRS;
8   import org.opengis.geometry.MismatchedDimensionException;
9   import org.opengis.referencing.FactoryException;
10  import org.opengis.referencing.operation.MathTransform;
11  import org.opengis.referencing.operation.TransformException;
12  
13  import au.gov.amsa.util.navigation.Position;
14  
15  import com.vividsolutions.jts.geom.Coordinate;
16  import com.vividsolutions.jts.geom.GeometryFactory;
17  
18  /**
19   * Uses geotools libraries to perform transformations between coordinate
20   * reference systems
21   * 
22   * @author dxm
23   * 
24   */
25  public class Projector {
26  
27  	private final ProjectorTarget target;
28  	private final ProjectorBounds bounds;
29  
30  	private final MathTransform transform;
31  	private final GeometryFactory geometryFactory;
32  
33  	public Projector(ProjectorBounds bounds, ProjectorTarget target) {
34  		this.target = target;
35  		this.bounds = bounds;
36  		try {
37  			transform = CRS.findMathTransform(
38  					FeatureUtil.getCrs(FeatureUtil.EPSG_4326),
39  					FeatureUtil.getCrs(bounds.getSrs()));
40  		} catch (FactoryException e) {
41  			throw new RuntimeException(e);
42  		}
43  		geometryFactory = new GeometryFactory();
44  	}
45  
46  	public Point toPoint(double lat, double lon) {
47  		Point2D point2D = toPoint2D(lat, lon);
48  		Point p = new Point();
49  		p.x = (int) Math.round(point2D.getX());
50  		p.y = (int) Math.round(point2D.getY());
51  		return p;
52  	}
53  
54  	public Point2D.Double toPointInSrs(double lat, double lon) {
55  		Coordinate coordinate = new Coordinate(lon, lat);
56  		com.vividsolutions.jts.geom.Point point = geometryFactory
57  				.createPoint(coordinate);
58  		try {
59  			point = (com.vividsolutions.jts.geom.Point) JTS.transform(point,
60  					transform);
61  			return new Point2D.Double(point.getX(), point.getY());
62  		} catch (MismatchedDimensionException e) {
63  			throw new RuntimeException(e);
64  		} catch (TransformException e) {
65  			throw new RuntimeException(e);
66  		}
67  	}
68  
69  	public Point2D.Double toPoint2D(double lat, double lon) {
70  		Coordinate coordinate = new Coordinate(lon, lat);
71  		com.vividsolutions.jts.geom.Point point = geometryFactory
72  				.createPoint(coordinate);
73  		try {
74  			point = (com.vividsolutions.jts.geom.Point) JTS.transform(point,
75  					transform);
76  		} catch (MismatchedDimensionException e) {
77  			throw new RuntimeException(e);
78  		} catch (TransformException e) {
79  			throw new RuntimeException(e);
80  		}
81  
82  		double proportionX = (point.getX() - bounds.getMinX())
83  				/ (bounds.getMaxX() - bounds.getMinX());
84  		double proportionY = (bounds.getMaxY() - point.getY())
85  				/ (bounds.getMaxY() - bounds.getMinY());
86  		Point2D.Double point2D = new Point2D.Double(proportionX
87  				* target.getWidth(), proportionY * target.getHeight());
88  		return point2D;
89  	}
90  
91  	public Position toPosition(double targetX, double targetY) {
92  		double proportionX = targetX / target.getWidth();
93  		double proportionY = targetY / target.getHeight();
94  		double x = proportionX * (bounds.getMaxX() - bounds.getMinX())
95  				+ bounds.getMinX();
96  		double y = bounds.getMaxY() - proportionY
97  				* (bounds.getMaxY() - bounds.getMinY());
98  		Coordinate coordinate = new Coordinate(x, y);
99  		com.vividsolutions.jts.geom.Point point = geometryFactory
100 				.createPoint(coordinate);
101 		try {
102 			point = (com.vividsolutions.jts.geom.Point) JTS.transform(point,
103 					transform.inverse());
104 		} catch (MismatchedDimensionException e) {
105 			throw new RuntimeException(e);
106 		} catch (TransformException e) {
107 			throw new RuntimeException(e);
108 		}
109 		return new Position(point.getY(), point.getX());
110 	}
111 
112 	@Override
113 	public String toString() {
114 		return "ProjectorImpl [target=" + target + ", bounds=" + bounds + "]";
115 	}
116 
117 }