Statistics
| Revision:

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

History | View | Annotate | Download (12.1 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 ViewPort(IProjection proj)
56
        {
57
                // Por defecto
58
                this.proj = proj;                
59
        }
60

    
61
        public boolean addExtentListener(ExtentListener arg0) {
62
                return extentListeners.add(arg0);
63
        }
64

    
65
        public boolean removeExtentListener(ExtentListener arg0) {
66
                return extentListeners.remove(arg0);
67
        }
68

    
69
    public int fromMapDistance(double d) {
70
        Point2D.Double pWorld = new Point2D.Double(1, 1);
71
        Point2D.Double pScreen = new Point2D.Double();
72

    
73
        double nuevoX;
74
        double nuevoY;
75
        double cX;
76
        double cY;
77

    
78
        try {
79
            trans.deltaTransform(pWorld, pScreen);
80
        } catch (Exception e) {
81
            System.err.print(e.getMessage());
82
        }
83

    
84
        return (int) (d * pScreen.x);
85
    }
86

    
87
    public Point2D fromMapPoint(double x, double y) {
88
        Point2D.Double pWorld = new Point2D.Double(x, y);
89
        Point2D.Double pScreen = new Point2D.Double();
90

    
91
        double nuevoX;
92
        double nuevoY;
93
        double cX;
94
        double cY;
95

    
96
        try {
97
            trans.transform(pWorld, pScreen);
98
        } catch (Exception e) {
99
            System.err.print(e.getMessage());
100
        }
101

    
102
        return pScreen;
103
    }
104

    
105
    public Point2D toMapPoint(int x, int y) {
106
            Point pScreen = new Point(x, y);
107
            return toMapPoint(pScreen);
108
    }
109

    
110
    public double toMapDistance(int d) {
111
             double dist = d / trans.getScaleX(); 
112
         return dist;
113
    }
114

    
115
    public Point2D toMapPoint(Point2D pScreen) {
116
        Point2D.Double pWorld = new Point2D.Double();
117
        AffineTransform at;
118

    
119
        try {
120
            at = trans.createInverse();
121
            at.transform(pScreen, pWorld);
122
        } catch (NoninvertibleTransformException e) {
123
            throw new RuntimeException(e);
124
        }
125

    
126
        return pWorld;
127
    }
128

    
129
    public void setPreviousExtent() {
130
    }
131

    
132
    public Rectangle2D getExtent() {
133
            return extent;
134
    }
135

    
136
    public void setExtent(Rectangle2D r) {
137
            if (extent!=null){
138
                    extents.put(extent);
139
            }
140
            //Esto comprueba que el extent no es de anchura o altura = "0" 
141
            //y si es as? lo redimensiona.
142
            if (r.getWidth()==0 || r.getHeight()==0){
143
                    extent=new Rectangle2D.Double(r.getMinX()-0.1,r.getMinY()-0.1,r.getWidth()+0.2,r.getHeight()+0.2);
144
            }else{
145
                    extent = r;
146
            }
147
            //Calcula la transformaci?n af?n
148
            calculateAffineTransform();
149
            
150
        // Lanzamos los eventos de extent cambiado
151
        callExtentListeners(adjustedExtent);           
152
    }
153

    
154
    public void setScale(double scale) {
155
            this.scale = scale;
156

    
157
            //Calcula la transformaci?n af?n
158
            calculateAffineTransform();
159

    
160
        // Lanzamos los eventos de extent cambiado
161
        callExtentListeners(adjustedExtent);
162
    }
163

    
164
    /**
165
     * Devuelve la escala. Debe estar siempre actualizada y
166
     * no calcularse nunca aqu? pues se utiliza en el dibujado
167
     * para cada geometr?a
168
     * @return
169
     */
170
    public double getScale() {
171
            return proj.getScale(extent.getMinX(), extent.getMaxX(), imageSize.getWidth(), dpi);
172
    }
173

    
174
        /**
175
         * @return
176
         */
177
        public AffineTransform getAffineTransform() {
178
                return trans;
179
        }
180
        /**
181
         * @return Returns the imageSize.
182
         */
183
        public Dimension getImageSize() {
184
                return imageSize;
185
        }
186
        /**
187
         * @param imageSize The imageSize to set.
188
         */
189
        public void setImageSize(Dimension imageSize) {
190
                this.imageSize = imageSize;
191
                calculateAffineTransform();
192
        }
193
        
194
    private void callExtentListeners(Rectangle2D newRect) {
195
            ExtentEvent ev = new ExtentEvent(newRect);
196
        for (int i = 0; i < extentListeners.size(); i++) {
197
            ExtentListener listener = (ExtentListener) extentListeners.get(i);
198
            listener.extentChanged(ev);
199
        }
200
    }
201

    
202

    
203
        /**
204
         * 
205
         */
206
        private void calculateAffineTransform() {
207
                if ((imageSize == null) || (extent == null)
208
                                || (imageSize.getWidth() <= 0) || (imageSize.getHeight() <=0)) return;
209
                                        
210
        AffineTransform escalado = new AffineTransform();
211
        AffineTransform translacion = new AffineTransform();
212

    
213
        double escalaX;
214
        double escalaY;
215

    
216
        escalaX = imageSize.getWidth() / extent.getWidth();
217
        escalaY = imageSize.getHeight() / extent.getHeight();
218
        
219
        double xCenter = extent.getCenterX();
220
        double yCenter = extent.getCenterY();
221
        double newHeight, newWidth;
222

    
223
        adjustedExtent = new Rectangle2D.Double();
224
        if (escalaX < escalaY) {
225
            scale = escalaX;
226
            newHeight = imageSize.getHeight() / scale;
227
            adjustedExtent.setRect(xCenter - extent.getWidth()/2.0, yCenter - newHeight/2.0, extent.getWidth(), newHeight);
228
        } else {
229
            scale = escalaY;
230
            newWidth = imageSize.getWidth() / scale;
231
            adjustedExtent.setRect(xCenter - newWidth/2.0, yCenter - extent.getHeight()/2.0, newWidth, extent.getHeight());
232
        }
233

    
234
        translacion.setToTranslation(-adjustedExtent.getX(), -adjustedExtent.getY() -
235
                        adjustedExtent.getHeight());
236
        escalado.setToScale(scale, -scale);
237

    
238
        AffineTransform offsetTrans = new AffineTransform();
239
        offsetTrans.setToTranslation(offset.getX(), offset.getY());
240

    
241
        trans.setToIdentity();
242
        trans.concatenate(offsetTrans);
243
        trans.concatenate(escalado);
244

    
245
        trans.concatenate(translacion);
246
        
247
        // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n 
248
        // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
249
        AffineTransform at;
250
                try {
251
                        at = trans.createInverse();
252
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
253
                Point2D.Float pProv = new Point2D.Float();
254
                at.deltaTransform(pPixel, pProv);
255

    
256
                dist1pixel = pProv.x;
257
                dist3pixel = 3 * pProv.x;
258
                        
259
                } catch (NoninvertibleTransformException e) {
260
                        System.err.println("transformada afin = " + trans.toString());
261
                        System.err.println("extent = " + extent.toString() + " imageSize= " + imageSize.toString());
262
                        throw new RuntimeException(e);
263
                }
264
        
265
        }
266
        public void setOffset(Point2D p){
267
                offset=p;
268
        }
269
        public void setBackColor(Color c) {
270
                backColor = c;
271
        }
272

    
273
        public Color getBackColor() {
274
                return backColor;
275
        }
276
        /**
277
         * @return Returns the adjustedExtent.
278
         */
279
        public Rectangle2D getAdjustedExtent() {
280
                return adjustedExtent;
281
        }
282
        /**
283
         * @return Returns the distanceUnits.
284
         */
285
        public int getDistanceUnits() {
286
                return distanceUnits;
287
        }
288
        /**
289
         * @param distanceUnits The distanceUnits to set.
290
         */
291
        public void setDistanceUnits(int distanceUnits) {
292
                this.distanceUnits = distanceUnits;
293
        }
294
        /**
295
         * @return Returns the mapUnits.
296
         */
297
        public int getMapUnits() {
298
                return mapUnits;
299
        }
300
        /**
301
         * @param mapUnits The mapUnits to set.
302
         */
303
        public void setMapUnits(int mapUnits) {
304
                this.mapUnits = mapUnits;
305
        }        
306
        
307
        public int getImageWidth(){
308
                return imageSize.width;
309
        }
310
        
311
        public int getImageHeight(){
312
                return imageSize.height;
313
        }
314
        public double getDist1pixel() {
315
                return dist1pixel;
316
        }
317
        public void setDist1pixel(double dist1pixel) {
318
                this.dist1pixel = dist1pixel;
319
        }
320
        public double getDist3pixel() {
321
                return dist3pixel;
322
        }
323
        public void setDist3pixel(double dist3pixel) {
324
                this.dist3pixel = dist3pixel;
325
        }
326
        /**
327
         * @return Returns the extents.
328
         */
329
        public ExtentHistory getExtents() {
330
                return extents;
331
        }
332

    
333
        /**
334
         * 
335
         */
336
        public void setExtentPrev() {
337
                   extent = extents.removePrev();
338
            //Calcula la transformaci?n af?n
339
            calculateAffineTransform();
340
        // Lanzamos los eventos de extent cambiado
341
        callExtentListeners(adjustedExtent);  
342
        }
343
        /**
344
         * @return Returns the proj.
345
         */
346
        public IProjection getProjection() {
347
                return proj;
348
        }
349
        /**
350
         * @param proj The proj to set.
351
         */
352
        public void setProjection(IProjection proj) {
353
                this.proj = proj;
354
        }
355
        public XMLEntity getXMLEntity(){
356
                XMLEntity xml=new XMLEntity();
357
                if (adjustedExtent != null){
358
                        xml.putProperty("adjustedExtentX",adjustedExtent.getX());
359
                        xml.putProperty("adjustedExtentY",adjustedExtent.getY());
360
                        xml.putProperty("adjustedExtentW",adjustedExtent.getWidth());
361
                        xml.putProperty("adjustedExtentH",adjustedExtent.getHeight());
362
                }
363
                xml.putProperty("backColor",StringUtilities.color2String(backColor));
364
                if (clip!=null){
365
                xml.putProperty("clipX",clip.getX());
366
                xml.putProperty("clipY",clip.getY());
367
                xml.putProperty("clipW",clip.getWidth());
368
                xml.putProperty("clipH",clip.getHeight());
369
                }
370
                xml.putProperty("dist1pixel",dist1pixel);
371
                xml.putProperty("dist3pixel",dist3pixel);
372
                xml.putProperty("distanceUnits",distanceUnits);
373
                if (extent!=null){
374
                        xml.putProperty("extentX",extent.getX());
375
                        xml.putProperty("extentY",extent.getY());
376
                        xml.putProperty("extentW",extent.getWidth());
377
                        xml.putProperty("extentH",extent.getHeight());
378
                }
379
                xml.addChild(extents.getXMLEntity());
380
                xml.putProperty("mapUnits",mapUnits);
381
                xml.putProperty("offsetX",offset.getX());
382
                xml.putProperty("offsetY",offset.getY());
383
                if (proj!=null){
384
                        xml.putProperty("proj",proj.getAbrev());
385
                }
386
                xml.putProperty("scale",scale);
387
                
388
                return xml;
389
        }
390
        public static ViewPort createFromXML(XMLEntity xml){
391
                ViewPort vp=new ViewPort(null);
392
                if (xml.contains("adjustedExtentX")){
393
                        vp.adjustedExtent=new Rectangle2D.Double(xml.getDoubleProperty("adjustedExtentX"),xml.getDoubleProperty("adjustedExtentY"),xml.getDoubleProperty("adjustedExtentW"),xml.getDoubleProperty("adjustedExtentH"));
394
                }
395
                if (xml.contains("backColor")){
396
                vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty("backColor")));
397
                }
