Statistics
| Revision:

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

History | View | Annotate | Download (21.8 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.geom.AffineTransform;
47
import java.awt.geom.NoninvertibleTransformException;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.util.ArrayList;
51

    
52
import org.cresques.cts.GeoCalc;
53
import org.cresques.cts.IProjection;
54
import org.cresques.cts.gt2.CSUTM;
55

    
56
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
57
import com.iver.utiles.StringUtilities;
58
import com.iver.utiles.XMLEntity;
59

    
60

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

    
78
        /**
79
         * Resoluci?n (Puntos por pulgada) de la vista actual. Se necesita para los
80
         * c?lculos de escala geogr?fica.
81
         */
82
        private static int dpi = java.awt.Toolkit.getDefaultToolkit()
83
                                                                                         .getScreenResolution();
84
        private Rectangle2D extent;
85
        private Rectangle2D adjustedExtent;
86
        private ExtentHistory extents = new ExtentHistory();
87
        private Dimension imageSize;
88
        private AffineTransform trans = new AffineTransform();
89
        private int distanceUnits = METROS;
90
        private int mapUnits = METROS;
91
        private ArrayList listeners = new ArrayList();
92
        private Point2D offset = new Point2D.Double(0, 0);
93
        private Rectangle2D clip;
94
        private Color backColor = null; //Color.WHITE;
95
        private IProjection proj;
96
        private double dist1pixel;
97
        private double dist3pixel;
98
        private double scale;
99
        private Rectangle2D cliprect;
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
                try {
146
                        trans.deltaTransform(pWorld, pScreen);
147
                } catch (Exception e) {
148
                        System.err.print(e.getMessage());
149
                }
150

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

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

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

    
172
                return pScreen;
173
        }
174

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

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

    
197
                return toMapPoint(pScreen);
198
        }
199

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

    
216
                return dist;
217
        }
218

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

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

    
239
                return pWorld;
240
        }
241

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

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

    
262
                return dist;
263
        }
264

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
401
                for (int i = 0; i < listeners.size(); i++) {
402
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
403
                        listener.backColorChanged(ce);
404
                }
405
        }
406
        /**
407
         * Llamada a los listeners tras el cambio de extent.
408
         *
409
         * @param newRect Extent.
410
         */
411
        private void callProjectionChanged(IProjection projection) {
412
                ProjectionEvent ev = ProjectionEvent.createProjectionEvent(projection);
413

    
414
                for (int i = 0; i < listeners.size(); i++) {
415
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
416
                        listener.projectionChanged(ev);
417
                }
418
        }
419

    
420
        
421
        /**
422
         * C?lculo de la matriz de transformaci?n.
423
         *
424
         * @throws RuntimeException
425
         */
426
        private void calculateAffineTransform() {
427
                if ((imageSize == null) || (extent == null) ||
428
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
429
                        return;
430
                }
431

    
432
                AffineTransform escalado = new AffineTransform();
433
                AffineTransform translacion = new AffineTransform();
434

    
435
                double escalaX;
436
                double escalaY;
437

    
438
                escalaX = imageSize.getWidth() / extent.getWidth();
439
                escalaY = imageSize.getHeight() / extent.getHeight();
440

    
441
                double xCenter = extent.getCenterX();
442
                double yCenter = extent.getCenterY();
443
                double newHeight;
444
                double newWidth;
445

    
446
                adjustedExtent = new Rectangle2D.Double();
447

    
448
                if (escalaX < escalaY) {
449
                        scale = escalaX;
450
                        newHeight = imageSize.getHeight() / scale;
451
                        adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
452
                                yCenter - (newHeight / 2.0), extent.getWidth(), newHeight);
453
                } else {
454
                        scale = escalaY;
455
                        newWidth = imageSize.getWidth() / scale;
456
                        adjustedExtent.setRect(xCenter - (newWidth / 2.0),
457
                                yCenter - (extent.getHeight() / 2.0), newWidth,
458
                                extent.getHeight());
459
                }
460

    
461
                translacion.setToTranslation(-getAdjustedExtent().getX(),
462
                        -getAdjustedExtent().getY() - getAdjustedExtent().getHeight());
463
                escalado.setToScale(scale, -scale);
