Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / geo / ViewPortData.java @ 92

History | View | Annotate | Download (5.8 KB)

1
/*
2
 * Created on 25-abr-2004
3
 */
4
package org.cresques.geo;
5

    
6
import java.awt.Dimension;
7
import java.awt.geom.AffineTransform;
8
import java.awt.geom.Dimension2D;
9
import java.awt.geom.Point2D;
10
import java.text.DecimalFormat;
11

    
12
import org.cresques.px.Extent;
13

    
14
/**
15
 * Datos de vista sobre las capas.
16
 * 
17
 * Mantiene un conjunto de datos necesarios, que describen el modo de
18
 * ver las capas actual.
19
 * 
20
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
21
 */
22
public class ViewPortData implements Projected {
23
        /**
24
         * Tipo de proyecci?n de la vista.
25
         */
26
        Projection proj = null;
27
        /**
28
         * Amplitud de la vista, en coordenadas proyectadas.
29
         */
30
        Extent extent = null;
31
        /**
32
         * Tama?o de la vista, en coordenadas de dispositivo.
33
         */
34
        Dimension2D size = null;
35
        /**
36
         * Transformaci?n af?n usada en la vista actual.
37
         */
38
        public AffineTransform mat = null;
39
        /**
40
         * Resoluci?n (Puntos por pulgada) de la vista actual.
41
         * Se necesita para los c?lculos de escala geogr?fica.
42
         */
43
        int dpi = 72;
44
        
45
        public ViewPortData(){ }
46

    
47
        public ViewPortData(Projection proj, Extent extent, Dimension2D size) {
48
                this.proj = proj;
49
                this.extent = extent;
50
                this.size = size;
51
                mat = new AffineTransform();
52
                mat.scale(1.0, -1.0);
53
        }
54

    
55
        public Projection getProjection() { return proj; }
56
        public void setProjection(Projection proj) { this.proj = proj; }
57
        public void reProject(ReProjection rp) {
58
                // TODO metodo reProject pendiente de implementar
59
        }
60

    
61
        public AffineTransform getMat() { return mat; }
62
        public void setMat(AffineTransform mat) { this.mat = mat; }
63

    
64
        public Object clone() {
65
                ViewPortData vp = new ViewPortData();
66
                if (mat != null)
67
                        vp.mat = new AffineTransform(mat);
68
                if (extent != null)
69
                        vp.extent = new Extent(extent);
70
                vp.proj = proj;
71
                vp.size = size;
72
                vp.dpi = dpi;
73
                return vp;
74
        }
75

    
76
        public double getWidth() { return size.getWidth(); } 
77
        public double getHeight() { return size.getHeight(); }
78
        /**
79
         * 
80
         */
81
        public Dimension2D getSize() {
82
                return size;                
83
        }
84
        public void setSize(double w, double h) {
85
                setSize(new Dimension((int) w, (int) h));
86
        }
87
        public void setSize(Dimension2D sz) {
88
                size = sz;
89
                reExtent();
90
        }
91
        
92
        public Extent getExtent() { return extent; }
93
        public void setExtent(Dimension2D sz) {
94
                Point2D.Double pt0 = new Point2D.Double(0,0), 
95
                        ptSz = new Point2D.Double(sz.getWidth(), sz.getHeight());
96
                try {
97
                        mat.inverseTransform(pt0, pt0);
98
                        mat.inverseTransform(ptSz, ptSz);
99
                } catch (Exception e) {
100
                        e.printStackTrace();
101
                }
102
                extent = new Extent(pt0, ptSz);
103
        }
104
        public void reExtent() {
105
                setExtent(size);
106
        }
107
        public void setDPI(int dpi) { this.dpi = dpi; }
108

    
109
        /**
110
         * zoom a un marco.
111
         * 
112
         * @param extent
113
         */
114
        
115
        public void zoom(Extent extent) {
116
                double [] scale = extent.getScale(getWidth(), getHeight());
117
                double escala = Math.min(scale[0], scale[1]);
118
                
119
                mat.setToIdentity();
120
                mat.scale(escala, -escala);
121
                mat.translate(-extent.minX(), -extent.maxY());
122
                this.extent = extent;
123
                reExtent();
124
        }
125
        
126
        /**
127
         * zoom centrado en un punto.
128
         * 
129
         * @param zoom
130
         * @param pt
131
         */
132
        
133
        public void zoom(double zoom, Point2D pt) {
134
                zoom(zoom, zoom, pt);
135
        }
136
        public void zoom(double zx, double zy, Point2D pt) {
137
                centerAt(pt);
138
                mat.scale(zx, zy);
139
                centerAt(pt);
140
                reExtent();
141
        }
142
        
143
        /**
144
         * Zoom a una escala (geogr?fica);
145
         * 
146
         * @param scale
147
         */
148
        
149
        public void zoomToGeoScale(double scale) {
150
                double actual = getGeoScale();
151
                double f = actual/scale;
152
                zoomToCenter(f);
153
        }
154
        
155
        /**
156
         * Zoom a una escala (geogr?fica);
157
         * 
158
         * @param scale
159
         */
160
        
161
        public void zoomToCenter(double f) {
162
                Point2D.Double ptCenter = new Point2D.Double(getWidth()/2.0,getHeight()/2.0);
163
                try {
164
                        mat.inverseTransform(ptCenter, ptCenter);
165
                } catch (Exception e) {
166
                        e.printStackTrace();
167
                }
168
                zoom(f, ptCenter);
169
        }
170
        
171
        /**
172
         * Centrar en un punto.
173
         * 
174
         * @param pt
175
         */
176
        
177
        public void centerAt(Point2D pt) {
178
                Point2D.Double ptCenter = new Point2D.Double(getWidth()/2.0,getHeight()/2.0);
179
                try {
180
                        mat.inverseTransform(ptCenter, ptCenter);
181
                        mat.translate(ptCenter.x-pt.getX(), ptCenter.y-pt.getY());
182
                } catch (Exception e) {
183
                        e.printStackTrace();
184
                }
185
                reExtent();
186
        }
187
        
188
        /**
189
         * Desplaza la vista actual.
190
         * 
191
         * @param pt
192
         */
193
        
194
        public void pan(Point2D ptIni, Point2D ptFin) {
195
                mat.translate(ptFin.getX()-ptIni.getX(), ptFin.getY()-ptIni.getY());
196
                reExtent();
197
        }
198
        
199
        public Point2D getCenter() {
200
                Point2D.Double ptCenter = new Point2D.Double(getWidth()/2.0,getHeight()/2.0);
201
                try {
202
                        mat.inverseTransform(ptCenter, ptCenter);
203
                } catch (Exception e) {
204
                        e.printStackTrace();
205
                }
206
                return ptCenter;
207
        }
208
        
209
        /**
210
         * Escala Geogr?fica.
211
         * 
212
         * @param dpi resolucion en puntos por pulgada
213
         */
214
        
215
        public double getGeoScale() {
216
                double scale = 0.0;
217
                if (proj.getClass() == UtmZone.class) { // UTM;
218
                        scale = (extent.maxX()-extent.minX())*        // metros
219
                                (dpi / 2.54 * 100.0)/                                // px / metro
220
                                getWidth();                                                        // pixels
221
                } else if (proj.getClass() == Geodetic.class) { // Geodetic
222
                        scale = (extent.maxX()-extent.minX())*                // grados
223
                                // 1852.0 metros x minuto de meridiano
224
                                (dpi / 2.54 * 100.0 * 1852.0 * 60.0)/        // px / metro
225
                                getWidth();                                                                // pixels
226
                } else if (proj.getClass() == Mercator.class) { // Mercator
227
                        Projection prj = Geodetic.getProjection(proj.getEllipsoid());
228
                        GeoPoint pt1 = (GeoPoint) prj.createPoint(1.0,0.0);
229
                        GeoPoint pt2 = (GeoPoint) prj.createPoint(2.0,0.0);
230
                        ProjPoint ppt1 = (ProjPoint) proj.createPoint(0.0, 0.0);
231
                        ProjPoint ppt2 = (ProjPoint) proj.createPoint(0.0, 0.0);
232
                        ((Mercator) proj).fromGeo(pt1, ppt1);
233
                        ((Mercator) proj).fromGeo(pt2, ppt2);
234
                        //scale = ppt2.getX()-ppt1.getX();
235
                        scale =  ((extent.maxX()-extent.minX())/ (ppt2.getX()-ppt1.getX()) ) *
236
                        //scale = ((extent.maxX()-extent.minX())/ getWidth());// *
237
                                (dpi / 2.54 * 100.0 * 1852.0 * 60.0) /
238
                                getWidth();
239
                } 
240
                return scale;
241
        }
242
        
243
        public String getGeoScaleAsString(String fmt) {
244
                DecimalFormat format = new DecimalFormat(fmt);
245
                return "1:"+format.format(getGeoScale());
246
        }
247

    
248
}