Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / ViewPortData.java @ 12522

History | View | Annotate | Download (8.83 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. 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
package org.gvsig.raster.datastruct;
20

    
21
import java.awt.Dimension;
22
import java.awt.geom.AffineTransform;
23
import java.awt.geom.Dimension2D;
24
import java.awt.geom.Point2D;
25
import java.text.DecimalFormat;
26

    
27
import org.cresques.cts.ICoordTrans;
28
import org.cresques.cts.IProjection;
29
import org.cresques.geo.Projected;
30

    
31

    
32
/**
33
 * Datos de vista sobre las capas.
34
 *
35
 * Mantiene un conjunto de datos necesarios, que describen el modo de
36
 * ver las capas actual.
37
 *
38
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
39
 */
40
public class ViewPortData implements Projected {
41
    /**
42
     * Tipo de proyecci?n de la vista.
43
     */
44
    IProjection proj = null;
45

    
46
    /**
47
     * Sistema de coordenadas de la vista.
48
     */
49
    IProjection cs = null;
50

    
51
    /**
52
     * Amplitud de la vista, en coordenadas proyectadas.
53
     */
54
    Extent extent = null;
55

    
56
    /**
57
     * Tama?o de la vista, en coordenadas de dispositivo.
58
     */
59
    Dimension2D size = null;
60

    
61
    /**
62
     * Transformaci?n af?n usada en la vista actual.
63
     */
64
    public AffineTransform mat = null;
65

    
66
    /**
67
     * Resoluci?n (Puntos por pulgada) de la vista actual.
68
     * Se necesita para los c?lculos de escala geogr?fica.
69
     */
70
    int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
71

    
72
    public ViewPortData() {
73
    }
74

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

    
83
    public IProjection getProjection() {
84
        return proj;
85
    }
86

    
87
    public void setProjection(IProjection proj) {
88
        this.proj = proj;
89
    }
90

    
91
    public void reProject(ICoordTrans rp) {
92
        // TODO metodo reProject pendiente de implementar
93
    }
94

    
95
    public void setCoordSys(IProjection cs) {
96
        this.cs = cs;
97
    }
98

    
99
    //public void setCoordTrans(ICoordTrans ct) { this.ct = ct; }
100
    public AffineTransform getMat() {
101
        return mat;
102
    }
103

    
104
    public void setMat(AffineTransform mat) {
105
        this.mat = mat;
106
    }
107

    
108
    public Object clone() {
109
        ViewPortData vp = new ViewPortData();
110

    
111
        if (mat != null) {
112
            vp.mat = new AffineTransform(mat);
113
        }
114

    
115
        if (extent != null) {
116
            vp.extent = new Extent(extent);
117
        }
118

    
119
        vp.proj = proj;
120
        vp.size = size;
121
        vp.dpi = dpi;
122

    
123
        return vp;
124
    }
125

    
126
    public double getWidth() {
127
        return size.getWidth();
128
    }
129

    
130
    public double getHeight() {
131
        return size.getHeight();
132
    }
133

    
134
    /**
135
     *
136
     */
137
    public Dimension2D getSize() {
138
        return size;
139
    }
140

    
141
    public void setSize(double w, double h) {
142
        setSize(new Dimension((int) w, (int) h));
143
    }
144

    
145
    public void setSize(Dimension2D sz) {
146
        size = sz;
147
        reExtent();
148
    }
149

    
150
    public Extent getExtent() {
151
        return extent;
152
    }
153

    
154
    public void setExtent(Dimension2D sz) {
155
        Point2D.Double pt0 = new Point2D.Double(0, 0);
156
        Point2D.Double ptSz = new Point2D.Double(sz.getWidth(), sz.getHeight());
157

    
158
        try {
159
            mat.inverseTransform(pt0, pt0);
160
            mat.inverseTransform(ptSz, ptSz);
161
        } catch (Exception e) {
162
            e.printStackTrace();
163
        }
164

    
165
        extent = new Extent(pt0, ptSz);
166
    }
167
    
168
    /**
169
     * Asigna la extensi?n del raster
170
     * @param ext Extent
171
     */
172
    public void setExtent(Extent ext) {
173
            extent = ext;
174
    }
175

    
176
    public void reExtent() {
177
        setExtent(size);
178
    }
179

    
180
    public void setDPI(int dpi) {
181
        this.dpi = dpi;
182
    }
183

    
184
    public int getDPI() {
185
        return this.dpi;
186
    }
187

    
188
    /**
189
     * zoom a un marco.
190
     *
191
     * @param extent
192
     */
193
    public void zoom(Extent extent) {
194
        double[] scale = extent.getScale(getWidth(), getHeight());
195
        double escala = Math.min(scale[0], scale[1]);
196

    
197
        mat.setToIdentity();
198
        mat.scale(escala, -escala);
199
        mat.translate(-extent.minX(), -extent.maxY());
200
        this.extent = extent;
201
        reExtent();
202
    }
203

    
204
    /**
205
     * zoom centrado en un punto.
206
     *
207
     * @param zoom
208
     * @param pt
209
     */
210
    public void zoom(double zoom, Point2D pt) {
211
        zoom(zoom, zoom, pt);
212
    }
213

    
214
    public void zoom(double zx, double zy, Point2D pt) {
215
        centerAt(pt);
216
        mat.scale(zx, zy);
217
        centerAt(pt);
218
        reExtent();
219
    }
220

    
221
    /**
222
     * Zoom a una escala (geogr?fica);
223
     *
224
     * @param scale
225
     */
226
    public void zoomToGeoScale(double scale) {
227
        double actual = getGeoScale();
228
        double f = actual / scale;
229
        zoomToCenter(f);
230
    }
231

    
232
    /**
233
     * Zoom a una escala (geogr?fica);
234
     *
235
     * @param scale
236
     */
237
    public void zoomToCenter(double f) {
238
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
239
                                                     getHeight() / 2.0);
240

    
241
        try {
242
            mat.inverseTransform(ptCenter, ptCenter);
243
        } catch (Exception e) {
244
            e.printStackTrace();
245
        }
246

    
247
        zoom(f, ptCenter);
248
    }
249

    
250
    /**
251
     * Centrar en un punto.
252
     *
253
     * @param pt
254
     */
255
    public void centerAt(Point2D pt) {
256
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
257
                                                     getHeight() / 2.0);
258

    
259
        try {
260
            mat.inverseTransform(ptCenter, ptCenter);
261
            mat.translate(ptCenter.x - pt.getX(), ptCenter.y - pt.getY());
262
        } catch (Exception e) {
263
            e.printStackTrace();
264
        }
265

    
266
        reExtent();
267
    }
