Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / shared / ViewPortData.java @ 11067

History | View | Annotate | Download (8.68 KB)

1 10740 nacho
/* 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.shared;
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
    public void reExtent() {
169
        setExtent(size);
170
    }
171
172
    public void setDPI(int dpi) {
173
        this.dpi = dpi;
174
    }
175
176
    public int getDPI() {
177
        return this.dpi;
178
    }
179
180
    /**
181
     * zoom a un marco.
182
     *
183
     * @param extent
184
     */
185
    public void zoom(Extent extent) {
186
        double[] scale = extent.getScale(getWidth(), getHeight());
187
        double escala = Math.min(scale[0], scale[1]);
188
189
        mat.setToIdentity();
190
        mat.scale(escala, -escala);
191
        mat.translate(-extent.minX(), -extent.maxY());
192
        this.extent = extent;
193
        reExtent();
194
    }
195
196
    /**
197
     * zoom centrado en un punto.
198
     *
199
     * @param zoom
200
     * @param pt
201
     */
202
    public void zoom(double zoom, Point2D pt) {
203
        zoom(zoom, zoom, pt);
204
    }
205
206
    public void zoom(double zx, double zy, Point2D pt) {
207
        centerAt(pt);
208
        mat.scale(zx, zy);
209
        centerAt(pt);
210
        reExtent();
211
    }
212
213
    /**
214
     * Zoom a una escala (geogr?fica);
215
     *
216
     * @param scale
217
     */
218
    public void zoomToGeoScale(double scale) {
219
        double actual = getGeoScale();
220
        double f = actual / scale;
221
        zoomToCenter(f);
222
    }
223
224
    /**
225
     * Zoom a una escala (geogr?fica);
226
     *
227
     * @param scale
228
     */
229
    public void zoomToCenter(double f) {
230
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
231
                                                     getHeight() / 2.0);
232
233
        try {
234
            mat.inverseTransform(ptCenter, ptCenter);
235
        } catch (Exception e) {
236
            e.printStackTrace();
237
        }
238
239
        zoom(f, ptCenter);
240
    }
241
242
    /**
243
     * Centrar en un punto.
244
     *
245
     * @param pt
246
     */
247
    public void centerAt(Point2D pt) {
248
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
249
                                                     getHeight() / 2.0);
250
251
        try {
252
            mat.inverseTransform(ptCenter, ptCenter);
253
            mat.translate(ptCenter.x - pt.getX(), ptCenter.y - pt.getY());
254
        } catch (Exception e) {
255
            e.printStackTrace();
256
        }
257
258
        reExtent();
259
    }
260
261
    /**
262
     * Desplaza la vista actual.
263
     *
264
     * @param pt
265
     */
266
    public void pan(Point2D ptIni, Point2D ptFin) {
267
        mat.translate(ptFin.getX() - ptIni.getX(), ptFin.getY() - ptIni.getY());
268
        reExtent();
269
    }
270
271
    public Point2D getCenter() {
272
        Point2D.Double ptCenter = new Point2D.Double(getWidth() / 2.0,
273
                                                     getHeight() / 2.0);
274
275
        try {
276
            mat.inverseTransform(ptCenter, ptCenter);
277
        } catch (Exception e) {
278
            e.printStackTrace();
279
        }
280
281
        return ptCenter;
282
    }
283
284
    /**
285
     * Escala Geogr?fica.
286
     *
287
     * @param dpi resolucion en puntos por pulgada
288
     */
289
    public double getGeoScale() {
290
        /* // TODO Actulizarlo para Geotools2
291
        double scale = 0.0;
292
        if (proj.getClass() == UtmZone.class) { // UTM;
293
                scale = (extent.maxX()-extent.minX())*        // metros
294
                        (dpi / 2.54 * 100.0)/                                // px / metro
295
                        getWidth();                                                        // pixels
296
        } else if (proj.getClass() == Geodetic.class) { // Geodetic
297
                scale = (extent.maxX()-extent.minX())*                // grados
298
                        // 1852.0 metros x minuto de meridiano
299
                        (dpi / 2.54 * 100.0 * 1852.0 * 60.0)/        // px / metro
300
                        getWidth();                                                                // pixels
301
        } else if (proj.getClass() == Mercator.class) { // Mercator
302
                Projection prj = Geodetic.getProjection((Ellipsoid) proj.getDatum());
303
                GeoPoint pt1 = (GeoPoint) prj.createPoint(1.0,0.0);
304
                GeoPoint pt2 = (GeoPoint) prj.createPoint(2.0,0.0);
305
                ProjPoint ppt1 = (ProjPoint) proj.createPoint(0.0, 0.0);
306
                ProjPoint ppt2 = (ProjPoint) proj.createPoint(0.0, 0.0);
307
                ((Mercator) proj).fromGeo(pt1, ppt1);
308
                ((Mercator) proj).fromGeo(pt2, ppt2);
309
                //scale = ppt2.getX()-ppt1.getX();
310
                scale =  ((extent.maxX()-extent.minX())/ (ppt2.getX()-ppt1.getX()) ) *
311
                //scale = ((extent.maxX()-extent.minX())/ getWidth());// *
312
                        (dpi / 2.54 * 100.0 * 1852.0 * 60.0) /
313
                        getWidth();
314
        } */
315
        return proj.getScale(extent.minX(), extent.maxX(), getWidth(), dpi);
316
    }
317
318
    public String getGeoScaleAsString(String fmt) {
319
        DecimalFormat format = new DecimalFormat(fmt);
320
321
        return "1:" + format.format(getGeoScale());
322
    }
323
}