Statistics
| Revision:

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

History | View | Annotate | Download (21.1 KB)

1 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 4910 jaume
         * <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 1036 vcaballero
         * @return Extent.
294 956 vcaballero
         */
295
        public Rectangle2D getExtent() {
296
                return extent;
297
        }
298 213 fernando
299
        /**
300 1036 vcaballero
         * Inserta el extent.
301 956 vcaballero
         *
302 1036 vcaballero
         * @param r Extent.
303 956 vcaballero
         */
304
        public void setExtent(Rectangle2D r) {
305
                if (extent != null) {
306
                        extents.put(extent);
307
                }
308
309 3884 caballero
                //Esto comprueba que el extent no es de anchura o altura = "0"
310 956 vcaballero
                //y si es as? lo redimensiona.
311 2409 caballero
                if (r!=null &&((r.getWidth() == 0) || (r.getHeight() == 0))) {
312 956 vcaballero
                        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 3683 caballero
                callExtentChanged(getAdjustedExtent());
323 956 vcaballero
        }
324
325
        /**
326 1036 vcaballero
         * Inserta la escala.
327 2970 fjp
     * TODO: (FJP) Que alguien me explique qu? sentido tiene esto
328 956 vcaballero
         *
329 1036 vcaballero
         * @param scale escala.
330 956 vcaballero
         */
331 1135 vcaballero
        public void setScale() {
332
                //this.scale = scale;
333 956 vcaballero
334
                //Calcula la transformaci?n af?n
335
                calculateAffineTransform();
336
337
                // Lanzamos los eventos de extent cambiado
338 3683 caballero
                callExtentChanged(getAdjustedExtent());
339 956 vcaballero
        }
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 1036 vcaballero
         * @return Escala.
346 213 fernando
         */
347 956 vcaballero
        public double getScale() {
348
                return proj.getScale(extent.getMinX(), extent.getMaxX(),
349
                        imageSize.getWidth(), dpi);
350
        }
351
352
        /**
353 1036 vcaballero
         * Devuelve la matriz de transformaci?n.
354 956 vcaballero
         *
355 1036 vcaballero
         * @return Matriz de transformaci?n.
356 956 vcaballero
         */
357 213 fernando
        public AffineTransform getAffineTransform() {
358
                return trans;
359
        }
360 956 vcaballero
361 213 fernando
        /**
362 1036 vcaballero
         * Devuelve las dimensiones de la imagen.
363 956 vcaballero
         *
364 213 fernando
         * @return Returns the imageSize.
365
         */
366
        public Dimension getImageSize() {
367
                return imageSize;
368
        }
369 956 vcaballero
370 213 fernando
        /**
371 1036 vcaballero
         * Inserta las dimensiones de la imagen.
372 956 vcaballero
         *
373 213 fernando
         * @param imageSize The imageSize to set.
374
         */
375
        public void setImageSize(Dimension imageSize) {
376
                this.imageSize = imageSize;
377
                calculateAffineTransform();
378
        }
379
380 956 vcaballero
        /**
381 1036 vcaballero
         * Llamada a los listeners tras el cambio de extent.
382 956 vcaballero
         *
383 1036 vcaballero
         * @param newRect Extent.
384 956 vcaballero
         */
385 1197 fernando
        private void callExtentChanged(Rectangle2D newRect) {
386 1082 fernando
                ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
387 655 fjp
388 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
389
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
390 956 vcaballero
                        listener.extentChanged(ev);
391
                }
392
        }
393
394 213 fernando
        /**
395 1036 vcaballero
         * Llamada a los listeners tras el cambio de color.
396 956 vcaballero
         *
397 1036 vcaballero
         * @param c Color.
398 213 fernando
         */
399 1197 fernando
        private void callColorChanged(Color c) {
400 1082 fernando
                ColorEvent ce = ColorEvent.createColorEvent(c);
401 956 vcaballero
402 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
403
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
404 956 vcaballero
                        listener.backColorChanged(ce);
405
                }
406
        }
407
408
        /**
409 1036 vcaballero
         * C?lculo de la matriz de transformaci?n.
410 956 vcaballero
         *
411 3884 caballero
         * @throws RuntimeException
412 956 vcaballero
         */
