Statistics
| Revision:

root / org.gvsig.geotools.proj / trunk / org.gvsig.geotools.proj / org.gvsig.geotools.proj.catalog.impl / src / main / java / org / gvsig / geotools / proj / catalog / DefaultTransformationDefinition.java @ 828

History | View | Annotate | Download (6.79 KB)

1
package org.gvsig.geotools.proj.catalog;
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.proj.CoordinateReferenceSystem;
9
import org.gvsig.proj.CoordinateTransformation;
10
import org.gvsig.proj.catalog.CRSDefinition;
11
import org.gvsig.proj.catalog.TextSerialization.Format;
12
import org.gvsig.proj.catalog.TextSerialization.WKTConvention;
13
import org.gvsig.proj.catalog.TransformationDefinition;
14
import org.gvsig.proj.catalog.exception.NoninvertibleTransformException;
15
import org.gvsig.proj.catalog.exception.TransformationException;
16
import org.gvsig.proj.catalog.ref.Extent;
17
import org.opengis.referencing.ReferenceIdentifier;
18
import org.opengis.referencing.operation.CoordinateOperation;
19
import org.opengis.referencing.operation.MathTransform;
20
import org.opengis.referencing.operation.TransformException;
21
import org.opengis.referencing.operation.Transformation;
22

    
23
public class DefaultTransformationDefinition implements TransformationDefinition,
24
                CoordinateTransformation {
25
        final private CoordinateOperation operation;
26
        private String wkt = null;
27
        private MathTransform mathTransform = null;
28
        private DefaultCRSDefinition source = null;
29
        private DefaultCRSDefinition target = null;
30
        // FIXME: do we need the inverse concept in the definition??
31
        boolean inverse = false;
32
        
33
        public DefaultTransformationDefinition(CoordinateOperation operation) {
34
                this.operation = operation;
35
        }
36
        
37
        public DefaultTransformationDefinition(CoordinateOperation operation, boolean inverse) throws NoninvertibleTransformException {
38
                this.operation = operation;
39
                try {
40
                        operation.getMathTransform().inverse();
41
                }
42
                catch (org.opengis.referencing.operation.NoninvertibleTransformException e) {
43
                        throw new NoninvertibleTransformException(operation.getName().getCode());
44
                }
45
                this.inverse = inverse;
46
        }
47
        
48
        
49
        public DefaultTransformationDefinition(String wkt) throws ParseException {
50
                this.wkt = wkt;
51
                Parser parser = new Parser();
52
                Object obj = parser.parseObject(wkt);
53
                this.operation = (CoordinateOperation) obj;
54
        }
55

    
56
        public CoordinateOperation getInternalOperation() {
57
                return operation;
58
        }
59
        
60
        @Override
61
        public CRSDefinition getSourceDefinition() {
62
                // FIXME: concurrency
63
                if (source==null) {
64
                        if (inverse) {
65
                                source = new DefaultCRSDefinition(operation.getTargetCRS());
66
                        }
67
                        else {
68
                                source = new DefaultCRSDefinition(operation.getSourceCRS());                                
69
                        }
70
                }
71
                return source;
72
        }
73

    
74
        @Override
75
        public CRSDefinition getTargetDefinition() {
76
                // FIXME: concurrency
77
                if (target==null) {
78
                        if (inverse) {
79
                                target = new DefaultCRSDefinition(operation.getSourceCRS());        
80
                        }
81
                        else {
82
                                target = new DefaultCRSDefinition(operation.getTargetCRS());
83
                        }
84
                }
85
                return target;
86
        }
87

    
88
        @Override
89
        public String getName() {
90
                // FIXME: compare with identifier
91
                return operation.getName().toString();
92
        }
93

    
94
        @Override
95
        public String getIdentifier() {
96
                // FIXME: compare with name
97
                Set<ReferenceIdentifier> ids = operation.getIdentifiers();
98
                for (ReferenceIdentifier id: ids) {
99
                        String fullCode = id.toString();
100
                        String code = id.getCode();
101
                }
102
                return operation.getName().toString();
103
        }
104

    
105
        @Override
106
        public Set<String> getIdentifiers() {
107
                HashSet<String> ids = new HashSet<String>();
108
                for (ReferenceIdentifier id: operation.getIdentifiers()) {
109
                        ids.add(id.toString());
110
                }
111
                return ids;
112
        }
113

    
114
        @Override
115
        public String getAuthorityName() {
116
                return operation.getName().getAuthority().getTitle().toString();
117
        }
118

    
119
        @Override
120
        public String getOperationVersion() {
121
                return operation.getOperationVersion();
122
        }
123

    
124
        @Override
125
        public Extent getDomainOfValidity() {
126
                // TODO Auto-generated method stub
127
                return null;
128
        }
129

    
130
        @Override
131
        public String getDescription() {
132
                return operation.getScope() + "\nRemarks: " + operation.getRemarks();
133
        }
134

    
135
        @Override
136
        public boolean isTransformation() {
137
                return (operation instanceof Transformation);
138
        }
139

    
140
        @Override
141
        public TransformationDefinition getInverseDefinition() throws NoninvertibleTransformException {
142
                return new DefaultTransformationDefinition(this.operation, true);
143
        }
144

    
145
        @Override
146
        public String toWKT() throws UnsupportedOperationException {
147
                // TODO Auto-generated method stub
148
                // FIXME: review inverse transformation to be correctly stored as wkt
149
                // FIXME: think if we should convert to WKT the operation or the mathTransform
150
                // MathTransforms seem to be the only option that ensures the transformation can be recreated
151
                // But... source and target crs will be lost!!
152
                if (wkt!=null) {
153
                        return wkt;
154
                }
155
                return operation.toWKT();
156
        }
157
        
158

    
159
        @Override
160
        public String toString(Format format, WKTConvention convention) throws UnsupportedOperationException {
161
                // TODO Auto-generated method stub
162
                return null;
163
        }
164

    
165
        @Override
166
        public String toString(Format format, WKTConvention convention, int indentation)
167
                        throws UnsupportedOperationException {
168
                // TODO Auto-generated method stub
169
                return null;
170
        }
171
        
172
        public boolean isInverse() {
173
                return inverse;
174
        }
175

    
176
        @Override
177
        public CoordinateReferenceSystem getSourceCRS() {
178
                return (CoordinateReferenceSystem) getSourceDefinition();
179
        }
180

    
181
        @Override
182
        public CoordinateReferenceSystem getTargetCRS() {
183
                return (CoordinateReferenceSystem) getTargetDefinition();
184
        }
185
        
186
        @Override
187
        public void apply(double[] point) throws TransformationException {
188
                try {
189
                        getMathTransform().transform(point, 0, point, 0, 1);
190
                } catch (TransformException e) {
191
                        throw new TransformationException(getIdentifier(), e);
192

    
193
                }
194
        }
195

    
196
        @Override
197
        public void apply(double[] srcPoint, double[] dstPoint) throws TransformationException {
198
                try {
199
                        getMathTransform().transform(srcPoint, 0, dstPoint, 0, 1);
200
                } catch (TransformException e) {
201
                        throw new TransformationException(getIdentifier(), e);
202

    
203
                }
204
        }
205
        
206

    
207
        @Override
208
        public void apply(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts) throws TransformationException {
209
                try {
210
                        getMathTransform().transform(srcPts, srcOff, dstPts, dstOff, numPts);
211
                } catch (TransformException e) {
212
                        throw new TransformationException(getIdentifier(), e);
213
                }
214
        }
215
        
216
        protected MathTransform getMathTransform() throws TransformationException {
217
                try {
218
                        if (mathTransform==null) {
219
                                if (inverse) {
220
                                        mathTransform = operation.getMathTransform().inverse();
221
                                }
222
                                else {
223
                                        mathTransform = operation.getMathTransform();
224
                                }
225
                        }
226

    
227
                } catch (TransformException e) {
228
                        throw new TransformationException(getIdentifier(), e);
229
                }
230
                return mathTransform;
231
        }
232

    
233
        @Override
234
        public CoordinateTransformation getInverse() throws NoninvertibleTransformException {
235
                if (inverse) {
236
                        return new DefaultTransformationDefinition(this.operation);        
237
                }
238
                return new DefaultTransformationDefinition(this.operation, true);
239
        }
240

    
241
        @Override
242
        public TransformationDefinition getDefinition() {
243
                return this;
244
        }
245

    
246
        @Override
247
        public boolean isIdentity() throws TransformationException {
248
                return getMathTransform().isIdentity();
249
        }
250

    
251
}