Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / CrsGT.java @ 359

History | View | Annotate | Download (10.6 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.opengis.referencing.crs.CoordinateReferenceSystem;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
import org.gvsig.crs.proj.CrsProj;
60
import org.gvsig.crs.proj.CrsProjException;
61
import org.gvsig.crs.proj.OperationCrsException;
62
import org.gvsig.fmap.crs.CRSFactory;
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 Logger logger = LoggerFactory.getLogger(CrsGT.class);
72

    
73
        private static final Color basicGridColor = new Color(64, 64, 64, 128);
74

    
75
        /**
76
         * CRS GeoApi. Nucleo de la clare CrsGT.
77
         */
78
        private CoordinateReferenceSystem         crsGT                        = null;
79

    
80
        /**
81
         *Cadena proj4
82
         */
83
        private String                                                 proj4String         = null;
84

    
85
        private Color                                                 gridColor                 = basicGridColor;
86

    
87
        /**
88
         * Par?metros de transformaci?n para el CRS fuente en formato proj4.
89
         */
90
        private String                                                 sourceTrParams                        = null;
91

    
92
        /**
93
         * Par?metros de transformaci?n para el CRS destino en formato proj4.
94
         */
95
        private String                                                 targetTrParams                        = null;
96

    
97
        /**
98
         * CRS operable con proj4.
99
         */
100
        private CrsProj                                         crsProj                        = null;
101

    
102
        /**
103
         * CRS Base operable con proj4.
104
         */
105
        private CrsProj                                         crsProjBase                = null;
106

    
107
        /**
108
         * Provisional, hasta que se elimine getCrsWkt de ICrs.
109
         */
110
        private CrsWkt                                                 crsWkt                        = null;
111

    
112
        /**
113
         * Conversor de CRSs a cadenas proj4.
114
         */
115
        private Proj4                                                 proj4                        = null;
116

    
117

    
118

    
119
        /**
120
         * Constructor a partir de un CoordinateReferenceSystem
121
         *
122
         * @param crsGT
123
         * @throws CrsProjException
124
         */
125
        public CrsGT(CoordinateReferenceSystem crsGT){
126
                this.crsGT = crsGT;
127
        }
128

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

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

    
139
        public String getWKT() {
140
                return crsGT.toWKT();
141
        }
142

    
143
        public void setTransformationParams(String SourceParams, String TargetParams) {
144
                this.sourceTrParams = SourceParams;
145
                this.targetTrParams = TargetParams;
146
        }
147

    
148
        /**
149
         * Devuelve los parametros de la transformacion del crs fuente
150
         * @return
151
         */
152
        public String getSourceTransformationParams() {
153
                return this.sourceTrParams;
154
        }
155

    
156
        /**
157
         * Devuelve los parametros de la transformacion del crs destino
158
         * @return
159
         */
160
        public String getTargetTransformationParams() {
161
                return this.targetTrParams;
162
        }
163

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

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

    
171
        }
172

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

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

    
182
        public ICoordTrans getCT(IProjection dest) {
183

    
184
                try {
185
                        if (dest == this)
186
                                return null;
187
                        COperation operation = null;
188
                        if(((ICrs)dest).getSourceTransformationParams() != null || ((ICrs)dest).getTargetTransformationParams() != null)
189
                                operation = new COperation(this, (ICrs)dest,((ICrs)dest).getTargetTransformationParams(),((ICrs)dest).getSourceTransformationParams());
190
                        else
191
                                operation = new COperation(this, (ICrs)dest,sourceTrParams,targetTrParams);
192
                        return operation;
193
                }catch (CrsException e) {
194
            throw new RuntimeException(e);
195
                }
196

    
197
                /*
198
                try {
199
                        operation = new COperation(this, (ICrs)dest);
200
                } catch (CrsException e) {
201
                        // TODO Auto-generated catch block
202
                        logger...
203
                }
204

205
                if (!getTransformationParams().equals("")){
206
                        if (isParamsInTarget())
207
                                operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
208
                        else
209
                                operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
210
                        return operation;
211
                }
212

213
                return operation;        */
214
        }
215

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

    
222
        public Color getGridColor() {
223
                return gridColor;
224
        }
225

    
226
        public double getScale(double minX, double maxX, double width, double dpi) {
227
                double scale = 0D;
228
                //Esto ahora se hace fuera
229
//        if (!isProjected()) { // Es geogr?fico; calcula la escala.
230
//            scale = ((maxX - minX) * // grados
231
//
232
//            // 1852.0 metros x minuto de meridiano
233
//            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
234
//                    width; // pixels
235
//        }
236
//        else{
237
                 scale = ((maxX - minX) * // metros
238
                    (dpi / 2.54 * 100.0)) / // px / metro
239
                    width; // pixels
240
//        }
241
        return scale;
242
        }
