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 @ 2623

History | View | Annotate | Download (8.69 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.fmap.dal.coverage.datastruct.Extent;
33
import org.gvsig.fmap.dal.coverage.datastruct.ViewPortData;
34
import org.gvsig.timesupport.Time;
35

    
36

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

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

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

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

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

    
79
        /**
80
     * Resoluci?n (Puntos por pulgada) de la vista actual.
81
     * Se necesita para los c?lculos de escala geogr?fica.
82
     */
83
    int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
84

    
85
    public DefaultViewPortData() {
86
            mat = new AffineTransform();
87
            mat.scale(1.0, -1.0);
88
    }
89

    
90
    public DefaultViewPortData(IProjection proj, Extent extent, Dimension2D size) {
91
        this.proj = proj;
92
        this.extent = extent;
93
        this.viewSize = size;
94
        mat = new AffineTransform();
95
        mat.scale(1.0, -1.0);
96
    }
97

    
98
    public IProjection getProjection() {
99
        return proj;
100
    }
101

    
102
    public void setProjection(IProjection proj) {
103
        this.proj = proj;
104
    }
105

    
106
    public void reProject(ICoordTrans rp) {
107
        // TODO metodo reProject pendiente de implementar
108
    }
109

    
110
    public void setCoordSys(IProjection cs) {
111
        this.cs = cs;
112
    }
113

    
114
    //public void setCoordTrans(ICoordTrans ct) { this.ct = ct; }
115
    public AffineTransform getMat() {
116
        return mat;
117
    }
118

    
119
    public void setMat(AffineTransform mat) {
120
        this.mat = mat;
121
    }
122

    
123
    public Object clone() {
124
        DefaultViewPortData vp = new DefaultViewPortData();
125

    
126
        if (mat != null) {
127
            vp.mat = new AffineTransform(mat);
128
        }
129

    
130
        if (extent != null) {
131
            vp.extent = new ExtentImpl(extent);
132
        }
133

    
134
        vp.proj = proj;
135
        vp.viewSize = viewSize;
136
        vp.dpi = dpi;
137

    
138
        return vp;
139
    }
140

    
141
    /**
142
     * Obtiene el ancho de la vista
143
     * @return
144
     */
145
    public double getWidth() {
146
        return viewSize.getWidth();
147
    }
148

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

    
157
    /**
158
     * Obtiene el tama?o de la vista
159
     */
160
    public Dimension2D getSize() {
161
        return viewSize;
162
    }
163

    
164
    /**
165
     * Asigna el ancho y alto de la vista
166
     * @param w Ancho
167
     * @param h Alto
168
     */
169
    public void setSize(double w, double h) {
170
        setSize(new Dimension((int) w, (int) h));
171
    }
172
    
173
    public void setSize(Dimension2D sz) {
174
            viewSize = sz;
175
        reExtent();
176
    }
177

    
178
    public Extent getExtent() {
179
        return extent;
180
    }
181

    
182
    public void setExtent(Dimension2D sz) {
183
        Point2D.Double pt0 = new Point2D.Double(0, 0);
184
        Point2D.Double ptSz = new Point2D.Double(sz.getWidth(), sz.getHeight());
185

    
186
        try {
187
            mat.inverseTransform(pt0, pt0);
188
            mat.inverseTransform(ptSz, ptSz);
189
        } catch (Exception e) {
190
            e.printStackTrace();
191
        }
192

    
193
        extent = new ExtentImpl(pt0, ptSz);
194
    }
195
    
196
    /**
197
     * Asigna la extensi?n del raster
198
     * @param ext Extent
199
     */
200
    public void setExtent(Extent ext) {
201
            extent = ext;
202
    }
203

    
204
    public void reExtent() {
205
        setExtent(viewSize);
206
    }
207

    
208
    public void setDPI(int dpi) {
209
        this.dpi = dpi;
210
    }
211

    
212
    public int getDPI() {
213
        return this.dpi;
214
    }
215

    
216
    /**
217
     * zoom a un marco.
218
     *
219
     * @param extent
220
     */
221
    public void zoom(Extent extent) {
222
        double[] scale = extent.getScale(getWidth(), getHeight());
223
        double escala = Math.min(scale[0], scale[1]);
224

    
225
        mat.setToIdentity();
226
        mat.scale(escala, -escala);
227
        mat.translate(-extent.minX(), -extent.maxY());
228
        this.extent = extent;
229
        reExtent();
230
    }
231

    
232
    /**
233
     * zoom centrado en un punto.
234
     *
235
     * @param zoom
236
     * @param pt
237
     */
238
    public void zoom(double zoom, Point2D pt) {
239
        zoom(zoom, zoom, pt);
240
    }
241

    
242
    public void zoom(double zx, double zy, Point2D pt) {
243
        centerAt(pt);
244
        mat.scale(zx, zy);
245
        centerAt(pt);
246
        reExtent();
247
    }
248

    
249
    /**
250
     * Zoom a una escala (geogr?fica);
251
     *
252
     * @param scale
253
     */
254
    public void zoomToCenter(double f) {
255
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
256
                                                     getHeight() / 2.0);
257

    
258
        try {
259
            mat.inverseTransform(ptCenter, ptCenter);
260
        } catch (Exception e) {
261
            e.printStackTrace();
262
        }
263

    
264
        zoom(f, ptCenter);
265
    }