413 213 fernando
        private void calculateAffineTransform() {
414 956 vcaballero
                if ((imageSize == null) || (extent == null) ||
415
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
416
                        return;
417
                }
418 213 fernando
419 956 vcaballero
                AffineTransform escalado = new AffineTransform();
420
                AffineTransform translacion = new AffineTransform();
421 213 fernando
422 956 vcaballero
                double escalaX;
423
                double escalaY;
424 213 fernando
425 956 vcaballero
                escalaX = imageSize.getWidth() / extent.getWidth();
426
                escalaY = imageSize.getHeight() / extent.getHeight();
427 213 fernando
428 956 vcaballero
                double xCenter = extent.getCenterX();
429
                double yCenter = extent.getCenterY();
430
                double newHeight;
431
                double newWidth;
432 213 fernando
433 956 vcaballero
                adjustedExtent = new Rectangle2D.Double();
434 213 fernando
435 956 vcaballero
                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 213 fernando
448 3683 caballero
                translacion.setToTranslation(-getAdjustedExtent().getX(),
449
                        -getAdjustedExtent().getY() - getAdjustedExtent().getHeight());
450 956 vcaballero
                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 3884 caballero
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
462 956 vcaballero
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
463
                AffineTransform at;
464
465 237 fjp
                try {
466
                        at = trans.createInverse();
467 956 vcaballero
468 237 fjp
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
469 956 vcaballero
                        Point2D.Float pProv = new Point2D.Float();
470
                        at.deltaTransform(pPixel, pProv);
471 237 fjp
472 956 vcaballero
                        dist1pixel = pProv.x;
473
                        dist3pixel = 3 * pProv.x;
474 237 fjp
                } catch (NoninvertibleTransformException e) {
475 867 fjp
                        System.err.println("transformada afin = " + trans.toString());
476 956 vcaballero
                        System.err.println("extent = " + extent.toString() +
477
                                " imageSize= " + imageSize.toString());
478 918 fernando
                        throw new RuntimeException(e);
479 237 fjp
                }
480 213 fernando
        }
481 956 vcaballero
482
        /**
483 1036 vcaballero
         * Inserta la desviaci?n.
484 956 vcaballero
         *
485 1036 vcaballero
         * @param p Punto.
486 956 vcaballero
         */
487
        public void setOffset(Point2D p) {
488
                offset = p;
489 351 vcaballero
        }
490 956 vcaballero
        /**
491 1317 vcaballero
         * Devuelve la desviaci?n.
492
         *
493
         * @param p Punto.
494
         */
495
        public Point2D getOffset() {
496
                return offset;
497
        }
498
        /**
499 1036 vcaballero
         * Inserta el color de fondo.
500 956 vcaballero
         *
501 1036 vcaballero
         * @param c Color de fondo.
502 956 vcaballero
         */
503 213 fernando
        public void setBackColor(Color c) {
504
                backColor = c;
505 1197 fernando
                callColorChanged(backColor);
506 213 fernando
        }
507
508 956 vcaballero
        /**
509 1036 vcaballero
         * Devuelve el color de fondo.
510 956 vcaballero
         *
511 1036 vcaballero
         * @return Color de fondo.
512 956 vcaballero
         */
513 213 fernando
        public Color getBackColor() {
514
                return backColor;
515
        }
516 956 vcaballero
517 213 fernando
        /**
518 4910 jaume
         * Returns the extent currently covered by the view.
519 956 vcaballero
         *
520 213 fernando
         * @return Returns the adjustedExtent.
521
         */
522
        public Rectangle2D getAdjustedExtent() {
523 3683 caballero
                if (cliprect!=null){
524
                        return adjustedExtent.createIntersection(cliprect);
525
                }
526 213 fernando
                return adjustedExtent;
527
        }
528 956 vcaballero
529 213 fernando
        /**
530 1036 vcaballero
         * Devuelve la unidad de medida.
531 956 vcaballero
         *
532 213 fernando
         * @return Returns the distanceUnits.
533
         */
534
        public int getDistanceUnits() {
535
                return distanceUnits;
536
        }
