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 11241 dguerrero
/* 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 11398 dguerrero
import org.geotools.referencing.crs.AbstractDerivedCRS;
53
import org.geotools.referencing.crs.AbstractSingleCRS;
54
import org.geotools.referencing.datum.DefaultGeodeticDatum;
55 11304 dguerrero
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 11241 dguerrero
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 11304 dguerrero
        private static final Color basicGridColor = new Color(64, 64, 64, 128);
69 11241 dguerrero
70 11306 dguerrero
        /**
71
         * CRS GeoApi. Nucleo de la clare CrsGT.
72
         */
73 11304 dguerrero
        private CoordinateReferenceSystem         crsGT                        = null;
74 11306 dguerrero
75
        /**
76
         *Cadena proj4
77
         */
78 11813 dguerrero
        private String                                                 proj4String         = null;
79 11306 dguerrero
80 11304 dguerrero
        private Color                                                 gridColor                 = basicGridColor;
81 11306 dguerrero
82
        /**
83 11957 dguerrero
         * Par?metros de transformaci?n para el CRS fuente en formato proj4.
84 11306 dguerrero
         */
85 12406 dguerrero
        private String                                                 sourceTrParams                        = null;
86 11241 dguerrero
87
        /**
88 11957 dguerrero
         * Par?metros de transformaci?n para el CRS destino en formato proj4.
89
         */
90 12406 dguerrero
        private String                                                 targetTrParams                        = null;
91 11957 dguerrero
92
        /**
93 11306 dguerrero
         * 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 11813 dguerrero
        /**
108
         * Conversor de CRSs a cadenas proj4.
109
         */
110
        private Proj4                                                 proj4                        = null;
111 11306 dguerrero
112
113 11813 dguerrero
114 11306 dguerrero
        /**
115 11241 dguerrero
         * Constructor a partir de un CoordinateReferenceSystem
116
         *
117
         * @param crsGT
118 11306 dguerrero
         * @throws CrsProjException
119 11241 dguerrero
         */
120 11306 dguerrero
        public CrsGT(CoordinateReferenceSystem crsGT){
121 11241 dguerrero
                this.crsGT = crsGT;
122
        }
123
124
        public int getCode() {
125 11304 dguerrero
                return Integer.valueOf(getAbrev().split(":")[1]).intValue();
126 11241 dguerrero
        }
127
128
        public CrsWkt getCrsWkt() {
129 11810 dguerrero
                if (crsWkt==null)
130
                        crsWkt = new CrsWkt(crsGT);
131 11306 dguerrero
                return crsWkt;
132 11241 dguerrero
        }
133
134
        public String getWKT() {
135 11306 dguerrero
                return crsGT.toWKT();
136 11241 dguerrero
        }
137
138 11957 dguerrero
        public void setTransformationParams(String SourceParams, String TargetParams) {
139 12406 dguerrero
                this.sourceTrParams = SourceParams;
140
                this.targetTrParams = TargetParams;
141 11241 dguerrero
        }
142 18139 jlgomez
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 11241 dguerrero
159
        public Point2D createPoint(double x, double y) {
160 21313 dguerrero
                return new Point2D.Double(x,y);
161 11241 dguerrero
        }
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 11460 jlgomez
        public String getAbrev() {
174 11398 dguerrero
                return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
175 11241 dguerrero
        }
176
177
        public ICoordTrans getCT(IProjection dest) {
178 11813 dguerrero
179 11304 dguerrero
                try {
180 22496 dguerrero
                        if (dest == this)
181
                                return null;
182 18139 jlgomez
                        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 11813 dguerrero
                        return operation;
188
                }catch (CrsException e) {
189
                        // TODO Auto-generated catch block
190
                        e.printStackTrace();
191
                        return null;
192
                }
193
194
                /*
195
                try {
196 11304 dguerrero
                        operation = new COperation(this, (ICrs)dest);
197
                } catch (CrsException e) {
198
                        // TODO Auto-generated catch block
199
                        e.printStackTrace();
200
                }
201

202 11810 dguerrero
                if (!getTransformationParams().equals("")){
203
                        if (isParamsInTarget())
204 11813 dguerrero
                                operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
205 11304 dguerrero
                        else
206 11813 dguerrero
                                operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
207 11304 dguerrero
                        return operation;
208
                }
209

210 11863 dguerrero
                return operation;        */
211 11241 dguerrero
        }