464

    
465
                AffineTransform offsetTrans = new AffineTransform();
466
                offsetTrans.setToTranslation(offset.getX(), offset.getY());
467

    
468
                trans.setToIdentity();
469
                trans.concatenate(offsetTrans);
470
                trans.concatenate(escalado);
471

    
472
                trans.concatenate(translacion);
473

    
474
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
475
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
476
                AffineTransform at;
477

    
478
                try {
479
                        at = trans.createInverse();
480

    
481
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
482
                        Point2D.Float pProv = new Point2D.Float();
483
                        at.deltaTransform(pPixel, pProv);
484

    
485
                        dist1pixel = pProv.x;
486
                        dist3pixel = 3 * pProv.x;
487
                } catch (NoninvertibleTransformException e) {
488
                        System.err.println("transformada afin = " + trans.toString());
489
                        System.err.println("extent = " + extent.toString() +
490
                                " imageSize= " + imageSize.toString());
491
                        throw new RuntimeException(e);
492
                }
493
        }
494

    
495
        /**
496
         * Inserta la desviaci?n.
497
         *
498
         * @param p Punto.
499
         */
500
        public void setOffset(Point2D p) {
501
                offset = p;
502
        }
503
        /**
504
         * The offset is the position where to start drawing. The offset of a View is
505
         * always (0, 0) because the drawing area fits with the full window area. But in
506
         * a Layout it is up to the place where the FFrameView is located.
507
         *
508
         * @param p Point, in pixels, where the map starts.
509
         */
510
        public Point2D getOffset() {
511
                return offset;
512
        }
513
        /**
514
         * Inserta el color de fondo.
515
         *
516
         * @param c Color de fondo.
517
         */
518
        public void setBackColor(Color c) {
519
                backColor = c;
520
                callColorChanged(backColor);
521
        }
522

    
523
        /**
524
         * Devuelve el color de fondo.
525
         *
526
         * @return Color de fondo.
527
         */
528
        public Color getBackColor() {
529
                return backColor;
530
        }
531

    
532
        /**
533
         * Returns the extent currently covered by the view.
534
         *
535
         * @return Returns the adjustedExtent.
536
         */
537
        public Rectangle2D getAdjustedExtent() {
538
                if (cliprect!=null){
539
                        return adjustedExtent.createIntersection(cliprect);
540
                }
541
                return adjustedExtent;
542
        }
543

    
544
        /**
545
         * Devuelve la unidad de medida.
546
         *
547
         * @return Returns the distanceUnits.
548
         */
549
        public int getDistanceUnits() {
550
                return distanceUnits;
551
        }
552

    
553
        /**
554
         * Inserta la unidad de medida.
555
         *
556
         * @param distanceUnits The distanceUnits to set.
557
         */
558
        public void setDistanceUnits(int distanceUnits) {
559
                this.distanceUnits = distanceUnits;
560
        }
561

    
562
        /**
563
         * Devuelve la unidad de medida del mapa.
564
         *
565
         * @return Returns the mapUnits.
566
         */
567
        public int getMapUnits() {
568
                return mapUnits;
569
        }
570

    
571
        /**
572
         * Inserta la unidad de medida del mapa.
573
         *
574
         * @param mapUnits The mapUnits to set.
575
         */
576
        public void setMapUnits(int mapUnits) {
577
                this.mapUnits = mapUnits;
578
        }
579

    
580
        /**
581
         * Devuelve la anchura de la imagen.
582
         *
583
         * @return anchura en pixels de la imagen.
584
         */
585
        public int getImageWidth() {
586
                return imageSize.width;
587
        }
588

    
589
        /**
590
         * Devuelve la altura de la imagen.
591
         *
592
         * @return altura de la imagen.
593
         */
594
        public int getImageHeight() {
595
                return imageSize.height;
596
        }
597

    
598
        /**
599
         * Devuelve la distancia real de un pixel.
600
         *
601
         * @return Distancia real de un pixel.
602
         */
603
        public double getDist1pixel() {
604
                return dist1pixel;
605
        }
606

    
607
        /**
608
         * Inserta la distancia real de un pixel.
609
         *
610
         * @param dist1pixel Distancia real de un pixel.
611
         */
