Statistics
| Revision:

root / trunk / libraries / libJCRS / src / org / gvsig / crs / Crs.java @ 8001

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

    
316
        public Point2D createPoint(double x, double y) {
317
                return new Point2D.Double(x,y);
318
        }
319

    
320
        public void drawGrid(Graphics2D g, ViewPortData vp) {
321
                // TODO Auto-generated method stub
322
                
323
        }
324

    
325
        public void setGridColor(Color c) {
326
                gridColor = c;
327
        }
328

    
329
        public Color getGridColor() {
330
                return gridColor;
331
        }
332

    
333
        public ICoordTrans getCT(IProjection dest) {
334
                ICrs crs = (ICrs) dest;                
335
                try {
336
                        if (!this.getNadTarget().equals("")) {
337
                                crs = new Crs(crs.getCode(), crs.getWKT(), this.getNadTarget());
338
                                return new COperation(this, crs);
339
                        }
340
                        else return new COperation(this, crs);
341
                } catch (CrsException e) {
342
                        // TODO Auto-generated catch block
343
                        e.printStackTrace();
344
                }
345
                return null;
346
        }
347
        
348
        public Point2D toGeo(Point2D pt) {
349
                if (isProjected()){
350
                        double x[] = {pt.getX()};
351
                        double y[] = {pt.getY()};
352
                        double z[] = {0D};
353
                        try {
354
                                OperationCrs.operate( x , y, z,
355
                                                crsProj4,crsBase);
356
                        } catch (OperationCrsException e) {
357
                                // TODO Auto-generated catch block
358
                                e.printStackTrace();
359
                        } catch (CrsEpsgException e) {
360
                                // TODO Auto-generated catch block
361
                                e.printStackTrace();
362
                        }
363
                        return new Point2D.Double(x[0],y[0]);
364
                }
365
                else
366
                        return pt;
367
        }
368

    
369
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
370
                // TODO Auto-generated method stub
371
                return null;
372
        }
373

    
374
        public boolean isProjected() {
375
                return !crsProj4.isLatlong();
376
        }
377

    
378
        public double getScale(double minX, double maxX, double width, double dpi) {
379
                double scale = 0D;
380
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
381
            scale = ((maxX - minX) * // grados
382

    
383
            // 1852.0 metros x minuto de meridiano
384
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
385
                    width; // pixels
386
        }
387
        else{
388
                 scale = ((maxX - minX) * // metros
389
                    (dpi / 2.54 * 100.0)) / // px / metro
390
                    width; // pixels
391
        }
392
        return scale;
393
        }
394
        
395
        public void setCode(int epsg_cod){
396
                epsg_code = epsg_cod;
397
        }
398

    
399
        public int getCode() {
400
                // TODO Auto-generated method stub
401
                return epsg_code;
402
        }
403

    
404
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double changeUnits, double dpi) {
405
                // TODO Auto-generated method stub
406
                return null;
407
        }
408
        
409
}