Statistics
| Revision:

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

History | View | Annotate | Download (21.3 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
         * The offset is the position where to start drawing. The offset of a View is
492
         * always (0, 0) because the drawing area fits with the full window area. But in
493
         * a Layout it is up to the place where the FFrameView is located.
494
         *
495
         * @param p Point, in pixels, where the map starts.
496
         */
497
        public Point2D getOffset() {
498
                return offset;
499
        }
500
        /**
501
         * Inserta el color de fondo.
502
         *
503
         * @param c Color de fondo.
504
         */
505
        public void setBackColor(Color c) {
506
                backColor = c;
507
                callColorChanged(backColor);
508
        }
509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
686
                xml.putProperty("dist1pixel", dist1pixel);
687
                xml.putProperty("dist3pixel", dist3pixel);
688
                xml.putProperty("distanceUnits", distanceUnits);
689

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

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

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

    
706
                xml.putProperty("scale", scale);
707

    
708
                return xml;
709
        }
710

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

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

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

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

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

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

    
752
                        //Calcula la transformaci?n af?n
753
                        vp.calculateAffineTransform();
754

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

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

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

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

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

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

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

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

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

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

    
813
                        //Calcula la transformaci?n af?n
814
                        vp.calculateAffineTransform();
815

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

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

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

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

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

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

    
853
                return str;
854
        }
855

    
856
        public void setClipRect(Rectangle2D rectView) {
857
                cliprect=rectView;
858

    
859
        }
860

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

    
868

    
869
}