Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / geo / ViewPortData.java @ 157

History | View | Annotate | Download (6.21 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.cts.ICoordTrans;
13
import org.cresques.cts.IProjection;
14
import org.cresques.px.Extent;
15

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

    
53
        public ViewPortData(IProjection proj, Extent extent, Dimension2D size) {
54
                this.proj = proj;
55
                this.extent = extent;
56
                this.size = size;
57
                mat = new AffineTransform();
58
                mat.scale(1.0, -1.0);
59
        }
60

    
61
        public IProjection getProjection() { return proj; }
62
        public void setProjection(IProjection proj) { this.proj = proj; }
63
        public void reProject(ICoordTrans rp) {
64
                // TODO metodo reProject pendiente de implementar
65
        }
66

    
67
        public void setCoordSys(IProjection cs) { this.cs = cs; }
68
        //public void setCoordTrans(ICoordTrans ct) { this.ct = ct; }
69

    
70
        public AffineTransform getMat() { return mat; }
71
        public void setMat(AffineTransform mat) { this.mat = mat; }
72

    
73
        public Object clone() {
74
                ViewPortData vp = new ViewPortData();
75
                if (mat != null)
76
                        vp.mat = new AffineTransform(mat);
77
                if (extent != null)
78
                        vp.extent = new Extent(extent);
79
                vp.proj = proj;
80
                vp.size = size;
81
                vp.dpi = dpi;
82
                return vp;
83
        }
84

    
85
        public double getWidth() { return size.getWidth(); } 
86
        public double getHeight() { return size.getHeight(); }
87
        /**
88
         * 
89
         */
90
        public Dimension2D getSize() {
91
                return size;                
92
        }
93
        public void setSize(double w, double h) {
94
                setSize(new Dimension((int) w, (int) h));
95
        }
96
        public void setSize(Dimension2D sz) {
97
                size = sz;
98
                reExtent();
99
        }
100
        
101
        public Extent getExtent() { return extent; }
102
        public void setExtent(Dimension2D sz) {
103
                Point2D.Double pt0 = new Point2D.Double(0,0), 
104
                        ptSz = new Point2D.Double(sz.getWidth(), sz.getHeight());
105
                try {
106
                        mat.inverseTransform(pt0, pt0);
107
                        mat.inverseTransform(ptSz, ptSz);
108
                } catch (Exception e) {
109
                        e.printStackTrace();
110
                }
111
                extent = new Extent(pt0, ptSz);
112
        }
113
        public void reExtent() {
114
                setExtent(size);
115
        }
116
        public void setDPI(int dpi) { this.dpi = dpi; }
117
        public int getDPI() { return this.dpi; }
118

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

    
259
}