Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / DefaultViewPortData.java @ 2840

History | View | Annotate | Download (9.05 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.datastruct;
23

    
24
import java.awt.Dimension;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.Dimension2D;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IProjection;
32
import org.gvsig.compat.CompatLocator;
33
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
34
import org.gvsig.fmap.dal.coverage.datastruct.ViewPortData;
35
import org.gvsig.timesupport.Time;
36
import org.gvsig.tools.persistence.case1.model.ViewPort;
37

    
38

    
39
/**
40
 * Datos de vista sobre las capas.
41
 *
42
 * Mantiene un conjunto de datos necesarios, que describen el modo de
43
 * ver las capas actual.
44
 *
45
 * @author "Luis W. Sevilla"
46
 */
47
public class DefaultViewPortData implements ViewPortData {
48
    /**
49
     * Tipo de proyecci?n de la vista.
50
     */
51
    IProjection proj = null;
52

    
53
    /**
54
     * Sistema de coordenadas de la vista.
55
     */
56
    IProjection cs = null;
57

    
58
    /**
59
     * Amplitud de la vista, en coordenadas proyectadas.
60
     */
61
    Extent extent = null;
62

    
63
    /**
64
     * Tama?o de la vista, en coordenadas de dispositivo.
65
     */
66
    Dimension2D viewSize = null;
67

    
68
    /**
69
     * Transformaci?n af?n usada en la vista actual.
70
     */
71
    public AffineTransform mat = null;
72
    
73
    /**
74
     * Informaci?n de tama?o en pixeles. Esta no es calculada. Solo se almacena
75
     * si el usuario la necesita.
76
     */
77
    public Point2D pxSize = null;
78
    
79
    private Time time = null;
80

    
81
        /**
82
     * Resoluci?n (Puntos por pulgada) de la vista actual.
83
     * Se necesita para los c?lculos de escala geogr?fica.
84
     * 
85
     * Ojo: la vista podr?a tener una resoluci?n distinta que la pantalla (por
86
     * ejemplo en el Layout), por lo que los usuarios de esta clase deber?an
87
     * hacer un setDPI con la resoluci?n correcta (normalmente la del ViewPort). 
88
     * 
89
     * @see ViewPort#getDPI()
90
     */
91
    int dpi = CompatLocator.getGraphicsUtils().getScreenDPI();
92

    
93
    public DefaultViewPortData() {
94
            mat = new AffineTransform();
95
            mat.scale(1.0, -1.0);
96
    }
97

    
98
    public DefaultViewPortData(IProjection proj, Extent extent, Dimension2D size) {
99
        this.proj = proj;
100
        this.extent = extent;
101
        this.viewSize = size;
102
        mat = new AffineTransform();
103
        mat.scale(1.0, -1.0);
104
    }
105

    
106
    public IProjection getProjection() {
107
        return proj;
108
    }
109

    
110
    public void setProjection(IProjection proj) {
111
        this.proj = proj;
112
    }
113

    
114
    public void reProject(ICoordTrans rp) {
115
        // TODO metodo reProject pendiente de implementar
116
    }
117

    
118
    public void setCoordSys(IProjection cs) {
119
        this.cs = cs;
120
    }
121

    
122
    //public void setCoordTrans(ICoordTrans ct) { this.ct = ct; }
123
    public AffineTransform getMat() {
124
        return mat;
125
    }
126

    
127
    public void setMat(AffineTransform mat) {
128
        this.mat = mat;
129
    }
130

    
131
    public Object clone() {
132
        DefaultViewPortData vp = new DefaultViewPortData();
133

    
134
        if (mat != null) {
135
            vp.mat = new AffineTransform(mat);
136
        }
137

    
138
        if (extent != null) {
139
            vp.extent = new ExtentImpl(extent);
140
        }
141

    
142
        vp.proj = proj;
143
        vp.viewSize = viewSize;
144
        vp.dpi = dpi;
145

    
146
        return vp;
147
    }
148

    
149
    /**
150
     * Obtiene el ancho de la vista
151
     * @return
152
     */
153
    public double getWidth() {
154
        return viewSize.getWidth();
155
    }
156

    
157
    /**
158
     * Obtiene el alto de la vista
159
     * @return
160
     */
161
    public double getHeight() {
162
        return viewSize.getHeight();
163
    }
164

    
165
    /**
166
     * Obtiene el tama?o de la vista
167
     */
168
    public Dimension2D getSize() {
169
        return viewSize;
170
    }
171

    
172
    /**
173
     * Asigna el ancho y alto de la vista
174
     * @param w Ancho
175
     * @param h Alto
176
     */
177
    public void setSize(double w, double h) {
178
        setSize(new Dimension((int) w, (int) h));
179
    }
180
    
181
    public void setSize(Dimension2D sz) {
182
            viewSize = sz;
183
        reExtent();
184
    }
185

    
186
    public Extent getExtent() {
187
        return extent;
188
    }
189

    
190
    public void setExtent(Dimension2D sz) {
191
        Point2D.Double pt0 = new Point2D.Double(0, 0);
192
        Point2D.Double ptSz = new Point2D.Double(sz.getWidth(), sz.getHeight());
193

    
194
        try {
195
            mat.inverseTransform(pt0, pt0);
196
            mat.inverseTransform(ptSz, ptSz);
197
        } catch (Exception e) {
198
            e.printStackTrace();
199
        }
200

    
201
        extent = new ExtentImpl(pt0, ptSz);
202
    }
203
    
204
    /**
205
     * Asigna la extensi?n del raster
206
     * @param ext Extent
207
     */
208
    public void setExtent(Extent ext) {
209
            extent = ext;
210
    }
211

    
212
    public void reExtent() {
213
        setExtent(viewSize);
214
    }
215

    
216
    public void setDPI(int dpi) {
217
        this.dpi = dpi;
218
    }
219

    
220
    public int getDPI() {
221
        return this.dpi;
222
    }
223

    
224
    /**
225
     * zoom a un marco.
226
     *
227
     * @param extent
228
     */
229
    public void zoom(Extent extent) {
230
        double[] scale = extent.getScale(getWidth(), getHeight());
231
        double escala = Math.min(scale[0], scale[1]);
232

    
233
        mat.setToIdentity();
234
        mat.scale(escala, -escala);
235
        mat.translate(-extent.minX(), -extent.maxY());
236
        this.extent = extent;
237
        reExtent();
238
    }
239

    
240
    /**
241
     * zoom centrado en un punto.
242
     *
243
     * @param zoom
244
     * @param pt
245
     */
246
    public void zoom(double zoom, Point2D pt) {
247
        zoom(zoom, zoom, pt);
248
    }
249

    
250
    public void zoom(double zx, double zy, Point2D pt) {
251
        centerAt(pt);
252
        mat.scale(zx, zy);
253
        centerAt(pt);
254
        reExtent();
255
    }
256

    
257
    /**
258
     * Zoom a una escala (geogr?fica);
259
     *
260
     * @param scale
261
     */
262
    public void zoomToCenter(double f) {
263
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
264
                                                     getHeight() / 2.0);
265

    
266
        try {
267
            mat.inverseTransform(ptCenter, ptCenter);
268
        } catch (Exception e) {
269
            e.printStackTrace();
270
        }
271

    
272
        zoom(f, ptCenter);
273
    }
274

    
275
    /**
276
     * Centrar en un punto.
277
     *
278
     * @param pt
279
     */
280
    public void centerAt(Point2D pt) {
281
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
282
                                                     getHeight() / 2.0);
283

    
284
        try {
285
            mat.inverseTransform(ptCenter, ptCenter);
286
            mat.translate(ptCenter.x - pt.getX(), ptCenter.y - pt.getY());
287
        } catch (Exception e) {
288
            e.printStackTrace();
289
        }
290

    
291
        reExtent();
292
    }