212
213
        public IDatum getDatum() {
214 11277 dguerrero
                DefaultGeodeticDatum datumGT = (DefaultGeodeticDatum)((AbstractSingleCRS)crsGT).getDatum();
215
                CRSDatum datum = new CRSDatum(datumGT.getEllipsoid().getSemiMajorAxis(),datumGT.getEllipsoid().getInverseFlattening());
216
                return datum;
217 11241 dguerrero
        }
218
219
        public Color getGridColor() {
220 11304 dguerrero
                return gridColor;
221 11241 dguerrero
        }
222
223
        public double getScale(double minX, double maxX, double width, double dpi) {
224 11304 dguerrero
                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 11241 dguerrero
        }
239 12026 dguerrero
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 11241 dguerrero
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 11304 dguerrero
                gridColor = c;
278 11241 dguerrero
        }
279
280
        public Point2D toGeo(Point2D pt) {
281 11304 dguerrero
                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 11813 dguerrero
                                                getCrsProj(),getCrsProjBase());
288 11304 dguerrero
                        } 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 11241 dguerrero
        }
297 11304 dguerrero
298
        /**
299 11460 jlgomez
         * @param targetParam
300
         */
301 11957 dguerrero
        /*public void setParamsInTarget(boolean targetParam) {
302 11810 dguerrero
                this.paramsInTarget = targetParam;
303 11957 dguerrero
        }*/
304 11460 jlgomez
305
        /**
306 11304 dguerrero
         *
307 11813 dguerrero
         * @return Cadena proj4 Correspondiente al CRS.
308 12509 jlgomez
         * @throws CrsException
309 11304 dguerrero
         */
310 12509 jlgomez
        public String getProj4String() throws CrsException {
311 11813 dguerrero
                if (proj4String == null)
312 12509 jlgomez
                proj4String = getProj4().exportToProj4(crsGT);
313
314 11810 dguerrero
                return proj4String;
315 11304 dguerrero
        }
316
317
        public CoordinateReferenceSystem getCrsGT() {
318
                return crsGT;
319
        }
320 11460 jlgomez
321
        public CrsProj getCrsProj() {
322 11813 dguerrero
                if (crsProj == null)
323 12509 jlgomez
                        try {
324
                                crsProj = new CrsProj(getProj4String());
325
                        } catch (CrsException e) {
326
                                // TODO Auto-generated catch block
327
                                e.printStackTrace();
328
                        }
329 11460 jlgomez
                return crsProj;
330
        }
331 11304 dguerrero
332 11813 dguerrero
        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 12406 dguerrero
356
        /**
357
         * @return Authority:code:proj@Sourceparam@TargerParam@
358
         */
359 12201 dguerrero
        public String getFullCode() {
360 12406 dguerrero
                if (this.sourceTrParams == null && this.targetTrParams == null)
361 12301 dguerrero
                        return getAbrev();
362
                String sourceParams = "";
363
                String targetParams = "";
364 12406 dguerrero
                if (sourceTrParams != null)
365
                        sourceParams = sourceTrParams;
366
                if (targetTrParams != null)
367
                        targetParams = targetTrParams;
368 12301 dguerrero
369
                return getAbrev()+":proj@"+sourceParams+"@"+targetParams;
370 12201 dguerrero
        }
371
372 29000 cmartinez
    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 18139 jlgomez
383 29000 cmartinez
                }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 11241 dguerrero
}