Statistics
| Revision:

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

History | View | Annotate | Download (21.1 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 java.awt.Color;
44
import java.awt.Dimension;
45
import java.awt.Point;
46
import java.awt.Rectangle;
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.NoninvertibleTransformException;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.util.ArrayList;
52

    
53
import org.cresques.cts.GeoCalc;
54
import org.cresques.cts.IProjection;
55
import org.cresques.cts.ProjectionPool;
56
import org.cresques.cts.gt2.CSUTM;
57

    
58
import com.iver.utiles.StringUtilities;
59
import com.iver.utiles.XMLEntity;
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
        private Rectangle2D cliprect;
101

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

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

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

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

    
146
                try {
147
                        trans.deltaTransform(pWorld, pScreen);
148
                } catch (Exception e) {
149
                        System.err.print(e.getMessage());
150
                }
151

    
152
                return (int) (d * pScreen.x);
153
        }
154

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

    
167
                try {
168
                        trans.transform(pWorld, pScreen);
169
                } catch (Exception e) {
170
                        System.err.print(e.getMessage());
171
                }
172

    
173
                return pScreen;
174
        }
175

    
176
        /**
177
         * Devuelve el punto en pixels a partir de un punto real.
178
         *
179
         * @param point Punto real.
180
         *
181
         * @return Punto en pixels.
182
         */
183
        public Point2D fromMapPoint(Point2D point) {
184
                return fromMapPoint(point.getX(), point.getY());
185
        }
186

    
187
        /**
188
         * Devuelve un punto real a partir de una coordenada X e Y en pixels.
189
         *
190
         * @param x Coordenada X en pixels.
191
         * @param y Coordenada Y en pixels.
192
         *
193
         * @return Punto real.
194
         */
195
        public Point2D toMapPoint(int x, int y) {
196
                Point pScreen = new Point(x, y);
197

    
198
                return toMapPoint(pScreen);
199
        }
200

    
201
        public Rectangle2D toMapRectangle(Rectangle2D r){
202
                double w=toMapDistance((int)r.getWidth());
203
                double h=toMapDistance((int)r.getHeight());
204
                Point2D p1=toMapPoint((int)r.getX(),(int)r.getY());
205
                return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h);
206
        }
207
        /**
208
         * Devuelve la distancia real a partir de la distancia en pixels.
209
         *
210
         * @param d Distancia en pixels.
211
         *
212
         * @return Distancia real.
213
         */
214
        public double toMapDistance(int d) {
215
                double dist = d / trans.getScaleX();
216

    
217
                return dist;
218
        }
219

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

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

    
240
                return pWorld;
241
        }
242

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

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

    
263
                return dist;
264
        }
265

    
266
        /**
267
         * Rellena el extent anterior como actual.
268
         */
269
        public void setPreviousExtent() {
270
                extent = extents.removePrev();
271

    
272
                //Calcula la transformaci?n af?n
273
                calculateAffineTransform();
274

    
275
                // Lanzamos los eventos de extent cambiado
276
                callExtentChanged(getAdjustedExtent());
277
        }
278

    
279
        /**
280
         * <p>
281
         * When the zoom changes (for instance when using the zoom in or zoom out tools,
282
         * but also zooming to a selected feature or shape) the extent that covers that
283
         * area is the value returned by this method. It is not the actual area shown
284
         * in the view because it does not care about the aspect ratio. However, any
285
         * part of the real world contained in this extent is shown in the view.<br>
286
         * </p>
287
         * <p>
288
         * Probably <b>this is not what you are looking for</b>. If you are looking for
289
         * the complete extent currently shown, you must use getAdjustedExtent() method
290
         * which returns the extent that contains this one but regarding the current
291
         * view's aspect ratio.
292
         * </p>
293
         * @return Extent.
294
         */
295
        public Rectangle2D getExtent() {
296
                return extent;
297
        }
298

    
299
        /**
300
         * Inserta el extent.
301
         *
302
         * @param r Extent.
303
         */
