Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.projection / org.gvsig.projection.cresques / org.gvsig.projection.cresques.impl / src / main / java / org / cresques / impl / cts / wkt / WKT.java @ 40559

History | View | Annotate | Download (8.69 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.cresques.impl.cts.wkt;
25

    
26
import java.util.ArrayList;
27

    
28
/**
29
 * Generates a WKT from CRS parameters.
30
 * as, for example in
31
 * "GEOGCS[\"ETRS89\","+
32
        "DATUM[\"European_Terrestrial_Reference_System_1989\","+
33
            "SPHEROID[\"GRS 1980\",6378137,298.257222101,"+
34
                "AUTHORITY[\"EPSG\",\"7019\"]],"+
35
            "AUTHORITY[\"EPSG\",\"6258\"]," +
36
            "TOWGS84[0,0,0,0,0,0,0]],"+
37
        "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"+
38
        "UNIT[\"degree\",0.01745329251994328, AUTHORITY[\"EPSG\",\"9122\"]],"+
39
        "AUTHORITY[\"EPSG\",\"4258\"]]";
40
    
41
 * @author Luis W. Sevilla <sevilla_lui@gva.es>
42
 *
43
 */
44
public abstract class WKT {
45

    
46
        static class Authority {
47
                String name;
48
                String code;
49
                public Authority(String n, String c) {
50
                        name = n;
51
                        code = c;
52
                }
53
                public String toWKT() {
54
                        return "AUTHORITY[\""+name+"\",\""+code+"\"]";
55
                }
56
        }
57

    
58
        static class Parameter {
59
                protected String name;
60
                private String value;
61
                protected Authority authority; // optional
62
                
63
                public Parameter(String n) {
64
                        name = n;
65
                }
66
                
67
                public Parameter(String n, String v) {
68
                        name = n;
69
                        value = v;
70
                }
71
                public Parameter(String n, Authority a) {
72
                        name = n;
73
                        authority = a;
74
                }
75
                
76
                public String toWKT() {
77
                        String txt = "PARAMETER[\""+name+"\","+value;
78
                        if (authority != null)
79
                                txt += ","+authority.toWKT();
80
                        return txt + "]";
81
                }
82
        }
83

    
84
        static class Spheroid {
85
                String name;
86
                double semiMajor;
87
                double inverseFlattening;
88
                Authority authority = null; // optional
89
                
90
                public Spheroid(String n, double s, double f) {
91
                        name = n;
92
                        semiMajor = s;
93
                        inverseFlattening = f;
94
                }
95
                public Spheroid(String n, double s, double f, Authority a) {
96
                        name = n;
97
                        semiMajor = s;
98
                        inverseFlattening = f;
99
                        authority = a;
100
                }
101
                public String toWKT() {
102
                        String txt = "SPHEROID[\""+name+"\","+
103
                                semiMajor+","+inverseFlattening;
104
                        if (authority != null)
105
                                txt += ","+authority.toWKT();
106
                        return txt + "]";
107
                }
108
        }
109

    
110
        static class ToWGS84 {
111
                double dx = 0;
112
                double dy = 0;
113
                double dz = 0;
114
                double ex = 0;
115
                double ey = 0;
116
                double ez = 0;
117
                double ppm = 0;
118
                public ToWGS84() {
119
                }
120
                public ToWGS84(double dx, double dy, double dz, double ex, double ey, double ez, double ppm) {
121
                        this.dx = dx;
122
                        this.dy = dy;
123
                        this.dz = dz;
124
                        this.ex = ex;
125
                        this.ey = ey;
126
                        this.ez = ez;
127
                        this.ppm = ppm;
128
                }
129
                public String toWKT() {
130
                        return "TOWGS84["+dx+","+dy+","+dz+","+ex+","+ey+","+ez+","+ppm+"]";
131
                }
132
        }
133

    
134
        static class Datum {
135
                String name;
136
                Spheroid spheroid;
137
                ToWGS84 towgs84 = new ToWGS84(); // optional
138
                Authority authority; // optional
139
                public Datum(String n, Spheroid s) {
140
                        init (n, s, new ToWGS84(), null);
141
                }
142
                public Datum(String n, Spheroid s, ToWGS84 t) {
143
                        init (n, s, t, null);
144
                }
145
                public Datum(String n, Spheroid s, Authority a) {
146
                        init (n, s, new ToWGS84(), a);
147
                }
148
                public Datum(String n, Spheroid s, ToWGS84 t, Authority a) {
149
                        init (n, s, t, a);
150
                }
151
                private void init(String n, Spheroid s,
152
                        ToWGS84 t, Authority a) {
153
                        name = n;
154
                        spheroid = s;
155
                        towgs84 = t;
156
                        authority = a;
157
                }
158
                public String toWKT() {
159
                        String txt = "DATUM[\""+name+"\","+spheroid.toWKT();
160
                        if (towgs84 != null)
161
                                txt += ","+towgs84.toWKT();
162
                        if (authority != null)
163
                                txt += ","+authority.toWKT();
164
                        return txt+"]";
165
                }
166
        }
167

    
168
        static class PrimeM {
169
                String name;
170
                double longitude;
171
                Authority authority = null; // optional
172
                
173
                public PrimeM(String n, double l) {
174
                        name = n;
175
                        longitude = l;
176
                }
177
                
178
                public PrimeM(String n, double l, Authority a) {
179
                        name = n;
180
                        longitude = l;
181
                        authority = a;
182
                }
183
                
184
                public String toWKT() {
185
                        String txt = "PRIMEM[\""+name+"\","+longitude;
186
                        if (authority != null)
187
                                txt += ","+authority.toWKT();
188
                        return txt + "]";
189
                }
190
        }
191

    
192
        static class Unit {
193
                String name;
194
                double conversionFactor;
195
                Authority authority; // optional
196
                
197
                public Unit(String n, double l) {
198
                        name = n;
199
                        conversionFactor = l;
200
                }
201
                
202
                public Unit(String n, double l, Authority a) {
203
                        name = n;
204
                        conversionFactor = l;
205
                        authority = a;
206
                }
207
                
208
                public String toWKT() {
209
                        String txt = "UNIT[\""+name+"\","+conversionFactor;
210
                        if (authority != null)
211
                                txt += ","+authority.toWKT();
212
                        return txt + "]";
213
                }
214
        }
215
        
216
        static class Orientation {
217
                public final static int NORTH = 0;
218
                public final static int SOUTH = 1;
219
                public final static int EAST = 2;
220
                public final static int WEST = 3;
221
                public final static int UP = 4;
222
                public final static int DOWN = 5;
223
                public final static int OTHER = 6;
224
                protected final String [] txt = {
225
                        "NORTH","SOUTH","EAST","WEST","UP","DOWN","OTHER"
226
                };
227
                protected int to = -1;
228
                
229
                public Orientation(int to) {
230
                        this.to = to;
231
                }
232
        }
233
        
234
        static class Axis extends Orientation {
235
                String name;
236
                
237
                public Axis(String name, int to) {
238
                        super(to);
239
                        this.name = name;
240
                }
241
                
242
                public String toWKT() {
243
                        return "AXIS[\""+name+"\","+txt[to]+ "]";
244
                }
245
        }
246
        
247
        static class TwinAxes {
248
                Axis axe1;
249
                Axis axe2;
250
                
251
                public TwinAxes(Axis a1, Axis a2) {
252
                        axe1 = a1;
253
                        axe2 = a2;
254
                }
255

    
256
                public String toWKT() {
257
                        return axe1.toWKT()+","+axe2.toWKT();
258
                }
259

    
260
        }
261

    
262
        static class GeogCS extends WKT {
263
                String name;
264
                Datum datum;
265
                PrimeM primeMeridian;
266
                Unit unit;
267
                TwinAxes axes = null; // optional
268
                Authority authority = null; // optional
269
                
270
                public GeogCS(String n, Datum d, PrimeM p, Unit u) {
271
                        init(n, d, p, u, null, null);
272
                }
273
                
274
                public GeogCS(String n, Datum d, PrimeM p, Unit u,
275
                                Authority a) {
276
                        init(n, d, p, u, null, a);
277
                }
278
                
279
                public GeogCS(String n, Datum d, PrimeM p, Unit u,
280
                                TwinAxes t) {
281
                        init(n, d, p, u, t, null);
282
                }
283
                
284
                public GeogCS(String n, Datum d, PrimeM p, Unit u,
285
                                TwinAxes t, Authority a) {
286
                        init(n, d, p, u, t, a);
287
                }
288
                
289
                private void init(String n, Datum d, PrimeM p, Unit u,
290
                        TwinAxes t, Authority a) {
291
                        name = n;
292
                        datum = d;
293
                        primeMeridian = p;
294
                        unit = u;
295
                        axes = t;
296
                        authority = a;
297
                }
298
                
299
                public String toWKT() {
300
                        String txt = "GEOGCS[\""+name+"\","+datum.toWKT()+
301
                                ","+primeMeridian.toWKT()+","+unit.toWKT();
302
                        if (axes != null)
303
                                txt += ","+axes.toWKT();
304
                        if (authority != null)
305
                                txt += ","+authority.toWKT();
306
                        return txt+"]";
307
                }
308
        }
309

    
310
        static class Projection extends Parameter {
311
                public Projection(String n) {
312
                        super(n);
313
                }
314
                
315
                public Projection(String n, Authority a) {
316
                        super(n,a);
317
                }
318
                
319
                public String toWKT() {
320
                        String txt = "PROJECTION[\""+name+"\"";
321
                        if (authority != null)
322
                                txt += ","+authority.toWKT();
323
                        return txt + "]";
324
                }
325
        }
326

    
327
        static class ProjCS extends Parameter {
328
                GeogCS geogcs;
329
                Projection proj;
330
                ArrayList params = new ArrayList();
331
                Unit unit;
332
                TwinAxes axes = null; // optional
333

    
334
                public ProjCS(String n, GeogCS cs, Projection prj, Unit u ) {
335
                        super(n);
336
                        Parameter [] p = {};
337
                        init(n, cs, prj, p, u, null);
338
                }
339
                
340
                public ProjCS(String n, GeogCS cs, Projection prj,
341
                                Parameter [] p, Unit u ) {
342
                        super(n);
343
                        init(n, cs, prj, p, u, null);
344
                }
345
                
346
                public ProjCS(String n, GeogCS cs, Projection prj,
347
                                Unit u, TwinAxes t, Authority a ) {
348
                        super(n, a);
349
                        Parameter [] p = {};
350
                        init(n, cs, prj, p, u, t);
351
                }
352
                
353
                public ProjCS(String n, GeogCS cs, Projection prj,
354
                                Parameter [] p, Unit u, TwinAxes t, Authority a ) {
355
                        super(n, a);
356
                        init(n, cs, prj, p, u, t);
357
                }
358
                
359
                private void init(String n, GeogCS cs, Projection prj,
360
                        Parameter [] p, Unit u, TwinAxes t) {
361
                        geogcs = cs;
362
                        proj = prj;
363
                        for (int i=0; i<p.length; i++)
364
                                params.add(p[i]);
365
                        unit = u;
366
                        axes = t;
367
                }
368

    
369
                public void add(Parameter p) {
370
                        params.add(p);
371
                }
372
                
373
                public String toWKT() {
374
                        String txt = "PROJCS[\""+name+"\","+geogcs.toWKT()+
375
                                ","+proj.toWKT();
376
                        for (int i=0; i<params.size();i++)
377
                                txt += ","+((Parameter)params.get(i)).toWKT();
378
                        txt += ","+unit.toWKT();
379
                        if (axes != null)
380
                                txt += ","+axes.toWKT();
381
                        if (authority != null)
382
                                txt += ","+authority.toWKT();
383
                        return txt+"]";
384
                }
385
        }
386

    
387
        abstract public String toWKT();
388
}