Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / Crs.java @ 8426

History | View | Annotate | Download (11.7 KB)

1
package org.gvsig.crs;
2

    
3
import java.awt.Color;
4
import java.awt.Graphics2D;
5
import java.awt.geom.Point2D;
6
import java.awt.geom.Rectangle2D;
7

    
8
import org.cresques.cts.ICoordTrans;
9
import org.cresques.cts.IDatum;
10
import org.cresques.cts.IProjection;
11
import org.cresques.geo.ViewPortData;
12
import org.gvsig.crs.ogr.CrsOgr;
13
import org.gvsig.crs.ogr.CrsOgrException;
14
import org.gvsig.crs.ogr.OGRException;
15
import org.gvsig.crs.ogr.OGRSpatialReference;
16
import org.gvsig.crs.ogr.crsgdalException;
17
import org.gvsig.crs.proj.CrsProj;
18
import org.gvsig.crs.proj.CrsProjException;
19
import org.gvsig.crs.proj.OperationCrs;
20
import org.gvsig.crs.proj.OperationCrsException;
21

    
22
public class Crs implements ICrs {
23
        private static final Color basicGridColor = new Color(64, 64, 64, 128);
24
        private String proj4;
25
        private String trans;
26
        //private String transOrigin = "";
27
        private String abrev;
28
        private String name = "";
29
        private CrsProj crsProj4;
30
        private CrsProj crsBase = null;
31
        private CrsWkt crsWkt;
32
        private int epsg_code = 23030;
33
        boolean targetNad = false;
34
        String nad = "";
35
        String wkt = null;
36
        Color gridColor = basicGridColor;
37
        CRSDatum datum = null;
38

    
39
        /**
40
         * Construye un CRS a partir del c?digo EPSG o de la cadena WKT.
41
         * 
42
         * @param code
43
         * @throws CrsException
44
         */
45
        public Crs(String code) throws CrsException {
46
                String fullCode;
47
                setWKT(code);
48
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
49
                        fullCode = code;
50
                else fullCode = "EPSG:"+code;
51
                String cod = "";
52
                if(code.length() < 15 ) {
53
                        code = code.substring(code.indexOf(":")+1);
54
                        try {
55
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
56
                                crsWkt = new CrsWkt(fullCode);
57
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
58
                                CrsOgr.importFromEPSG(oSRSSource, 
59
                                                Integer.parseInt(code));
60
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
61
                                proj4 = CrsOgr.exportToProj4(oSRSSource);
62
                                crsProj4 = new CrsProj(proj4);
63
                                setName(fullCode);
64
                                setAbrev(fullCode);
65
                                
66
                        } catch (OGRException e) {
67
                                throw new CrsException(e);
68
                        } catch (crsgdalException e) {
69
                                throw new CrsException(e);
70
                        } catch (CrsOgrException e) {
71
                                throw new CrsException(e);
72
                        }
73
                }else {
74
                        String aux;
75
                        String code2 = "";
76
                        for(int i = 0; i < code.length(); i++) {
77
                                aux = ""+code.charAt(i);
78
                                if(!aux.equals(" ")) {
79
                                        code2+=aux;
80
                                }else {
81
                                        code2 += "";
82
                                }
83
                        }
84
                        crsWkt = new CrsWkt(code2);
85
                    try {
86
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
87
                            CrsOgr.importFromWkt(oSRSSource,code2);
88
                            proj4 =CrsOgr.exportToProj4(oSRSSource);
89
                                crsProj4 = new CrsProj(proj4);
90
                            setName(fullCode);
91
                            setAbrev(crsWkt.getName());
92
                    } catch (OGRException e) {
93
                                throw new CrsException(e);
94
                        } catch (crsgdalException e) {
95
                                throw new CrsException(e);
96
                        } catch (CrsOgrException e) {
97
                                throw new CrsException(e);
98
                        }
99
                }
100
                //        Asignar el datum y el crs base (en el caso de ser projectado):
101
                if (!(crsWkt.getSpheroid()[1].equals("")||crsWkt.getSpheroid()[2].equals(""))){
102
                        double eSemiMajorAxis = Double.valueOf(crsWkt.getSpheroid()[1]).doubleValue();
103
                        double eIFlattening = Double.valueOf(crsWkt.getSpheroid()[2]).doubleValue();
104
                        datum = new CRSDatum(eSemiMajorAxis,eIFlattening);
105
                        
106
                        //crs base (en el caso de ser projectado):
107
                        if(this.isProjected()){
108
                                String proj4Base = "+proj=latlong +a=" + crsWkt.getSpheroid()[1] + " +rf=" + crsWkt.getSpheroid()[2] ;
109
                                crsBase = new CrsProj(proj4Base);
110
                        }
111
                }
112
        }        
113
        
114
        /**
115
         *Construye un CRS a partir del c?digo EPSG o de la cadena WKT.
116
         * 
117
         * @param epsg_cod
118
         * @param code
119
         * @throws CrsException
120
         */
121
        public Crs(int epsg_cod, String code) throws CrsException {
122
                String fullCode;
123
                setWKT(code);
124
                setCode(epsg_cod);
125
                if(code != null || code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
126
                        fullCode = code;
127
                else fullCode = "EPSG:"+code;
128
                String cod = "";
129
                if(code.length() < 15 ) {
130
                        code = code.substring(code.indexOf(":")+1);
131
                        try {
132
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
133
                                crsWkt = new CrsWkt(fullCode);
134
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
135
                                CrsOgr.importFromEPSG(oSRSSource, 
136
                                                Integer.parseInt(code));
137
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
138
                                proj4 = CrsOgr.exportToProj4(oSRSSource);
139
                                crsProj4 = new CrsProj(proj4);
140
                                setName(fullCode);
141
                                setAbrev(fullCode);
142
                                
143
                        } catch (OGRException e) {
144
                                throw new CrsException(e);
145
                        } catch (crsgdalException e) {
146
                                throw new CrsException(e);
147
                        } catch (CrsOgrException e) {
148
                                throw new CrsException(e);
149
                        }
150
                }else {
151
                        String aux;
152
                        String code2 = "";
153
                        for(int i = 0; i < code.length(); i++) {
154
                                aux = ""+code.charAt(i);
155
                                if(!aux.equals(" ")) {
156
                                        code2+=aux;
157
                                }else {
158
                                        code2 += "";
159
                                }
160
                        }
161
                        crsWkt = new CrsWkt(code2);
162
                    try {
163
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
164
                            CrsOgr.importFromWkt(oSRSSource,code2);
165
                            proj4 = CrsOgr.exportToProj4(oSRSSource);
166
                                crsProj4 = new CrsProj(proj4);
167
                            setName(fullCode);
168
                            //setAbrev(crsWkt.getName());
169
                            setAbrev(crsWkt.getAuthority()[0]+":"+crsWkt.getAuthority()[1]);
170
                    } catch (OGRException e) {
171
                                throw new CrsException(e);
172
                        } catch (crsgdalException e) {
173
                                throw new CrsException(e);
174
                        } catch (CrsOgrException e) {
175
                                throw new CrsException(e);
176
                        }
177
                }
178
                //        Asignar el datum:
179
                if (!(crsWkt.getSpheroid()[1].equals("")||crsWkt.getSpheroid()[2].equals(""))){
180
                        double eSemiMajorAxis = Double.valueOf(crsWkt.getSpheroid()[1]).doubleValue();
181
                        double eIFlattening = Double.valueOf(crsWkt.getSpheroid()[2]).doubleValue();
182
                        datum = new CRSDatum(eSemiMajorAxis,eIFlattening);
183
                        
184
                        // Crs base (en el caso de ser projectado):
185
                        if(this.isProjected()){
186
                                String proj4Base = "+proj=latlong +a=" + crsWkt.getSpheroid()[1] + " +rf=" + crsWkt.getSpheroid()[2] ;
187
                                crsBase = new CrsProj(proj4Base);
188
                        }
189
                }
190
        }
191
        
192
        /**
193
         * Construye un CRS a partir del c?digo EPSG o de la cadena WKT.
194
         * En el caso de WKT le a?ade a la cadena proj4 el string params.
195
         * 
196
         * @param epsg_cod
197
         * @param code
198
         * @param params
199
         * @throws CrsException
200
         */
201
        public Crs(int epsg_cod, String code,String params) throws CrsException {
202
                String fullCode;
203
                setCode(epsg_cod);
204
                setWKT(code);
205
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
206
                        fullCode = code;
207
                else fullCode = "EPSG:"+code;
208
                String cod = "";
209
                if(code.length() < 15 ) {
210
                        code = code.substring(code.indexOf(":")+1);
211
                        try {
212
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
213
                                crsWkt = new CrsWkt(fullCode);
214
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
215
                                CrsOgr.importFromEPSG(oSRSSource, 
216
                                                Integer.parseInt(code));
217
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
218
                                proj4 = CrsOgr.exportToProj4(oSRSSource);
219
                                crsProj4 = new CrsProj(proj4);
220
                                setName(fullCode);
221
                                setAbrev(fullCode);
222
                                
223
                        } catch (OGRException e) {
224
                                throw new CrsException(e);
225
                        } catch (crsgdalException e) {
226
                                throw new CrsException(e);
227
                        } catch (CrsOgrException e) {
228
                                throw new CrsException(e);
229
                        }
230
                }else {
231
                        String aux;
232
                        String code2 = "";
233
                        for(int i = 0; i < code.length(); i++) {
234
                                aux = ""+code.charAt(i);
235
                                if(!aux.equals(" ")) {
236
                                        code2+=aux;
237
                                }else {
238
                                        code2 += "";
239
                                }
240
                        }
241
                        crsWkt = new CrsWkt(code2);
242
                    try {
243
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
244
                            CrsOgr.importFromWkt(oSRSSource,code2);
245
                            proj4 =CrsOgr.exportToProj4(oSRSSource)+params+" ";
246
                                crsProj4 = new CrsProj(proj4);
247
                            setName(fullCode);
248
                            setAbrev(crsWkt.getName());
249
                    } catch (OGRException e) {
250
                                throw new CrsException(e);
251
                        } catch (crsgdalException e) {
252
                                throw new CrsException(e);
253
                        } catch (CrsOgrException e) {
254
                                throw new CrsException(e);
255
                        }
256
                }
257
                //        Asignar el datum:
258
                if (!(crsWkt.getSpheroid()[1].equals("")||crsWkt.getSpheroid()[2].equals(""))){
259
                        double eSemiMajorAxis = Double.valueOf(crsWkt.getSpheroid()[1]).doubleValue();
260
                        double eIFlattening = Double.valueOf(crsWkt.getSpheroid()[2]).doubleValue();
261
                        datum = new CRSDatum(eSemiMajorAxis,eIFlattening);
262
                        
263
                        // Crs base (en el caso de ser projectado):
264
                        if(this.isProjected()){
265
                                String proj4Base = "+proj=latlong +a=" + crsWkt.getSpheroid()[1] + " +rf=" + crsWkt.getSpheroid()[2] ;
266
                                crsBase = new CrsProj(proj4Base);
267
                        }
268
                }
269
        }
270
                
271
/*        public Crs(CrsEpsg source) throws CrsException {
272
                crsProj4 = source;
273
        }*/
274
        
275
        public void setTrans(String code) {
276
                trans = code;
277
                changeTrans(trans);
278
        }
279
        
280
        public void changeTrans(String code) {
281
                crsProj4.changeStrCrs(code);
282
        }
283
        
284
        public String getAbrev() {
285
                return abrev;
286
        }
287
        
288
        protected void setAbrev(String code) {
289
                abrev = code;
290
        }
291
        
292
        public void setName(String nom) {
293
                name = nom;
294
        }
295
        
296
        public IDatum getDatum() {
297
                /*CSDatum datum = new CSDatum();
298
                try {
299
                        datum.fromWKT(getWKT());
300
                } catch (FactoryException e) {
301
                        // TODO Auto-generated catch block
302
                        e.printStackTrace();
303
                }*/
304
                return datum;
305
        }
306
        
307
        public CrsWkt getCrsWkt() {
308
                return crsWkt;
309
        }
310
        
311
        public void setWKT(String wkt){
312
                this.wkt = wkt;
313
        }
314
        
315
        public String getWKT(){
316
                return this.wkt;
317
        }
318
        
319
        public void setNadTarget(String nad){
320
                this.nad = nad;
321
        }
322
        
323
        public String getNadTarget(){
324
                return this.nad;
325
        }
326
        
327
        public ICOperation getCOp(ICrs target) throws CrsException {                
328
                if (!this.getNadTarget().equals("")) {                        
329
                        ICrs crs = new Crs(target.getCode(), target.getWKT(), this.getNadTarget());
330
                        return new COperation(this, crs);
331
                }
332
                else return new COperation(this, target);
333
        }
334
        
335
        protected CrsProj getCrsEpsg() {
336
                return crsProj4;
337
        }
338

    
339
        public Point2D createPoint(double x, double y) {
340
                return new Point2D.Double(x,y);
341
        }
342

    
343
        public void drawGrid(Graphics2D g, ViewPortData vp) {
344
                // TODO Auto-generated method stub
345
                
346
        }
347

    
348
        public void setGridColor(Color c) {
349
                gridColor = c;
350
        }
351

    
352
        public Color getGridColor() {
353
                return gridColor;
354
        }
355

    
356
        public ICoordTrans getCT(IProjection dest) {
357
                ICrs crs = (ICrs) dest;                
358
                try {
359
                        if (!this.getNadTarget().equals("")) {
360
                                crs = new Crs(crs.getCode(), crs.getWKT(), this.getNadTarget());
361
                                return new COperation(this, crs);
362
                        }
363
                        else return new COperation(this, crs);
364
                } catch (CrsException e) {
365
                        // TODO Auto-generated catch block
366
                        e.printStackTrace();
367
                }
368
                return null;
369
        }
370
        
371
        public Point2D toGeo(Point2D pt) {
372
                if (isProjected()){
373
                        double x[] = {pt.getX()};
374
                        double y[] = {pt.getY()};
375
                        double z[] = {0D};
376
                        try {
377
                                OperationCrs.operate( x , y, z,
378
                                                crsProj4,crsBase);
379
                        } catch (OperationCrsException e) {
380
                                // TODO Auto-generated catch block
381
                                e.printStackTrace();
382
                        } catch (CrsProjException e) {
383
                                // TODO Auto-generated catch block
384
                                e.printStackTrace();
385
                        }
386
                        return new Point2D.Double(x[0],y[0]);
387
                }
388
                else
389
                        return pt;
390
        }
391

    
392
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
393
                // TODO Auto-generated method stub
394
                return null;
395
        }
396

    
397
        public boolean isProjected() {
398
                return !crsProj4.isLatlong();                
399
        }
400

    
401
        public double getScale(double minX, double maxX, double width, double dpi) {
402
                double scale = 0D;
403
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
404
            scale = ((maxX - minX) * // grados
405

    
406
            // 1852.0 metros x minuto de meridiano
407
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
408
                    width; // pixels
409
        }
410
        else{
411
                 scale = ((maxX - minX) * // metros
412
                    (dpi / 2.54 * 100.0)) / // px / metro
413
                    width; // pixels
414
        }
415
        return scale;
416
        }
417
        
418
        public void setCode(int epsg_cod){
419
                epsg_code = epsg_cod;
420
        }
421

    
422
        public int getCode() {
423
                // TODO Auto-generated method stub
424
                return epsg_code;
425
        }
426

    
427
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double changeUnits, double dpi) {
428
                // TODO Auto-generated method stub
429
                return null;
430
        }
431
        
432
}