root / org.gvsig.geotools.proj / trunk / org.gvsig.geotools.proj / org.gvsig.geotools.proj.lib.impl / src / main / java / org / gvsig / geotools / proj / lib / DefaultTransformation.java @ 828
History | View | Annotate | Download (4.83 KB)
1 |
package org.gvsig.geotools.proj.lib; |
---|---|
2 |
|
3 |
import java.text.ParseException; |
4 |
import java.util.HashSet; |
5 |
import java.util.Set; |
6 |
|
7 |
import org.geotools.referencing.wkt.Parser; |
8 |
import org.gvsig.geotools.proj.catalog.DefaultCRSDefinition; |
9 |
import org.gvsig.geotools.proj.catalog.DefaultTransformationDefinition; |
10 |
import org.gvsig.proj.CoordinateReferenceSystem; |
11 |
import org.gvsig.proj.CoordinateReferenceSystemLocator; |
12 |
import org.gvsig.proj.CoordinateTransformation; |
13 |
import org.gvsig.proj.catalog.CRSDefinition; |
14 |
import org.gvsig.proj.catalog.TextSerialization.Format; |
15 |
import org.gvsig.proj.catalog.TextSerialization.WKTConvention; |
16 |
import org.gvsig.proj.catalog.TransformationDefinition; |
17 |
import org.gvsig.proj.catalog.exception.NoninvertibleTransformException; |
18 |
import org.gvsig.proj.catalog.exception.TransformationException; |
19 |
import org.gvsig.proj.catalog.exception.UnsupportedCoordinateReferenceSystemException; |
20 |
import org.gvsig.proj.catalog.ref.Extent; |
21 |
import org.opengis.referencing.ReferenceIdentifier; |
22 |
import org.opengis.referencing.operation.CoordinateOperation; |
23 |
import org.opengis.referencing.operation.MathTransform; |
24 |
import org.opengis.referencing.operation.TransformException; |
25 |
import org.opengis.referencing.operation.Transformation; |
26 |
|
27 |
public class DefaultTransformation implements |
28 |
CoordinateTransformation { |
29 |
final protected CoordinateOperation operation; |
30 |
protected MathTransform mathTransform = null; |
31 |
protected DefaultCRS source = null; |
32 |
protected DefaultCRS target = null; |
33 |
final protected boolean inverse; |
34 |
final protected TransformationDefinition definition; |
35 |
|
36 |
public DefaultTransformation(CoordinateOperation operation, TransformationDefinition definition) {
|
37 |
this.operation = operation;
|
38 |
this.definition = definition;
|
39 |
this.inverse = false; |
40 |
} |
41 |
|
42 |
public DefaultTransformation(DefaultTransformationDefinition definition)
|
43 |
throws UnsupportedOperationException, ParseException { |
44 |
this.definition = definition;
|
45 |
this.operation = definition.getInternalOperation();
|
46 |
this.inverse = false; |
47 |
} |
48 |
|
49 |
public DefaultTransformation(CoordinateOperation operation, TransformationDefinition definition, boolean inverse) { |
50 |
this.operation = operation;
|
51 |
this.definition = definition;
|
52 |
this.inverse = inverse;
|
53 |
} |
54 |
|
55 |
public boolean isInverse() { |
56 |
return inverse;
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
public CoordinateReferenceSystem getSourceCRS() {
|
61 |
// FIXME: concurrency
|
62 |
if (source==null) { |
63 |
if (inverse) {
|
64 |
source = new DefaultCRS(operation.getTargetCRS(), getDefinition().getSourceDefinition());
|
65 |
} |
66 |
else {
|
67 |
source = new DefaultCRS(operation.getSourceCRS(), getDefinition().getSourceDefinition());
|
68 |
} |
69 |
} |
70 |
return source;
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public CoordinateReferenceSystem getTargetCRS() {
|
75 |
// FIXME: concurrency
|
76 |
if (target==null) { |
77 |
if (inverse) {
|
78 |
target = new DefaultCRS(operation.getSourceCRS(), getDefinition().getTargetDefinition());
|
79 |
} |
80 |
else {
|
81 |
target = new DefaultCRS(operation.getTargetCRS(), getDefinition().getTargetDefinition());
|
82 |
} |
83 |
} |
84 |
return target;
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void apply(double[] point) throws TransformationException { |
89 |
try {
|
90 |
getMathTransform().transform(point, 0, point, 0, 1); |
91 |
} catch (TransformException e) {
|
92 |
throw new TransformationException(getDefinition().getIdentifier(), e); |
93 |
|
94 |
} |
95 |
} |
96 |
|
97 |
@Override
|
98 |
public void apply(double[] srcPoint, double[] dstPoint) throws TransformationException { |
99 |
try {
|
100 |
getMathTransform().transform(srcPoint, 0, dstPoint, 0, 1); |
101 |
} catch (TransformException e) {
|
102 |
throw new TransformationException(getDefinition().getIdentifier(), e); |
103 |
|
104 |
} |
105 |
} |
106 |
|
107 |
|
108 |
@Override
|
109 |
public void apply(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts) throws TransformationException { |
110 |
try {
|
111 |
getMathTransform().transform(srcPts, srcOff, dstPts, dstOff, numPts); |
112 |
} catch (TransformException e) {
|
113 |
throw new TransformationException(getDefinition().getIdentifier(), e); |
114 |
} |
115 |
} |
116 |
|
117 |
protected MathTransform getMathTransform() throws TransformationException { |
118 |
try {
|
119 |
if (mathTransform==null) { |
120 |
if (inverse) {
|
121 |
mathTransform = operation.getMathTransform().inverse(); |
122 |
} |
123 |
else {
|
124 |
mathTransform = operation.getMathTransform(); |
125 |
} |
126 |
} |
127 |
|
128 |
} catch (TransformException e) {
|
129 |
throw new TransformationException(getDefinition().getIdentifier(), e); |
130 |
} |
131 |
return mathTransform;
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public CoordinateTransformation getInverse() throws NoninvertibleTransformException { |
136 |
if (inverse) {
|
137 |
return new DefaultTransformation(this.operation, this.definition, false); |
138 |
} |
139 |
return new DefaultTransformation(this.operation, this.definition, true); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public TransformationDefinition getDefinition() {
|
144 |
return definition;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public boolean isIdentity() throws TransformationException { |
149 |
return getMathTransform().isIdentity();
|
150 |
} |
151 |
|
152 |
@Override
|
153 |
protected DefaultTransformation clone() throws CloneNotSupportedException { |
154 |
return new DefaultTransformation(this.operation, this.definition, this.inverse); |
155 |
} |
156 |
|
157 |
} |