304
        public void setExtent(Rectangle2D r) {
305
                if (extent != null) {
306
                        extents.put(extent);
307
                }
308

    
309
                //Esto comprueba que el extent no es de anchura o altura = "0"
310
                //y si es as? lo redimensiona.
311
                if (r!=null &&((r.getWidth() == 0) || (r.getHeight() == 0))) {
312
                        extent = new Rectangle2D.Double(r.getMinX() - 0.1,
313
                                        r.getMinY() - 0.1, r.getWidth() + 0.2, r.getHeight() + 0.2);
314
                } else {
315
                        extent = r;
316
                }
317

    
318
                //Calcula la transformaci?n af?n
319
                calculateAffineTransform();
320

    
321
                // Lanzamos los eventos de extent cambiado
322
                callExtentChanged(getAdjustedExtent());
323
        }
324

    
325
        /**
326
         * Inserta la escala.
327
     * TODO: (FJP) Que alguien me explique qu? sentido tiene esto
328
         *
329
         * @param scale escala.
330
         */
331
        public void setScale() {
332
                //this.scale = scale;
333

    
334
                //Calcula la transformaci?n af?n
335
                calculateAffineTransform();
336

    
337
                // Lanzamos los eventos de extent cambiado
338
                callExtentChanged(getAdjustedExtent());
339
        }
340

    
341
        /**
342
         * Devuelve la escala. Debe estar siempre actualizada y no calcularse nunca
343
         * aqu? pues se utiliza en el dibujado para cada geometr?a
344
         *
345
         * @return Escala.
346
         */
347
        public double getScale() {
348
                return proj.getScale(extent.getMinX(), extent.getMaxX(),
349
                        imageSize.getWidth(), dpi);
350
        }
351

    
352
        /**
353
         * Devuelve la matriz de transformaci?n.
354
         *
355
         * @return Matriz de transformaci?n.
356
         */
357
        public AffineTransform getAffineTransform() {
358
                return trans;
359
        }
360

    
361
        /**
362
         * Devuelve las dimensiones de la imagen.
363
         *
364
         * @return Returns the imageSize.
365
         */
366
        public Dimension getImageSize() {
367
                return imageSize;
368
        }
369

    
370
        /**
371
         * Inserta las dimensiones de la imagen.
372
         *
373
         * @param imageSize The imageSize to set.
374
         */
375
        public void setImageSize(Dimension imageSize) {
376
                this.imageSize = imageSize;
377
                calculateAffineTransform();
378
        }
379

    
380
        /**
381
         * Llamada a los listeners tras el cambio de extent.
382
         *
383
         * @param newRect Extent.
384
         */
385
        private void callExtentChanged(Rectangle2D newRect) {
386
                ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
387

    
388
                for (int i = 0; i < listeners.size(); i++) {
389
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
390
                        listener.extentChanged(ev);
391
                }
392
        }
393

    
394
        /**
395
         * Llamada a los listeners tras el cambio de color.
396
         *
397
         * @param c Color.
398
         */
399
        private void callColorChanged(Color c) {
400
                ColorEvent ce = ColorEvent.createColorEvent(c);
401

    
402
                for (int i = 0; i < listeners.size(); i++) {
403
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
404
                        listener.backColorChanged(ce);
405
                }
406
        }
407

    
408
        /**
409
         * C?lculo de la matriz de transformaci?n.
410
         *
411
         * @throws RuntimeException
412
         */
413
        private void calculateAffineTransform() {
414
                if ((imageSize == null) || (extent == null) ||
415
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
416
                        return;
417
                }
418

    
419
                AffineTransform escalado = new AffineTransform();
420
                AffineTransform translacion = new AffineTransform();
421

    
422
                double escalaX;
423
                double escalaY;
424

    
425
                escalaX = imageSize.getWidth() / extent.getWidth();
426
                escalaY = imageSize.getHeight() / extent.getHeight();
427

    
428
                double xCenter = extent.getCenterX();
429
                double yCenter = extent.getCenterY();
430
                double newHeight;
431
                double newWidth;
432

    
433
                adjustedExtent = new Rectangle2D.Double();
434

    
435
                if (escalaX < escalaY) {
436
                        scale = escalaX;
437
                        newHeight = imageSize.getHeight() / scale;
438
                        adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
439
                                yCenter - (newHeight / 2.0), extent.getWidth(), newHeight);
440
                } else {
441
                        scale = escalaY;
442
                        newWidth = imageSize.getWidth() / scale;
443
                        adjustedExtent.setRect(xCenter - (newWidth / 2.0),
444
                                yCenter - (extent.getHeight() / 2.0), newWidth,
445
                                extent.getHeight());
446
                }
