Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 449

History | View | Annotate | Download (10.3 KB)

1
/* Generated by Together */
2

    
3
package com.iver.cit.gvsig.fmap;
4

    
5
import java.awt.Color;
6
import java.awt.Dimension;
7
import java.awt.Point;
8
import java.awt.geom.AffineTransform;
9
import java.awt.geom.NoninvertibleTransformException;
10
import java.awt.geom.Point2D;
11
import java.awt.geom.Rectangle2D;
12
import java.util.ArrayList;
13

    
14
import org.cresques.cts.IProjection;
15
import org.cresques.cts.ProjectionPool;
16

    
17
import com.iver.utiles.StringUtilities;
18
import com.iver.utiles.XMLEntity;
19

    
20
public class ViewPort {        
21
        public static int KILOMETROS = 1;
22
        public static int METROS = 2;
23
        public static int MILLAS = 3;
24
        public static int YARDAS = 4;
25
        public static int PIES = 5;
26
        public static int PULGADAS = 6;
27

    
28
        /**
29
         * Resoluci?n (Puntos por pulgada) de la vista actual.
30
         * Se necesita para los c?lculos de escala geogr?fica.
31
         */
32
        private static int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
33
        
34
    private Rectangle2D extent;
35
    private Rectangle2D adjustedExtent;
36
    private ExtentHistory extents = new ExtentHistory();
37
    private Dimension imageSize;
38
    private AffineTransform trans = new AffineTransform();
39
        private int distanceUnits = METROS;
40
        private int mapUnits = METROS;
41

    
42
    private ArrayList extentListeners=new ArrayList();
43
    private Point2D offset = new Point2D.Double(0, 0);
44
    private Rectangle2D clip;
45
        private Color backColor = Color.WHITE;
46
        
47
        private IProjection proj;
48
        
49
    private double dist1pixel;
50
    private double dist3pixel;
51
        
52
        
53
        private double scale;
54

    
55
        public boolean addExtentListener(ExtentListener arg0) {
56
                return extentListeners.add(arg0);
57
        }
58

    
59
        public boolean removeExtentListener(ExtentListener arg0) {
60
                return extentListeners.remove(arg0);
61
        }
62

    
63
    public int fromMapDistance(double d) {
64
        Point2D.Double pWorld = new Point2D.Double(1, 1);
65
        Point2D.Double pScreen = new Point2D.Double();
66

    
67
        double nuevoX;
68
        double nuevoY;
69
        double cX;
70
        double cY;
71

    
72
        try {
73
            trans.deltaTransform(pWorld, pScreen);
74
        } catch (Exception e) {
75
            System.err.print(e.getMessage());
76
        }
77

    
78
        return (int) (d * pScreen.x);
79
    }
80

    
81
    public Point2D fromMapPoint(double x, double y) {
82
        Point2D.Double pWorld = new Point2D.Double(x, y);
83
        Point2D.Double pScreen = new Point2D.Double();
84

    
85
        double nuevoX;
86
        double nuevoY;
87
        double cX;
88
        double cY;
89

    
90
        try {
91
            trans.transform(pWorld, pScreen);
92
        } catch (Exception e) {
93
            System.err.print(e.getMessage());
94
        }
95

    
96
        return pScreen;
97
    }
98

    
99
    public Point2D toMapPoint(int x, int y) {
100
            Point pScreen = new Point(x, y);
101
            return toMapPoint(pScreen);
102
    }
103

    
104
    public double toMapDistance(int d) {
105
             double dist = d / trans.getScaleX(); 
106
         return dist;
107
    }
108

    
109
    public Point2D toMapPoint(Point2D pScreen) {
110
        Point2D.Double pWorld = new Point2D.Double();
111
        AffineTransform at;
112

    
113
        try {
114
            at = trans.createInverse();
115
            at.transform(pScreen, pWorld);
116
        } catch (NoninvertibleTransformException e) {
117
            throw new RuntimeException(e);
118
        }
119

    
120
        return pWorld;
121
    }
122

    
123
    public void setPreviousExtent() {
124
    }
125

    
126
    public Rectangle2D getExtent() {
127
            return extent;
128
    }
129

    
130
    public void setExtent(Rectangle2D r) {
131
            
132
            //Esto comprueba que el extent no es de anchura o altura = "0" 
133
            //y si es as? lo redimensiona.
134
            if (r.getWidth()==0 || r.getHeight()==0){
135
                    extent=new Rectangle2D.Double(r.getMinX()-0.1,r.getMinY()-0.1,r.getWidth()+0.2,r.getHeight()+0.2);
136
            }else{
137
            extent = r;
138
            }
139
            //TODO calcular la escala sin usar setScale
140
            
141
            //Calcula la transformaci?n af?n
142
            calculateAffineTransform();
143
    }
144

    
145
    public void setScale(double scale) {
146
            this.scale = scale;
147
            
148
            //TODO calcular el extent sin usar setExtent
149
            
150
            //Calcula la transformaci?n af?n
151
            calculateAffineTransform();
152
    }
153

    
154
    /**
155
     * Devuelve la escala. Debe estar siempre actualizada y
156
     * no calcularse nunca aqu? pues se utiliza en el dibujado
157
     * para cada geometr?a
158
     * @return
159
     */
160
    public double getScale() {
161
            return proj.getScale(extent.getMinX(), extent.getMaxX(), imageSize.getWidth(), dpi);
162
    }
163

    
164
        /**
165
         * @return
166
         */
167
        public AffineTransform getAffineTransform() {
168
                return trans;
169
        }
170
        /**
171
         * @return Returns the imageSize.
172
         */
173
        public Dimension getImageSize() {
174
                return imageSize;
175
        }
176
        /**
177
         * @param imageSize The imageSize to set.
178
         */
179
        public void setImageSize(Dimension imageSize) {
180
                this.imageSize = imageSize;
181
                calculateAffineTransform();
182
        }
183

    
184
        /**
185
         * 
186
         */
187
        private void calculateAffineTransform() {
188
                if ((imageSize == null) || (extent == null)) return;
189
                        
190
        AffineTransform escalado = new AffineTransform();
191
        AffineTransform translacion = new AffineTransform();
192

    
193
        double escalaX;
194
        double escalaY;
195

    
196
        escalaX = imageSize.getWidth() / extent.getWidth();
197
        escalaY = imageSize.getHeight() / extent.getHeight();
198

    
199
        adjustedExtent = new Rectangle2D.Double();
200
        if (escalaX < escalaY) {
201
            scale = escalaX;
202
            adjustedExtent.setRect(extent.getX(), extent.getY(), extent.getWidth(), imageSize.getHeight() / scale);
203
        } else {
204
            scale = escalaY;
205
            adjustedExtent.setRect(extent.getX(), extent.getY(), imageSize.getWidth() / scale, extent.getHeight());
206
        }
207

    
208
        translacion.setToTranslation(-adjustedExtent.getX(), -adjustedExtent.getY() -
209
                        adjustedExtent.getHeight());
210
        escalado.setToScale(scale, -scale);
211

    
212
        AffineTransform offsetTrans = new AffineTransform();
213
        offsetTrans.setToTranslation(offset.getX(), offset.getY());
214

    
215
        trans.setToIdentity();
216
        trans.concatenate(offsetTrans);
217
        trans.concatenate(escalado);
218

    
219
        trans.concatenate(translacion);
220

    
221
        
222
        // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n 
223
        // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
224
        AffineTransform at;
225
                try {
226
                        at = trans.createInverse();
227
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
228
                Point2D.Float pProv = new Point2D.Float();
229
                at.deltaTransform(pPixel, pProv);
230

    
231
                dist1pixel = pProv.x;
232
                dist3pixel = 3 * pProv.x;
233
                        
234
                } catch (NoninvertibleTransformException e) {
235
                        e.printStackTrace();
236
                }
237
        
238
        }
239
        public void setOffset(Point2D p){
240
                offset=p;
241
        }
242
        public void setBackColor(Color c) {
243
                backColor = c;
244
        }
245

    
246
        public Color getBackColor() {
247
                return backColor;
248
        }
249
        /**
250
         * @return Returns the adjustedExtent.
251
         */
252
        public Rectangle2D getAdjustedExtent() {
253
                return adjustedExtent;
254
        }
255
        /**
256
         * @return Returns the distanceUnits.
257
         */
258
        public int getDistanceUnits() {
259
                return distanceUnits;
260
        }
261
        /**
262
         * @param distanceUnits The distanceUnits to set.
263
         */
264
        public void setDistanceUnits(int distanceUnits) {
265
                this.distanceUnits = distanceUnits;
266
        }
267
        /**
268
         * @return Returns the mapUnits.
269
         */
270
        public int getMapUnits() {
271
                return mapUnits;
272
        }
273
        /**
274
         * @param mapUnits The mapUnits to set.
275
         */
276
        public void setMapUnits(int mapUnits) {
277
                this.mapUnits = mapUnits;
278
        }        
279
        
280
        public int getImageWidth(){
281
                return imageSize.width;
282
        }
283
        
284
        public int getImageHeight(){
285
                return imageSize.height;
286
        }
287
        public double getDist1pixel() {
288
                return dist1pixel;
289
        }
290
        public void setDist1pixel(double dist1pixel) {
291
                this.dist1pixel = dist1pixel;
292
        }
293
        public double getDist3pixel() {
294
                return dist3pixel;
295
        }
296
        public void setDist3pixel(double dist3pixel) {
297
                this.dist3pixel = dist3pixel;
298
        }
299
        /**
300
         * @return Returns the extents.
301
         */
302
        public ExtentHistory getExtents() {
303
                return extents;
304
        }
305

    
306
        /**
307
         * 
308
         */
309
        public void setExtentPrev() {
310
                setExtent(extents.removePrev());
311
        }
312
        /**
313
         * @return Returns the proj.
314
         */
315
        public IProjection getProjection() {
316
                return proj;
317
        }
318
        /**
319
         * @param proj The proj to set.
320
         */
321
        public void setProjection(IProjection proj) {
322
                this.proj = proj;
323
        }
324
        public XMLEntity getXMLEntity(){
325
                XMLEntity xml=new XMLEntity();
326
                xml.putProperty("adjustedExtentX",adjustedExtent.getX());
327
                xml.putProperty("adjustedExtentY",adjustedExtent.getY());
328
                xml.putProperty("adjustedExtentW",adjustedExtent.getWidth());
329
                xml.putProperty("adjustedExtentH",adjustedExtent.getHeight());
330
                xml.putProperty("backColor",StringUtilities.color2String(backColor));
331
                if (clip!=null){
332
                xml.putProperty("clipX",clip.getX());
333
                xml.putProperty("clipY",clip.getY());
334
                xml.putProperty("clipW",clip.getWidth());
335
                xml.putProperty("clipH",clip.getHeight());
336
                }
337
                xml.putProperty("dist1pixel",dist1pixel);
338
                xml.putProperty("dist3pixel",dist3pixel);
339
                xml.putProperty("distanceUnits",distanceUnits);
340
                xml.putProperty("extentX",extent.getX());
341
                xml.putProperty("extentY",extent.getY());
342
                xml.putProperty("extentW",extent.getWidth());
343
                xml.putProperty("extentH",extent.getHeight());
344
                xml.addChild(extents.getXMLEntity());
345
                xml.putProperty("mapUnits",mapUnits);
346
                xml.putProperty("offsetX",offset.getX());
347
                xml.putProperty("offsetY",offset.getY());
348
                if (proj!=null){
349
                        xml.putProperty("proj",proj.getAbrev());
350
                }
351
                xml.putProperty("scale",scale);
352
                
353
                return xml;
354
        }
355
        public static ViewPort createFromXML(XMLEntity xml){
356
                ViewPort vp=new ViewPort();
357
                vp.adjustedExtent=new Rectangle2D.Double(xml.getDoubleProperty("adjustedExtentX"),xml.getDoubleProperty("adjustedExtentY"),xml.getDoubleProperty("adjustedExtentW"),xml.getDoubleProperty("adjustedExtentH"));
358
                if (xml.getStringProperty("backColor")!=null){
359
                vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty("backColor")));
360
                }
361
                vp.clip=new Rectangle2D.Double(xml.getDoubleProperty("clipX"),xml.getDoubleProperty("clipY"),xml.getDoubleProperty("clipW"),xml.getDoubleProperty("clipH"));
362
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
363
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
364
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
365
                vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),xml.getDoubleProperty("extentY"),xml.getDoubleProperty("extentW"),xml.getDoubleProperty("extentH")));
366
                vp.extents=ExtentHistory.createFromXML(xml.getChild(0));
367
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
368
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),xml.getDoubleProperty("offsetY")));
369
                if (xml.getStringProperty("proj")!=null){
370
                        vp.proj=ProjectionPool.get(xml.getStringProperty("proj"));
371
                }
372
                vp.setScale(xml.getDoubleProperty("scale"));
373
                return vp;
374
        }
375
}