Statistics
| Revision:

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

History | View | Annotate | Download (8.44 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.geotools.referencing.crs.AbstractDerivedCRS;
53
import org.geotools.referencing.crs.AbstractSingleCRS;
54
import org.geotools.referencing.datum.DefaultGeodeticDatum;
55
import org.gvsig.crs.proj.CrsProj;
56
import org.gvsig.crs.proj.CrsProjException;
57
import org.gvsig.crs.proj.JNIBaseCrs;
58
import org.gvsig.crs.proj.OperationCrsException;
59
import org.opengis.referencing.crs.CoordinateReferenceSystem;
60

    
61
/**
62
 * Clase que representa un CRS basado en GeoTools/GeoApi.
63
 * 
64
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
65
 *
66
 */
67
public class CrsGT implements ICrs {
68
        private static final Color basicGridColor = new Color(64, 64, 64, 128);
69
        
70
        /**
71
         * CRS GeoApi. Nucleo de la clare CrsGT.
72
         */
73
        private CoordinateReferenceSystem         crsGT                        = null;
74
        
75
        /**
76
         *Cadena proj4
77
         */
78
        private String                                                 proj4String         = null;
79
        
80
        private Color                                                 gridColor                 = basicGridColor;
81
        
82
        /**
83
         * Par?metros de transformaci?n para el CRS fuente en formato proj4.
84
         */
85
        private String                                                 SourceTrParams                        = null;
86
        
87
        /**
88
         * Par?metros de transformaci?n para el CRS destino en formato proj4.
89
         */
90
        private String                                                 TargetTrParams                        = null;
91
        
92
        /**
93
         * CRS operable con proj4.
94
         */
95
        private CrsProj                                         crsProj                        = null;
96
        
97
        /**
98
         * CRS Base operable con proj4.
99
         */
100
        private CrsProj                                         crsProjBase                = null;
101
        
102
        /**
103
         * Provisional, hasta que se elimine getCrsWkt de ICrs.
104
         */
105
        private CrsWkt                                                 crsWkt                        = null;
106
        
107
        /**
108
         * Conversor de CRSs a cadenas proj4.
109
         */
110
        private Proj4                                                 proj4                        = null;
111
        
112
        
113
        
114
        /**
115
         * Constructor a partir de un CoordinateReferenceSystem
116
         * 
117
         * @param crsGT
118
         * @throws CrsProjException 
119
         */
120
        public CrsGT(CoordinateReferenceSystem crsGT){
121
                this.crsGT = crsGT;
122
        }
123

    
124
        public int getCode() {
125
                return Integer.valueOf(getAbrev().split(":")[1]).intValue();
126
        }
127

    
128
        public CrsWkt getCrsWkt() {
129
                if (crsWkt==null)
130
                        crsWkt = new CrsWkt(crsGT);
131
                return crsWkt;
132
        }
133

    
134
        public String getWKT() {
135
                return crsGT.toWKT();
136
        }
137

    
138
        public void setTransformationParams(String SourceParams, String TargetParams) {
139
                this.SourceTrParams = SourceParams;
140
                this.TargetTrParams = TargetParams;
141
        }
142

    
143
        public Point2D createPoint(double x, double y) {
144
                // TODO Auto-generated method stub
145
                return null;
146
        }
147

    
148
        public void drawGrid(Graphics2D g, ViewPortData vp) {
149
                // TODO Auto-generated method stub
150

    
151
        }
152

    
153
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
154
                // TODO Auto-generated method stub
155
                return null;
156
        }
157

    
158
        public String getAbrev() {                
159
                return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
160
        }
161

    
162
        public ICoordTrans getCT(IProjection dest) {
163
                
164
                try {
165
                        COperation operation = new COperation(this, (ICrs)dest,SourceTrParams,TargetTrParams);
166
                        return operation;
167
                }catch (CrsException e) {
168
                        // TODO Auto-generated catch block
169
                        e.printStackTrace();
170
                        return null;
171
                }
172
                
173
                /*
174
                try {
175
                        operation = new COperation(this, (ICrs)dest);
176
                } catch (CrsException e) {
177
                        // TODO Auto-generated catch block
178
                        e.printStackTrace();
179
                }
180
                
181
                if (!getTransformationParams().equals("")){
182
                        if (isParamsInTarget())
183
                                operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
184
                        else
185
                                operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
186
                        return operation;
187
                }
188
                
189
                return operation;        */
190
        }
191

    
192
        public IDatum getDatum() {
193
                DefaultGeodeticDatum datumGT = (DefaultGeodeticDatum)((AbstractSingleCRS)crsGT).getDatum();
194
                CRSDatum datum = new CRSDatum(datumGT.getEllipsoid().getSemiMajorAxis(),datumGT.getEllipsoid().getInverseFlattening());
195
                return datum;
196
        }
197

    
198
        public Rectangle2D getExtent(Rectangle2D extent, double scale,
199
                        double wImage, double hImage, double changeUnits, double dpi) {
200
                // TODO Auto-generated method stub
201
                return null;
202
        }
203

    
204
        public Color getGridColor() {
205
                return gridColor;
206
        }
207

    
208
        public double getScale(double minX, double maxX, double width, double dpi) {
209
                double scale = 0D;
210
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
211
            scale = ((maxX - minX) * // grados
212

    
213
            // 1852.0 metros x minuto de meridiano
214
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
215
                    width; // pixels
216
        }
217
        else{
218
                 scale = ((maxX - minX) * // metros
219
                    (dpi / 2.54 * 100.0)) / // px / metro
220
                    width; // pixels
221
        }
222
        return scale;
223
        }
224
        
225
        public double getScale(double minX, double maxX, double minY, double maxY, double width, double dpi) {
226
                
227
                double scale = 0D;
228
                double incX = (maxX-minX);
229
                
230
                if (!isProjected()) {
231
                        double a = getDatum().getESemiMajorAxis();
232
                        double invF = getDatum().getEIFlattening();
233
                        double meanY = (minY+maxY)/2.0;
234
                        double radius = 0.0;
235
                        
236
                        
237
                        if (invF == Double.POSITIVE_INFINITY){
238
                                radius = a;
239
                        }
240
                        else{
241
                                double e2 = 2.0/invF-Math.pow(1.0/invF,2.0);
242
                                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);
243
                        }
244
                        incX *= Math.PI/180.0*radius;
245
                }
246
                        
247
                scale = (incX * // metros
248
                                (dpi / 2.54 * 100.0)) / // px / metro
249
                                        width; // pixels
250
                
251
        return scale;
252
        }