243

    
244
        public double getScale(double minX, double maxX, double minY, double maxY, double width, double dpi) {
245

    
246
                double scale = 0D;
247
                double incX = (maxX-minX);
248

    
249
                if (!isProjected()) {
250
                        double a = getDatum().getESemiMajorAxis();
251
                        double invF = getDatum().getEIFlattening();
252
                        double meanY = (minY+maxY)/2.0;
253
                        double radius = 0.0;
254

    
255

    
256
                        if (invF == Double.POSITIVE_INFINITY){
257
                                radius = a;
258
                        }
259
                        else{
260
                                double e2 = 2.0/invF-Math.pow(1.0/invF,2.0);
261
                                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);
262
                        }
263
                        incX *= Math.PI/180.0*radius;
264
                }
265

    
266
                scale = (incX * // metros
267
                                (dpi / 2.54 * 100.0)) / // px / metro
268
                                        width; // pixels
269

    
270
        return scale;
271
        }
272

    
273
        public boolean isProjected() {
274
                if (crsGT instanceof AbstractDerivedCRS){
275
                        return true;
276
                }else
277
                        return false;
278
        }
279

    
280
        public void setGridColor(Color c) {
281
                gridColor = c;
282
        }
283

    
284
        public Point2D toGeo(Point2D pt) {
285
                if (isProjected()){
286
                        double x[] = {pt.getX()};
287
                        double y[] = {pt.getY()};
288
                        double z[] = {0D};
289
                        try {
290
                            CrsProj.operate( x , y, z,
291
                                                getCrsProj(),getCrsProjBase());
292
                        } catch (OperationCrsException e) {
293
                throw new RuntimeException(e);
294
                        }
295
                        return new Point2D.Double(x[0],y[0]);
296
                }
297
                else
298
                        return pt;
299
        }
300

    
301
        /**
302
         * @param targetParam
303
         */
304
        /*public void setParamsInTarget(boolean targetParam) {
305
                this.paramsInTarget = targetParam;
306
        }*/
307

    
308
        /**
309
         *
310
         * @return Cadena proj4 Correspondiente al CRS.
311
         * @throws CrsException
312
         */
313
        public String getProj4String() throws CrsException {
314
                if (proj4String == null)
315
                proj4String = getProj4().exportToProj4(crsGT);
316

    
317
                return proj4String;
318
        }
319

    
320
        public CoordinateReferenceSystem getCrsGT() {
321
                return crsGT;
322
        }
323

    
324
        public CrsProj getCrsProj() {
325
                if (crsProj == null)
326
                        try {
327
                                crsProj = new CrsProj(getProj4String());
328
                        } catch (CrsException e) {
329
                    throw new RuntimeException(e);
330
                        }
331
                return crsProj;
332
        }
333

    
334
        private CrsProj getCrsProjBase() {
335
                if (crsProjBase == null){
336
                        AbstractDerivedCRS derivedCRS = (AbstractDerivedCRS)crsGT;
337
                        try {
338
                                crsProjBase = new CrsProj(getProj4().exportToProj4(derivedCRS.getBaseCRS()));
339
                        } catch (CrsException e) {
340
                throw new RuntimeException(e);
341
                        }
342
                }
343
                return crsProjBase;
344
        }
345

    
346
        private Proj4 getProj4() {
347
                if (proj4 == null)
348
                        try {
349
                                proj4 = new Proj4();
350
                        } catch (CrsException e) {
351
                throw new RuntimeException(e);
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 / mapUnits;
381
                        hExtent =h * scale / mapUnits;
382

    
383
                        //En las no proyectadas, ahora viene el calculo hecho
384
                        //al haber incluido los grados en el array DISTANCETRANS2METER del MapContext
385
//                }else {
386
//                        w = ((wImage / dpi) * 2.54);
387
//                        h = ((hImage / dpi) * 2.54);
388
//                        wExtent =(w*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
389
//                        hExtent =(h*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
390
//                }
391
            double xExtent = extent.getCenterX() - wExtent/2;
392
                double yExtent = extent.getCenterY() - hExtent/2;
393
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
394
            return  rec;
395
    }
396

    
397
    public Object clone() throws CloneNotSupportedException {
398
        return CRSFactory.getCRS( this.getFullCode() );
399
     }
400

    
401
}