Statistics
| Revision:

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

History | View | Annotate | Download (9.32 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.ogr.CrsOgr;
14
import org.gvsig.crs.ogr.CrsOgrException;
15
import org.gvsig.crs.ogr.OGRException;
16
import org.gvsig.crs.ogr.OGRSpatialReference;
17
import org.gvsig.crs.ogr.crsgdalException;
18

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

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

    
290
        public Point2D createPoint(double x, double y) {
291
                return new Point2D.Double(x,y);
292
        }
293

    
294
        public void drawGrid(Graphics2D g, ViewPortData vp) {
295
                // TODO Auto-generated method stub
296
                
297
        }
298

    
299
        public void setGridColor(Color c) {
300
                gridColor = c;
301
        }
302

    
303
        public Color getGridColor() {
304
                return gridColor;
305
        }
306

    
307
        public ICoordTrans getCT(IProjection dest) {
308
                ICrs crs = (ICrs) dest;                
309
                try {
310
                        if (!this.getNadTarget().equals("")) {
311
                                crs = new Crs(crs.getCode(), crs.getWKT(), this.getNadTarget());
312
                                return new COperation(this, crs);
313
                        }
314
                        else return new COperation(this, crs);
315
                } catch (CrsException e) {
316
                        // TODO Auto-generated catch block
317
                        e.printStackTrace();
318
                }
319
                return null;
320
        }
321

    
322
        public Point2D toGeo(Point2D pt) {
323
                // TODO Auto-generated method stub
324
                return pt;
325
        }
326

    
327
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
328
                // TODO Auto-generated method stub
329
                return null;
330
        }
331

    
332
        public boolean isProjected() {
333
                return !crsProj4.isLongLat();
334
        }
335

    
336
        public double getScale(double minX, double maxX, double width, double dpi) {
337
                // TODO Auto-generated method stub
338
                return -1;
339
        }
340
        
341
        public void setCode(int epsg_cod){
342
                epsg_code = epsg_cod;
343
        }
344

    
345
        public int getCode() {
346
                // TODO Auto-generated method stub
347
                return epsg_code;
348
        }
349

    
350
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double changeUnits, double dpi) {
351
                // TODO Auto-generated method stub
352
                return null;
353
        }
354
        
355
}