253

    
254
        public boolean isProjected() {
255
                if (crsGT instanceof AbstractDerivedCRS){
256
                        return true;
257
                }else
258
                        return false;
259
        }
260

    
261
        public void setGridColor(Color c) {
262
                gridColor = c;
263
        }
264

    
265
        public Point2D toGeo(Point2D pt) {
266
                if (isProjected()){
267
                        double x[] = {pt.getX()};
268
                        double y[] = {pt.getY()};
269
                        double z[] = {0D};
270
                        try {
271
                                JNIBaseCrs.operate( x , y, z,
272
                                                getCrsProj(),getCrsProjBase());
273
                        } catch (OperationCrsException e) {
274
                                // TODO Auto-generated catch block
275
                                e.printStackTrace();
276
                        } catch (CrsProjException e) {
277
                                // TODO Auto-generated catch block
278
                                e.printStackTrace();
279
                        }
280
                        return new Point2D.Double(x[0],y[0]);
281
                }
282
                else
283
                        return pt;
284
        }
285
        
286
        /**
287
         * @param targetParam
288
         */
289
        /*public void setParamsInTarget(boolean targetParam) {
290
                this.paramsInTarget = targetParam;
291
        }*/
292
        
293
        /**
294
         * 
295
         * @return Cadena proj4 Correspondiente al CRS.
296
         */
297
        public String getProj4String() {
298
                if (proj4String == null)
299
                        try {
300
                                proj4String = getProj4().exportToProj4(crsGT);
301
                        } catch (CrsException e) {
302
                                // TODO Auto-generated catch block
303
                                e.printStackTrace();
304
                        }
305
                return proj4String;
306
        }
307

    
308
        public CoordinateReferenceSystem getCrsGT() {
309
                return crsGT;
310
        }
311
        
312
        public CrsProj getCrsProj() {
313
                if (crsProj == null)
314
                        crsProj = new CrsProj(getProj4String()); 
315
                return crsProj;
316
        }
317

    
318
        private CrsProj getCrsProjBase() {
319
                if (crsProjBase == null){
320
                        AbstractDerivedCRS derivedCRS = (AbstractDerivedCRS)crsGT;
321
                        try {
322
                                crsProjBase = new CrsProj(getProj4().exportToProj4(derivedCRS.getBaseCRS()));
323
                        } catch (CrsException e) {
324
                                // TODO Auto-generated catch block
325
                                e.printStackTrace();
326
                        }
327
                }
328
                return crsProjBase;
329
        }
330

    
331
        private Proj4 getProj4() {
332
                if (proj4 == null)
333
                        try {
334
                                proj4 = new Proj4();
335
                        } catch (CrsException e) {
336
                                // TODO Auto-generated catch block
337
                                e.printStackTrace();
338
                        }
339
                return proj4;
340
        }
341

    
342
        public String getFullCode() {
343
                // TODO Auto-generated method stub
344
                return null;
345
        }
346

    
347
}