Statistics
| Revision:

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

History | View | Annotate | Download (20.4 KB)

1 1100 fjp
/* 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 213 fernando
package com.iver.cit.gvsig.fmap;
42
43
import java.awt.Color;
44
import java.awt.Dimension;
45 234 fjp
import java.awt.Point;
46 4213 caballero
import java.awt.Rectangle;
47 213 fernando
import java.awt.geom.AffineTransform;
48 234 fjp
import java.awt.geom.NoninvertibleTransformException;
49 213 fernando
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.util.ArrayList;
52
53 3884 caballero
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 346 fernando
58 3884 caballero
import com.iver.utiles.StringUtilities;
59
import com.iver.utiles.XMLEntity;
60
61
62 956 vcaballero
/**
63 1036 vcaballero
 * Clase con atributos de la vista.
64 1443 jmorell
 * 050211, jmorell: A?ado los Grados como unidad de mapa.
65 956 vcaballero
 *
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 1443 jmorell
        public static int GRADOS = 8;
78 435 vcaballero
79 346 fernando
        /**
80 956 vcaballero
         * Resoluci?n (Puntos por pulgada) de la vista actual. Se necesita para los
81
         * c?lculos de escala geogr?fica.
82 346 fernando
         */
83 956 vcaballero
        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 213 fernando
        private int distanceUnits = METROS;
91
        private int mapUnits = METROS;
92 1197 fernando
        private ArrayList listeners = new ArrayList();
93 956 vcaballero
        private Point2D offset = new Point2D.Double(0, 0);
94
        private Rectangle2D clip;
95 1282 fjp
        private Color backColor = null; //Color.WHITE;
96 346 fernando
        private IProjection proj;
97 956 vcaballero
        private double dist1pixel;
98
        private double dist3pixel;
99 213 fernando
        private double scale;
100 3683 caballero
        private Rectangle2D cliprect;
101 956 vcaballero
102
        /**
103
         * Crea un nuevo ViewPort.
104
         *
105 1036 vcaballero
         * @param proj Proyecci?n.
106 956 vcaballero
         */
107
        public ViewPort(IProjection proj) {
108 867 fjp
                // Por defecto
109 956 vcaballero
                this.proj = proj;
110 867 fjp
        }
111 213 fernando
112 956 vcaballero
        /**
113 1036 vcaballero
         * A?ade un ViewPortListener al extentListener.
114 956 vcaballero
         *
115 1036 vcaballero
         * @param arg0 ViewPortListener.
116 956 vcaballero
         *
117 1036 vcaballero
         * @return True si ha sido a?adida correctamente.
118 956 vcaballero
         */
119 1197 fernando
        public boolean addViewPortListener(ViewPortListener arg0) {
120
                return listeners.add(arg0);
121 213 fernando
        }
122
123 956 vcaballero
        /**
124 1036 vcaballero
         * Borra el ViewPortListener que se pasa como par?metro delos
125
         * extentListener.
126 956 vcaballero
         *
127 1036 vcaballero
         * @param arg0 ViewPortListener.
128 956 vcaballero
         *
129 1036 vcaballero
         * @return True si ha sido borrado correctamente.
130 956 vcaballero
         */
131 1197 fernando
        public boolean removeViewPortListener(ViewPortListener arg0) {
132
                return listeners.remove(arg0);
133 213 fernando
        }
134
135 956 vcaballero
        /**
136 1036 vcaballero
         * Devuelve la distancia en pixels a partir de una distancia real.
137 956 vcaballero
         *
138 1036 vcaballero
         * @param d Distancia real.
139 956 vcaballero
         *
140 1036 vcaballero
         * @return Distancia en pixels.
141 956 vcaballero
         */
142
        public int fromMapDistance(double d) {
143
                Point2D.Double pWorld = new Point2D.Double(1, 1);
144
                Point2D.Double pScreen = new Point2D.Double();
145 234 fjp
146 956 vcaballero
                try {
147
                        trans.deltaTransform(pWorld, pScreen);
148
                } catch (Exception e) {
149
                        System.err.print(e.getMessage());
150
                }
151 213 fernando
152 956 vcaballero
                return (int) (d * pScreen.x);
153
        }
