root / org.gvsig.geotools.proj / trunk / org.gvsig.geotools.proj / org.gvsig.geotools.proj.lib.impl / src / main / java / org / gvsig / geotools / proj / lib / DefaultCRS.java @ 858
History | View | Annotate | Download (3.15 KB)
1 | 828 | cmartinez | package org.gvsig.geotools.proj.lib; |
---|---|---|---|
2 | |||
3 | import javax.measure.Unit; |
||
4 | import javax.measure.quantity.Length; |
||
5 | |||
6 | import org.cresques.cts.CRSAdapter; |
||
7 | import org.cresques.cts.IProjection; |
||
8 | import org.gvsig.geotools.proj.catalog.DefaultCRSDefinition; |
||
9 | import org.gvsig.geotools.proj.catalog.utils.DistanceCalculator; |
||
10 | import org.gvsig.proj.catalog.CRSDefinition; |
||
11 | import org.gvsig.proj.catalog.exception.CoordinateReferenceSystemException; |
||
12 | import org.gvsig.proj.catalog.exception.TransformationException; |
||
13 | import org.opengis.referencing.crs.CoordinateReferenceSystem; |
||
14 | import org.opengis.referencing.crs.GeneralDerivedCRS; |
||
15 | import org.opengis.referencing.crs.GeographicCRS; |
||
16 | import org.opengis.referencing.crs.ProjectedCRS; |
||
17 | import org.opengis.referencing.operation.CoordinateOperation; |
||
18 | 831 | cmartinez | import org.opengis.referencing.operation.TransformException; |
19 | 828 | cmartinez | |
20 | public class DefaultCRS implements org.gvsig.proj.CoordinateReferenceSystem { |
||
21 | final private CoordinateReferenceSystem crs; |
||
22 | final private CRSDefinition definition; |
||
23 | |||
24 | public DefaultCRS(DefaultCRSDefinition definition) {
|
||
25 | this.definition = definition;
|
||
26 | this.crs = definition.getInternalCRS();
|
||
27 | } |
||
28 | 831 | cmartinez | |
29 | 828 | cmartinez | DefaultCRS(CoordinateReferenceSystem crs, CRSDefinition definition) { |
30 | this.crs = crs;
|
||
31 | this.definition = definition;
|
||
32 | } |
||
33 | |||
34 | CoordinateReferenceSystem getInternalCRS() { |
||
35 | return crs;
|
||
36 | } |
||
37 | |||
38 | @Override
|
||
39 | public boolean isProjected() { |
||
40 | return (crs instanceof ProjectedCRS |
||
41 | || (crs instanceof GeneralDerivedCRS &&
|
||
42 | ((GeneralDerivedCRS)crs).getBaseCRS() instanceof ProjectedCRS));
|
||
43 | } |
||
44 | |||
45 | |||
46 | @Override
|
||
47 | public boolean isGeographic() { |
||
48 | return (crs instanceof GeographicCRS |
||
49 | || (crs instanceof GeneralDerivedCRS &&
|
||
50 | ((GeneralDerivedCRS)crs).getBaseCRS() instanceof GeographicCRS));
|
||
51 | } |
||
52 | |||
53 | @Override
|
||
54 | 831 | cmartinez | public void toGeo(double[] point) throws TransformationException { |
55 | 828 | cmartinez | if (isProjected()) {
|
56 | CoordinateOperation toGeo = ((GeneralDerivedCRS)crs).getConversionFromBase(); |
||
57 | 831 | cmartinez | try {
|
58 | toGeo.getMathTransform().inverse().transform(point, 0, point, 0, 1); |
||
59 | } catch (TransformException e) {
|
||
60 | throw new TransformationException(e); |
||
61 | } |
||
62 | return;
|
||
63 | 828 | cmartinez | } |
64 | if (isGeographic()) {
|
||
65 | 831 | cmartinez | return;
|
66 | 828 | cmartinez | } |
67 | 831 | cmartinez | throw new TransformationException("The CRS is not projected nor geographic"); |
68 | 828 | cmartinez | } |
69 | |||
70 | @Override
|
||
71 | public double getDistance(double[] point1, double[] point2, Unit<Length> targetUnit) throws CoordinateReferenceSystemException { |
||
72 | return DistanceCalculator.getDistance(this.crs, point1, point2, true, targetUnit); |
||
73 | } |
||
74 | |||
75 | @Override
|
||
76 | public double getDistance(double[] point1, double[] point2, boolean useBaseCRS, Unit<Length> targetUnit) throws CoordinateReferenceSystemException { |
||
77 | return DistanceCalculator.getDistance(this.crs, point1, point2, useBaseCRS, targetUnit); |
||
78 | } |
||
79 | |||
80 | @Override
|
||
81 | public CRSDefinition getDefinition() {
|
||
82 | return definition;
|
||
83 | } |
||
84 | |||
85 | @Override
|
||
86 | public DefaultCRS clone() throws CloneNotSupportedException { |
||
87 | return new DefaultCRS(this.crs, this.definition); |
||
88 | } |
||
89 | |||
90 | @Override
|
||
91 | public IProjection getIProjection() {
|
||
92 | return new CRSAdapter(this); |
||
93 | } |
||
94 | 858 | cmartinez | |
95 | @Override
|
||
96 | public boolean equals(Object obj) { |
||
97 | if (obj instanceof org.gvsig.proj.CoordinateReferenceSystem) { |
||
98 | return getDefinition().equals(((org.gvsig.proj.CoordinateReferenceSystem)obj).getDefinition());
|
||
99 | } |
||
100 | return false; |
||
101 | } |
||
102 | 828 | cmartinez | } |