537 956 vcaballero
538 213 fernando
        /**
539 1036 vcaballero
         * Inserta la unidad de medida.
540 956 vcaballero
         *
541 213 fernando
         * @param distanceUnits The distanceUnits to set.
542
         */
543
        public void setDistanceUnits(int distanceUnits) {
544
                this.distanceUnits = distanceUnits;
545
        }
546 956 vcaballero
547 213 fernando
        /**
548 1036 vcaballero
         * Devuelve la unidad de medida del mapa.
549 956 vcaballero
         *
550 213 fernando
         * @return Returns the mapUnits.
551
         */
552
        public int getMapUnits() {
553
                return mapUnits;
554
        }
555 956 vcaballero
556 213 fernando
        /**
557 1036 vcaballero
         * Inserta la unidad de medida del mapa.
558 956 vcaballero
         *
559 213 fernando
         * @param mapUnits The mapUnits to set.
560
         */
561
        public void setMapUnits(int mapUnits) {
562
                this.mapUnits = mapUnits;
563 956 vcaballero
        }
564
565
        /**
566 1036 vcaballero
         * Devuelve la anchura de la imagen.
567 956 vcaballero
         *
568 1036 vcaballero
         * @return anchura en pixels de la imagen.
569 956 vcaballero
         */
570
        public int getImageWidth() {
571 214 fernando
                return imageSize.width;
572 213 fernando
        }
573 956 vcaballero
574
        /**
575 1036 vcaballero
         * Devuelve la altura de la imagen.
576 956 vcaballero
         *
577 1036 vcaballero
         * @return altura de la imagen.
578 956 vcaballero
         */
579
        public int getImageHeight() {
580 214 fernando
                return imageSize.height;
581
        }
582 956 vcaballero
583
        /**
584 1036 vcaballero
         * Devuelve la distancia real de un pixel.
585 956 vcaballero
         *
586 1036 vcaballero
         * @return Distancia real de un pixel.
587 956 vcaballero
         */
588 237 fjp
        public double getDist1pixel() {
589
                return dist1pixel;
590
        }
591 956 vcaballero
592
        /**
593 1036 vcaballero
         * Inserta la distancia real de un pixel.
594 956 vcaballero
         *
595 1036 vcaballero
         * @param dist1pixel Distancia real de un pixel.
596 956 vcaballero
         */
597 237 fjp
        public void setDist1pixel(double dist1pixel) {
598
                this.dist1pixel = dist1pixel;
599
        }
600 956 vcaballero
601
        /**
602 1036 vcaballero
         * Devuelve la distancia real de tres pixel.
603 956 vcaballero
         *
604 1036 vcaballero
         * @return Distancia real de tres pixel.
605 956 vcaballero
         */
606 237 fjp
        public double getDist3pixel() {
607
                return dist3pixel;
608
        }
609 956 vcaballero
610
        /**
611 1036 vcaballero
         * Inserta la distancia real de tres pixels.
612 956 vcaballero
         *
613 1036 vcaballero
         * @param dist3pixel Distancia real de tres pixels.
614 956 vcaballero
         */
615 237 fjp
        public void setDist3pixel(double dist3pixel) {
616
                this.dist3pixel = dist3pixel;
617
        }
618 956 vcaballero
619 277 fernando
        /**
620 1036 vcaballero
         * Devuelve los Extents anteriores almacenados.
621 956 vcaballero
         *
622 277 fernando
         * @return Returns the extents.
623
         */
624
        public ExtentHistory getExtents() {
625
                return extents;
626
        }
627
628
        /**
629 1036 vcaballero
         * Devuelve la proyecci?n.
630 956 vcaballero
         *
631 346 fernando
         * @return Returns the proj.
632
         */
633
        public IProjection getProjection() {
634
                return proj;
635
        }
636 956 vcaballero
637 346 fernando
        /**
638 1036 vcaballero
         * Inserta la proyecci?n.
639 956 vcaballero
         *
640 346 fernando
         * @param proj The proj to set.
641
         */
642
        public void setProjection(IProjection proj) {
643
                this.proj = proj;
644
        }