154 234 fjp
155 956 vcaballero
        /**
156 1036 vcaballero
         * Devuelve un punto en pixels a partir de una coordenada X e Y real.
157 956 vcaballero
         *
158 1036 vcaballero
         * @param x Coordenada X real.
159
         * @param y Coordenada Y real.
160 956 vcaballero
         *
161 1036 vcaballero
         * @return Punto en pixels.
162 956 vcaballero
         */
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 234 fjp
167 956 vcaballero
                try {
168
                        trans.transform(pWorld, pScreen);
169
                } catch (Exception e) {
170
                        System.err.print(e.getMessage());
171
                }
172 213 fernando
173 956 vcaballero
                return pScreen;
174
        }
175 1036 vcaballero
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 1004 vcaballero
        public Point2D fromMapPoint(Point2D point) {
184 1036 vcaballero
                return fromMapPoint(point.getX(), point.getY());
185 1004 vcaballero
        }
186 213 fernando
187 956 vcaballero
        /**
188 1036 vcaballero
         * Devuelve un punto real a partir de una coordenada X e Y en pixels.
189 956 vcaballero
         *
190 1036 vcaballero
         * @param x Coordenada X en pixels.
191
         * @param y Coordenada Y en pixels.
192 956 vcaballero
         *
193 1036 vcaballero
         * @return Punto real.
194 956 vcaballero
         */
195
        public Point2D toMapPoint(int x, int y) {
196
                Point pScreen = new Point(x, y);
197 213 fernando
198 956 vcaballero
                return toMapPoint(pScreen);
199
        }
200 234 fjp
201 4213 caballero
        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 956 vcaballero
        /**
208 1036 vcaballero
         * Devuelve la distancia real a partir de la distancia en pixels.
209 956 vcaballero
         *
210 1036 vcaballero
         * @param d Distancia en pixels.
211 956 vcaballero
         *
212 1036 vcaballero
         * @return Distancia real.
213 956 vcaballero
         */
214
        public double toMapDistance(int d) {
215
                double dist = d / trans.getScaleX();
216 234 fjp
217 956 vcaballero
                return dist;
218
        }
219 213 fernando
220 956 vcaballero
        /**
221 1036 vcaballero
         * Devuelve un punto real a partir de un punto en pixels.
222 956 vcaballero
         *
223 1036 vcaballero
         * @param pScreen Punto en pixels.
224 956 vcaballero
         *
225 1036 vcaballero
         * @return Punto real.
226 956 vcaballero
         *
227 1036 vcaballero
         * @throws RuntimeException
228 956 vcaballero
         */
229
        public Point2D toMapPoint(Point2D pScreen) {
230
                Point2D.Double pWorld = new Point2D.Double();
231
                AffineTransform at;
232 213 fernando
233 956 vcaballero
                try {
234
                        at = trans.createInverse();
235
                        at.transform(pScreen, pWorld);
236
                } catch (NoninvertibleTransformException e) {
237
                        throw new RuntimeException(e);
238
                }
239 213 fernando
240 956 vcaballero
                return pWorld;
241
        }
242 1036 vcaballero
243 999 luisw
        /**
244 1036 vcaballero
         * 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 999 luisw
         * @param pt1
250
         * @param pt2
251 1036 vcaballero
         *
252
         * @return distancia real.
253 999 luisw
         */
254
        public double distanceWorld(Point2D pt1, Point2D pt2) {
255
                double dist = -1;
256
                dist = pt1.distance(pt2);
257 1036 vcaballero
258
                if ((proj != null) && !(proj instanceof CSUTM)) {
259
                        dist = new GeoCalc(proj).distanceVincenty(proj.toGeo(pt1),
260
                                        proj.toGeo(pt2));
261
                }
262
263 999 luisw
                return dist;
264
        }
265 213 fernando
266 956 vcaballero
        /**
267 1036 vcaballero
         * Rellena el extent anterior como actual.
268 956 vcaballero
         */
269
        public void setPreviousExtent() {
270
                extent = extents.removePrev();
271 918 fernando
272 956 vcaballero
                //Calcula la transformaci?n af?n
273
                calculateAffineTransform();
274 680 fernando
275 956 vcaballero
                // Lanzamos los eventos de extent cambiado
276 3683 caballero
                callExtentChanged(getAdjustedExtent());
277 956 vcaballero
        }
278 213 fernando
279 956 vcaballero
        /**
280 1036 vcaballero
         * Devuelve el extent.
281 956 vcaballero
         *
282 1036 vcaballero
         * @return Extent.
283 956 vcaballero
         */
