Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 2183

History | View | Annotate | Download (19.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap;
42

    
43
import com.iver.utiles.StringUtilities;
44
import com.iver.utiles.XMLEntity;
45

    
46
import org.cresques.cts.GeoCalc;
47
import org.cresques.cts.IProjection;
48
import org.cresques.cts.ProjectionPool;
49
import org.cresques.cts.gt2.CSUTM;
50

    
51
import java.awt.Color;
52
import java.awt.Dimension;
53
import java.awt.Point;
54
import java.awt.geom.AffineTransform;
55
import java.awt.geom.NoninvertibleTransformException;
56
import java.awt.geom.Point2D;
57
import java.awt.geom.Rectangle2D;
58

    
59
import java.util.ArrayList;
60

    
61

    
62
/**
63
 * Clase con atributos de la vista.
64
 * 050211, jmorell: A?ado los Grados como unidad de mapa.
65
 *
66
 * @author Vicente Caballero Navarro
67
 */
68
public class ViewPort {
69
        public static int KILOMETROS = 0;
70
        public static int METROS = 1;
71
        public static int CENTIMETRO = 2;
72
        public static int MILIMETRO = 3;
73
        public static int MILLAS = 4;
74
        public static int YARDAS = 5;
75
        public static int PIES = 6;
76
        public static int PULGADAS = 7;
77
        public static int GRADOS = 8;
78

    
79
        /**
80
         * Resoluci?n (Puntos por pulgada) de la vista actual. Se necesita para los
81
         * c?lculos de escala geogr?fica.
82
         */
83
        private static int dpi = java.awt.Toolkit.getDefaultToolkit()
84
                                                                                         .getScreenResolution();
85
        private Rectangle2D extent;
86
        private Rectangle2D adjustedExtent;
87
        private ExtentHistory extents = new ExtentHistory();
88
        private Dimension imageSize;
89
        private AffineTransform trans = new AffineTransform();
90
        private int distanceUnits = METROS;
91
        private int mapUnits = METROS;
92
        private ArrayList listeners = new ArrayList();
93
        private Point2D offset = new Point2D.Double(0, 0);
94
        private Rectangle2D clip;
95
        private Color backColor = null; //Color.WHITE;
96
        private IProjection proj;
97
        private double dist1pixel;
98
        private double dist3pixel;
99
        private double scale;
100

    
101
        /**
102
         * Crea un nuevo ViewPort.
103
         *
104
         * @param proj Proyecci?n.
105
         */
106
        public ViewPort(IProjection proj) {
107
                // Por defecto
108
                this.proj = proj;
109
        }
110

    
111
        /**
112
         * A?ade un ViewPortListener al extentListener.
113
         *
114
         * @param arg0 ViewPortListener.
115
         *
116
         * @return True si ha sido a?adida correctamente.
117
         */
118
        public boolean addViewPortListener(ViewPortListener arg0) {
119
                return listeners.add(arg0);
120
        }
121

    
122
        /**
123
         * Borra el ViewPortListener que se pasa como par?metro delos
124
         * extentListener.
125
         *
126
         * @param arg0 ViewPortListener.
127
         *
128
         * @return True si ha sido borrado correctamente.
129
         */
130
        public boolean removeViewPortListener(ViewPortListener arg0) {
131
                return listeners.remove(arg0);
132
        }
133

    
134
        /**
135
         * Devuelve la distancia en pixels a partir de una distancia real.
136
         *
137
         * @param d Distancia real.
138
         *
139
         * @return Distancia en pixels.
140
         */
141
        public int fromMapDistance(double d) {
142
                Point2D.Double pWorld = new Point2D.Double(1, 1);
143
                Point2D.Double pScreen = new Point2D.Double();
144

    
145
                double nuevoX;
146
                double nuevoY;
147
                double cX;
148
                double cY;
149

    
150
                try {
151
                        trans.deltaTransform(pWorld, pScreen);
152
                } catch (Exception e) {
153
                        System.err.print(e.getMessage());
154
                }
155

    
156
                return (int) (d * pScreen.x);
157
        }
158

    
159
        /**
160
         * Devuelve un punto en pixels a partir de una coordenada X e Y real.
161
         *
162
         * @param x Coordenada X real.
163
         * @param y Coordenada Y real.
164
         *
165
         * @return Punto en pixels.
166
         */
167
        public Point2D fromMapPoint(double x, double y) {
168
                Point2D.Double pWorld = new Point2D.Double(x, y);
169
                Point2D.Double pScreen = new Point2D.Double();
170

    
171
                double nuevoX;
172
                double nuevoY;
173
                double cX;
174
                double cY;
175

    
176
                try {
177
                        trans.transform(pWorld, pScreen);
178
                } catch (Exception e) {
179
                        System.err.print(e.getMessage());
180
                }
181

    
182
                return pScreen;
183
        }
184

    
185
        /**
186
         * Devuelve el punto en pixels a partir de un punto real.
187
         *
188
         * @param point Punto real.
189
         *
190
         * @return Punto en pixels.
191
         */
192
        public Point2D fromMapPoint(Point2D point) {
193
                return fromMapPoint(point.getX(), point.getY());
194
        }
195

    
196
        /**
197
         * Devuelve un punto real a partir de una coordenada X e Y en pixels.
198
         *
199
         * @param x Coordenada X en pixels.
200
         * @param y Coordenada Y en pixels.
201
         *
202
         * @return Punto real.
203
         */
204
        public Point2D toMapPoint(int x, int y) {
205
                Point pScreen = new Point(x, y);
206

    
207
                return toMapPoint(pScreen);
208
        }
209

    
210
        /**
211
         * Devuelve la distancia real a partir de la distancia en pixels.
212
         *
213
         * @param d Distancia en pixels.
214
         *
215
         * @return Distancia real.
216
         */
217
        public double toMapDistance(int d) {
218
                double dist = d / trans.getScaleX();
219

    
220
                return dist;
221
        }
222

    
223
        /**
224
         * Devuelve un punto real a partir de un punto en pixels.
225
         *
226
         * @param pScreen Punto en pixels.
227
         *
228
         * @return Punto real.
229
         *
230
         * @throws RuntimeException
231
         */
232
        public Point2D toMapPoint(Point2D pScreen) {
233
                Point2D.Double pWorld = new Point2D.Double();
234
                AffineTransform at;
235

    
236
                try {
237
                        at = trans.createInverse();
238
                        at.transform(pScreen, pWorld);
239
                } catch (NoninvertibleTransformException e) {
240
                        throw new RuntimeException(e);
241
                }
242

    
243
                return pWorld;
244
        }
245

    
246
        /**
247
         * Calcula la distancia entre dos puntos en unidades de usuario. Los puntos
248
         * est?n en unidades de usuario. Se tiene en cuenta la proyecci?n, con lo
249
         * que es INDISPENSABLE que la variable proj contenga el valor correcto de
250
         * la proyecci?n.
251
         *
252
         * @param pt1
253
         * @param pt2
254
         *
255
         * @return distancia real.
256
         */
257
        public double distanceWorld(Point2D pt1, Point2D pt2) {
258
                double dist = -1;
259
                dist = pt1.distance(pt2);
260

    
261
                if ((proj != null) && !(proj instanceof CSUTM)) {
262
                        dist = new GeoCalc(proj).distanceVincenty(proj.toGeo(pt1),
263
                                        proj.toGeo(pt2));
264
                }
265

    
266
                return dist;
267
        }
268

    
269
        /**
270
         * Rellena el extent anterior como actual.
271
         */
272
        public void setPreviousExtent() {
273
                extent = extents.removePrev();
274

    
275
                //Calcula la transformaci?n af?n
276
                calculateAffineTransform();
277

    
278
                // Lanzamos los eventos de extent cambiado
279
                callExtentChanged(adjustedExtent);
280
        }
281

    
282
        /**
283
         * Devuelve el extent.
284
         *
285
         * @return Extent.
286
         */
287
        public Rectangle2D getExtent() {
288
                return extent;
289
        }
290

    
291
        /**
292
         * Inserta el extent.
293
         *
294
         * @param r Extent.
295
         */
296
        public void setExtent(Rectangle2D r) {
297
                if (extent != null) {
298
                        extents.put(extent);
299
                }
300

    
301
                //Esto comprueba que el extent no es de anchura o altura = "0" 
302
                //y si es as? lo redimensiona.
303
                if ((r.getWidth() == 0) || (r.getHeight() == 0)) {
304
                        extent = new Rectangle2D.Double(r.getMinX() - 0.1,
305
                                        r.getMinY() - 0.1, r.getWidth() + 0.2, r.getHeight() + 0.2);
306
                } else {
307
                        extent = r;
308
                }
309

    
310
                //Calcula la transformaci?n af?n
311
                calculateAffineTransform();
312

    
313
                // Lanzamos los eventos de extent cambiado
314
                callExtentChanged(adjustedExtent);
315
        }
316

    
317
        /**
318
         * Inserta la escala.
319
         *
320
         * @param scale escala.
321
         */
322
        public void setScale() {
323
                //this.scale = scale;
324

    
325
                //Calcula la transformaci?n af?n
326
                calculateAffineTransform();
327

    
328
                // Lanzamos los eventos de extent cambiado
329
                callExtentChanged(adjustedExtent);
330
        }
331

    
332
        /**
333
         * Devuelve la escala. Debe estar siempre actualizada y no calcularse nunca
334
         * aqu? pues se utiliza en el dibujado para cada geometr?a
335
         *
336
         * @return Escala.
337
         */
338
        public double getScale() {
339
                return proj.getScale(extent.getMinX(), extent.getMaxX(),
340
                        imageSize.getWidth(), dpi);
341
        }
342

    
343
        /**
344
         * Devuelve la matriz de transformaci?n.
345
         *
346
         * @return Matriz de transformaci?n.
347
         */
348
        public AffineTransform getAffineTransform() {
349
                return trans;
350
        }
351

    
352
        /**
353
         * Devuelve las dimensiones de la imagen.
354
         *
355
         * @return Returns the imageSize.
356
         */
357
        public Dimension getImageSize() {
358
                return imageSize;
359
        }
360

    
361
        /**
362
         * Inserta las dimensiones de la imagen.
363
         *
364
         * @param imageSize The imageSize to set.
365
         */
366
        public void setImageSize(Dimension imageSize) {
367
                this.imageSize = imageSize;
368
                calculateAffineTransform();
369
        }
370

    
371
        /**
372
         * Llamada a los listeners tras el cambio de extent.
373
         *
374
         * @param newRect Extent.
375
         */
376
        private void callExtentChanged(Rectangle2D newRect) {
377
                ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
378

    
379
                for (int i = 0; i < listeners.size(); i++) {
380
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
381
                        listener.extentChanged(ev);
382
                }
383
        }
384

    
385
        /**
386
         * Llamada a los listeners tras el cambio de color.
387
         *
388
         * @param c Color.
389
         */
390
        private void callColorChanged(Color c) {
391
                ColorEvent ce = ColorEvent.createColorEvent(c);
392

    
393
                for (int i = 0; i < listeners.size(); i++) {
394
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
395
                        listener.backColorChanged(ce);
396
                }
397
        }
398

    
399
        /**
400
         * C?lculo de la matriz de transformaci?n.
401
         *
402
         * @throws RuntimeException 
403
         */
404
        private void calculateAffineTransform() {
405
                if ((imageSize == null) || (extent == null) ||
406
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
407
                        return;
408
                }
409

    
410
                AffineTransform escalado = new AffineTransform();
411
                AffineTransform translacion = new AffineTransform();
412

    
413
                double escalaX;
414
                double escalaY;
415

    
416
                escalaX = imageSize.getWidth() / extent.getWidth();
417
                escalaY = imageSize.getHeight() / extent.getHeight();
418

    
419
                double xCenter = extent.getCenterX();
420
                double yCenter = extent.getCenterY();
421
                double newHeight;
422
                double newWidth;
423

    
424
                adjustedExtent = new Rectangle2D.Double();
425

    
426
                if (escalaX < escalaY) {
427
                        scale = escalaX;
428
                        newHeight = imageSize.getHeight() / scale;
429
                        adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
430
                                yCenter - (newHeight / 2.0), extent.getWidth(), newHeight);
431
                } else {
432
                        scale = escalaY;
433
                        newWidth = imageSize.getWidth() / scale;
434
                        adjustedExtent.setRect(xCenter - (newWidth / 2.0),
435
                                yCenter - (extent.getHeight() / 2.0), newWidth,
436
                                extent.getHeight());
437
                }
438

    
439
                translacion.setToTranslation(-adjustedExtent.getX(),
440
                        -adjustedExtent.getY() - adjustedExtent.getHeight());
441
                escalado.setToScale(scale, -scale);
442

    
443
                AffineTransform offsetTrans = new AffineTransform();
444
                offsetTrans.setToTranslation(offset.getX(), offset.getY());
445

    
446
                trans.setToIdentity();
447
                trans.concatenate(offsetTrans);
448
                trans.concatenate(escalado);
449

    
450
                trans.concatenate(translacion);
451

    
452
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n 
453
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
454
                AffineTransform at;
455

    
456
                try {
457
                        at = trans.createInverse();
458

    
459
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
460
                        Point2D.Float pProv = new Point2D.Float();
461
                        at.deltaTransform(pPixel, pProv);
462

    
463
                        dist1pixel = pProv.x;
464
                        dist3pixel = 3 * pProv.x;
465
                } catch (NoninvertibleTransformException e) {
466
                        System.err.println("transformada afin = " + trans.toString());
467
                        System.err.println("extent = " + extent.toString() +
468
                                " imageSize= " + imageSize.toString());
469
                        throw new RuntimeException(e);
470
                }
471
        }
472

    
473
        /**
474
         * Inserta la desviaci?n.
475
         *
476
         * @param p Punto.
477
         */
478
        public void setOffset(Point2D p) {
479
                offset = p;
480
        }
481
        /**
482
         * Devuelve la desviaci?n.
483
         *
484
         * @param p Punto.
485
         */
486
        public Point2D getOffset() {
487
                return offset;
488
        }
489
        /**
490
         * Inserta el color de fondo.
491
         *
492
         * @param c Color de fondo.
493
         */
494
        public void setBackColor(Color c) {
495
                backColor = c;
496
                callColorChanged(backColor);
497
        }
498

    
499
        /**
500
         * Devuelve el color de fondo.
501
         *
502
         * @return Color de fondo.
503
         */
504
        public Color getBackColor() {
505
                return backColor;
506
        }
507

    
508
        /**
509
         * Devuelve el extent ajustado.
510
         *
511
         * @return Returns the adjustedExtent.
512
         */
513
        public Rectangle2D getAdjustedExtent() {
514
                return adjustedExtent;
515
        }
516

    
517
        /**
518
         * Devuelve la unidad de medida.
519
         *
520
         * @return Returns the distanceUnits.
521
         */
522
        public int getDistanceUnits() {
523
                return distanceUnits;
524
        }
525

    
526
        /**
527
         * Inserta la unidad de medida.
528
         *
529
         * @param distanceUnits The distanceUnits to set.
530
         */
531
        public void setDistanceUnits(int distanceUnits) {
532
                this.distanceUnits = distanceUnits;
533
        }
534

    
535
        /**
536
         * Devuelve la unidad de medida del mapa.
537
         *
538
         * @return Returns the mapUnits.
539
         */
540
        public int getMapUnits() {
541
                return mapUnits;
542
        }
543

    
544
        /**
545
         * Inserta la unidad de medida del mapa.
546
         *
547
         * @param mapUnits The mapUnits to set.
548
         */
549
        public void setMapUnits(int mapUnits) {
550
                this.mapUnits = mapUnits;
551
        }
552

    
553
        /**
554
         * Devuelve la anchura de la imagen.
555
         *
556
         * @return anchura en pixels de la imagen.
557
         */
558
        public int getImageWidth() {
559
                return imageSize.width;
560
        }
561

    
562
        /**
563
         * Devuelve la altura de la imagen.
564
         *
565
         * @return altura de la imagen.
566
         */
567
        public int getImageHeight() {
568
                return imageSize.height;
569
        }
570

    
571
        /**
572
         * Devuelve la distancia real de un pixel.
573
         *
574
         * @return Distancia real de un pixel.
575
         */
576
        public double getDist1pixel() {
577
                return dist1pixel;
578
        }
579

    
580
        /**
581
         * Inserta la distancia real de un pixel.
582
         *
583
         * @param dist1pixel Distancia real de un pixel.
584
         */
585
        public void setDist1pixel(double dist1pixel) {
586
                this.dist1pixel = dist1pixel;
587
        }
588

    
589
        /**
590
         * Devuelve la distancia real de tres pixel.
591
         *
592
         * @return Distancia real de tres pixel.
593
         */
594
        public double getDist3pixel() {
595
                return dist3pixel;
596
        }
597

    
598
        /**
599
         * Inserta la distancia real de tres pixels.
600
         *
601
         * @param dist3pixel Distancia real de tres pixels.
602
         */
603
        public void setDist3pixel(double dist3pixel) {
604
                this.dist3pixel = dist3pixel;
605
        }
606

    
607
        /**
608
         * Devuelve los Extents anteriores almacenados.
609
         *
610
         * @return Returns the extents.
611
         */
612
        public ExtentHistory getExtents() {
613
                return extents;
614
        }
615

    
616
        /**
617
         * Devuelve la proyecci?n.
618
         *
619
         * @return Returns the proj.
620
         */
621
        public IProjection getProjection() {
622
                return proj;
623
        }
624

    
625
        /**
626
         * Inserta la proyecci?n.
627
         *
628
         * @param proj The proj to set.
629
         */
630
        public void setProjection(IProjection proj) {
631
                this.proj = proj;
632
        }
633
        
634
        /**
635
         * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
636
         * un calculateAffineTransform, ni recalcula el adjustedExtent.
637
         * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
638
         * RasterAdapter, y que recibiera un ViewPortData. 
639
         * @param at
640
         */
641
        public void setAffineTransform(AffineTransform at) 
642
        {
643
            this.trans = at;
644
        }
645

    
646
        /**
647
         * Devuelve el XMLEntity.
648
         *
649
         * @return XMLEntity.
650
         */
651
        public XMLEntity getXMLEntity() {
652
                XMLEntity xml = new XMLEntity();
653
                xml.putProperty("className",this.getClass().getName());
654

    
655
                if (adjustedExtent != null) {
656
                        xml.putProperty("adjustedExtentX", adjustedExtent.getX());
657
                        xml.putProperty("adjustedExtentY", adjustedExtent.getY());
658
                        xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
659
                        xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
660
                }
661

    
662
                if (backColor != null)
663
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
664

    
665
                if (clip != null) {
666
                        xml.putProperty("clipX", clip.getX());
667
                        xml.putProperty("clipY", clip.getY());
668
                        xml.putProperty("clipW", clip.getWidth());
669
                        xml.putProperty("clipH", clip.getHeight());
670
                }
671

    
672
                xml.putProperty("dist1pixel", dist1pixel);
673
                xml.putProperty("dist3pixel", dist3pixel);
674
                xml.putProperty("distanceUnits", distanceUnits);
675

    
676
                if (extent != null) {
677
                        xml.putProperty("extentX", extent.getX());
678
                        xml.putProperty("extentY", extent.getY());
679
                        xml.putProperty("extentW", extent.getWidth());
680
                        xml.putProperty("extentH", extent.getHeight());
681
                }
682

    
683
                xml.addChild(extents.getXMLEntity());
684
                xml.putProperty("mapUnits", mapUnits);
685
                xml.putProperty("offsetX", offset.getX());
686
                xml.putProperty("offsetY", offset.getY());
687

    
688
                if (proj != null) {
689
                        xml.putProperty("proj", proj.getAbrev());
690
                }
691

    
692
                xml.putProperty("scale", scale);
693

    
694
                return xml;
695
        }
696

    
697
        /**
698
         * Crea un nuevo ViewPort a partir del XMLEntity.
699
         *
700
         * @param xml XMLEntity.
701
         *
702
         * @return Nuevo ViewPort.
703
         */
704
        public static ViewPort createFromXML03(XMLEntity xml) {
705
                ViewPort vp = new ViewPort(null);
706

    
707
                if (xml.contains("adjustedExtentX")) {
708
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
709
                                                "adjustedExtentX"),
710
                                        xml.getDoubleProperty("adjustedExtentY"),
711
                                        xml.getDoubleProperty("adjustedExtentW"),
712
                                        xml.getDoubleProperty("adjustedExtentH"));
713
                }
