1 package au.gov.amsa.geo.projection;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import org.apache.commons.io.IOUtils;
8 import org.geotools.geometry.jts.JTS;
9 import org.geotools.referencing.CRS;
10 import org.opengis.geometry.MismatchedDimensionException;
11 import org.opengis.referencing.FactoryException;
12 import org.opengis.referencing.NoSuchAuthorityCodeException;
13 import org.opengis.referencing.crs.CoordinateReferenceSystem;
14 import org.opengis.referencing.operation.MathTransform;
15 import org.opengis.referencing.operation.TransformException;
16
17 import au.gov.amsa.geo.model.Position;
18
19 import com.vividsolutions.jts.geom.Coordinate;
20 import com.vividsolutions.jts.geom.GeometryFactory;
21 import com.vividsolutions.jts.geom.Point;
22
23 public class FeatureUtil {
24 public static final String EPSG_4326 = "EPSG:4326";
25 public static final String EPSG_900913 = "EPSG:900913";
26 public static final String EPSG_3857 = "EPSG:3857";
27
28
29
30 public static final String EPSG_102100 = "EPSG:102100";
31
32 private static Map<String, CoordinateReferenceSystem> crs = new HashMap<String, CoordinateReferenceSystem>();
33
34 public static synchronized CoordinateReferenceSystem getCrs(String epsg) {
35 try {
36 if (crs.get(epsg) != null)
37 return crs.get(epsg);
38
39 if (epsg.equals(EPSG_900913)) {
40 String wkt = IOUtils.toString(FeatureUtil.class
41 .getResourceAsStream("/epsg/EPSG_900913.txt"));
42 crs.put(epsg, CRS.parseWKT(wkt));
43 } else if (epsg.equals(EPSG_102100)) {
44 String wkt = IOUtils.toString(FeatureUtil.class
45 .getResourceAsStream("/epsg/EPSG_102100.txt"));
46 crs.put(epsg, CRS.parseWKT(wkt));
47 } else if (epsg.equals(EPSG_3857)) {
48 String wkt = IOUtils.toString(FeatureUtil.class
49 .getResourceAsStream("/epsg/EPSG_3857.txt"));
50 crs.put(epsg, CRS.parseWKT(wkt));
51 } else
52 crs.put(epsg, CRS.decode(epsg, true));
53 return crs.get(epsg);
54 } catch (FactoryException e) {
55 throw new RuntimeException("could not load " + epsg, e);
56 } catch (IOException e) {
57 throw new RuntimeException(e);
58 }
59 }
60
61 public static Point createPoint(double lat, double lon, String srsName) {
62 GeometryFactory geometryFactory = new GeometryFactory();
63 Coordinate coordinate = new Coordinate(lon, lat);
64 Point point = geometryFactory.createPoint(coordinate);
65
66 try {
67 if (!srsName.equals(EPSG_4326)) {
68 MathTransform transform = CRS.findMathTransform(getCrs(EPSG_4326), getCrs(srsName));
69 point = (Point) JTS.transform(point, transform);
70 }
71 return point;
72
73 } catch (NoSuchAuthorityCodeException e) {
74 throw new RuntimeException(e);
75 } catch (FactoryException e) {
76 throw new RuntimeException(e);
77 } catch (MismatchedDimensionException e) {
78 throw new RuntimeException(e);
79 } catch (TransformException e) {
80 throw new RuntimeException(e);
81 }
82 }
83
84 public static Position convertToLatLon(double x, double y, String srsName) {
85 GeometryFactory geometryFactory = new GeometryFactory();
86 Coordinate coordinate = new Coordinate(x, y);
87 Point point = geometryFactory.createPoint(coordinate);
88
89 try {
90 if (!srsName.equals(EPSG_4326)) {
91 MathTransform transform = CRS.findMathTransform(getCrs(EPSG_4326), getCrs(srsName));
92 point = (Point) JTS.transform(point, transform.inverse());
93 }
94 return new Position(point.getY(), point.getX());
95 } catch (NoSuchAuthorityCodeException e) {
96 throw new RuntimeException(e);
97 } catch (FactoryException e) {
98 throw new RuntimeException(e);
99 } catch (MismatchedDimensionException e) {
100 throw new RuntimeException(e);
101 } catch (TransformException e) {
102 throw new RuntimeException(e);
103 }
104 }
105
106 }