Statistics
| Revision:

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

History | View | Annotate | Download (6.83 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
        private boolean                                         targetNad                 = false;
76
        /**
77
         *Cadena proj4
78
         */
79
        private String                                                 proj4                         = null;
80
        
81
        private Color                                                 gridColor                 = basicGridColor;
82
        
83
        /**
84
         * Par?metro nadgrid para proj4.
85
         */
86
        private String                                                 nad                                = null;
87
        
88
        /**
89
         * CRS operable con proj4.
90
         */
91
        private CrsProj                                         crsProj                        = null;
92
        
93
        /**
94
         * CRS Base operable con proj4.
95
         */
96
        private CrsProj                                         crsProjBase                = null;
97
        
98
        /**
99
         * Provisional, hasta que se elimine getCrsWkt de ICrs.
100
         */
101
        private CrsWkt                                                 crsWkt                        = null;
102
        
103
        
104
        
105
        /**
106
         * Constructor a partir de un CoordinateReferenceSystem
107
         * 
108
         * @param crsGT
109
         * @throws CrsProjException 
110
         */
111
        public CrsGT(CoordinateReferenceSystem crsGT){
112
                this.crsGT = crsGT;
113
                Proj4 proj4;
114
                try {
115
                        proj4 = new Proj4();
116
                        crsProj = new CrsProj(proj4.exportToProj4(crsGT));
117
                        if (crsGT instanceof AbstractDerivedCRS){
118
                                AbstractDerivedCRS derivedCRS = (AbstractDerivedCRS)crsGT;
119
                                crsProjBase = new CrsProj(proj4.exportToProj4(derivedCRS.getBaseCRS()));
120
                        }
121
                } catch (CrsException e) {
122
                        // TODO Auto-generated catch block
123
                        e.printStackTrace();
124
                }
125
                
126
                crsWkt = new CrsWkt(crsGT);
127
        }
128

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

    
133
        public CrsWkt getCrsWkt() {
134
                return crsWkt;
135
        }
136

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

    
141
        public void setNadGrid(String nad) {
142
                this.nad = nad;
143

    
144
        }
145

    
146
        public void setNadInTarget(boolean targetNad) {
147
                // TODO Auto-generated method stub
148

    
149
        }
150

    
151
        public Point2D createPoint(double x, double y) {
152
                // TODO Auto-generated method stub
153
                return null;
154
        }
155

    
156
        public void drawGrid(Graphics2D g, ViewPortData vp) {
157
                // TODO Auto-generated method stub
158

    
159
        }
160

    
161
        public Point2D fromGeo(Point2D gPt, Point2D mPt) {
162
                // TODO Auto-generated method stub
163
                return null;
164
        }
165

    
166
        public String getAbrev() {
167
                AbstractSingleCRS crs = (AbstractSingleCRS)crsGT;
168
                String authority = crs.getName().toString().split(":")[0];
169
                //return ((AbstractSingleCRS)crsGT).getIdentifier(Citations.fromName(authority)).toString();
170
                return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
171
        }
172

    
173
        public ICoordTrans getCT(IProjection dest) {
174
                COperation operation = null;
175
                CrsGT crsDest = (CrsGT)dest;
176
                try {
177
                        operation = new COperation(this, (ICrs)dest);
178
                } catch (CrsException e) {
179
                        // TODO Auto-generated catch block
180
                        e.printStackTrace();
181
                }
182
                
183
                if (!getNadGrid().equals("")){
184
                        if (isTargetNad())
185
                                operation.setNadCrsProj(new CrsProj(crsDest.getProj4()+getNadGrid()), true);
186
                        else
187
                                operation.setNadCrsProj(new CrsProj(getProj4()+getNadGrid()), false);
188
                        return operation;
189
                }
190
                
191
                return operation;                
192
        }
193

    
194
        public IDatum getDatum() {
195
                DefaultGeodeticDatum datumGT = (DefaultGeodeticDatum)((AbstractSingleCRS)crsGT).getDatum();
196
                CRSDatum datum = new CRSDatum(datumGT.getEllipsoid().getSemiMajorAxis(),datumGT.getEllipsoid().getInverseFlattening());
197
                return datum;
198
        }
199

    
200
        public Rectangle2D getExtent(Rectangle2D extent, double scale,
201
                        double wImage, double hImage, double changeUnits, double dpi) {
202
                // TODO Auto-generated method stub
203
                return null;
204
        }
205

    
206
        public Color getGridColor() {
207
                return gridColor;
208
        }
209

    
210
        public double getScale(double minX, double maxX, double width, double dpi) {
211
                double scale = 0D;
212
        if (!isProjected()) { // Es geogr?fico; calcula la escala.
213
            scale = ((maxX - minX) * // grados
214

    
215
            // 1852.0 metros x minuto de meridiano
216
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
217
                    width; // pixels
218
        }
219
        else{
220
                 scale = ((maxX - minX) * // metros
221
                    (dpi / 2.54 * 100.0)) / // px / metro
222
                    width; // pixels
223
        }
224
        return scale;
225
        }
226

    
227
        public boolean isProjected() {
228
                if (crsGT instanceof AbstractDerivedCRS){
229
                        return true;
230
                }else
231
                        return false;
232
        }
233

    
234
        public void setGridColor(Color c) {
235
                gridColor = c;
236
        }
237

    
238
        public Point2D toGeo(Point2D pt) {
239
                if (isProjected()){
240
                        double x[] = {pt.getX()};
241
                        double y[] = {pt.getY()};
242
                        double z[] = {0D};
243
                        try {
244
                                JNIBaseCrs.operate( x , y, z,
245
                                                crsProj,crsProjBase);
246
                        } catch (OperationCrsException e) {
247
                                // TODO Auto-generated catch block
248
                                e.printStackTrace();
249
                        } catch (CrsProjException e) {
250
                                // TODO Auto-generated catch block
251
                                e.printStackTrace();
252
                        }
253
                        return new Point2D.Double(x[0],y[0]);
254
                }
255
                else
256
                        return pt;
257
        }
258

    
259
        public String getNadGrid() {
260
                return nad;
261
        }
262
        
263
        /**
264
         * @return
265
         */
266
        public boolean isTargetNad() {
267
                return targetNad;
268
        }
269
        
270
        /**
271
         * 
272
         * @return
273
         */
274
        public String getProj4() {
275
                return proj4;
276
        }
277

    
278
        public CoordinateReferenceSystem getCrsGT() {
279
                return crsGT;
280
        }
281

    
282
}