284
        public Rectangle2D getExtent() {
285
                return extent;
286
        }
287 213 fernando
288
        /**
289 1036 vcaballero
         * Inserta el extent.
290 956 vcaballero
         *
291 1036 vcaballero
         * @param r Extent.
292 956 vcaballero
         */
293
        public void setExtent(Rectangle2D r) {
294
                if (extent != null) {
295
                        extents.put(extent);
296
                }
297
298 3884 caballero
                //Esto comprueba que el extent no es de anchura o altura = "0"
299 956 vcaballero
                //y si es as? lo redimensiona.
300 2409 caballero
                if (r!=null &&((r.getWidth() == 0) || (r.getHeight() == 0))) {
301 956 vcaballero
                        extent = new Rectangle2D.Double(r.getMinX() - 0.1,
302
                                        r.getMinY() - 0.1, r.getWidth() + 0.2, r.getHeight() + 0.2);
303
                } else {
304
                        extent = r;
305
                }
306
307
                //Calcula la transformaci?n af?n
308
                calculateAffineTransform();
309
310
                // Lanzamos los eventos de extent cambiado
311 3683 caballero
                callExtentChanged(getAdjustedExtent());
312 956 vcaballero
        }
313
314
        /**
315 1036 vcaballero
         * Inserta la escala.
316 2970 fjp
     * TODO: (FJP) Que alguien me explique qu? sentido tiene esto
317 956 vcaballero
         *
318 1036 vcaballero
         * @param scale escala.
319 956 vcaballero
         */
320 1135 vcaballero
        public void setScale() {
321
                //this.scale = scale;
322 956 vcaballero
323
                //Calcula la transformaci?n af?n
324
                calculateAffineTransform();
325
326
                // Lanzamos los eventos de extent cambiado
327 3683 caballero
                callExtentChanged(getAdjustedExtent());
328 956 vcaballero
        }
329
330
        /**
331
         * Devuelve la escala. Debe estar siempre actualizada y no calcularse nunca
332
         * aqu? pues se utiliza en el dibujado para cada geometr?a
333
         *
334 1036 vcaballero
         * @return Escala.
335 213 fernando
         */
336 956 vcaballero
        public double getScale() {
337
                return proj.getScale(extent.getMinX(), extent.getMaxX(),
338
                        imageSize.getWidth(), dpi);
339
        }
340
341
        /**
342 1036 vcaballero
         * Devuelve la matriz de transformaci?n.
343 956 vcaballero
         *
344 1036 vcaballero
         * @return Matriz de transformaci?n.
345 956 vcaballero
         */
346 213 fernando
        public AffineTransform getAffineTransform() {
347
                return trans;
348
        }
349 956 vcaballero
350 213 fernando
        /**
351 1036 vcaballero
         * Devuelve las dimensiones de la imagen.
352 956 vcaballero
         *
353 213 fernando
         * @return Returns the imageSize.
354
         */
355
        public Dimension getImageSize() {
356
                return imageSize;
357
        }
358 956 vcaballero
359 213 fernando
        /**
360 1036 vcaballero
         * Inserta las dimensiones de la imagen.
361 956 vcaballero
         *
362 213 fernando
         * @param imageSize The imageSize to set.
363
         */
364
        public void setImageSize(Dimension imageSize) {
365
                this.imageSize = imageSize;
366
                calculateAffineTransform();
367
        }
368
369 956 vcaballero
        /**
370 1036 vcaballero
         * Llamada a los listeners tras el cambio de extent.
371 956 vcaballero
         *
372 1036 vcaballero
         * @param newRect Extent.
373 956 vcaballero
         */
374 1197 fernando
        private void callExtentChanged(Rectangle2D newRect) {
375 1082 fernando
                ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
376 655 fjp
377 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
378
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
379 956 vcaballero
                        listener.extentChanged(ev);
380
                }
381
        }
382
383 213 fernando
        /**
384 1036 vcaballero
         * Llamada a los listeners tras el cambio de color.
385 956 vcaballero
         *
386 1036 vcaballero
         * @param c Color.
387 213 fernando
         */
388 1197 fernando
        private void callColorChanged(Color c) {
389 1082 fernando
                ColorEvent ce = ColorEvent.createColorEvent(c);
390 956 vcaballero
391 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
392
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
393 956 vcaballero
                        listener.backColorChanged(ce);
394
                }
395
        }
