Statistics
| Revision:

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

History | View | Annotate | Download (8.44 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 11957 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
        private String                                                 TargetTrParams                        = null;
91
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
                this.SourceTrParams = SourceParams;
140
                this.TargetTrParams = TargetParams;
141 11241 dguerrero
        }
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 11460 jlgomez
        public String getAbrev() {
159 11398 dguerrero
                return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
160 11241 dguerrero
        }
161
162
        public ICoordTrans getCT(IProjection dest) {
163 11813 dguerrero
164 11304 dguerrero
                try {
165 11957 dguerrero
                        COperation operation = new COperation(this, (ICrs)dest,SourceTrParams,TargetTrParams);
166 11813 dguerrero
                        return operation;
167
                }catch (CrsException e) {
168
                        // TODO Auto-generated catch block
169
                        e.printStackTrace();
170
                        return null;
171
                }
172
173
                /*
174
                try {
175 11304 dguerrero
                        operation = new COperation(this, (ICrs)dest);
176
                } catch (CrsException e) {
177
                        // TODO Auto-generated catch block
178
                        e.printStackTrace();
179
                }
180

181 11810 dguerrero
                if (!getTransformationParams().equals("")){
182
                        if (isParamsInTarget())
183 11813 dguerrero
                                operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
184 11304 dguerrero
                        else
185 11813 dguerrero
                                operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
186 11304 dguerrero
                        return operation;
187
                }
188

189 11863 dguerrero
                return operation;        */
190 11241 dguerrero
        }
191
192
        public IDatum getDatum() {
193 11277 dguerrero
                DefaultGeodeticDatum datumGT = (DefaultGeodeticDatum)((AbstractSingleCRS)crsGT).getDatum();
194
                CRSDatum datum = new CRSDatum(datumGT.getEllipsoid().getSemiMajorAxis(),datumGT.getEllipsoid().getInverseFlattening());
195
                return datum;
196 11241 dguerrero
        }
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 11304 dguerrero
                return gridColor;
206 11241 dguerrero
        }
207
208
        public double getScale(double minX, double maxX, double width, double dpi) {
209 11304 dguerrero
                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 11241 dguerrero
        }
224 12026 dguerrero
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 11241 dguerrero
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 11304 dguerrero
                gridColor = c;
263 11241 dguerrero
        }
264
265
        public Point2D toGeo(Point2D pt) {
266 11304 dguerrero
                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 11813 dguerrero
                                                getCrsProj(),getCrsProjBase());
273 11304 dguerrero
                        } 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 11241 dguerrero
        }
285 11304 dguerrero
286
        /**
287 11460 jlgomez
         * @param targetParam
288
         */
289 11957 dguerrero
        /*public void setParamsInTarget(boolean targetParam) {
290 11810 dguerrero
                this.paramsInTarget = targetParam;
291 11957 dguerrero
        }*/
292 11460 jlgomez
293
        /**
294 11304 dguerrero
         *
295 11813 dguerrero
         * @return Cadena proj4 Correspondiente al CRS.
296 11304 dguerrero
         */
297 11813 dguerrero
        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 11810 dguerrero
                return proj4String;
306 11304 dguerrero
        }
307
308
        public CoordinateReferenceSystem getCrsGT() {
309
                return crsGT;
310
        }
311 11460 jlgomez
312
        public CrsProj getCrsProj() {
313 11813 dguerrero
                if (crsProj == null)
314
                        crsProj = new CrsProj(getProj4String());
315 11460 jlgomez
                return crsProj;
316
        }
317 11304 dguerrero
318 11813 dguerrero
        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 12201 dguerrero
        public String getFullCode() {
343
                // TODO Auto-generated method stub
344
                return null;
345
        }
346
347 11241 dguerrero
}