Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libJCRS / src / org / gvsig / crs / CrsGT.java @ 29000

History | View | Annotate | Download (9.98 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
        /**
144
         * Devuelve los parametros de la transformacion del crs fuente
145
         * @return
146
         */
147
        public String getSourceTransformationParams() {
148
                return this.sourceTrParams;
149
        }
150
        
151
        /**
152
         * Devuelve los parametros de la transformacion del crs destino
153
         * @return
154
         */
155
        public String getTargetTransformationParams() {
156
                return this.targetTrParams;
157
        }
158

    
159
        public Point2D createPoint(double x, double y) {
160
                return new Point2D.Double(x,y);
161
        }
162

    
163
        public void drawGrid(Graphics2D g, ViewPortData vp) {
164
                // TODO Auto-generated method stub
165

    
166
        }
167

    
168
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
169
                // TODO Auto-generated method stub
170
                return null;
171
        }
172

    
173
        public String getAbrev() {                
174
                return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
175
        }
176

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

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

    
219
        public Color getGridColor() {
220
                return gridColor;
221
        }
222

    
223
        public double getScale(double minX, double maxX, double width, double dpi) {
224
                double scale = 0D;
225
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
226
            scale = ((maxX - minX) * // grados
227

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

    
269
        public boolean isProjected() {
270
                if (crsGT instanceof AbstractDerivedCRS){
271
                        return true;
272
                }else
273
                        return false;
274
        }
275

    
276
        public void setGridColor(Color c) {
277
                gridColor = c;
278
        }
279

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

    
317
        public CoordinateReferenceSystem getCrsGT() {
318
                return crsGT;
319
        }
320
        
321
        public CrsProj getCrsProj() {
322
                if (crsProj == null)
323
                        try {
324
                                crsProj = new CrsProj(getProj4String());
325
                        } catch (CrsException e) {
326
                                // TODO Auto-generated catch block
327
                                e.printStackTrace();
328
                        } 
329
                return crsProj;
330
        }
331

    
332
        private CrsProj getCrsProjBase() {
333
                if (crsProjBase == null){
334
                        AbstractDerivedCRS derivedCRS = (AbstractDerivedCRS)crsGT;
335
                        try {
336
                                crsProjBase = new CrsProj(getProj4().exportToProj4(derivedCRS.getBaseCRS()));
337
                        } catch (CrsException e) {
338
                                // TODO Auto-generated catch block
339
                                e.printStackTrace();
340
                        }
341
                }
342
                return crsProjBase;
343
        }
344

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

    
372
    public Rectangle2D getExtent(Rectangle2D extent,double scale,double wImage,double hImage,double mapUnits,double distanceUnits,double dpi) {
373
            double w =0;
374
                double h =0;
375
                double wExtent =0;
376
                double hExtent =0;
377
            if (isProjected()) {
378
                        w = ((wImage / dpi) * 2.54);
379
                        h = ((hImage / dpi) * 2.54);
380
                        wExtent =w * scale*distanceUnits/ mapUnits;
381
                        hExtent =h * scale*distanceUnits/ mapUnits;
382

    
383
                }else {
384
                        w = ((wImage / dpi) * 2.54);
385
                        h = ((hImage / dpi) * 2.54);
386
                        wExtent =(w*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
387
                        hExtent =(h*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
388
                }
389
            double xExtent = extent.getCenterX() - wExtent/2;
390
                double yExtent = extent.getCenterY() - hExtent/2;
391
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
392
            return  rec;
393
    }
394

    
395
}