Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libJCRS / src / org / gvsig / crs / CrsGT.java @ 21314

History | View | Annotate | Download (9.59 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
import sun.rmi.runtime.GetThreadPoolAction;
62
import sun.rmi.transport.Target;
63

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

    
127
        public int getCode() {
128
                return Integer.valueOf(getAbrev().split(":")[1]).intValue();
129
        }
130

    
131
        public CrsWkt getCrsWkt() {
132
                if (crsWkt==null)
133
                        crsWkt = new CrsWkt(crsGT);
134
                return crsWkt;
135
        }
136

    
137
        public String getWKT() {
138
                return crsGT.toWKT();
139
        }
140

    
141
        public void setTransformationParams(String SourceParams, String TargetParams) {
142
                this.sourceTrParams = SourceParams;
143
                this.targetTrParams = TargetParams;
144
        }
145
        
146
        /**
147
         * Devuelve los parametros de la transformacion del crs fuente
148
         * @return
149
         */
150
        public String getSourceTransformationParams() {
151
                return this.sourceTrParams;
152
        }
153
        
154
        /**
155
         * Devuelve los parametros de la transformacion del crs destino
156
         * @return
157
         */
158
        public String getTargetTransformationParams() {
159
                return this.targetTrParams;
160
        }
161

    
162
        public Point2D createPoint(double x, double y) {
163
                return new Point2D.Double(x,y);
164
        }
165

    
166
        public void drawGrid(Graphics2D g, ViewPortData vp) {
167
                // TODO Auto-generated method stub
168

    
169
        }
170

    
171
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
172
                // TODO Auto-generated method stub
173
                return null;
174
        }
175

    
176
        public String getAbrev() {                
177
                return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
178
        }
179

    
180
        public ICoordTrans getCT(IProjection dest) {
181
                
182
                try {
183
                        COperation operation = null;
184
                        if(((ICrs)dest).getSourceTransformationParams() != null || ((ICrs)dest).getTargetTransformationParams() != null) 
185
                                operation = new COperation(this, (ICrs)dest,((ICrs)dest).getTargetTransformationParams(),((ICrs)dest).getSourceTransformationParams());
186
                        else
187
                                operation = new COperation(this, (ICrs)dest,sourceTrParams,targetTrParams);
188
                        return operation;
189
                }catch (CrsException e) {
190
                        // TODO Auto-generated catch block
191
                        e.printStackTrace();
192
                        return null;
193
                }
194
                
195
                /*
196
                try {
197
                        operation = new COperation(this, (ICrs)dest);
198
                } catch (CrsException e) {
199
                        // TODO Auto-generated catch block
200
                        e.printStackTrace();
201
                }
202
                
203
                if (!getTransformationParams().equals("")){
204
                        if (isParamsInTarget())
205
                                operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
206
                        else
207
                                operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
208
                        return operation;
209
                }
210
                
211
                return operation;        */
212
        }
213

    
214
        public IDatum getDatum() {
215
                DefaultGeodeticDatum datumGT = (DefaultGeodeticDatum)((AbstractSingleCRS)crsGT).getDatum();
216
                CRSDatum datum = new CRSDatum(datumGT.getEllipsoid().getSemiMajorAxis(),datumGT.getEllipsoid().getInverseFlattening());
217
                return datum;
218
        }
219

    
220
        public Rectangle2D getExtent(Rectangle2D extent, double scale,
221
                        double wImage, double hImage, double changeUnits, double dpi) {
222
                // TODO Auto-generated method stub
223
                return null;
224
        }
225

    
226
        public Color getGridColor() {
227
                return gridColor;
228
        }
229

    
230
        public double getScale(double minX, double maxX, double width, double dpi) {
231
                double scale = 0D;
232
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
233
            scale = ((maxX - minX) * // grados
234

    
235
            // 1852.0 metros x minuto de meridiano
236
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
237
                    width; // pixels
238
        }
239
        else{
240
                 scale = ((maxX - minX) * // metros
241
                    (dpi / 2.54 * 100.0)) / // px / metro
242
                    width; // pixels
243
        }
244
        return scale;
245
        }
246
        