268

    
269
    /**
270
     * Desplaza la vista actual.
271
     *
272
     * @param pt
273
     */
274
    public void pan(Point2D ptIni, Point2D ptFin) {
275
        mat.translate(ptFin.getX() - ptIni.getX(), ptFin.getY() - ptIni.getY());
276
        reExtent();
277
    }
278

    
279
    public Point2D getCenter() {
280
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
281
                                                     getHeight() / 2.0);
282

    
283
        try {
284
            mat.inverseTransform(ptCenter, ptCenter);
285
        } catch (Exception e) {
286
            e.printStackTrace();
287
        }
288

    
289
        return ptCenter;
290
    }
291

    
292
    /**
293
     * Escala Geogr?fica.
294
     *
295
     * @param dpi resolucion en puntos por pulgada
296
     */
297
    public double getGeoScale() {
298
        /* // TODO Actulizarlo para Geotools2
299
        double scale = 0.0;
300
        if (proj.getClass() == UtmZone.class) { // UTM;
301
                scale = (extent.maxX()-extent.minX())*        // metros
302
                        (dpi / 2.54 * 100.0)/                                // px / metro
303
                        getWidth();                                                        // pixels
304
        } else if (proj.getClass() == Geodetic.class) { // Geodetic
305
                scale = (extent.maxX()-extent.minX())*                // grados
306
                        // 1852.0 metros x minuto de meridiano
307
                        (dpi / 2.54 * 100.0 * 1852.0 * 60.0)/        // px / metro
308
                        getWidth();                                                                // pixels
309
        } else if (proj.getClass() == Mercator.class) { // Mercator
310
                Projection prj = Geodetic.getProjection((Ellipsoid) proj.getDatum());
311
                GeoPoint pt1 = (GeoPoint) prj.createPoint(1.0,0.0);
312
                GeoPoint pt2 = (GeoPoint) prj.createPoint(2.0,0.0);
313
                ProjPoint ppt1 = (ProjPoint) proj.createPoint(0.0, 0.0);
314
                ProjPoint ppt2 = (ProjPoint) proj.createPoint(0.0, 0.0);
315
                ((Mercator) proj).fromGeo(pt1, ppt1);
316
                ((Mercator) proj).fromGeo(pt2, ppt2);
317
                //scale = ppt2.getX()-ppt1.getX();
318
                scale =  ((extent.maxX()-extent.minX())/ (ppt2.getX()-ppt1.getX()) ) *
319
                //scale = ((extent.maxX()-extent.minX())/ getWidth());// *
320
                        (dpi / 2.54 * 100.0 * 1852.0 * 60.0) /
321
                        getWidth();
322
        } */
323
        return proj.getScale(extent.minX(), extent.maxX(), getWidth(), dpi);
324
    }
325

    
326
    public String getGeoScaleAsString(String fmt) {
327
        DecimalFormat format = new DecimalFormat(fmt);
328

    
329
        return "1:" + format.format(getGeoScale());
330
    }
331
}