447

    
448
                translacion.setToTranslation(-getAdjustedExtent().getX(),
449
                        -getAdjustedExtent().getY() - getAdjustedExtent().getHeight());
450
                escalado.setToScale(scale, -scale);
451

    
452
                AffineTransform offsetTrans = new AffineTransform();
453
                offsetTrans.setToTranslation(offset.getX(), offset.getY());
454

    
455
                trans.setToIdentity();
456
                trans.concatenate(offsetTrans);
457
                trans.concatenate(escalado);
458

    
459
                trans.concatenate(translacion);
460

    
461
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
462
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
463
                AffineTransform at;
464

    
465
                try {
466
                        at = trans.createInverse();
467

    
468
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
469
                        Point2D.Float pProv = new Point2D.Float();
470
                        at.deltaTransform(pPixel, pProv);
471

    
472
                        dist1pixel = pProv.x;
473
                        dist3pixel = 3 * pProv.x;
474
                } catch (NoninvertibleTransformException e) {
475
                        System.err.println("transformada afin = " + trans.toString());
476
                        System.err.println("extent = " + extent.toString() +
477
                                " imageSize= " + imageSize.toString());
478
                        throw new RuntimeException(e);
479
                }
480
        }
481

    
482
        /**
483
         * Inserta la desviaci?n.
484
         *
485
         * @param p Punto.
486
         */
487
        public void setOffset(Point2D p) {
488
                offset = p;
489
        }
490
        /**
491
         * Devuelve la desviaci?n.
492
         *
493
         * @param p Punto.
494
         */
495
        public Point2D getOffset() {
496
                return offset;
497
        }
498
        /**
499
         * Inserta el color de fondo.
500
         *
501
         * @param c Color de fondo.
502
         */
503
        public void setBackColor(Color c) {
504
                backColor = c;
505
                callColorChanged(backColor);
506
        }
507

    
508
        /**
509
         * Devuelve el color de fondo.
510
         *
511
         * @return Color de fondo.
512
         */
513
        public Color getBackColor() {
514
                return backColor;
515
        }
516

    
517
        /**
518
         * Returns the extent currently covered by the view.
519
         *
520
         * @return Returns the adjustedExtent.
521
         */
522
        public Rectangle2D getAdjustedExtent() {
523
                if (cliprect!=null){
524
                        return adjustedExtent.createIntersection(cliprect);
525
                }
526
                return adjustedExtent;
527
        }
528

    
529
        /**
530
         * Devuelve la unidad de medida.
531
         *
532
         * @return Returns the distanceUnits.
533
         */
534
        public int getDistanceUnits() {
535
                return distanceUnits;
536
        }
537

    
538
        /**
539
         * Inserta la unidad de medida.
540
         *
541
         * @param distanceUnits The distanceUnits to set.
542
         */
543
        public void setDistanceUnits(int distanceUnits) {
544
                this.distanceUnits = distanceUnits;
545
        }
546

    
547
        /**
548
         * Devuelve la unidad de medida del mapa.
549
         *
550
         * @return Returns the mapUnits.
551
         */
552
        public int getMapUnits() {
553
                return mapUnits;
554
        }
555

    
556
        /**
557
         * Inserta la unidad de medida del mapa.
558
         *
559
         * @param mapUnits The mapUnits to set.
560
         */
561
        public void setMapUnits(int mapUnits) {
562
                this.mapUnits = mapUnits;
563
        }
564

    
565
        /**
566
         * Devuelve la anchura de la imagen.
567
         *
568
         * @return anchura en pixels de la imagen.
569
         */
570
        public int getImageWidth() {
571
                return imageSize.width;
572
        }
573

    
574
        /**
575
         * Devuelve la altura de la imagen.
576
         *
577
         * @return altura de la imagen.
578
         */
579
        public int getImageHeight() {
580
                return imageSize.height;
581
        }
582

    
583
        /**
584
         * Devuelve la distancia real de un pixel.
585
         *
586
         * @return Distancia real de un pixel.
587
         */
588
        public double getDist1pixel() {
589
                return dist1pixel;
590
        }