396
397
        /**
398 1036 vcaballero
         * C?lculo de la matriz de transformaci?n.
399 956 vcaballero
         *
400 3884 caballero
         * @throws RuntimeException
401 956 vcaballero
         */
402 213 fernando
        private void calculateAffineTransform() {
403 956 vcaballero
                if ((imageSize == null) || (extent == null) ||
404
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
405
                        return;
406
                }
407 213 fernando
408 956 vcaballero
                AffineTransform escalado = new AffineTransform();
409
                AffineTransform translacion = new AffineTransform();
410 213 fernando
411 956 vcaballero
                double escalaX;
412
                double escalaY;
413 213 fernando
414 956 vcaballero
                escalaX = imageSize.getWidth() / extent.getWidth();
415
                escalaY = imageSize.getHeight() / extent.getHeight();
416 213 fernando
417 956 vcaballero
                double xCenter = extent.getCenterX();
418
                double yCenter = extent.getCenterY();
419
                double newHeight;
420
                double newWidth;
421 213 fernando
422 956 vcaballero
                adjustedExtent = new Rectangle2D.Double();
423 213 fernando
424 956 vcaballero
                if (escalaX < escalaY) {
425
                        scale = escalaX;
426
                        newHeight = imageSize.getHeight() / scale;
427
                        adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
428
                                yCenter - (newHeight / 2.0), extent.getWidth(), newHeight);
429
                } else {
430
                        scale = escalaY;
431
                        newWidth = imageSize.getWidth() / scale;
432
                        adjustedExtent.setRect(xCenter - (newWidth / 2.0),
433
                                yCenter - (extent.getHeight() / 2.0), newWidth,
434
                                extent.getHeight());
435
                }
436 213 fernando
437 3683 caballero
                translacion.setToTranslation(-getAdjustedExtent().getX(),
438
                        -getAdjustedExtent().getY() - getAdjustedExtent().getHeight());
439 956 vcaballero
                escalado.setToScale(scale, -scale);
440
441
                AffineTransform offsetTrans = new AffineTransform();
442
                offsetTrans.setToTranslation(offset.getX(), offset.getY());
443
444
                trans.setToIdentity();
445
                trans.concatenate(offsetTrans);
446
                trans.concatenate(escalado);
447
448
                trans.concatenate(translacion);
449
450 3884 caballero
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
451 956 vcaballero
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
452
                AffineTransform at;
453
454 237 fjp
                try {
455
                        at = trans.createInverse();
456 956 vcaballero
457 237 fjp
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
458 956 vcaballero
                        Point2D.Float pProv = new Point2D.Float();
459
                        at.deltaTransform(pPixel, pProv);
460 237 fjp
461 956 vcaballero
                        dist1pixel = pProv.x;
462
                        dist3pixel = 3 * pProv.x;
463 237 fjp
                } catch (NoninvertibleTransformException e) {
464 867 fjp
                        System.err.println("transformada afin = " + trans.toString());
465 956 vcaballero
                        System.err.println("extent = " + extent.toString() +
466
                                " imageSize= " + imageSize.toString());
467 918 fernando
                        throw new RuntimeException(e);
468 237 fjp
                }
469 213 fernando
        }
470 956 vcaballero
471
        /**
472 1036 vcaballero
         * Inserta la desviaci?n.
473 956 vcaballero
         *
474 1036 vcaballero
         * @param p Punto.
475 956 vcaballero
         */
476
        public void setOffset(Point2D p) {
477
                offset = p;
478 351 vcaballero
        }
479 956 vcaballero
        /**
480 1317 vcaballero
         * Devuelve la desviaci?n.
481
         *
482
         * @param p Punto.
483
         */
484
        public Point2D getOffset() {
485
                return offset;
486
        }
487
        /**
488 1036 vcaballero
         * Inserta el color de fondo.
489 956 vcaballero
         *
490 1036 vcaballero
         * @param c Color de fondo.
491 956 vcaballero
         */
492 213 fernando
        public void setBackColor(Color c) {
493
                backColor = c;
494 1197 fernando
                callColorChanged(backColor);
495 213 fernando
        }
496
497 956 vcaballero
        /**
498 1036 vcaballero
         * Devuelve el color de fondo.
499 956 vcaballero
         *
500 1036 vcaballero
         * @return Color de fondo.
501 956 vcaballero
         */
502 213 fernando
        public Color getBackColor() {
503
                return backColor;
504
        }