645 3884 caballero
646 1152 fjp
        /**
647
         * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
648
         * un calculateAffineTransform, ni recalcula el adjustedExtent.
649
         * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
650 3884 caballero
         * RasterAdapter, y que recibiera un ViewPortData.
651 1152 fjp
         * @param at
652
         */
653 3884 caballero
        public void setAffineTransform(AffineTransform at)
654 1152 fjp
        {
655
            this.trans = at;
656
        }
657 956 vcaballero
658
        /**
659 1036 vcaballero
         * Devuelve el XMLEntity.
660 956 vcaballero
         *
661 1036 vcaballero
         * @return XMLEntity.
662 956 vcaballero
         */
663
        public XMLEntity getXMLEntity() {
664
                XMLEntity xml = new XMLEntity();
665 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
666 956 vcaballero
667
                if (adjustedExtent != null) {
668
                        xml.putProperty("adjustedExtentX", adjustedExtent.getX());
669
                        xml.putProperty("adjustedExtentY", adjustedExtent.getY());
670
                        xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
671
                        xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
672 741 vcaballero
                }
673 956 vcaballero
674 1282 fjp
                if (backColor != null)
675
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
676 956 vcaballero
677
                if (clip != null) {
678
                        xml.putProperty("clipX", clip.getX());
679
                        xml.putProperty("clipY", clip.getY());
680
                        xml.putProperty("clipW", clip.getWidth());
681
                        xml.putProperty("clipH", clip.getHeight());
682 435 vcaballero
                }
683 956 vcaballero
684
                xml.putProperty("dist1pixel", dist1pixel);
685
                xml.putProperty("dist3pixel", dist3pixel);
686
                xml.putProperty("distanceUnits", distanceUnits);
687
688
                if (extent != null) {
689
                        xml.putProperty("extentX", extent.getX());
690
                        xml.putProperty("extentY", extent.getY());
691
                        xml.putProperty("extentW", extent.getWidth());
692
                        xml.putProperty("extentH", extent.getHeight());
693 741 vcaballero
                }
694 956 vcaballero
695 449 vcaballero
                xml.addChild(extents.getXMLEntity());
696 956 vcaballero
                xml.putProperty("mapUnits", mapUnits);
697
                xml.putProperty("offsetX", offset.getX());
698
                xml.putProperty("offsetY", offset.getY());
699
700
                if (proj != null) {
701
                        xml.putProperty("proj", proj.getAbrev());
702 442 vcaballero
                }
703 956 vcaballero
704
                xml.putProperty("scale", scale);
705
706 435 vcaballero
                return xml;
707
        }
708 956 vcaballero
709
        /**
710 1036 vcaballero
         * Crea un nuevo ViewPort a partir del XMLEntity.
711 956 vcaballero
         *
712 1036 vcaballero
         * @param xml XMLEntity.
713 956 vcaballero
         *
714 1036 vcaballero
         * @return Nuevo ViewPort.
715 956 vcaballero
         */
716 2183 fernando
        public static ViewPort createFromXML03(XMLEntity xml) {
717
                ViewPort vp = new ViewPort(null);
718
719
                if (xml.contains("adjustedExtentX")) {
720
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
721
                                                "adjustedExtentX"),
722
                                        xml.getDoubleProperty("adjustedExtentY"),
723
                                        xml.getDoubleProperty("adjustedExtentW"),
724
                                        xml.getDoubleProperty("adjustedExtentH"));
725
                }
726
727
                if (xml.contains("backColor")) {
728
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
729
                                                "backColor")));
730
                }
731
732
                if (xml.contains("clipX")) {
733
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
734
                                        xml.getDoubleProperty("clipY"),
735
                                        xml.getDoubleProperty("clipW"),
736
                                        xml.getDoubleProperty("clipH"));
737
                }
738
739
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
740
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
741
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
742
                vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
743
744
                if (xml.contains("extentX")) {
745
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
746
                                        xml.getDoubleProperty("extentY"),
747
                                        xml.getDoubleProperty("extentW"),
748
                                        xml.getDoubleProperty("extentH")));
749
750
                        //Calcula la transformaci?n af?n
751
                        vp.calculateAffineTransform();