612
        public void setDist1pixel(double dist1pixel) {
613
                this.dist1pixel = dist1pixel;
614
        }
615

    
616
        /**
617
         * Devuelve la distancia real de tres pixel.
618
         *
619
         * @return Distancia real de tres pixel.
620
         */
621
        public double getDist3pixel() {
622
                return dist3pixel;
623
        }
624

    
625
        /**
626
         * Inserta la distancia real de tres pixels.
627
         *
628
         * @param dist3pixel Distancia real de tres pixels.
629
         */
630
        public void setDist3pixel(double dist3pixel) {
631
                this.dist3pixel = dist3pixel;
632
        }
633

    
634
        /**
635
         * Devuelve los Extents anteriores almacenados.
636
         *
637
         * @return Returns the extents.
638
         */
639
        public ExtentHistory getExtents() {
640
                return extents;
641
        }
642

    
643
        /**
644
         * Devuelve la proyecci?n.
645
         *
646
         * @return Returns the proj.
647
         */
648
        public IProjection getProjection() {
649
                return proj;
650
        }
651

    
652
        /**
653
         * Inserta la proyecci?n.
654
         *
655
         * @param proj The proj to set.
656
         */
657
        public void setProjection(IProjection proj) {
658
                if(this.proj == null || !this.proj.getAbrev().equals(proj.getAbrev())) {
659
                        this.proj = proj;                
660
                        callProjectionChanged(proj);
661
                }
662
        }
663

    
664
        /**
665
         * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
666
         * un calculateAffineTransform, ni recalcula el adjustedExtent.
667
         * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
668
         * RasterAdapter, y que recibiera un ViewPortData.
669
         * @param at
670
         */
671
        public void setAffineTransform(AffineTransform at)
672
        {
673
            this.trans = at;
674
        }
675

    
676
        /**
677
         * Devuelve el XMLEntity.
678
         *
679
         * @return XMLEntity.
680
         */
681
        public XMLEntity getXMLEntity() {
682
                XMLEntity xml = new XMLEntity();
683
                xml.putProperty("className",this.getClass().getName());
684

    
685
                if (adjustedExtent != null) {
686
                        xml.putProperty("adjustedExtentX", adjustedExtent.getX());
687
                        xml.putProperty("adjustedExtentY", adjustedExtent.getY());
688
                        xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
689
                        xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
690
                }
691

    
692
                if (backColor != null)
693
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
694

    
695
                if (clip != null) {
696
                        xml.putProperty("clipX", clip.getX());
697
                        xml.putProperty("clipY", clip.getY());
698
                        xml.putProperty("clipW", clip.getWidth());
699
                        xml.putProperty("clipH", clip.getHeight());
700
                }
701

    
702
                xml.putProperty("dist1pixel", dist1pixel);
703
                xml.putProperty("dist3pixel", dist3pixel);
704
                xml.putProperty("distanceUnits", distanceUnits);
705

    
706
                if (extent != null) {
707
                        xml.putProperty("extentX", extent.getX());
708
                        xml.putProperty("extentY", extent.getY());
709
                        xml.putProperty("extentW", extent.getWidth());
710
                        xml.putProperty("extentH", extent.getHeight());
711
                }
712

    
713
                xml.addChild(extents.getXMLEntity());
714
                xml.putProperty("mapUnits", mapUnits);
715
                xml.putProperty("offsetX", offset.getX());
716
                xml.putProperty("offsetY", offset.getY());
717

    
718
                if (proj != null) {
719
                        xml.putProperty("proj", proj.getAbrev());
720
                }
721

    
722
                xml.putProperty("scale", scale);
723

    
724
                return xml;
725
        }
726

    
727
        /**
728
         * Crea un nuevo ViewPort a partir del XMLEntity.
729
         *
730
         * @param xml XMLEntity.
731
         *
732
         * @return Nuevo ViewPort.
733
         */
734
        public static ViewPort createFromXML03(XMLEntity xml) {
735
                ViewPort vp = new ViewPort(null);
736

    
737
                if (xml.contains("adjustedExtentX")) {
738
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
739
                                                "adjustedExtentX"),
740
                                        xml.getDoubleProperty("adjustedExtentY"),
741
                                        xml.getDoubleProperty("adjustedExtentW"),
742
                                        xml.getDoubleProperty("adjustedExtentH"));