505 956 vcaballero
506 213 fernando
        /**
507 1036 vcaballero
         * Devuelve el extent ajustado.
508 956 vcaballero
         *
509 213 fernando
         * @return Returns the adjustedExtent.
510
         */
511
        public Rectangle2D getAdjustedExtent() {
512 3683 caballero
                if (cliprect!=null){
513
                        return adjustedExtent.createIntersection(cliprect);
514
                }
515 213 fernando
                return adjustedExtent;
516
        }
517 956 vcaballero
518 213 fernando
        /**
519 1036 vcaballero
         * Devuelve la unidad de medida.
520 956 vcaballero
         *
521 213 fernando
         * @return Returns the distanceUnits.
522
         */
523
        public int getDistanceUnits() {
524
                return distanceUnits;
525
        }
526 956 vcaballero
527 213 fernando
        /**
528 1036 vcaballero
         * Inserta la unidad de medida.
529 956 vcaballero
         *
530 213 fernando
         * @param distanceUnits The distanceUnits to set.
531
         */
532
        public void setDistanceUnits(int distanceUnits) {
533
                this.distanceUnits = distanceUnits;
534
        }
535 956 vcaballero
536 213 fernando
        /**
537 1036 vcaballero
         * Devuelve la unidad de medida del mapa.
538 956 vcaballero
         *
539 213 fernando
         * @return Returns the mapUnits.
540
         */
541
        public int getMapUnits() {
542
                return mapUnits;
543
        }
544 956 vcaballero
545 213 fernando
        /**
546 1036 vcaballero
         * Inserta la unidad de medida del mapa.
547 956 vcaballero
         *
548 213 fernando
         * @param mapUnits The mapUnits to set.
549
         */
550
        public void setMapUnits(int mapUnits) {
551
                this.mapUnits = mapUnits;
552 956 vcaballero
        }
553
554
        /**
555 1036 vcaballero
         * Devuelve la anchura de la imagen.
556 956 vcaballero
         *
557 1036 vcaballero
         * @return anchura en pixels de la imagen.
558 956 vcaballero
         */
559
        public int getImageWidth() {
560 214 fernando
                return imageSize.width;
561 213 fernando
        }
562 956 vcaballero
563
        /**
564 1036 vcaballero
         * Devuelve la altura de la imagen.
565 956 vcaballero
         *
566 1036 vcaballero
         * @return altura de la imagen.
567 956 vcaballero
         */
568
        public int getImageHeight() {
569 214 fernando
                return imageSize.height;
570
        }
571 956 vcaballero
572
        /**
573 1036 vcaballero
         * Devuelve la distancia real de un pixel.
574 956 vcaballero
         *
575 1036 vcaballero
         * @return Distancia real de un pixel.
576 956 vcaballero
         */
577 237 fjp
        public double getDist1pixel() {
578
                return dist1pixel;
579
        }
580 956 vcaballero
581
        /**
582 1036 vcaballero
         * Inserta la distancia real de un pixel.
583 956 vcaballero
         *
584 1036 vcaballero
         * @param dist1pixel Distancia real de un pixel.
585 956 vcaballero
         */
586 237 fjp
        public void setDist1pixel(double dist1pixel) {
587
                this.dist1pixel = dist1pixel;
588
        }
589 956 vcaballero
590
        /**
591 1036 vcaballero
         * Devuelve la distancia real de tres pixel.
592 956 vcaballero
         *
593 1036 vcaballero
         * @return Distancia real de tres pixel.
594 956 vcaballero
         */
595 237 fjp
        public double getDist3pixel() {
596
                return dist3pixel;
597
        }
598 956 vcaballero
599
        /**
600 1036 vcaballero
         * Inserta la distancia real de tres pixels.
601 956 vcaballero
         *
602 1036 vcaballero
         * @param dist3pixel Distancia real de tres pixels.
603 956 vcaballero
         */
604 237 fjp
        public void setDist3pixel(double dist3pixel) {
605
                this.dist3pixel = dist3pixel;
606
        }
607 956 vcaballero
608 277 fernando
        /**
609 1036 vcaballero
         * Devuelve los Extents anteriores almacenados.
610 956 vcaballero
         *
611 277 fernando
         * @return Returns the extents.
612
         */
613
        public ExtentHistory getExtents() {
614
                return extents;
615
        }
