Statistics
| Revision:

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

History | View | Annotate | Download (7.4 KB)

1
package org.gvsig.crs;
2

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

    
7
import org.cresques.cts.ICoordTrans;
8
import org.cresques.cts.IDatum;
9
import org.cresques.cts.IProjection;
10
import org.cresques.geo.ViewPortData;
11
import org.gvsig.crs.epsg.CrsEpsg;
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

    
18
public class Crs implements ICrs {
19
        private String proj4;
20
        private String trans;
21
        //private String transOrigin = "";
22
        private String abrev;
23
        private String name = "";
24
        private CrsEpsg crsProj4;
25
        private CrsWkt crsWkt;
26
        private int epsg_code = 23030;
27

    
28
        public Crs(String code) throws CrsException {
29
                String fullCode;
30
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
31
                        fullCode = code;
32
                else fullCode = "EPSG:"+code;
33
                String cod = "";
34
                if(code.length() < 15 ) {
35
                        code = code.substring(code.indexOf(":")+1);
36
                        try {
37
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
38
                                crsWkt = new CrsWkt(fullCode);
39
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
40
                                CrsOgr.importFromEPSG(oSRSSource, 
41
                                                Integer.parseInt(code));
42
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
43
                                proj4 = CrsOgr.exportToProj4(oSRSSource);
44
                                crsProj4 = new CrsEpsg(proj4);
45
                                setName(fullCode);
46
                                setAbrev(fullCode);
47
                                
48
                        } catch (OGRException e) {
49
                                throw new CrsException(e);
50
                        } catch (crsgdalException e) {
51
                                throw new CrsException(e);
52
                        } catch (CrsOgrException e) {
53
                                throw new CrsException(e);
54
                        }
55
                }else {
56
                        String aux;
57
                        String code2 = "";
58
                        for(int i = 0; i < code.length(); i++) {
59
                                aux = ""+code.charAt(i);
60
                                if(!aux.equals(" ")) {
61
                                        code2+=aux;
62
                                }else {
63
                                        code2 += "";
64
                                }
65
                        }
66
                        crsWkt = new CrsWkt(code2);
67
                    try {
68
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
69
                            CrsOgr.importFromWkt(oSRSSource,code2);
70
                            proj4 =CrsOgr.exportToProj4(oSRSSource);
71
                                crsProj4 = new CrsEpsg(proj4);
72
                            setName(fullCode);
73
                            setAbrev(crsWkt.getName());
74
                    } catch (OGRException e) {
75
                                throw new CrsException(e);
76
                        } catch (crsgdalException e) {
77
                                throw new CrsException(e);
78
                        } catch (CrsOgrException e) {
79
                                throw new CrsException(e);
80
                        }
81
                }
82
        }        
83
        
84
        public Crs(int epsg_cod, String code) throws CrsException {
85
                String fullCode;
86
                setCode(epsg_cod);
87
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
88
                        fullCode = code;
89
                else fullCode = "EPSG:"+code;
90
                String cod = "";
91
                if(code.length() < 15 ) {
92
                        code = code.substring(code.indexOf(":")+1);
93
                        try {
94
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
95
                                crsWkt = new CrsWkt(fullCode);
96
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
97
                                CrsOgr.importFromEPSG(oSRSSource, 
98
                                                Integer.parseInt(code));
99
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
100
                                proj4 = CrsOgr.exportToProj4(oSRSSource);
101
                                crsProj4 = new CrsEpsg(proj4);
102
                                setName(fullCode);
103
                                setAbrev(fullCode);
104
                                
105
                        } catch (OGRException e) {
106
                                throw new CrsException(e);
107
                        } catch (crsgdalException e) {
108
                                throw new CrsException(e);
109
                        } catch (CrsOgrException e) {
110
                                throw new CrsException(e);
111
                        }
112
                }else {
113
                        String aux;
114
                        String code2 = "";
115
                        for(int i = 0; i < code.length(); i++) {
116
                                aux = ""+code.charAt(i);
117
                                if(!aux.equals(" ")) {
118
                                        code2+=aux;
119
                                }else {
120
                                        code2 += "";
121
                                }
122
                        }
123
                        crsWkt = new CrsWkt(code2);
124
                    try {
125
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
126
                            CrsOgr.importFromWkt(oSRSSource,code2);
127
                            proj4 = CrsOgr.exportToProj4(oSRSSource);
128
                                crsProj4 = new CrsEpsg(proj4);
129
                            setName(fullCode);
130
                            setAbrev(crsWkt.getName());
131
                    } catch (OGRException e) {
132
                                throw new CrsException(e);
133
                        } catch (crsgdalException e) {
134
                                throw new CrsException(e);
135
                        } catch (CrsOgrException e) {
136
                                throw new CrsException(e);
137
                        }
138
                }
139
        }
140
        
141
        public Crs(int epsg_cod, String code,String params) throws CrsException {
142
                String fullCode;
143
                setCode(epsg_cod);
144
                if(code.charAt(0) == 'E' || code.charAt(0) == 'G' || code.charAt(0) == 'P')
145
                        fullCode = code;
146
                else fullCode = "EPSG:"+code;
147
                String cod = "";
148
                if(code.length() < 15 ) {
149
                        code = code.substring(code.indexOf(":")+1);
150
                        try {
151
                                //Creamos el objeto que tendra los diferentes parametros de la cadena Wkt
152
                                crsWkt = new CrsWkt(fullCode);
153
                                OGRSpatialReference oSRSSource = new OGRSpatialReference();
154
                                CrsOgr.importFromEPSG(oSRSSource, 
155
                                                Integer.parseInt(code));
156
                                //Guardamos la cadena Proj4 devuelva por la OGRSpatialReference
157
                                proj4 = CrsOgr.exportToProj4(oSRSSource);
158
                                crsProj4 = new CrsEpsg(proj4);
159
                                setName(fullCode);
160
                                setAbrev(fullCode);
161
                                
162
                        } catch (OGRException e) {
163
                                throw new CrsException(e);
164
                        } catch (crsgdalException e) {
165
                                throw new CrsException(e);
166
                        } catch (CrsOgrException e) {
167
                                throw new CrsException(e);
168
                        }
169
                }else {
170
                        String aux;
171
                        String code2 = "";
172
                        for(int i = 0; i < code.length(); i++) {
173
                                aux = ""+code.charAt(i);
174
                                if(!aux.equals(" ")) {
175
                                        code2+=aux;
176
                                }else {
177
                                        code2 += "";
178
                                }
179
                        }
180
                        crsWkt = new CrsWkt(code2);
181
                    try {
182
                            OGRSpatialReference oSRSSource = new OGRSpatialReference();
183
                            CrsOgr.importFromWkt(oSRSSource,code2);
184
                            proj4 =CrsOgr.exportToProj4(oSRSSource)+params;
185
                                crsProj4 = new CrsEpsg(proj4);
186
                            setName(fullCode);
187
                            setAbrev(crsWkt.getName());
188
                    } catch (OGRException e) {
189
                                throw new CrsException(e);
190
                        } catch (crsgdalException e) {
191
                                throw new CrsException(e);
192
                        } catch (CrsOgrException e) {
193
                                throw new CrsException(e);
194
                        }
195
                }
196
        }
197
        
198
        public Crs(CrsEpsg source) throws CrsException {
199
                crsProj4 = source;
200
        }
201
        
202
        public void setTrans(String code) {
203
                trans = code;
204
                changeTrans(trans);
205
        }
206
        
207
        public void changeTrans(String code) {
208
                crsProj4.changeStrCrs(code);
209
        }
210
        
211
        public String getAbrev() {
212
                return abrev;
213
        }
214
        
215
        protected void setAbrev(String code) {
216
                abrev = code;
217
        }
218
        
219
        public void setName(String nom) {
220
                name = nom;
221
        }
222
        
223
        public IDatum getDatum() {
224
                return null;
225
        }
226
        
227
        public CrsWkt getCrsWkt() {
228
                return crsWkt;
229
        }
230
        
231
        public ICOperation getCOp(ICrs target) throws CrsException {
232
                return new COperation(this, target);
233
        }
234
        
235
        protected CrsEpsg getCrsEpsg() {
236
                return crsProj4;
237
        }
238

    
239
        public Point2D createPoint(double x, double y) {
240
                return new Point2D.Double(x,y);
241
        }
242

    
243
        public void drawGrid(Graphics2D g, ViewPortData vp) {
244
                // TODO Auto-generated method stub
245
                
246
        }
247

    
248
        public void setGridColor(Color c) {
249
                // TODO Auto-generated method stub
250
                
251
        }
252

    
253
        public Color getGridColor() {
254
                // TODO Auto-generated method stub
255
                return null;
256
        }
257

    
258
        public ICoordTrans getCT(IProjection dest) {
259
                try {
260
                        return new COperation(this, (ICrs) dest);
261
                } catch (CrsException e) {
262
                        // TODO Auto-generated catch block
263
                        e.printStackTrace();
264
                }
265
                return null;
266
        }
267

    
268
        public Point2D toGeo(Point2D pt) {
269
                // TODO Auto-generated method stub
270
                return null;
271
        }
272

    
273
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
274
                // TODO Auto-generated method stub
275
                return null;
276
        }
277

    
278
        public boolean isProjected() {
279
                return !crsProj4.isLongLat();
280
        }
281

    
282
        public double getScale(double minX, double maxX, double width, double dpi) {
283
                // TODO Auto-generated method stub
284
                return -1;
285
        }
286
        
287
        public void setCode(int epsg_cod){
288
                epsg_code = epsg_cod;
289
        }
290

    
291
        public int getCode() {
292
                // TODO Auto-generated method stub
293
                return epsg_code;
294
        }
295
}