Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / cts / wkt / WKT.java @ 8120

History | View | Annotate | Download (7.75 KB)

1
package org.cresques.cts.wkt;
2

    
3
import java.util.ArrayList;
4

    
5
/**
6
 * Generates a WKT from CRS parameters.
7
 * as, for example in
8
 * "GEOGCS[\"ETRS89\","+
9
        "DATUM[\"European_Terrestrial_Reference_System_1989\","+
10
            "SPHEROID[\"GRS 1980\",6378137,298.257222101,"+
11
                "AUTHORITY[\"EPSG\",\"7019\"]],"+
12
            "AUTHORITY[\"EPSG\",\"6258\"]," +
13
            "TOWGS84[0,0,0,0,0,0,0]],"+
14
        "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"+
15
        "UNIT[\"degree\",0.01745329251994328, AUTHORITY[\"EPSG\",\"9122\"]],"+
16
        "AUTHORITY[\"EPSG\",\"4258\"]]";
17
    
18
 * @author Luis W. Sevilla <sevilla_lui@gva.es>
19
 *
20
 */
21
public abstract class WKT {
22

    
23
        static class Authority {
24
                String name;
25
                String code;
26
                public Authority(String n, String c) {
27
                        name = n;
28
                        code = c;
29
                }
30
                public String toWKT() {
31
                        return "AUTHORITY[\""+name+"\",\""+code+"\"]";
32
                }
33
        }
34

    
35
        static class Parameter {
36
                protected String name;
37
                private String value;
38
                protected Authority authority; // optional
39
                
40
                public Parameter(String n) {
41
                        name = n;
42
                }
43
                
44
                public Parameter(String n, String v) {
45
                        name = n;
46
                        value = v;
47
                }
48
                public Parameter(String n, Authority a) {
49
                        name = n;
50
                        authority = a;
51
                }
52
                
53
                public String toWKT() {
54
                        String txt = "PARAMETER[\""+name+"\","+value;
55
                        if (authority != null)
56
                                txt += ","+authority.toWKT();
57
                        return txt + "]";
58
                }
59
        }
60

    
61
        static class Spheroid {
62
                String name;
63
                double semiMajor;
64
                double inverseFlattening;
65
                Authority authority = null; // optional
66
                
67
                public Spheroid(String n, double s, double f) {
68
                        name = n;
69
                        semiMajor = s;
70
                        inverseFlattening = f;
71
                }
72
                public Spheroid(String n, double s, double f, Authority a) {
73
                        name = n;
74
                        semiMajor = s;
75
                        inverseFlattening = f;
76
                        authority = a;
77
                }
78
                public String toWKT() {
79
                        String txt = "SPHEROID[\""+name+"\","+
80
                                semiMajor+","+inverseFlattening;
81
                        if (authority != null)
82
                                txt += ","+authority.toWKT();
83
                        return txt + "]";
84
                }
85
        }
86

    
87
        static class ToWGS84 {
88
                double dx = 0;
89
                double dy = 0;
90
                double dz = 0;
91
                double ex = 0;
92
                double ey = 0;
93
                double ez = 0;
94
                double ppm = 0;
95
                public ToWGS84() {
96
                }
97
                public ToWGS84(double dx, double dy, double dz, double ex, double ey, double ez, double ppm) {
98
                        this.dx = dx;
99
                        this.dy = dy;
100
                        this.dz = dz;
101
                        this.ex = ex;
102
                        this.ey = ey;
103
                        this.ez = ez;
104
                        this.ppm = ppm;
105
                }
106
                public String toWKT() {
107
                        return "TOWGS84["+dx+","+dy+","+dz+","+ex+","+ey+","+ez+","+ppm+"]";
108
                }
109
        }
110

    
111
        static class Datum {
112
                String name;
113
                Spheroid spheroid;
114
                ToWGS84 towgs84 = new ToWGS84(); // optional
115
                Authority authority; // optional
116
                public Datum(String n, Spheroid s) {
117
                        init (n, s, new ToWGS84(), null);
118
                }
119
                public Datum(String n, Spheroid s, ToWGS84 t) {
120
                        init (n, s, t, null);
121
                }
122
                public Datum(String n, Spheroid s, Authority a) {
123
                        init (n, s, new ToWGS84(), a);
124
                }
125
                public Datum(String n, Spheroid s, ToWGS84 t, Authority a) {
126
                        init (n, s, t, a);
127
                }
128
                private void init(String n, Spheroid s,
129
                        ToWGS84 t, Authority a) {
130
                        name = n;
131
                        spheroid = s;
132
                        towgs84 = t;
133
                        authority = a;
134
                }
135
                public String toWKT() {
136
                        String txt = "DATUM[\""+name+"\","+spheroid.toWKT();
137
                        if (towgs84 != null)
138
                                txt += ","+towgs84.toWKT();
139
                        if (authority != null)
140
                                txt += ","+authority.toWKT();
141
                        return txt+"]";
142
                }
143
        }
144

    
145
        static class PrimeM {
146
                String name;
147
                double longitude;
148
                Authority authority = null; // optional
149
                
150
                public PrimeM(String n, double l) {
151
                        name = n;
152
                        longitude = l;
153
                }
154
                
155
                public PrimeM(String n, double l, Authority a) {
156
                        name = n;
157
                        longitude = l;
158
                        authority = a;
159
                }
160
                
161
                public String toWKT() {
162
                        String txt = "PRIMEM[\""+name+"\","+longitude;
163
                        if (authority != null)
164
                                txt += ","+authority.toWKT();
165
                        return txt + "]";
166
                }
167
        }
168

    
169
        static class Unit {
170
                String name;
171
                double conversionFactor;
172
                Authority authority; // optional
173
                
174
                public Unit(String n, double l) {
175
                        name = n;
176
                        conversionFactor = l;
177
                }
178
                
179
                public Unit(String n, double l, Authority a) {
180
                        name = n;
181
                        conversionFactor = l;
182
                        authority = a;
183
                }
184
                
185
                public String toWKT() {
186
                        String txt = "UNIT[\""+name+"\","+conversionFactor;
187
                        if (authority != null)
188
                                txt += ","+authority.toWKT();
189
                        return txt + "]";
190
                }
191
        }
192
        
193
        static class Orientation {
194
                public final static int NORTH = 0;
195
                public final static int SOUTH = 1;
196
                public final static int EAST = 2;
197
                public final static int WEST = 3;
198
                public final static int UP = 4;
199
                public final static int DOWN = 5;
200
                public final static int OTHER = 6;
201
                protected final String [] txt = {
202
                        "NORTH","SOUTH","EAST","WEST","UP","DOWN","OTHER"
203
                };
204
                protected int to = -1;
205
                
206
                public Orientation(int to) {
207
                        this.to = to;
208
                }
209
        }
210
        
211
        static class Axis extends Orientation {
212
                String name;
213
                
214
                public Axis(String name, int to) {
215
                        super(to);
216
                        this.name = name;
217
                }
218
                
219
                public String toWKT() {
220
                        return "AXIS[\""+name+"\","+txt[to]+ "]";
221
                }
222
        }
223
        
224
        static class TwinAxes {
225
                Axis axe1;
226
                Axis axe2;
227
                
228
                public TwinAxes(Axis a1, Axis a2) {
229
                        axe1 = a1;
230
                        axe2 = a2;
231
                }
232

    
233
                public String toWKT() {
234
                        return axe1.toWKT()+","+axe2.toWKT();
235
                }
236

    
237
        }
238

    
239
        static class GeogCS extends WKT {
240
                String name;
241
                Datum datum;
242
                PrimeM primeMeridian;
243
                Unit unit;
244
                TwinAxes axes = null; // optional
245
                Authority authority = null; // optional
246
                
247
                public GeogCS(String n, Datum d, PrimeM p, Unit u) {
248
                        init(n, d, p, u, null, null);
249
                }
250
                
251
                public GeogCS(String n, Datum d, PrimeM p, Unit u,
252
                                Authority a) {
253
                        init(n, d, p, u, null, a);
254
                }
255
                
256
                public GeogCS(String n, Datum d, PrimeM p, Unit u,
257
                                TwinAxes t) {
258
                        init(n, d, p, u, t, null);
259
                }
260
                
261
                public GeogCS(String n, Datum d, PrimeM p, Unit u,
262
                                TwinAxes t, Authority a) {
263
                        init(n, d, p, u, t, a);
264
                }
265
                
266
                private void init(String n, Datum d, PrimeM p, Unit u,
267
                        TwinAxes t, Authority a) {
268
                        name = n;
269
                        datum = d;
270
                        primeMeridian = p;
271
                        unit = u;
272
                        axes = t;
273
                        authority = a;
274
                }
275
                
276
                public String toWKT() {
277
                        String txt = "GEOGCS[\""+name+"\","+datum.toWKT()+
278
                                ","+primeMeridian.toWKT()+","+unit.toWKT();
279
                        if (axes != null)
280
                                txt += ","+axes.toWKT();
281
                        if (authority != null)
282
                                txt += ","+authority.toWKT();
283
                        return txt+"]";
284
                }
285
        }
286

    
287
        static class Projection extends Parameter {
288
                public Projection(String n) {
289
                        super(n);
290
                }
291
                
292
                public Projection(String n, Authority a) {
293
                        super(n,a);
294
                }
295
                
296
                public String toWKT() {
297
                        String txt = "PROJECTION[\""+name+"\"";
298
                        if (authority != null)
299
                                txt += ","+authority.toWKT();
300
                        return txt + "]";
301
                }
302
        }
303

    
304
        static class ProjCS extends Parameter {
305
                GeogCS geogcs;
306
                Projection proj;
307
                ArrayList params = new ArrayList();
308
                Unit unit;
309
                TwinAxes axes = null; // optional
310

    
311
                public ProjCS(String n, GeogCS cs, Projection prj, Unit u ) {
312
                        super(n);
313
                        Parameter [] p = {};
314
                        init(n, cs, prj, p, u, null);
315
                }
316
                
317
                public ProjCS(String n, GeogCS cs, Projection prj,
318
                                Parameter [] p, Unit u ) {
319
                        super(n);
320
                        init(n, cs, prj, p, u, null);
321
                }
322
                
323
                public ProjCS(String n, GeogCS cs, Projection prj,
324
                                Unit u, TwinAxes t, Authority a ) {
325
                        super(n, a);
326
                        Parameter [] p = {};
327
                        init(n, cs, prj, p, u, t);
328
                }
329
                
330
                public ProjCS(String n, GeogCS cs, Projection prj,
331
                                Parameter [] p, Unit u, TwinAxes t, Authority a ) {
332
                        super(n, a);
333
                        init(n, cs, prj, p, u, t);
334
                }
335
                
336
                private void init(String n, GeogCS cs, Projection prj,
337
                        Parameter [] p, Unit u, TwinAxes t) {
338
                        geogcs = cs;
339
                        proj = prj;
340
                        for (int i=0; i<p.length; i++)
341
                                params.add(p[i]);
342
                        unit = u;
343
                        axes = t;
344
                }
345

    
346
                public void add(Parameter p) {
347
                        params.add(p);
348
                }
349
                
350
                public String toWKT() {
351
                        String txt = "PROJCS[\""+name+"\","+geogcs.toWKT()+
352
                                ","+proj.toWKT();
353
                        for (int i=0; i<params.size();i++)
354
                                txt += ","+((Parameter)params.get(i)).toWKT();
355
                        txt += ","+unit.toWKT();
356
                        if (axes != null)
357
                                txt += ","+axes.toWKT();
358
                        if (authority != null)
359
                                txt += ","+authority.toWKT();
360
                        return txt+"]";
361
                }
362
        }
363

    
364
        abstract public String toWKT();
365
}