Statistics
| Revision:

root / org.gvsig.proj / branches / refactor2018 / org.gvsig.proj / org.gvsig.proj.lib / org.gvsig.proj.lib.api / src / main / java / org / cresques / geo / ViewPortData.java @ 827

History | View | Annotate | Download (8.83 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.cresques.geo;
25

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

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

    
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
 * cmartinez: Esta clase no deber?a formar parte de una API, pero
43
 * se deja hasta que se aborde el refactoring de libProjection.
44
 *
45
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
46
 */
47
public class ViewPortData implements Projected {
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
    Dimension size = null;
67

    
68
    /**
69
     * Transformaci?n af?n usada en la vista actual.
70
     */
71
    public AffineTransform mat = null;
72

    
73
    /**
74
     * Resoluci?n (Puntos por pulgada) de la vista actual.
75
     * Se necesita para los c?lculos de escala geogr?fica.
76
     */
77
    int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
78

    
79
    public ViewPortData() {
80
    }
81

    
82
    public ViewPortData(IProjection proj, Extent extent, Dimension size) {
83
        this.proj = proj;
84
        this.extent = extent;
85
        this.size = size;
86
        mat = new AffineTransform();
87
        mat.scale(1.0, -1.0);
88
    }
89

    
90
    public IProjection getProjection() {
91
        return proj;
92
    }
93

    
94
    public void setProjection(IProjection proj) {
95
        this.proj = proj;
96
    }
97

    
98
    public void reProject(ICoordTrans rp) {
99
        // TODO metodo reProject pendiente de implementar
100
    }
101

    
102
    public void setCoordSys(IProjection cs) {
103
        this.cs = cs;
104
    }
105

    
106
    //public void setCoordTrans(ICoordTrans ct) { this.ct = ct; }
107
    public AffineTransform getMat() {
108
        return mat;
109
    }
110

    
111
    public void setMat(AffineTransform mat) {
112
        this.mat = mat;
113
    }
114

    
115
    public Object clone() {
116
        ViewPortData vp = new ViewPortData();
117

    
118
        if (mat != null) {
119
            vp.mat = new AffineTransform(mat);
120
        }
121

    
122
        if (extent != null) {
123
            vp.extent = new Extent(extent);
124
        }
125

    
126
        vp.proj = proj;
127
        vp.size = size;
128
        vp.dpi = dpi;
129

    
130
        return vp;
131
    }
132

    
133
    public double getWidth() {
134
        return size.width;
135
    }
136

    
137
    public double getHeight() {
138
        return size.height;
139
    }
140

    
141
    /**
142
     *
143
     */
144
    public Dimension getSize() {
145
        return size;
146
    }
147

    
148
    public void setSize(double w, double h) {
149
        setSize(new Dimension((int) w, (int) h));
150
    }
151

    
152
    public void setSize(Dimension sz) {
153
        size = sz;
154
        reExtent();
155
    }
156

    
157
    public Extent getExtent() {
158
        return extent;
159
    }
160

    
161
    public void setExtent(Dimension sz) {
162
        Point2D.Double pt0 = new Point2D.Double(0, 0);
163
        Point2D.Double ptSz = new Point2D.Double(sz.width, sz.height);
164

    
165
        try {
166
            mat.inverseTransform(pt0, pt0);
167
            mat.inverseTransform(ptSz, ptSz);
168
        } catch (Exception e) {
169
            e.printStackTrace();
170
        }
171

    
172
        extent = new Extent(pt0, ptSz);
173
    }
174

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

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

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

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

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

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

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

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

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

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

    
246
        zoom(f, ptCenter);
247
    }
248

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

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

    
265
        reExtent();
266
    }
267

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

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

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

    
288
        return ptCenter;
289
    }
290

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

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

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