714

    
715
                if (xml.contains("backColor")) {
716
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
717
                                                "backColor")));
718
                }
719

    
720
                if (xml.contains("clipX")) {
721
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
722
                                        xml.getDoubleProperty("clipY"),
723
                                        xml.getDoubleProperty("clipW"),
724
                                        xml.getDoubleProperty("clipH"));
725
                }
726

    
727
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
728
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
729
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
730
                vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
731

    
732
                if (xml.contains("extentX")) {
733
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
734
                                        xml.getDoubleProperty("extentY"),
735
                                        xml.getDoubleProperty("extentW"),
736
                                        xml.getDoubleProperty("extentH")));
737

    
738
                        //Calcula la transformaci?n af?n
739
                        vp.calculateAffineTransform();
740

    
741
                        // Lanzamos los eventos de extent cambiado
742
                        // vp.callExtentListeners(vp.adjustedExtent);
743
                }
744

    
745
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
746
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
747
                                xml.getDoubleProperty("offsetY")));
748

    
749
                if (xml.contains("proj")) {
750
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
751
                }
752

    
753
                //vp.setScale(xml.getDoubleProperty("scale"));
754
                vp.setScale();
755
                return vp;
756
        }
757

    
758
        /**
759
         * Crea un nuevo ViewPort a partir del XMLEntity.
760
         *
761
         * @param xml XMLEntity.
762
         *
763
         * @return Nuevo ViewPort.
764
         */