616
617
        /**
618 1036 vcaballero
         * Devuelve la proyecci?n.
619 956 vcaballero
         *
620 346 fernando
         * @return Returns the proj.
621
         */
622
        public IProjection getProjection() {
623
                return proj;
624
        }
625 956 vcaballero
626 346 fernando
        /**
627 1036 vcaballero
         * Inserta la proyecci?n.
628 956 vcaballero
         *
629 346 fernando
         * @param proj The proj to set.
630
         */
631
        public void setProjection(IProjection proj) {
632
                this.proj = proj;
633
        }
634 3884 caballero
635 1152 fjp
        /**
636
         * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
637
         * un calculateAffineTransform, ni recalcula el adjustedExtent.
638
         * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
639 3884 caballero
         * RasterAdapter, y que recibiera un ViewPortData.
640 1152 fjp
         * @param at
641
         */
642 3884 caballero
        public void setAffineTransform(AffineTransform at)
643 1152 fjp
        {
644
            this.trans = at;
645
        }
646 956 vcaballero
647
        /**
648 1036 vcaballero
         * Devuelve el XMLEntity.
649 956 vcaballero
         *
650 1036 vcaballero
         * @return XMLEntity.
651 956 vcaballero
         */
652
        public XMLEntity getXMLEntity() {
653
                XMLEntity xml = new XMLEntity();
654 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
655 956 vcaballero
656
                if (adjustedExtent != null) {
657
                        xml.putProperty("adjustedExtentX", adjustedExtent.getX());
658
                        xml.putProperty("adjustedExtentY", adjustedExtent.getY());
659
                        xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
660
                        xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
661 741 vcaballero
                }
662 956 vcaballero
663 1282 fjp
                if (backColor != null)
664
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
665 956 vcaballero
666
                if (clip != null) {
667
                        xml.putProperty("clipX", clip.getX());
668
                        xml.putProperty("clipY", clip.getY());
669
                        xml.putProperty("clipW", clip.getWidth());
670
                        xml.putProperty("clipH", clip.getHeight());
671 435 vcaballero
                }
672 956 vcaballero
673
                xml.putProperty("dist1pixel", dist1pixel);
674
                xml.putProperty("dist3pixel", dist3pixel);
675
                xml.putProperty("distanceUnits", distanceUnits);
676
677
                if (extent != null) {
678
                        xml.putProperty("extentX", extent.getX());
679
                        xml.putProperty("extentY", extent.getY());
680
                        xml.putProperty("extentW", extent.getWidth());
681
                        xml.putProperty("extentH", extent.getHeight());
682 741 vcaballero
                }
683 956 vcaballero
684 449 vcaballero
                xml.addChild(extents.getXMLEntity());
685 956 vcaballero
                xml.putProperty("mapUnits", mapUnits);
686
                xml.putProperty("offsetX", offset.getX());
687
                xml.putProperty("offsetY", offset.getY());
688
689
                if (proj != null) {
690
                        xml.putProperty("proj", proj.getAbrev());
691 442 vcaballero
                }
692 956 vcaballero
693
                xml.putProperty("scale", scale);
694
695 435 vcaballero
                return xml;
696
        }
697 956 vcaballero
698
        /**
699 1036 vcaballero
         * Crea un nuevo ViewPort a partir del XMLEntity.
700 956 vcaballero
         *
701 1036 vcaballero
         * @param xml XMLEntity.
702 956 vcaballero
         *
703 1036 vcaballero
         * @return Nuevo ViewPort.
704 956 vcaballero
         */
705 2183 fernando
        public static ViewPort createFromXML03(XMLEntity xml) {
706
                ViewPort vp = new ViewPort(null);
707
708
                if (xml.contains("adjustedExtentX")) {
709
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
710
                                                "adjustedExtentX"),
711
                                        xml.getDoubleProperty("adjustedExtentY"),
712
                                        xml.getDoubleProperty("adjustedExtentW"),
713
                                        xml.getDoubleProperty("adjustedExtentH"));
714
                }
715
716
                if (xml.contains("backColor")) {
717
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
718
                                                "backColor")));
719
                }
720
721
                if (xml.contains("clipX")) {
722
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
723
                                        xml.getDoubleProperty("clipY"),
724
                                        xml.getDoubleProperty("clipW"),
725
                                        xml.getDoubleProperty("clipH"));
726
                }
727
728
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
729
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
730
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
731
                vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
