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 @ 853

History | View | Annotate | Download (8.16 KB)

1
package org.gvsig.geotools.proj.catalog;
2

    
3
import java.util.HashSet;
4
import java.util.List;
5
import java.util.Set;
6

    
7
import org.geotools.metadata.iso.citation.Citations;
8
import org.geotools.referencing.operation.transform.ConcatenatedTransform;
9
import org.geotools.referencing.wkt.Formatter;
10
import org.geotools.referencing.wkt.Symbols;
11
import org.gvsig.geotools.proj.catalog.utils.IdentifiedObjectUtils;
12
import org.gvsig.proj.catalog.CRSDefinition;
13
import org.gvsig.proj.catalog.TextSerialization.Format;
14
import org.gvsig.proj.catalog.TextSerialization.WKTConvention;
15
import org.gvsig.proj.catalog.TransformationDefinition;
16
import org.gvsig.proj.catalog.exception.CoordinateReferenceSystemException;
17
import org.gvsig.proj.catalog.exception.NoninvertibleTransformException;
18
import org.gvsig.proj.catalog.exception.TransformationException;
19
import org.gvsig.proj.catalog.exception.UnsupportedFormatException;
20
import org.gvsig.proj.catalog.exception.UnsupportedTransformationException;
21
import org.gvsig.proj.catalog.extent.Extent;
22
import org.opengis.referencing.ReferenceIdentifier;
23
import org.opengis.referencing.operation.ConcatenatedOperation;
24
import org.opengis.referencing.operation.CoordinateOperation;
25
import org.opengis.referencing.operation.MathTransform;
26
import org.opengis.referencing.operation.PassThroughOperation;
27
import org.opengis.referencing.operation.SingleOperation;
28
import org.opengis.referencing.operation.Transformation;
29

    
30
public class DefaultTransformationDefinition implements TransformationDefinition {
31
        final private CoordinateOperation operation;
32
        final private MathTransform mathTransform;
33
        final private DefaultCRSDefinition source;
34
        final private DefaultCRSDefinition target;
35
        // FIXME: do we need the inverse concept in the definition??
36
        final private boolean inverse;
37
        
38
        /**
39
         * Creates a DefaultTransformationDefinition object
40
         * 
41
         * @param operation
42
         * @param source
43
         * @param target 
44
         * @throws UnsupportedTransformationException 
45
         */
46
        public DefaultTransformationDefinition(CoordinateOperation operation,
47
                        DefaultCRSDefinition source, DefaultCRSDefinition target)
48
                                        throws UnsupportedTransformationException {
49
                if (source.getInternalCRS().equals(operation.getSourceCRS())
50
                                && target.getInternalCRS().equals(operation.getTargetCRS())) {
51
                        this.inverse = false;
52
                }
53
                else if (source.getInternalCRS().equals(operation.getTargetCRS())
54
                                && target.getInternalCRS().equals(operation.getSourceCRS())) {
55
                        this.inverse = true;
56
                }
57
                else {
58
                        throw new UnsupportedTransformationException(
59
                                        "The provided source and target do not match any of the sources and targets of the provided operation");
60
                }
61
                if (inverse) {
62
                        try {
63
                                this.mathTransform = operation.getMathTransform().inverse();
64
                        } catch (org.opengis.referencing.operation.NoninvertibleTransformException e) {
65
                                throw new UnsupportedTransformationException(e);
66
                        }
67
                }
68
                else {
69
                        this.mathTransform = operation.getMathTransform();
70
                }
71
                this.operation = operation;
72
                this.source = source;
73
                this.target = target;
74
        }
75
        
76
        /**
77
         * Creates a DefaultTransformationDefinition object
78
         * 
79
         * @param mathTransform
80
         * @param source
81
         * @param target
82
         * @throws UnsupportedTransformationException 
83
         */
84
        public DefaultTransformationDefinition(MathTransform mathTransform,
85
                        DefaultCRSDefinition source, DefaultCRSDefinition target) throws CoordinateReferenceSystemException {
86
                this.mathTransform = mathTransform;
87
                this.inverse = false;
88
                this.operation = null;
89
                this.source = source;
90
                this.target = target;
91
        }
92

    
93
        public CoordinateOperation getInternalOperation() {
94
                return operation;
95
        }
96

    
97
        @Override
98
        public DefaultCRSDefinition getSourceDefinition() {
99
                return source;
100
        }
101

    
102
        @Override
103
        public DefaultCRSDefinition getTargetDefinition() {
104
                return target;
105
        }
106

    
107
        @Override
108
        public String getName() {
109
                // FIXME: define in API whether null or blank ""  is allowed
110
                if (operation != null) {
111
                        return nameFromOperation(operation);
112
                }
113
                return nameFromMathTransform();
114
        }
115
        
116
        protected String nameFromMathTransform() {
117
                StringBuilder meaningfulName = new StringBuilder();
118
                appendSourceTargetToName(meaningfulName);
119
                meaningfulName.append(" using ");
120
                meaningfulName.append(mathTransform.getClass().getSimpleName());
121
                return meaningfulName.toString();
122
        }
123
        
124
        protected void appendSourceTargetToName(StringBuilder builder) {
125
                builder.append(source.getName());
126
                builder.append(" to ");
127
                builder.append(target.getName());
128
        }
129
        
130
        protected String nameFromOperation(CoordinateOperation op) {
131
                if (op instanceof ConcatenatedOperation) {
132
                        StringBuilder meaningfulName = new StringBuilder();
133
                        List<SingleOperation> opList = ((ConcatenatedOperation) op).getOperations();
134
                        for (SingleOperation singleOp: opList) {
135
                                if (singleOp instanceof Transformation) {
136
                                        meaningfulName.append("Concat operation using ");
137
                                        if (singleOp.getName() != null) {
138
                                                meaningfulName.append(singleOp.getName());
139
                                        }
140
                                        String id = IdentifiedObjectUtils.getIdentifier(singleOp);
141
                                        if (id!=null) {
142
                                                meaningfulName.append("[").append(id).append("]");
143
                                        }
144
                                }
145
                        }
146
                        if (op.getName() != null) {
147
                                if (meaningfulName.length()>0) {
148
                                        meaningfulName.append(" transforming ");
149
                                }
150
                                meaningfulName.append(op.getName().toString().replace("\u21e8", "=>"));        
151
                        }
152
                        if (meaningfulName.length()>0) {
153
                                return meaningfulName.toString();
154
                        }
155
                }
156
                else if (op instanceof PassThroughOperation) {
157
                        return nameFromOperation(((PassThroughOperation)op).getOperation());
158
                }
159
                if (op.getName() !=null) {
160
                        return op.getName().toString().replace("\u21e8", "=>");
161
                }
162
                return nameFromMathTransform();
163
        }
164

    
165
        @Override
166
        public String getIdentifier() {
167
                if (operation==null) {
168
                        return null;
169
                }
170
                return IdentifiedObjectUtils.getIdentifier(operation);
171
        }
172

    
173
        @Override
174
        public Set<String> getIdentifiers() {
175
                HashSet<String> ids = new HashSet<String>();
176
                for (ReferenceIdentifier id: operation.getIdentifiers()) {
177
                        ids.add(id.toString());
178
                }
179
                return ids;
180
        }
181

    
182
        @Override
183
        public String getAuthorityName() {
184
                if (operation==null ||
185
                                operation.getName()==null ||
186
                                                operation.getName().getAuthority()==null) {
187
                        return null;
188
                }
189
                return IdentifiedObjectUtils.getIdentifier(operation.getName().getAuthority().getIdentifiers());
190
        }
191

    
192
        @Override
193
        public String getOperationVersion() {
194
                if (operation==null) {
195
                        return null;
196
                }
197
                return operation.getOperationVersion();
198
        }
199

    
200
        @Override
201
        public Extent getDomainOfValidity() {
202
                // TODO Auto-generated method stub
203
                return null;
204
        }
205

    
206
        @Override
207
        public String getDescription() {
208
                if (operation==null) {
209
                        return null;
210
                }
211
                return operation.getScope() + "\nRemarks: " + operation.getRemarks();
212
        }
213

    
214
        @Override
215
        public TransformationDefinition getInverseDefinition()
216
                        throws NoninvertibleTransformException, UnsupportedTransformationException {
217
                if (this.operation==null) {
218
                        try {
219
                                return new DefaultTransformationDefinition(mathTransform.inverse(), target, source);
220
                        } catch (CoordinateReferenceSystemException
221
                                        | org.opengis.referencing.operation.NoninvertibleTransformException e) {
222
                                throw new NoninvertibleTransformException(e);
223
                        }        
224
                }
225
                return new DefaultTransformationDefinition(this.operation, target, source);
226
        }
227

    
228
        @Override
229
        public String toWKT() throws UnsupportedOperationException {
230
                return mathTransform.toWKT();
231
        }
232

    
233
        @Override
234
        public String toString(Format format) throws UnsupportedFormatException {
235
                if (format==Format.WKT1) {
236
                        return toWKT();
237
                }
238
                throw new UnsupportedFormatException(format);
239
        }
240

    
241
        @Override
242
        public String toString(Format format, WKTConvention convention, int indentation) throws UnsupportedFormatException {
243
                if (format==Format.WKT1) {
244
                        Formatter formatter = new Formatter(Symbols.DEFAULT, indentation);
245
                        if (convention==WKTConvention.EPSG) {
246
                                formatter.setAuthority(Citations.EPSG);
247
                        }
248
                        else if (convention==WKTConvention.ESRI) {
249
                                formatter.setAuthority(Citations.ESRI);
250
                        }
251
                        formatter.append(mathTransform);
252
                        return formatter.toString();
253
                }
254
                throw new UnsupportedFormatException(format);
255
        }
256
        
257
        public MathTransform getMathTransform() {
258
                return mathTransform;
259
        }
260

    
261
        /*
262
        @Override
263
        public boolean isIdentity() throws TransformationException {
264
                return mathTransform.isIdentity();
265
        }*/
266
        
267
        @Override
268
        public boolean equals(Object obj) {
269
                // TODO Auto-generated method stub
270
                return super.equals(obj);
271
        }
272

    
273
}