247
        public double getScale(double minX, double maxX, double minY, double maxY, double width, double dpi) {
248
                
249
                double scale = 0D;
250
                double incX = (maxX-minX);
251
                
252
                if (!isProjected()) {
253
                        double a = getDatum().getESemiMajorAxis();
254
                        double invF = getDatum().getEIFlattening();
255
                        double meanY = (minY+maxY)/2.0;
256
                        double radius = 0.0;
257
                        
258
                        
259
                        if (invF == Double.POSITIVE_INFINITY){
260
                                radius = a;
261
                        }
262
                        else{
263
                                double e2 = 2.0/invF-Math.pow(1.0/invF,2.0);
264
                                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);
265
                        }
266
                        incX *= Math.PI/180.0*radius;
267
                }
268
                        
269
                scale = (incX * // metros
270
                                (dpi / 2.54 * 100.0)) / // px / metro
271
                                        width; // pixels
272
                
273
        return scale;
274
        }
275

    
276
        public boolean isProjected() {
277
                if (crsGT instanceof AbstractDerivedCRS){
278
                        return true;
279
                }else
280
                        return false;
281
        }
282

    
283
        public void setGridColor(Color c) {
284
                gridColor = c;
285
        }
286

    
287
        public Point2D toGeo(Point2D pt) {
288
                if (isProjected()){
289
                        double x[] = {pt.getX()};
290
                        double y[] = {pt.getY()};
291
                        double z[] = {0D};
292
                        try {
293
                                JNIBaseCrs.operate( x , y, z,
294
                                                getCrsProj(),getCrsProjBase());
295
                        } catch (OperationCrsException e) {
296
                                // TODO Auto-generated catch block
297
                                e.printStackTrace();
298
                        }
299
                        return new Point2D.Double(x[0],y[0]);
300
                }
301
                else
302
                        return pt;
303
        }
304
        
305
        /**
306
         * @param targetParam
307
         */
308
        /*public void setParamsInTarget(boolean targetParam) {
309
                this.paramsInTarget = targetParam;
310
        }*/
311
        
312
        /**
313
         * 
314
         * @return Cadena proj4 Correspondiente al CRS.
315
         * @throws CrsException 
316
         */
317
        public String getProj4String() throws CrsException {
318
                if (proj4String == null)
319
                proj4String = getProj4().exportToProj4(crsGT);
320
                        
321
                return proj4String;
322
        }
323

    
324
        public CoordinateReferenceSystem getCrsGT() {
325
                return crsGT;
326
        }
327
        
328
        public CrsProj getCrsProj() {
329
                if (crsProj == null)
330
                        try {
331
                                crsProj = new CrsProj(getProj4String());
332
                        } catch (CrsException e) {
333
                                // TODO Auto-generated catch block
334
                                e.printStackTrace();
335
                        } 
336
                return crsProj;
337
        }
338

    
339
        private CrsProj getCrsProjBase() {
340
                if (crsProjBase == null){
341
                        AbstractDerivedCRS derivedCRS = (AbstractDerivedCRS)crsGT;
342
                        try {
343
                                crsProjBase = new CrsProj(getProj4().exportToProj4(derivedCRS.getBaseCRS()));
344
                        } catch (CrsException e) {
345
                                // TODO Auto-generated catch block
346
                                e.printStackTrace();
347
                        }
348
                }
349
                return crsProjBase;
350
        }
351

    
352
        private Proj4 getProj4() {
353
                if (proj4 == null)
354
                        try {
355
                                proj4 = new Proj4();
356
                        } catch (CrsException e) {
357
                                // TODO Auto-generated catch block
358
                                e.printStackTrace();
359
                        }
360
                return proj4;
361
        }
362
        
363
        /**
364
         * @return Authority:code:proj@Sourceparam@TargerParam@
365
         */
366
        public String getFullCode() {
367
                if (this.sourceTrParams == null && this.targetTrParams == null)
368
                        return getAbrev();
369
                String sourceParams = "";
370
                String targetParams = "";
371
                if (sourceTrParams != null)
372
                        sourceParams = sourceTrParams;
373
                if (targetTrParams != null)
374
                        targetParams = targetTrParams;
375
                
376
                return getAbrev()+":proj@"+sourceParams+"@"+targetParams;
377
        }
378

    
379
        public Rectangle2D getExtent(Rectangle2D arg0, double arg1, double arg2, double arg3, double arg4, double arg5, double arg6) {
380
                // TODO Auto-generated method stub
381
                return null;
382
        }
383

    
384
}