765
        public static ViewPort createFromXML(XMLEntity xml) {
766
                ViewPort vp = new ViewPort(null);
767

    
768
                if (xml.contains("adjustedExtentX")) {
769
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
770
                                                "adjustedExtentX"),
771
                                        xml.getDoubleProperty("adjustedExtentY"),
772
                                        xml.getDoubleProperty("adjustedExtentW"),
773
                                        xml.getDoubleProperty("adjustedExtentH"));
774
                }
775

    
776
                if (xml.contains("backColor")) {
777
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
778
                                                "backColor")));
779
                }
780

    
781
                if (xml.contains("clipX")) {
782
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
783
                                        xml.getDoubleProperty("clipY"),
784
                                        xml.getDoubleProperty("clipW"),
785
                                        xml.getDoubleProperty("clipH"));
786
                }
787

    
788
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
789
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
790
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
791
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
792

    
793
                if (xml.contains("extentX")) {
794
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
795
                                        xml.getDoubleProperty("extentY"),
796
                                        xml.getDoubleProperty("extentW"),
797
                                        xml.getDoubleProperty("extentH")));
798

    
799
                        //Calcula la transformaci?n af?n
800
                        vp.calculateAffineTransform();
801

    
802
                        // Lanzamos los eventos de extent cambiado
803
                        // vp.callExtentListeners(vp.adjustedExtent);
804
                }
805

    
806
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
807
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
808
                                xml.getDoubleProperty("offsetY")));
809

    
810
                if (xml.contains("proj")) {
811
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
812
                }
813

    
814
                //vp.setScale(xml.getDoubleProperty("scale"));
815
                vp.setScale();
816
                return vp;
817
        }
818

    
819
        /**
820
         * Clona el ViewPort.
821
         *
822
         * @return ViewPort clonado.
823
         */
824
        public ViewPort cloneViewPort() {
825
                return createFromXML(getXMLEntity());
826
        }
827

    
828
        /**
829
         * Devuelve el String con datos del ViewPort.
830
         *
831
         * @return Cadena con datos del ViewPort.
832
         */
833
        public String toString() {
834
                String str;
835
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
836
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
837
                        "\ntrans=" + trans;
838

    
839
                return str;
840
        }
841
}