266

    
267
    /**
268
     * Centrar en un punto.
269
     *
270
     * @param pt
271
     */
272
    public void centerAt(Point2D pt) {
273
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
274
                                                     getHeight() / 2.0);
275

    
276
        try {
277
            mat.inverseTransform(ptCenter, ptCenter);
278
            mat.translate(ptCenter.x - pt.getX(), ptCenter.y - pt.getY());
279
        } catch (Exception e) {
280
            e.printStackTrace();
281
        }
282

    
283
        reExtent();
284
    }
285

    
286
    /**
287
     * Desplaza la vista actual.
288
     *
289
     * @param pt
290
     */
291
    public void pan(Point2D ptIni, Point2D ptFin) {
292
        mat.translate(ptFin.getX() - ptIni.getX(), ptFin.getY() - ptIni.getY());
293
        reExtent();
294
    }
295

    
296
    public Point2D getCenter() {
297
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
298
                                                     getHeight() / 2.0);
299

    
300
        try {
301
            mat.inverseTransform(ptCenter, ptCenter);
302
        } catch (Exception e) {
303
            e.printStackTrace();
304
        }
305

    
306
        return ptCenter;
307
    }
308
    
309
    /**
310
     * Calcula la transformaci?n af?n.
311
     */
312
        public void calculateAffineTransform() {
313
                if ((viewSize == null) || (extent == null) ||
314
                        (viewSize.getWidth() <= 0) || (viewSize.getHeight() <= 0)) 
315
                        return;
316

    
317
                AffineTransform escalado = new AffineTransform();
318
                AffineTransform translacion = new AffineTransform();
319

    
320
                double escalaX;
321
                double escalaY;
322

    
323
                escalaX = viewSize.getWidth() / extent.width();
324
                escalaY = viewSize.getHeight() / extent.height();
325

    
326
                double xCenter = extent.getCenterX();
327
                double yCenter = extent.getCenterY();
328
                double newHeight;
329
                double newWidth;
330

    
331
                Rectangle2D adjustedExtent = new Rectangle2D.Double();
332
                double scale = 0;
333
                
334
                if (escalaX < escalaY) {
335
                        scale = escalaX;
336
                        newHeight = viewSize.getHeight() / scale;
337
                        adjustedExtent.setRect(xCenter - (extent.width() / 2.0),
338
                                        yCenter - (newHeight / 2.0), extent.width(), newHeight);
339
                } else {
340
                        scale = escalaY;
341
                        newWidth = viewSize.getWidth() / scale;
342
                        adjustedExtent.setRect(xCenter - (newWidth / 2.0),
343
                                        yCenter - (extent.height() / 2.0), newWidth,
344
                                        extent.height());
345
                }
346
                escalado.setToScale(scale, -scale);
347
                translacion.setToTranslation(-extent.getULX(), -extent.getULY());
348

    
349
                mat.setToIdentity();
350
                mat.concatenate(escalado);
351
                mat.concatenate(translacion);
352
        }
353
        
354
        public Time getTime() {
355
                return time;
356
        }
357

    
358
        public void setTime(Time time) {
359
                this.time = time;
360
        }
361
}