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 / DefaultCRSDefinition.java @ 828

History | View | Annotate | Download (7.64 KB)

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

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

    
8
import org.geotools.metadata.iso.citation.Citations;
9
import org.geotools.referencing.CRS;
10
import org.geotools.referencing.wkt.Formatter;
11
import org.geotools.referencing.wkt.Symbols;
12
import org.gvsig.geotools.proj.catalog.datum.DefaultDatum;
13
import org.gvsig.geotools.proj.catalog.utils.IdentifiedObjectUtils;
14
import org.gvsig.proj.catalog.CRSDefinition;
15
import org.gvsig.proj.catalog.CRSType;
16
import org.gvsig.proj.catalog.CoordinateSystemAxis;
17
import org.gvsig.proj.catalog.TextSerialization;
18
import org.gvsig.proj.catalog.TextSerialization.Format;
19
import org.gvsig.proj.catalog.TextSerialization.WKTConvention;
20
import org.gvsig.proj.catalog.TransformationDefinition;
21
import org.gvsig.proj.catalog.datum.Datum;
22
import org.gvsig.proj.catalog.ref.Extent;
23
import org.opengis.metadata.Identifier;
24
import org.opengis.referencing.FactoryException;
25
import org.opengis.referencing.ReferenceIdentifier;
26
import org.opengis.referencing.crs.CompoundCRS;
27
import org.opengis.referencing.crs.CoordinateReferenceSystem;
28
import org.opengis.referencing.crs.GeneralDerivedCRS;
29
import org.opengis.referencing.crs.GeocentricCRS;
30
import org.opengis.referencing.crs.GeographicCRS;
31
import org.opengis.referencing.crs.ProjectedCRS;
32
import org.opengis.referencing.crs.SingleCRS;
33
import org.opengis.referencing.crs.TemporalCRS;
34
import org.opengis.referencing.crs.VerticalCRS;
35
import org.opengis.referencing.operation.Conversion;
36

    
37
public class DefaultCRSDefinition implements CRSDefinition {
38
        private CoordinateReferenceSystem origCrs = null;
39
        private CoordinateReferenceSystem epsgCrs = null;
40
        private String wkt = null;
41

    
42
        public DefaultCRSDefinition(String wktDefinition) {
43
                this.wkt = wktDefinition;
44
                try {
45
                        this.origCrs = CRS.parseWKT(wktDefinition);
46
                } catch (FactoryException e) {
47
                        // TODO Auto-generated catch block
48
                        e.printStackTrace();
49
                }
50
        }
51
        
52
        public DefaultCRSDefinition(CoordinateReferenceSystem crs) {
53
                this.origCrs = crs;
54
        }
55

    
56
        public CoordinateReferenceSystem getInternalCRS() {
57
                return origCrs;
58
        }
59
        
60
        @Override
61
        public String getName() {
62
                return origCrs.getName().toString();
63
        }
64

    
65
        @Override
66
        public String getIdentifier() {
67
                String id = IdentifiedObjectUtils.getIdentifier(origCrs);
68
                if (id!=null) {
69
                        return id;
70
                }
71
                // FIXME: define in API whether null is allowed
72
                return IdentifiedObjectUtils.getIdentifier(getEpsgCrs());
73
        }
74
        
75
        /**
76
         * Gets an EPSG code which can be considered equivalent to this CRS or
77
         * null if not found
78
         * @return
79
         */
80
        protected CoordinateReferenceSystem getEpsgCrs() {
81
                if (epsgCrs==null) {
82
                        try {
83
                                String code = CRS.lookupIdentifier(Citations.EPSG, origCrs, true);
84
                                if (code!=null) {
85
                                        epsgCrs = CRS.decode("EPSG:"+code);
86
                                }
87
                        }
88
                        catch (FactoryException exc) {}
89
                }
90
                return epsgCrs;
91
        }
92

    
93
        @Override
94
        public Set<String> getIdentifiers() {
95
                HashSet<String> ids = new HashSet<String>();
96
                for (ReferenceIdentifier id: origCrs.getIdentifiers()) {
97
                        ids.add(id.toString());
98
                }
99
                if (!getAuthorityName().equals("EPSG") && getEpsgCrs() != null) {
100
                        for (ReferenceIdentifier id: getEpsgCrs().getIdentifiers()) {
101
                                ids.add(id.toString());
102
                        }
103
                }
104
                return ids;
105
        }
106

    
107
        @Override
108
        public String getAuthorityName() {
109
                for (Identifier id: origCrs.getName().getAuthority().getIdentifiers()) {
110
                        return id.getCode();
111
                }
112
                return null; // FIXME: define in API whether null is allowed 
113
        }
114

    
115
        @Override
116
        public Extent getDomainOfValidity() {
117
                // TODO Auto-generated method stub
118
                return null;
119
        }
120

    
121
        @Override
122
        public String getDescription() {
123
                return "Scope: " + origCrs.getScope() + "\nRemarks: " + origCrs.getRemarks();
124
        }
125

    
126
        @Override
127
        public CRSType getType() {
128
                if (origCrs instanceof ProjectedCRS) {
129
                        return CRSType.ProjectedCRSType;
130
                }
131
                if (origCrs instanceof GeographicCRS) {
132
                        return CRSType.GeographicCRSType;
133
                }
134
                if (origCrs instanceof GeocentricCRS) {
135
                        return CRSType.GeocentricCRSType;
136
                }
137
                if (origCrs instanceof GeneralDerivedCRS) {
138
                        return CRSType.OtherDerivedCRSType;
139
                }
140
                if (origCrs instanceof CompoundCRS) {
141
                        return CRSType.CompoundCRSType;
142
                }
143
                if (origCrs instanceof VerticalCRS) {
144
                        return CRSType.VerticalCRSType;
145
                }
146
                if (origCrs instanceof TemporalCRS) {
147
                        return CRSType.TemporalCRSType;
148
                }
149
                return CRSType.OtherCRSType;
150
        }
151

    
152
        @Override
153
        public String toWKT() throws UnsupportedOperationException {
154
                if (wkt!=null) {
155
                        return wkt;
156
                }
157
                return origCrs.toWKT();
158
        }
159
        
160

    
161
        @Override
162
        public String toString(Format format, WKTConvention convention) throws UnsupportedOperationException {
163
                return toString(format, convention, 0);
164
        }
165

    
166
        @Override
167
        public String toString(TextSerialization.Format format, WKTConvention convention, int indentation) throws UnsupportedOperationException {
168
                if (format==Format.WKT1) {
169
                        if (convention==WKTConvention.EPSG) {
170
                                Formatter formatter = new Formatter(Symbols.DEFAULT, indentation);
171
                                formatter.setAuthority(Citations.EPSG);
172
                                if (!this.getAuthorityName().equals("EPSG")) {
173
                                        String id;
174
                                        try {
175
                                                id = CRS.lookupIdentifier(Citations.EPSG, this.origCrs, true);
176
                                                if (id!=null) {
177
                                                        if (!id.startsWith("EPSG:")) {
178
                                                                id = "EPSG:" + id;
179
                                                        }
180
                                                        CoordinateReferenceSystem crs = CRS.decode(id);
181
                                                        formatter.append(crs);
182
                                                        return formatter.toString();
183
                                                }
184
                                        } catch (FactoryException e) {
185
                                        }
186
                                }
187
                                formatter.append(this.origCrs);
188
                                return formatter.toString();
189
                        }
190
                        if (convention==WKTConvention.ESRI) {
191
                                // FIXME: should we consider this.wkt if it is not null??
192
                                Formatter formatter = new Formatter(Symbols.DEFAULT, indentation);
193
                                formatter.setAuthority(Citations.ESRI);
194
                                formatter.append(this.origCrs);
195
                                return formatter.toString();
196
                        }
197
                        return toWKT();
198
                }
199
                throw new UnsupportedOperationException("Format not supported");
200
        }
201

    
202

    
203
        @Override
204
        public List<CRSDefinition> getComponents() {
205
                if (origCrs instanceof CompoundCRS) {
206
                        ArrayList<CRSDefinition> list = new ArrayList<CRSDefinition>();
207
                        for (CoordinateReferenceSystem component: ((CompoundCRS) origCrs).getCoordinateReferenceSystems()) {
208
                                list.add(new DefaultCRSDefinition(component));
209
                        }
210
                        return list;
211
                }
212
                return null;
213
        }
214

    
215
        @Override
216
        public int getAxisCount() {
217
                return origCrs.getCoordinateSystem().getDimension();
218
        }
219

    
220
        @Override
221
        public CoordinateSystemAxis getAxis(int dimension) throws IndexOutOfBoundsException {
222
                return new DefaultCoordinateSystemAxis(origCrs.getCoordinateSystem().getAxis(dimension));
223
        }
224

    
225
        @Override
226
        public CoordinateSystemAxis getAxisInternal(int dimension) throws IndexOutOfBoundsException {
227
                // TODO Auto-generated method stub
228
                return null;
229
        }
230

    
231
        @Override
232
        public CRSDefinition getBaseCRS() {
233
                if (origCrs instanceof GeneralDerivedCRS) {
234
                        CoordinateReferenceSystem base = ((GeneralDerivedCRS)origCrs).getBaseCRS();
235
                        return new DefaultCRSDefinition(base);
236
                }
237
                return null;
238
        }
239

    
240
        @Override
241
        public TransformationDefinition getConversionFromBase() {
242
                if (origCrs instanceof GeneralDerivedCRS) {
243
                        Conversion conversion = ((GeneralDerivedCRS)origCrs).getConversionFromBase();
244
                        return new DefaultTransformationDefinition(conversion);
245
                }
246
                return null;
247
        }
248

    
249
        @Override
250
        public Datum getDatum() {
251
                if (origCrs instanceof SingleCRS) {
252
                        SingleCRS singleCrs = (SingleCRS)origCrs;
253
                        return new DefaultDatum(singleCrs.getDatum());
254
                }
255
                else {
256
                        SingleCRS horizontalCrs = CRS.getHorizontalCRS(origCrs);
257
                        return new DefaultDatum(horizontalCrs.getDatum());
258
                }
259
        }
260
        
261
        @Override
262
        public Object clone() throws CloneNotSupportedException {
263
                return new DefaultCRSDefinition(this.origCrs);
264
        }
265

    
266
        @Override
267
        public boolean equals(CRSDefinition definition, boolean ignoreAxis, boolean ignoreMetadata) {
268
                // TODO Auto-generated method stub
269
                return this.equals(definition);
270
        }
271
}