732
733
                if (xml.contains("extentX")) {
734
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
735
                                        xml.getDoubleProperty("extentY"),
736
                                        xml.getDoubleProperty("extentW"),
737
                                        xml.getDoubleProperty("extentH")));
738
739
                        //Calcula la transformaci?n af?n
740
                        vp.calculateAffineTransform();
741
742
                        // Lanzamos los eventos de extent cambiado
743
                        // vp.callExtentListeners(vp.adjustedExtent);
744
                }
745
746
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
747
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
748
                                xml.getDoubleProperty("offsetY")));
749
750
                if (xml.contains("proj")) {
751
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
752
                }
753
754
                //vp.setScale(xml.getDoubleProperty("scale"));
755
                vp.setScale();
756
                return vp;
757
        }
758
759
        /**
760
         * Crea un nuevo ViewPort a partir del XMLEntity.
761
         *
762
         * @param xml XMLEntity.
763
         *
764
         * @return Nuevo ViewPort.
765
         */
766 956 vcaballero
        public static ViewPort createFromXML(XMLEntity xml) {
767
                ViewPort vp = new ViewPort(null);
768
769
                if (xml.contains("adjustedExtentX")) {
770
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
771
                                                "adjustedExtentX"),
772
                                        xml.getDoubleProperty("adjustedExtentY"),
773
                                        xml.getDoubleProperty("adjustedExtentW"),
774
                                        xml.getDoubleProperty("adjustedExtentH"));
775 741 vcaballero
                }
776 956 vcaballero
777
                if (xml.contains("backColor")) {
778
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
779
                                                "backColor")));
780 435 vcaballero
                }
781 956 vcaballero
782
                if (xml.contains("clipX")) {
783
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
784
                                        xml.getDoubleProperty("clipY"),
785
                                        xml.getDoubleProperty("clipW"),
786
                                        xml.getDoubleProperty("clipH"));
787 741 vcaballero
                }
788 956 vcaballero
789 442 vcaballero
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
790
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
791
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
792 956 vcaballero
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
793 918 fernando
794 956 vcaballero
                if (xml.contains("extentX")) {
795
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
796
                                        xml.getDoubleProperty("extentY"),
797
                                        xml.getDoubleProperty("extentW"),
798
                                        xml.getDoubleProperty("extentH")));
799
800
                        //Calcula la transformaci?n af?n
801
                        vp.calculateAffineTransform();
802
803
                        // Lanzamos los eventos de extent cambiado
804 1040 fjp
                        // vp.callExtentListeners(vp.adjustedExtent);
805 741 vcaballero
                }
806 956 vcaballero
807 442 vcaballero
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
808 956 vcaballero
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
809
                                xml.getDoubleProperty("offsetY")));
810
811
                if (xml.contains("proj")) {
812
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
813 442 vcaballero
                }
814 956 vcaballero
815 1135 vcaballero
                //vp.setScale(xml.getDoubleProperty("scale"));
816
                vp.setScale();
817 435 vcaballero
                return vp;
818
        }
819 956 vcaballero
820
        /**
821 1036 vcaballero
         * Clona el ViewPort.
822 956 vcaballero
         *
823 1036 vcaballero
         * @return ViewPort clonado.
824 956 vcaballero
         */
825
        public ViewPort cloneViewPort() {
826
                return createFromXML(getXMLEntity());
827 798 vcaballero
        }
828 956 vcaballero
829
        /**
830 1036 vcaballero
         * Devuelve el String con datos del ViewPort.
831 956 vcaballero
         *
832 1036 vcaballero
         * @return Cadena con datos del ViewPort.
833 956 vcaballero
         */
834
        public String toString() {
835 875 fjp
                String str;
836 956 vcaballero
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
837
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
838 875 fjp
                        "\ntrans=" + trans;
839 956 vcaballero
840 875 fjp
                return str;
841
        }
842 3683 caballero
843
        public void setClipRect(Rectangle2D rectView) {
844
                cliprect=rectView;
845 3884 caballero
846 3683 caballero
        }
847
848 4213 caballero
        public Rectangle2D fromMapRectangle(Rectangle2D r) {
849
                double w=fromMapDistance((int)r.getWidth());
850
                double h=fromMapDistance((int)r.getHeight());
851
                Point2D p1=fromMapPoint((int)r.getX(),(int)r.getY());
852
                return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h);
853
        }
854 3884 caballero
855 4213 caballero
856 213 fernando
}