Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / geo / ViewPortData.java @ 2312

History | View | Annotate | Download (7.11 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.geo;
25

    
26
import java.awt.Dimension;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Dimension2D;
29
import java.awt.geom.Point2D;
30
import java.text.DecimalFormat;
31

    
32
import org.cresques.cts.ICoordTrans;
33
import org.cresques.cts.IProjection;
34
import org.cresques.px.Extent;
35

    
36
/**
37
 * Datos de vista sobre las capas.
38
 * 
39
 * Mantiene un conjunto de datos necesarios, que describen el modo de
40
 * ver las capas actual.
41
 * 
42
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
43
 */
44
public class ViewPortData implements Projected {
45
        /**
46
         * Tipo de proyecci?n de la vista.
47
         */
48
        IProjection proj = null;
49
        /**
50
         * Sistema de coordenadas de la vista.
51
         */
52
        IProjection cs = null;
53
        /**
54
         * Amplitud de la vista, en coordenadas proyectadas.
55
         */
56
        Extent extent = null;
57
        /**
58
         * Tama?o de la vista, en coordenadas de dispositivo.
59
         */
60
        Dimension2D size = null;
61
        /**
62
         * Transformaci?n af?n usada en la vista actual.
63
         */
64
        public AffineTransform mat = null;
65
        /**
66
         * Resoluci?n (Puntos por pulgada) de la vista actual.
67
         * Se necesita para los c?lculos de escala geogr?fica.
68
         */
69
        int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
70
        
71
        public ViewPortData(){ }
72

    
73
        public ViewPortData(IProjection proj, Extent extent, Dimension2D size) {
74
                this.proj = proj;
75
                this.extent = extent;
76
                this.size = size;
77
                mat = new AffineTransform();
78
                mat.scale(1.0, -1.0);
79
        }
80

    
81
        public IProjection getProjection() { return proj; }
82
        public void setProjection(IProjection proj) { this.proj = proj; }
83
        public void reProject(ICoordTrans rp) {
84
                // TODO metodo reProject pendiente de implementar
85
        }
86

    
87
        public void setCoordSys(IProjection cs) { this.cs = cs; }
88
        //public void setCoordTrans(ICoordTrans ct) { this.ct = ct; }
89

    
90
        public AffineTransform getMat() { return mat; }
91
        public void setMat(AffineTransform mat) { this.mat = mat; }
92

    
93
        public Object clone() {
94
                ViewPortData vp = new ViewPortData();
95
                if (mat != null)
96
                        vp.mat = new AffineTransform(mat);
97
                if (extent != null)
98
                        vp.extent = new Extent(extent);
99
                vp.proj = proj;
100
                vp.size = size;
101
                vp.dpi = dpi;
102
                return vp;
103
        }
104

    
105
        public double getWidth() { return size.getWidth(); } 
106
        public double getHeight() { return size.getHeight(); }
107
        /**
108
         * 
109
         */
110
        public Dimension2D getSize() {
111
                return size;                
112
        }
113
        public void setSize(double w, double h) {
114
                setSize(new Dimension((int) w, (int) h));
115
        }
116
        public void setSize(Dimension2D sz) {
117
                size = sz;
118
                reExtent();
119
        }
120
        
121
        public Extent getExtent() { return extent; }
122
        public void setExtent(Dimension2D sz) {
123
                Point2D.Double pt0 = new Point2D.Double(0,0), 
124
                        ptSz = new Point2D.Double(sz.getWidth(), sz.getHeight());
125
                try {
126
                        mat.inverseTransform(pt0, pt0);
127
                        mat.inverseTransform(ptSz, ptSz);
128
                } catch (Exception e) {
129
                        e.printStackTrace();
130
                }
131
                extent = new Extent(pt0, ptSz);
132
        }
133
        public void reExtent() {
134
                setExtent(size);
135
        }
136
        public void setDPI(int dpi) { this.dpi = dpi; }
137
        public int getDPI() { return this.dpi; }
138

    
139
        /**
140
         * zoom a un marco.
141
         * 
142
         * @param extent
143
         */
144
        
145
        public void zoom(Extent extent) {
146
                double [] scale = extent.getScale(getWidth(), getHeight());
147
                double escala = Math.min(scale[0], scale[1]);
148
                
149
                mat.setToIdentity();
150
                mat.scale(escala, -escala);
151
                mat.translate(-extent.minX(), -extent.maxY());
152
                this.extent = extent;
153
                reExtent();
154
        }
155
        
156
        /**
157
         * zoom centrado en un punto.
158
         * 
159
         * @param zoom
160
         * @param pt
161
         */
162
        
163
        public void zoom(double zoom, Point2D pt) {
164
                zoom(zoom, zoom, pt);
165
        }
166
        public void zoom(double zx, double zy, Point2D pt) {
167
                centerAt(pt);
168
                mat.scale(zx, zy);
169
                centerAt(pt);
170
                reExtent();
171
        }
172
        
173
        /**
174
         * Zoom a una escala (geogr?fica);
175
         * 
176
         * @param scale
177
         */
178
        
179
        public void zoomToGeoScale(double scale) {
180
                double actual = getGeoScale();
181
                double f = actual/scale;
182
                zoomToCenter(f);
183
        }
184
        
185
        /**
186
         * Zoom a una escala (geogr?fica);
187
         * 
188
         * @param scale
189
         */
190
        
191
        public void zoomToCenter(double f) {
192
                Point2D.Double ptCenter = new Point2D.Double(getWidth()/2.0,getHeight()/2.0);
193
                try {
194
                        mat.inverseTransform(ptCenter, ptCenter);
195
                } catch (Exception e) {
196
                        e.printStackTrace();
197
                }
198
                zoom(f, ptCenter);
199
        }
200
        
201
        /**
202
         * Centrar en un punto.
203
         * 
204
         * @param pt
205
         */
206
        
207
        public void centerAt(Point2D pt) {
208
                Point2D.Double ptCenter = new Point2D.Double(getWidth()/2.0,getHeight()/2.0);
209
                try {
210
                        mat.inverseTransform(ptCenter, ptCenter);
211
                        mat.translate(ptCenter.x-pt.getX(), ptCenter.y-pt.getY());
212
                } catch (Exception e) {
213
                        e.printStackTrace();
214
                }
215
                reExtent();
216
        }
217
        
218
        /**
219
         * Desplaza la vista actual.
220
         * 
221
         * @param pt
222
         */
223
        
224
        public void pan(Point2D ptIni, Point2D ptFin) {
225
                mat.translate(ptFin.getX()-ptIni.getX(), ptFin.getY()-ptIni.getY());
226
                reExtent();
227
        }
228
        
229
        public Point2D getCenter() {
230
                Point2D.Double ptCenter = new Point2D.Double(getWidth()/2.0,getHeight()/2.0);
231
                try {
232
                        mat.inverseTransform(ptCenter, ptCenter);
233
                } catch (Exception e) {
234
                        e.printStackTrace();
235
                }
236
                return ptCenter;
237
        }
238
        
239
        /**
240
         * Escala Geogr?fica.
241
         * 
242
         * @param dpi resolucion en puntos por pulgada
243
         */
244
        
245
        public double getGeoScale() {
246
                /* // TODO Actulizarlo para Geotools2
247
                double scale = 0.0;
248
                if (proj.getClass() == UtmZone.class) { // UTM;
249
                        scale = (extent.maxX()-extent.minX())*        // metros
250
                                (dpi / 2.54 * 100.0)/                                // px / metro
251
                                getWidth();                                                        // pixels
252
                } else if (proj.getClass() == Geodetic.class) { // Geodetic
253
                        scale = (extent.maxX()-extent.minX())*                // grados
254
                                // 1852.0 metros x minuto de meridiano
255
                                (dpi / 2.54 * 100.0 * 1852.0 * 60.0)/        // px / metro
256
                                getWidth();                                                                // pixels
257
                } else if (proj.getClass() == Mercator.class) { // Mercator
258
                        Projection prj = Geodetic.getProjection((Ellipsoid) proj.getDatum());
259
                        GeoPoint pt1 = (GeoPoint) prj.createPoint(1.0,0.0);
260
                        GeoPoint pt2 = (GeoPoint) prj.createPoint(2.0,0.0);
261
                        ProjPoint ppt1 = (ProjPoint) proj.createPoint(0.0, 0.0);
262
                        ProjPoint ppt2 = (ProjPoint) proj.createPoint(0.0, 0.0);
263
                        ((Mercator) proj).fromGeo(pt1, ppt1);
264
                        ((Mercator) proj).fromGeo(pt2, ppt2);
265
                        //scale = ppt2.getX()-ppt1.getX();
266
                        scale =  ((extent.maxX()-extent.minX())/ (ppt2.getX()-ppt1.getX()) ) *
267
                        //scale = ((extent.maxX()-extent.minX())/ getWidth());// *
268
                                (dpi / 2.54 * 100.0 * 1852.0 * 60.0) /
269
                                getWidth();
270
                } */
271
                return proj.getScale(extent.minX(), extent.maxX(), getWidth(), dpi);
272
        }
273
        
274
        public String getGeoScaleAsString(String fmt) {
275
                DecimalFormat format = new DecimalFormat(fmt);
276
                return "1:"+format.format(getGeoScale());
277
        }
278

    
279
}