Statistics
| Revision:

root / branches / F2 / libraries / libJCRS / src / org / gvsig / crs / Crs.java @ 12201

History | View | Annotate | Download (19.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics2D;
45
import java.awt.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47

    
48
import org.cresques.cts.ICoordTrans;
49
import org.cresques.cts.IDatum;
50
import org.cresques.cts.IProjection;
51
import org.cresques.geo.ViewPortData;
52
import org.gvsig.crs.proj.CrsProj;
53
import org.gvsig.crs.proj.CrsProjException;
54
import org.gvsig.crs.proj.JNIBaseCrs;
55
import org.gvsig.crs.proj.OperationCrsException;
56
import org.opengis.referencing.crs.CoordinateReferenceSystem;
57

    
58
import es.gva.cit.jogr.CrsGdalException;
59
import es.gva.cit.jogr.CrsOgrException;
60
import es.gva.cit.jogr.OGRException;
61
import es.gva.cit.jogr.OGRSpatialReference;
62

    
63
/**
64
 * Clase que construye el CRS a partir de la cadena WKT
65
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
66
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
67
 * @author Miguel Garc?a Jim?nez (garciajimenez.miguel@gmail.com)
68
 *
69
 */
70
public class Crs implements ICrs {
71
        private static final Color basicGridColor = new Color(64, 64, 64, 128);
72
        private String proj4;
73
        private String trans;
74
        //private String transOrigin = "";
75
        private String abrev;
76
        private String name = "";
77
        private CrsProj crsProj4;
78
        private CrsProj crsBase = null;
79
        private CrsWkt crsWkt;
80
        private int epsg_code = 23030;
81
        String sourceTrParams = null;
82
        String targetTrParams = null;
83
        String wkt = null;
84
        Color gridColor = basicGridColor;
85
        CRSDatum datum = null;
86
        private CoordinateReferenceSystem gtCrs;
87

    
88
        public Crs(int epsgCode, int aut) throws CrsException
89
        {
90
                String strEpsgCode = "";
91
                if (aut == 1) {
92
                        strEpsgCode="EPSG:"+epsgCode;                        
93
                } else if (aut == 2){
94
                        strEpsgCode="ESRI:"+epsgCode;
95
                } else if (aut == 3){
96
                        strEpsgCode="IAU2000:"+epsgCode;
97
                } else if (aut == 4) {
98
                        strEpsgCode="USR:"+epsgCode;
99
                } else System.out.println("Error, autorithy err?neo");
100
                crsWkt=new CrsWkt(strEpsgCode);
101
                setWKT(crsWkt.getWkt());
102
        }
103
        
104
        
105
        /**
106
         * Construye un CRS a partir del c?digo EPSG o de la cadena WKT.
107
         * 
108
         * @param code
109
         * @throws CrsException
110
         */
111
        public Crs(String code) throws CrsException {
112
                String fullCode;
113
                setWKT(code);
114
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
115
                        fullCode = code;
116
                else fullCode = "EPSG:"+code;
117
                String cod = "";
118
                if(code.length() < 15 ) {
119
                        code = code.substring(code.indexOf(":")+1);
120
                        try {
121
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
122
                                crsWkt = new CrsWkt(fullCode);
123
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
124
                                oSRSSource.importFromEPSG(oSRSSource, 
125
                                                Integer.parseInt(code));
126
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
127
                                proj4 = oSRSSource.exportToProj4(oSRSSource);
128
                                crsProj4 = new CrsProj(proj4);
129
                                setName(fullCode);
130
                                setAbrev(fullCode);
131
                                
132
                        } catch (OGRException e) {
133
                                throw new CrsException(e);
134
                        }  catch (CrsOgrException e) {
135
                                throw new CrsException(e);
136
                        } catch (NumberFormatException e) {
137
                                // TODO Auto-generated catch block
138
                                e.printStackTrace();
139
                        } catch (CrsGdalException e) {
140
                                // TODO Auto-generated catch block
141
                                e.printStackTrace();
142
                        }
143
                }else {
144
                        String aux;
145
                        String code2 = "";
146
                        for(int i = 0; i < code.length(); i++) {
147
                                aux = ""+code.charAt(i);
148
                                if(!aux.equals(" ")) {
149
                                        code2+=aux;
150
                                }else {
151
                                        code2 += "";
152
                                }
153
                        }
154
                        crsWkt = new CrsWkt(code2);
155
                    try {
156
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
157
                            oSRSSource.importFromWkt(oSRSSource,code2);
158
                            proj4 =oSRSSource.exportToProj4(oSRSSource);
159
                                crsProj4 = new CrsProj(proj4);
160
                            setName(fullCode);
161
                            //setAbrev(crsWkt.getName());
162
                            setAbrev(crsWkt.getAuthority()[0]+":"+crsWkt.getAuthority()[1]);
163
                    } catch (OGRException e) {
164
                                throw new CrsException(e);
165
                        } catch (CrsOgrException e) {
166
                                throw new CrsException(e);
167
                        } catch (CrsGdalException e) {
168
                                // TODO Auto-generated catch block
169
                                e.printStackTrace();
170
                        }
171
                }
172
                //        Asignar el datum y el crs base (en el caso de ser projectado):
173
                if (!(crsWkt.getSpheroid()[1].equals("")||crsWkt.getSpheroid()[2].equals(""))){
174
                        double eSemiMajorAxis = Double.valueOf(crsWkt.getSpheroid()[1]).doubleValue();
175
                        double eIFlattening = Double.valueOf(crsWkt.getSpheroid()[2]).doubleValue();
176
                        datum = new CRSDatum(eSemiMajorAxis,eIFlattening);
177
                        
178
                        //crs base (en el caso de ser projectado):
179
                        if(this.isProjected()){
180
                                String proj4Base = "+proj=latlong +a=" + crsWkt.getSpheroid()[1] + " +rf=" + crsWkt.getSpheroid()[2] ;
181
                                crsBase = new CrsProj(proj4Base);
182
                        }
183
                }
184
        }        
185
        
186
        /**
187
         *Construye un CRS a partir del c?digo EPSG o de la cadena WKT.
188
         * 
189
         * @param epsg_cod
190
         * @param code
191
         * @throws CrsException
192
         */
193
        public Crs(int epsg_cod, String code) throws CrsException {
194
                String fullCode;
195
                setWKT(code);
196
                setCode(epsg_cod);
197
                if(code != null || code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
198
                        fullCode = code;
199
                else fullCode = "EPSG:"+code;
200
                String cod = "";
201
                if(code.length() < 15 ) {
202
                        code = code.substring(code.indexOf(":")+1);
203
                        try {
204
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
205
                                crsWkt = new CrsWkt(fullCode);
206
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
207
                                oSRSSource.importFromEPSG(oSRSSource, 
208
                                                Integer.parseInt(code));
209
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
210
                                proj4 = oSRSSource.exportToProj4(oSRSSource);
211
                                crsProj4 = new CrsProj(proj4);
212
                                setName(fullCode);
213
                                setAbrev(fullCode);
214
                                
215
                        } catch (OGRException e) {
216
                                throw new CrsException(e);
217
                        } catch (CrsOgrException e) {
218
                                throw new CrsException(e);
219
                        } catch (NumberFormatException e) {
220
                                // TODO Auto-generated catch block
221
                                e.printStackTrace();
222
                        } catch (CrsGdalException e) {
223
                                // TODO Auto-generated catch block
224
                                e.printStackTrace();
225
                        }
226
                }else {
227
                        String aux;
228
                        String code2 = "";
229
                        for(int i = 0; i < code.length(); i++) {
230
                                aux = ""+code.charAt(i);
231
                                if(!aux.equals(" ")) {
232
                                        code2+=aux;
233
                                }else {
234
                                        code2 += "";
235
                                }
236
                        }
237
                        crsWkt = new CrsWkt(code2);
238
                        /*
239
                         * Arreglo temporal para ver si funcionan de la iau2000 las proyecciones
240
                         * cilindrica equidistante y oblicua cilindrica equidistante
241
                         * que no estan en gdal, por lo que asignaremos directamente su cadena
242
                         * en proj4 para trabajar con ellas
243
                         */
244
                        if (!crsWkt.getProjection().equals("Equidistant_Cylindrical") && !crsWkt.getProjection().equals("Oblique_Cylindrical_Equal_Area")){
245
                            try {
246
                                    OGRSpatialReference oSRSSource = new OGRSpatialReference();
247
                                    oSRSSource.importFromWkt(oSRSSource,code2);
248
                                    proj4 = oSRSSource.exportToProj4(oSRSSource);
249
                                        crsProj4 = new CrsProj(proj4);
250
                                    setName(fullCode);
251
                                    //setAbrev(crsWkt.getName());
252
                                    setAbrev(crsWkt.getAuthority()[0]+":"+crsWkt.getAuthority()[1]);
253
                            } catch (OGRException e) {
254
                                        throw new CrsException(e);
255
                                } catch (CrsOgrException e) {
256
                                        throw new CrsException(e);
257
                                } catch (CrsGdalException e) {
258
                                        // TODO Auto-generated catch block
259
                                        e.printStackTrace();
260
                                }
261
                        }
262
                        else if (crsWkt.getProjection().equals("Equidistant_Cylindrical")){
263
                                String spheroid1 = crsWkt.getSpheroid()[1];
264
                                String spheroid2 = crsWkt.getSpheroid()[2];
265
                                String centralMeridian = "";
266
                                String falseNorthing = "";
267
                                String falseEasting = "";
268
                                String standardParallel1 = "0.0";
269
                                for (int i=0; i<crsWkt.getParam_name().length;i++){
270
                                        if (crsWkt.getParam_name()[i].equals("Central_Meridian"))
271
                                                centralMeridian = crsWkt.getParam_value()[i];        
272
                                        if (crsWkt.getParam_name()[i].equals("False_Easting"))
273
                                                falseEasting = crsWkt.getParam_value()[i];                                        
274
                                        if(crsWkt.getParam_name()[i].equals("False_Northing"))
275
                                                falseNorthing = crsWkt.getParam_value()[i];                                        
276
                                        if (crsWkt.getParam_name()[i].equals("Standard_Parallel_1"))
277
                                                standardParallel1 = crsWkt.getParam_value()[i];                                        
278
                                }
279
                                if (spheroid2.equals("0.0")){
280
                                        proj4 = "+proj=eqc +a=" + spheroid1 +
281
                                        " +lon_0="+ centralMeridian + " +x_0="+falseEasting+" +y_0="+falseNorthing +
282
                                        " +lat_ts="+standardParallel1;
283
                                } else {
284
                                        proj4 = "+proj=eqc +a="+ spheroid1 +" +rf=" + spheroid2 +
285
                                        " +lon_0="+ centralMeridian + " +x_0="+falseEasting+" +y_0="+falseNorthing +
286
                                        " +lat_ts="+standardParallel1;
287
                                }
288
                                
289
                                crsProj4 = new CrsProj(proj4);
290
                            setName(fullCode);
291
                            //setAbrev(crsWkt.getName());
292
                            setAbrev(crsWkt.getAuthority()[0]+":"+crsWkt.getAuthority()[1]);
293
                                
294
                        }
295
                        else if (crsWkt.getProjection().equals("Oblique_Cylindrical_Equal_Area")){
296
                                String spheroid1 = crsWkt.getSpheroid()[1];
297
                                String spheroid2 = crsWkt.getSpheroid()[2];
298
                                String centralMeridian = "";
299
                                String falseNorthing = "";
300
                                String falseEasting = "";
301
                                String standardParallel1 = "";
302
                                String standardParallel2 = "0.0";
303
                                for (int i=0; i<crsWkt.getParam_name().length;i++){                                        
304
                                        if (crsWkt.getParam_name()[i].equals("Central_Meridian"))
305
                                                centralMeridian = crsWkt.getParam_value()[i];        
306
                                        if (crsWkt.getParam_name()[i].equals("False_Easting"))
307
                                                falseEasting = crsWkt.getParam_value()[i];                                        
308
                                        if(crsWkt.getParam_name()[i].equals("False_Northing"))
309
                                                falseNorthing = crsWkt.getParam_value()[i];                                        
310
                                        if (crsWkt.getParam_name()[i].equals("Standard_Parallel_1"))
311
                                                standardParallel1 = crsWkt.getParam_value()[i];
312
                                        if (crsWkt.getParam_name()[i].equals("Standard_Parallel_2"))
313
                                                standardParallel2 = crsWkt.getParam_value()[i];                
314
                                }
315
                                if (spheroid2.equals("0.0")){
316
                                        proj4 = "+proj=ocea +a="+ spheroid1  +
317
                                        " +lon_0="+ centralMeridian + " +x_0="+falseEasting+" +y_0="+falseNorthing +
318
                                        " +lat_1="+standardParallel1+" +lat_2="+standardParallel2+" +lon_1=long_1" +
319
                                        " +lon_2=long_2 +no_defs";
320
                                } else {
321
                                        proj4 = "+proj=ocea +a="+ spheroid1 + " +rf=" + spheroid2 +
322
                                        " +lon_0="+ centralMeridian + " +x_0="+falseEasting+" +y_0="+falseNorthing +
323
                                        " +lat_1="+standardParallel1+" +lat_2="+standardParallel2+" +lon_1=long_1" +
324
                                        " +lon_2=long_2 +no_defs";
325
                                }
326
                                
327
                                crsProj4 = new CrsProj(proj4);
328
                            setName(fullCode);
329
                            //setAbrev(crsWkt.getName());
330
                            setAbrev(crsWkt.getAuthority()[0]+":"+crsWkt.getAuthority()[1]);
331
                        }
332
                }
333
                //        Asignar el datum:
334
                if (!(crsWkt.getSpheroid()[1].equals("")||crsWkt.getSpheroid()[2].equals(""))){
335
                        double eSemiMajorAxis = Double.valueOf(crsWkt.getSpheroid()[1]).doubleValue();
336
                        double eIFlattening = Double.valueOf(crsWkt.getSpheroid()[2]).doubleValue();
337
                        datum = new CRSDatum(eSemiMajorAxis,eIFlattening);
338
                        
339
                        // Crs base (en el caso de ser projectado):
340
                        if(this.isProjected()){
341
                                String proj4Base = "+proj=latlong +a=" + crsWkt.getSpheroid()[1] + " +rf=" + crsWkt.getSpheroid()[2] ;
342
                                crsBase = new CrsProj(proj4Base);
343
                        }
344
                }
345
        }
346
        
347
        /**
348
         * Construye un CRS a partir del c?digo EPSG o de la cadena WKT.
349
         * En el caso de WKT le a?ade a la cadena proj4 el string params.
350
         * 
351
         * @param epsg_cod
352
         * @param code
353
         * @param params
354
         * @throws CrsException
355
         */
356
        public Crs(int epsg_cod, String code,String params) throws CrsException {
357
                String fullCode;
358
                setCode(epsg_cod);
359
                setWKT(code);
360
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
361
                        fullCode = code;
362
                else fullCode = "EPSG:"+code;
363
                String cod = "";
364
                if(code.length() < 15 ) {
365
                        code = code.substring(code.indexOf(":")+1);
366
                        try {
367
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
368
                                crsWkt = new CrsWkt(fullCode);
369
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
370
                                oSRSSource.importFromEPSG(oSRSSource, 
371
                                                Integer.parseInt(code));
372
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
373
                                proj4 = oSRSSource.exportToProj4(oSRSSource);
374
                                crsProj4 = new CrsProj(proj4);
375
                                setName(fullCode);
376
                                setAbrev(fullCode);
377
                                
378
                        } catch (OGRException e) {
379
                                throw new CrsException(e);
380
                        } catch (CrsOgrException e) {
381
                                throw new CrsException(e);
382
                        } catch (NumberFormatException e) {
383
                                // TODO Auto-generated catch block
384
                                e.printStackTrace();
385
                        } catch (CrsGdalException e) {
386
                                // TODO Auto-generated catch block
387
                                e.printStackTrace();
388
                        }
389
                }else {
390
                        String aux;
391
                        String code2 = "";
392
                        for(int i = 0; i < code.length(); i++) {
393
                                aux = ""+code.charAt(i);
394
                                if(!aux.equals(" ")) {
395
                                        code2+=aux;
396
                                }else {
397
                                        code2 += "";
398
                                }
399
                        }
400
                        crsWkt = new CrsWkt(code2);
401
                    try {
402
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
403
                            oSRSSource.importFromWkt(oSRSSource,code2);
404
                            proj4 =oSRSSource.exportToProj4(oSRSSource)+params+" ";
405
                                crsProj4 = new CrsProj(proj4);
406
                            setName(fullCode);
407
                            //setAbrev(crsWkt.getName());
408
                            setAbrev(crsWkt.getAuthority()[0]+":"+crsWkt.getAuthority()[1]);
409
                    } catch (OGRException e) {
410
                                throw new CrsException(e);
411
                        } catch (CrsOgrException e) {
412
                                throw new CrsException(e);
413
                        } catch (CrsGdalException e) {
414
                                // TODO Auto-generated catch block
415
                                e.printStackTrace();
416
                        }
417
                }
418
                //        Asignar el datum:
419
                if (!(crsWkt.getSpheroid()[1].equals("")||crsWkt.getSpheroid()[2].equals(""))){
420
                        double eSemiMajorAxis = Double.valueOf(crsWkt.getSpheroid()[1]).doubleValue();
421
                        double eIFlattening = Double.valueOf(crsWkt.getSpheroid()[2]).doubleValue();
422
                        datum = new CRSDatum(eSemiMajorAxis,eIFlattening);
423
                        
424
                        // Crs base (en el caso de ser projectado):
425
                        if(this.isProjected()){
426
                                String proj4Base = "+proj=latlong +a=" + crsWkt.getSpheroid()[1] + " +rf=" + crsWkt.getSpheroid()[2] ;
427
                                crsBase = new CrsProj(proj4Base);
428
                        }
429
                }
430
        }
431
                
432
/*        public Crs(CrsEpsg source) throws CrsException {
433
                crsProj4 = source;
434
        }*/
435
        
436
        /**
437
         * @param code 
438
         */
439
        public void setTrans(String code) {
440
                trans = code;
441
                changeTrans(trans);
442
        }
443
        
444
        /**
445
         * 
446
         * @param code
447
         */
448
        public void changeTrans(String code) {
449
                crsProj4.changeStrCrs(code);
450
        }
451
        
452
        /**
453
         * 
454
         */
455
        public String getAbrev() {
456
                return abrev;
457
        }
458
        
459
        /**
460
         * 
461
         * @param code
462
         */
463
        protected void setAbrev(String code) {
464
                abrev = code;
465
        }
466
        
467
        /**
468
         * 
469
         * @param nom
470
         */
471
        public void setName(String nom) {
472
                name = nom;
473
        }
474
        
475
        /**
476
         * @return
477
         */
478
        public IDatum getDatum() {
479
                
480
                return datum;
481
        }
482
        
483
        /**
484
         * @return
485
         */
486
        public CrsWkt getCrsWkt() {
487
                return crsWkt;
488
        }
489
        
490
        /**
491
         * 
492
         * @param wkt
493
         */
494
        public void setWKT(String wkt){
495
                this.wkt = wkt;
496
        }
497
        
498
        /**
499
         * @return
500
         */
501
        public String getWKT(){
502
                return this.wkt;
503
        }
504
        
505
        /**
506
         * 
507
         * @param params
508
         */
509
        public void setTransformationParams(String sourceParams, String targetParams){
510
                this.sourceTrParams = sourceParams;
511
                this.targetTrParams = targetParams;
512
        }
513
        
514
        /**
515
         * @return
516
         */
517
        public String getTransformationParams(){
518
                return this.sourceTrParams;
519
        }
520
                
521
        /**
522
         * 
523
         * @return
524
         */
525
        public CrsProj getCrsProj() {
526
                return crsProj4;
527
        }
528

    
529
        /**
530
         * @param x
531
         * @param y
532
         * @return
533
         */
534
        public Point2D createPoint(double x, double y) {
535
                return new Point2D.Double(x,y);
536
        }
537

    
538
        /**
539
         * @param g
540
         * @param vp
541
         */
542
        public void drawGrid(Graphics2D g, ViewPortData vp) {
543
                // TODO Auto-generated method stub
544
                
545
        }
546

    
547
        /**
548
         * @param c
549
         */
550
        public void setGridColor(Color c) {
551
                gridColor = c;
552
        }
553

    
554
        /**
555
         * @return
556
         */
557
        public Color getGridColor() {
558
                return gridColor;
559
        }
560

    
561
        /**
562
         * @param dest
563
         * @return
564
         */
565
        public ICoordTrans getCT(IProjection dest) {
566
                
567
                
568
                try {
569
                        COperation operation = new COperation(this, (ICrs)dest,sourceTrParams,targetTrParams);
570
                        return operation;
571
                }catch (CrsException e) {
572
                        // TODO Auto-generated catch block
573
                        e.printStackTrace();
574
                        return null;
575
                }
576
                
577
                /*
578
                try {
579
                        operation = new COperation(this, (ICrs)dest);
580
                } catch (CrsException e) {
581
                        // TODO Auto-generated catch block
582
                        e.printStackTrace();
583
                }
584
                
585
                if (getTransformationParams()!=null){
586
                        if (isTargetParams())
587
                                operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
588
                        else
589
                                operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
590
                        
591
                        return operation;
592
                }
593
                
594
                return operation;        */        
595
        }
596
        
597
        /**
598
         * @param pt
599
         * @return
600
         */
601
        public Point2D toGeo(Point2D pt) {
602
                if (isProjected()){
603
                        double x[] = {pt.getX()};
604
                        double y[] = {pt.getY()};
605
                        double z[] = {0D};
606
                        try {
607
                                JNIBaseCrs.operate( x , y, z,
608
                                                crsProj4,crsBase);
609
                        } catch (OperationCrsException e) {
610
                                // TODO Auto-generated catch block
611
                                e.printStackTrace();
612
                        } catch (CrsProjException e) {
613
                                // TODO Auto-generated catch block
614
                                e.printStackTrace();
615
                        }
616
                        return new Point2D.Double(x[0],y[0]);
617
                }
618
                else
619
                        return pt;
620
        }
621

    
622
        /**
623
         * @param gPt
624
         * @param mPt
625
         * @return
626
         */
627
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
628
                // TODO Auto-generated method stub
629
                return null;
630
        }
631

    
632
        /**
633
         * @return
634
         */
635
        public boolean isProjected() {
636
                return !crsProj4.isLatlong();                
637
        }
638

    
639
        /**
640
         * @param minX
641
         * @param maxX
642
         * @param width
643
         * @param dpi
644
         * @return
645
         */
646
        public double getScale(double minX, double maxX, double width, double dpi) {
647
                double scale = 0D;
648
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
649
            scale = ((maxX - minX) * // grados
650

    
651
            // 1852.0 metros x minuto de meridiano
652
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
653
                    width; // pixels
654
        }
655
        else{
656
                 scale = ((maxX - minX) * // metros
657
                    (dpi / 2.54 * 100.0)) / // px / metro
658
                    width; // pixels
659
        }
660
        return scale;
661
        }
662
        
663
        public double getScale(double minX, double maxX, double minY, double maxY, double width, double dpi) {
664
                
665
                double scale = 0D;
666
                double incX = (maxX-minX);
667
                
668
                if (!isProjected()) {
669
                        double a = getDatum().getESemiMajorAxis();
670
                        double invF = getDatum().getEIFlattening();
671
                        double meanY = (minY+maxY)/2.0;
672
                        double radius = 0.0;
673
                        
674
                        
675
                        if (invF == Double.POSITIVE_INFINITY){
676
                                radius = a;
677
                        }
678
                        else{
679
                                double e2 = 2.0/invF-Math.pow(1.0/invF,2.0);
680
                                radius = a/Math.sqrt(1.0-e2*Math.pow(Math.sin(meanY*Math.PI/180.0),2.0))*Math.cos(meanY*Math.PI/180.0);
681
                        }
682
                        incX *= Math.PI/180.0*radius;
683
                }
684
                        
685
                scale = (incX * // metros
686
                                (dpi / 2.54 * 100.0)) / // px / metro
687
                                        width; // pixels
688
                
689
        return scale;
690
        }
691
        
692
        /**
693
         * 
694
         * @param epsg_cod
695
         */
696
        public void setCode(int epsg_cod){
697
                epsg_code = epsg_cod;
698
        }
699

    
700
        /**
701
         * @return
702
         */
703
        public int getCode() {
704
                // TODO Auto-generated method stub
705
                return epsg_code;
706
        }
707

    
708
        /**
709
         * 
710
         */
711
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double changeUnits, double dpi) {
712
                // TODO Auto-generated method stub
713
                return null;
714
        }
715

    
716
        /**
717
         * 
718
         * @return
719
         */
720
        public String getProj4String() {
721
                return proj4;
722
        }
723

    
724

    
725
        public String getFullCode() {
726
                // TODO Auto-generated method stub
727
                return null;
728
        }
729
        
730
}