743
                }
744

    
745
                if (xml.contains("backColor")) {
746
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
747
                                                "backColor")));
748
                }
749

    
750
                if (xml.contains("clipX")) {
751
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
752
                                        xml.getDoubleProperty("clipY"),
753
                                        xml.getDoubleProperty("clipW"),
754
                                        xml.getDoubleProperty("clipH"));
755
                }
756

    
757
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
758
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
759
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
760
                vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
761

    
762
                if (xml.contains("extentX")) {
763
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
764
                                        xml.getDoubleProperty("extentY"),
765
                                        xml.getDoubleProperty("extentW"),
766
                                        xml.getDoubleProperty("extentH")));
767

    
768
                        //Calcula la transformaci?n af?n
769
                        vp.calculateAffineTransform();
770

    
771
                        // Lanzamos los eventos de extent cambiado
772
                        // vp.callExtentListeners(vp.adjustedExtent);
773
                }
774

    
775
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
776
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
777
                                xml.getDoubleProperty("offsetY")));
778

    
779
                if (xml.contains("proj")) {
780
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
781
                }
782

    
783
                //vp.setScale(xml.getDoubleProperty("scale"));
784
                vp.setScale();
785
                return vp;
786
        }
787

    
788
        /**
789
         * Crea un nuevo ViewPort a partir del XMLEntity.
790
         *
791
         * @param xml XMLEntity.
792
         *
793
         * @return Nuevo ViewPort.
794
         */
795
        public static ViewPort createFromXML(XMLEntity xml) {
796
                ViewPort vp = new ViewPort(null);
797

    
798
                if (xml.contains("adjustedExtentX")) {
799
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
800
                                                "adjustedExtentX"),
801
                                        xml.getDoubleProperty("adjustedExtentY"),
802
                                        xml.getDoubleProperty("adjustedExtentW"),
803
                                        xml.getDoubleProperty("adjustedExtentH"));
804
                }
805

    
806
                if (xml.contains("backColor")) {
807
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
808
                                                "backColor")));
809
                }
810

    
811
                if (xml.contains("clipX")) {
812
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
813
                                        xml.getDoubleProperty("clipY"),
814
                                        xml.getDoubleProperty("clipW"),
815
                                        xml.getDoubleProperty("clipH"));
816
                }
817

    
818
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
819
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
820
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
821
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
822

    
823
                if (xml.contains("extentX")) {
824
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
825
                                        xml.getDoubleProperty("extentY"),
826
                                        xml.getDoubleProperty("extentW"),
827
                                        xml.getDoubleProperty("extentH")));
828

    
829
                        //Calcula la transformaci?n af?n
830
                        vp.calculateAffineTransform();
831

    
832
                        // Lanzamos los eventos de extent cambiado
833
                        // vp.callExtentListeners(vp.adjustedExtent);
834
                }
835

    
836
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
837
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
838
                                xml.getDoubleProperty("offsetY")));
839

    
840
                if (xml.contains("proj")) {
841
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
842
                }
843

    
844
                //vp.setScale(xml.getDoubleProperty("scale"));
845
                vp.setScale();
846
                return vp;
847
        }
848

    
849
        /**
850
         * Clona el ViewPort.
851
         *
852
         * @return ViewPort clonado.
853
         */
854
        public ViewPort cloneViewPort() {
855
                return createFromXML(getXMLEntity());
856
        }
857

    
858
        /**
859
         * Devuelve el String con datos del ViewPort.
860
         *
861
         * @return Cadena con datos del ViewPort.
862
         */
863
        public String toString() {
864
                String str;
865
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
866
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
867
                        "\ntrans=" + trans;
868

    
869
                return str;
870
        }
871

    
872
        public void setClipRect(Rectangle2D rectView) {
873
                cliprect=rectView;
874

    
875
        }
876

    
877
        public Rectangle2D fromMapRectangle(Rectangle2D r) {
878
                double w=fromMapDistance((int)r.getWidth());
879
                double h=fromMapDistance((int)r.getHeight());
880
                Point2D p1=fromMapPoint((int)r.getX(),(int)r.getY());
881
                return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h);
882
        }
883

    
884

    
885
}