398
                if (xml.contains("clipX")){
399
                        vp.clip=new Rectangle2D.Double(xml.getDoubleProperty("clipX"),xml.getDoubleProperty("clipY"),xml.getDoubleProperty("clipW"),xml.getDoubleProperty("clipH"));
400
                }
401
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
402
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
403
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
404
                vp.extents=ExtentHistory.createFromXML(xml.getChild(0));
405
                if (xml.contains("extentX")){
406
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),xml.getDoubleProperty("extentY"),xml.getDoubleProperty("extentW"),xml.getDoubleProperty("extentH")));
407

    
408
                    //Calcula la transformaci?n af?n
409
                    vp.calculateAffineTransform();
410
                // Lanzamos los eventos de extent cambiado
411
                vp.callExtentListeners(vp.adjustedExtent);  
412
                }
413
                
414
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
415
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),xml.getDoubleProperty("offsetY")));
416
                if (xml.contains("proj")){
417
                        vp.proj=ProjectionPool.get(xml.getStringProperty("proj"));
418
                }
419
                vp.setScale(xml.getDoubleProperty("scale"));
420
                return vp;
421
        }
422
        public ViewPort cloneViewPort(){
423
                return createFromXML(getXMLEntity());                
424
        }
425
        
426
        public String toString()
427
        {
428
                String str;
429
                str = "Datos del viewPort:\nExtent=" + extent +
430
                        "\nadjustedExtent=" + adjustedExtent + 
431
                        "\nimageSize=" + imageSize +
432
                        "\nescale=" + scale +
433
                        "\ntrans=" + trans;
434
                
435
                
436
                return str;
437
        }
438
}