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 @ 867
History | View | Annotate | Download (3.3 KB)
1 |
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 |
import org.opengis.referencing.operation.TransformException; |
19 |
|
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 |
|
29 |
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 |
public void toGeo(double[] point) throws TransformationException { |
55 |
if (isProjected()) {
|
56 |
CoordinateOperation toGeo = ((GeneralDerivedCRS)crs).getConversionFromBase(); |
57 |
try {
|
58 |
toGeo.getMathTransform().inverse().transform(point, 0, point, 0, 1); |
59 |
} catch (TransformException e) {
|
60 |
throw new TransformationException(e); |
61 |
} |
62 |
return;
|
63 |
} |
64 |
if (isGeographic()) {
|
65 |
return;
|
66 |
} |
67 |
throw new TransformationException("The CRS is not projected nor geographic"); |
68 |
} |
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 |
|
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 |
|
103 |
@Override
|
104 |
public int hashCode() { |
105 |
return getDefinition().hashCode();
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public String toString() { |
110 |
return getDefinition().toString();
|
111 |
} |
112 |
} |