752
753
                        // Lanzamos los eventos de extent cambiado
754
                        // vp.callExtentListeners(vp.adjustedExtent);
755
                }
756
757
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
758
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
759
                                xml.getDoubleProperty("offsetY")));
760
761
                if (xml.contains("proj")) {
762
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
763
                }
764
765
                //vp.setScale(xml.getDoubleProperty("scale"));
766
                vp.setScale();
767
                return vp;
768
        }
769
770
        /**
771
         * Crea un nuevo ViewPort a partir del XMLEntity.
772
         *
773
         * @param xml XMLEntity.
774
         *
775
         * @return Nuevo ViewPort.
776
         */
777 956 vcaballero
        public static ViewPort createFromXML(XMLEntity xml) {
778
                ViewPort vp = new ViewPort(null);
779
780
                if (xml.contains("adjustedExtentX")) {
781
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
782
                                                "adjustedExtentX"),
783
                                        xml.getDoubleProperty("adjustedExtentY"),
784
                                        xml.getDoubleProperty("adjustedExtentW"),
785
                                        xml.getDoubleProperty("adjustedExtentH"));
786 741 vcaballero
                }
787 956 vcaballero
788
                if (xml.contains("backColor")) {
789
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
790
                                                "backColor")));
791 435 vcaballero
                }
792 956 vcaballero
793
                if (xml.contains("clipX")) {
794
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
795
                                        xml.getDoubleProperty("clipY"),
796
                                        xml.getDoubleProperty("clipW"),
797
                                        xml.getDoubleProperty("clipH"));
798 741 vcaballero
                }
799 956 vcaballero
800 442 vcaballero
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
801
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
802
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
803 956 vcaballero
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
804 918 fernando
805 956 vcaballero
                if (xml.contains("extentX")) {
806
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
807
                                        xml.getDoubleProperty("extentY"),
808
                                        xml.getDoubleProperty("extentW"),
809
                                        xml.getDoubleProperty("extentH")));
810
811
                        //Calcula la transformaci?n af?n
812
                        vp.calculateAffineTransform();
813
814
                        // Lanzamos los eventos de extent cambiado
815 1040 fjp
                        // vp.callExtentListeners(vp.adjustedExtent);
816 741 vcaballero
                }
817 956 vcaballero
818 442 vcaballero
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
819 956 vcaballero
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
820
                                xml.getDoubleProperty("offsetY")));
821
822
                if (xml.contains("proj")) {
823
                        vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
824 442 vcaballero
                }
825 956 vcaballero
826 1135 vcaballero
                //vp.setScale(xml.getDoubleProperty("scale"));
827
                vp.setScale();
828 435 vcaballero
                return vp;
829
        }
830 956 vcaballero
831
        /**
832 1036 vcaballero
         * Clona el ViewPort.
833 956 vcaballero
         *
834 1036 vcaballero
         * @return ViewPort clonado.
835 956 vcaballero
         */
836
        public ViewPort cloneViewPort() {
837
                return createFromXML(getXMLEntity());
838 798 vcaballero
        }
839 956 vcaballero
840
        /**
841 1036 vcaballero
         * Devuelve el String con datos del ViewPort.
842 956 vcaballero
         *
843 1036 vcaballero
         * @return Cadena con datos del ViewPort.
844 956 vcaballero
         */
845
        public String toString() {
846 875 fjp
                String str;
847 956 vcaballero
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
848
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
849 875 fjp
                        "\ntrans=" + trans;
850 956 vcaballero
851 875 fjp
                return str;
852
        }
853 3683 caballero
854
        public void setClipRect(Rectangle2D rectView) {
855
                cliprect=rectView;
856 3884 caballero
857 3683 caballero
        }
858
859 4213 caballero
        public Rectangle2D fromMapRectangle(Rectangle2D r) {
860
                double w=fromMapDistance((int)r.getWidth());
861
                double h=fromMapDistance((int)r.getHeight());
862
                Point2D p1=fromMapPoint((int)r.getX(),(int)r.getY());
863
                return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h);
864
        }
865 3884 caballero
866 4213 caballero
867 213 fernando
}