591

    
592
        /**
593
         * Inserta la distancia real de un pixel.
594
         *
595
         * @param dist1pixel Distancia real de un pixel.
596
         */
597
        public void setDist1pixel(double dist1pixel) {
598
                this.dist1pixel = dist1pixel;
599
        }
600

    
601
        /**
602
         * Devuelve la distancia real de tres pixel.
603
         *
604
         * @return Distancia real de tres pixel.
605
         */
606
        public double getDist3pixel() {
607
                return dist3pixel;
608
        }
609

    
610
        /**
611
         * Inserta la distancia real de tres pixels.
612
         *
613
         * @param dist3pixel Distancia real de tres pixels.
614
         */
615
        public void setDist3pixel(double dist3pixel) {
616
                this.dist3pixel = dist3pixel;
617
        }
618

    
619
        /**
620
         * Devuelve los Extents anteriores almacenados.
621
         *
622
         * @return Returns the extents.
623
         */
624
        public ExtentHistory getExtents() {
625
                return extents;
626
        }
627

    
628
        /**
629
         * Devuelve la proyecci?n.
630
         *
631
         * @return Returns the proj.
632
         */
633
        public IProjection getProjection() {
634
                return proj;
635
        }
636

    
637
        /**
638
         * Inserta la proyecci?n.
639
         *
640
         * @param proj The proj to set.
641
         */
642
        public void setProjection(IProjection proj) {
643
                this.proj = proj;
644
        }
645

    
646
        /**
647
         * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
648
         * un calculateAffineTransform, ni recalcula el adjustedExtent.
649
         * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
650
         * RasterAdapter, y que recibiera un ViewPortData.
651
         * @param at
652
         */
653
        public void setAffineTransform(AffineTransform at)
654
        {
655
            this.trans = at;
656
        }
657

    
658
        /**
659
         * Devuelve el XMLEntity.
660
         *
661
         * @return XMLEntity.
662
         */
663
        public XMLEntity getXMLEntity() {
664
                XMLEntity xml = new XMLEntity();
665
                xml.putProperty("className",this.getClass().getName());
666

    
667
                if (adjustedExtent != null) {
668
                        xml.putProperty("adjustedExtentX", adjustedExtent.getX());
669
                        xml.putProperty("adjustedExtentY", adjustedExtent.getY());
670
                        xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
671
                        xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
672
                }
673

    
674
                if (backColor != null)
675
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
676

    
677
                if (clip != null) {
678
                        xml.putProperty("clipX", clip.getX());
679
                        xml.putProperty("clipY", clip.getY());
680
                        xml.putProperty("clipW", clip.getWidth());
681
                        xml.putProperty("clipH", clip.getHeight());
682
                }
683

    
684
                xml.putProperty("dist1pixel", dist1pixel);
685
                xml.putProperty("dist3pixel", dist3pixel);
686
                xml.putProperty("distanceUnits", distanceUnits);
687

    
688
                if (extent != null) {
689
                        xml.putProperty("extentX", extent.getX());
690
                        xml.putProperty("extentY", extent.getY());
691
                        xml.putProperty("extentW", extent.getWidth());
692
                        xml.putProperty("extentH", extent.getHeight());
693
                }
694

    
695
                xml.addChild(extents.getXMLEntity());
696
                xml.putProperty("mapUnits", mapUnits);
697
                xml.putProperty("offsetX", offset.getX());
698
                xml.putProperty("offsetY", offset.getY());
699

    
700
                if (proj != null) {
701
                        xml.putProperty("proj", proj.getAbrev());
702
                }
703

    
704
                xml.putProperty("scale", scale);
705

    
706
                return xml;
707
        }
708

    
709
        /**
710
         * Crea un nuevo ViewPort a partir del XMLEntity.
711
         *
712
         * @param xml XMLEntity.
713
         *
714
         * @return Nuevo ViewPort.
715
         */
716
        public static ViewPort createFromXML03(XMLEntity xml) {
717
                ViewPort vp = new ViewPort(null);
718

    
719
                if (xml.contains("adjustedExtentX")) {
720
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
721
                                                "adjustedExtentX"),
722
                                        xml.getDoubleProperty("adjustedExtentY"),
723
                                        xml.getDoubleProperty("adjustedExtentW"),
724
                                        xml.getDoubleProperty("adjustedExtentH"));