293

    
294
    /**
295
     * Desplaza la vista actual.
296
     *
297
     * @param pt
298
     */
299
    public void pan(Point2D ptIni, Point2D ptFin) {
300
        mat.translate(ptFin.getX() - ptIni.getX(), ptFin.getY() - ptIni.getY());
301
        reExtent();
302
    }
303

    
304
    public Point2D getCenter() {
305
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
306
                                                     getHeight() / 2.0);
307

    
308
        try {
309
            mat.inverseTransform(ptCenter, ptCenter);
310
        } catch (Exception e) {
311
            e.printStackTrace();
312
        }
313

    
314
        return ptCenter;
315
    }
316
    
317
    /**
318
     * Calcula la transformaci?n af?n.
319
     */
320
        public void calculateAffineTransform() {
321
                if ((viewSize == null) || (extent == null) ||
322
                        (viewSize.getWidth() <= 0) || (viewSize.getHeight() <= 0)) 
323
                        return;
324

    
325
                AffineTransform escalado = new AffineTransform();
326
                AffineTransform translacion = new AffineTransform();
327

    
328
                double escalaX;
329
                double escalaY;
330

    
331
                escalaX = viewSize.getWidth() / extent.width();
332
                escalaY = viewSize.getHeight() / extent.height();
333

    
334
                double xCenter = extent.getCenterX();
335
                double yCenter = extent.getCenterY();
336
                double newHeight;
337
                double newWidth;
338

    
339
                Rectangle2D adjustedExtent = new Rectangle2D.Double();
340
                double scale = 0;
341
                
342
                if (escalaX < escalaY) {
343
                        scale = escalaX;
344
                        newHeight = viewSize.getHeight() / scale;
345
                        adjustedExtent.setRect(xCenter - (extent.width() / 2.0),
346
                                        yCenter - (newHeight / 2.0), extent.width(), newHeight);
347
                } else {
348
                        scale = escalaY;
349
                        newWidth = viewSize.getWidth() / scale;
350
                        adjustedExtent.setRect(xCenter - (newWidth / 2.0),
351
                                        yCenter - (extent.height() / 2.0), newWidth,
352
                                        extent.height());
353
                }
354
                escalado.setToScale(scale, -scale);
355
                translacion.setToTranslation(-extent.getULX(), -extent.getULY());
356

    
357
                mat.setToIdentity();
358
                mat.concatenate(escalado);
359
                mat.concatenate(translacion);
360
        }
361
        
362
        public Time getTime() {
363
                return time;
364
        }
365

    
366
        public void setTime(Time time) {
367
                this.time = time;
368
        }
369
}