725
                }
726

    
727
                if (xml.contains("backColor")) {
728
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
729
                                                "backColor")));
730
                }
731

    
732
                if (xml.contains("clipX")) {
733
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
734
                                        xml.getDoubleProperty("clipY"),
735
                                        xml.getDoubleProperty("clipW"),
736
                                        xml.getDoubleProperty("clipH"));
737
                }
738

    
739
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
740
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
741
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
742
                vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
743

    
744
                if (xml.contains("extentX")) {
745
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
746
                                        xml.getDoubleProperty("extentY"),
747
                                        xml.getDoubleProperty("extentW"),
748
                                        xml.getDoubleProperty("extentH")));
749

    
750
                        //Calcula la transformaci?n af?n
751
                        vp.calculateAffineTransform();
752

    
753
                        // Lanzamos los eventos de extent cambiado
754
                        // vp.callExtentListeners(vp.adjustedExtent);
755
                }
756

    
757
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
758
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
759
                                xml.getDoubleProperty("offsetY")));
760

    
761
                if (xml.contains("proj")) {
762
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
763
                }
764

    
765
                //vp.setScale(xml.getDoubleProperty("scale"));
766
                vp.setScale();
767
                return vp;
768
        }
769

    
770
        /**
771
         * Crea un nuevo ViewPort a partir del XMLEntity.
772
         *
773
         * @param xml XMLEntity.
774
         *
775
         * @return Nuevo ViewPort.
776
         */
777
        public static ViewPort createFromXML(XMLEntity xml) {
778
                ViewPort vp = new ViewPort(null);
779

    
780
                if (xml.contains("adjustedExtentX")) {
781
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
782
                                                "adjustedExtentX"),
783
                                        xml.getDoubleProperty("adjustedExtentY"),
784
                                        xml.getDoubleProperty("adjustedExtentW"),
785
                                        xml.getDoubleProperty("adjustedExtentH"));
786
                }
787

    
788
                if (xml.contains("backColor")) {
789
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
790
                                                "backColor")));
791
                }
792

    
793
                if (xml.contains("clipX")) {
794
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
795
                                        xml.getDoubleProperty("clipY"),
796
                                        xml.getDoubleProperty("clipW"),
797
                                        xml.getDoubleProperty("clipH"));
798
                }
799

    
800
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
801
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
802
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
803
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
804

    
805
                if (xml.contains("extentX")) {
806
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
807
                                        xml.getDoubleProperty("extentY"),
808
                                        xml.getDoubleProperty("extentW"),
809
                                        xml.getDoubleProperty("extentH")));
810

    
811
                        //Calcula la transformaci?n af?n
812
                        vp.calculateAffineTransform();
813

    
814
                        // Lanzamos los eventos de extent cambiado
815
                        // vp.callExtentListeners(vp.adjustedExtent);
816
                }
817

    
818
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
819
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
820
                                xml.getDoubleProperty("offsetY")));
821

    
822
                if (xml.contains("proj")) {
823
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
824
                }
825

    
826
                //vp.setScale(xml.getDoubleProperty("scale"));
827
                vp.setScale();
828
                return vp;
829
        }
830

    
831
        /**
832
         * Clona el ViewPort.
833
         *
834
         * @return ViewPort clonado.
835
         */
836
        public ViewPort cloneViewPort() {
837
                return createFromXML(getXMLEntity());
838
        }
839

    
840
        /**
841
         * Devuelve el String con datos del ViewPort.
842
         *
843
         * @return Cadena con datos del ViewPort.
844
         */
845
        public String toString() {
846
                String str;
847
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
848
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
849
                        "\ntrans=" + trans;
850

    
851
                return str;
852
        }
853

    
854
        public void setClipRect(Rectangle2D rectView) {
855
                cliprect=rectView;
856

    
857
        }
858

    
859
        public Rectangle2D fromMapRectangle(Rectangle2D r) {
860
                double w=fromMapDistance((int)r.getWidth());
861
                double h=fromMapDistance((int)r.getHeight());
862
                Point2D p1=fromMapPoint((int)r.getX(),(int)r.getY());
863
                return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h);
864
        }
865

    
866

    
867
}