Statistics
| Revision:

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

History | View | Annotate | Download (21.8 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 213 fernando
import java.awt.geom.AffineTransform;
47 234 fjp
import java.awt.geom.NoninvertibleTransformException;
48 213 fernando
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.util.ArrayList;
51
52 3884 caballero
import org.cresques.cts.GeoCalc;
53
import org.cresques.cts.IProjection;
54
import org.cresques.cts.gt2.CSUTM;
55 346 fernando
56 6164 luisw2
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
57 3884 caballero
import com.iver.utiles.StringUtilities;
58
import com.iver.utiles.XMLEntity;
59
60
61 956 vcaballero
/**
62 1036 vcaballero
 * Clase con atributos de la vista.
63 1443 jmorell
 * 050211, jmorell: A?ado los Grados como unidad de mapa.
64 956 vcaballero
 *
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 1443 jmorell
        public static int GRADOS = 8;
77 435 vcaballero
78 346 fernando
        /**
79 956 vcaballero
         * Resoluci?n (Puntos por pulgada) de la vista actual. Se necesita para los
80
         * c?lculos de escala geogr?fica.
81 346 fernando
         */
82 956 vcaballero
        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 213 fernando
        private int distanceUnits = METROS;
90
        private int mapUnits = METROS;
91 1197 fernando
        private ArrayList listeners = new ArrayList();
92 956 vcaballero
        private Point2D offset = new Point2D.Double(0, 0);
93
        private Rectangle2D clip;
94 1282 fjp
        private Color backColor = null; //Color.WHITE;
95 346 fernando
        private IProjection proj;
96 956 vcaballero
        private double dist1pixel;
97
        private double dist3pixel;
98 213 fernando
        private double scale;
99 3683 caballero
        private Rectangle2D cliprect;
100 956 vcaballero
101
        /**
102
         * Crea un nuevo ViewPort.
103
         *
104 1036 vcaballero
         * @param proj Proyecci?n.
105 956 vcaballero
         */
106
        public ViewPort(IProjection proj) {
107 867 fjp
                // Por defecto
108 956 vcaballero
                this.proj = proj;
109 867 fjp
        }
110 213 fernando
111 956 vcaballero
        /**
112 1036 vcaballero
         * A?ade un ViewPortListener al extentListener.
113 956 vcaballero
         *
114 1036 vcaballero
         * @param arg0 ViewPortListener.
115 956 vcaballero
         *
116 1036 vcaballero
         * @return True si ha sido a?adida correctamente.
117 956 vcaballero
         */
118 1197 fernando
        public boolean addViewPortListener(ViewPortListener arg0) {
119
                return listeners.add(arg0);
120 213 fernando
        }
121
122 956 vcaballero
        /**
123 1036 vcaballero
         * Borra el ViewPortListener que se pasa como par?metro delos
124
         * extentListener.
125 956 vcaballero
         *
126 1036 vcaballero
         * @param arg0 ViewPortListener.
127 956 vcaballero
         *
128 1036 vcaballero
         * @return True si ha sido borrado correctamente.
129 956 vcaballero
         */
130 1197 fernando
        public boolean removeViewPortListener(ViewPortListener arg0) {
131
                return listeners.remove(arg0);
132 213 fernando
        }
133
134 956 vcaballero
        /**
135 1036 vcaballero
         * Devuelve la distancia en pixels a partir de una distancia real.
136 956 vcaballero
         *
137 1036 vcaballero
         * @param d Distancia real.
138 956 vcaballero
         *
139 1036 vcaballero
         * @return Distancia en pixels.
140 956 vcaballero
         */
141
        public int fromMapDistance(double d) {
142
                Point2D.Double pWorld = new Point2D.Double(1, 1);
143
                Point2D.Double pScreen = new Point2D.Double();
144 234 fjp
145 956 vcaballero
                try {
146
                        trans.deltaTransform(pWorld, pScreen);
147
                } catch (Exception e) {
148
                        System.err.print(e.getMessage());
149
                }
150 213 fernando
151 956 vcaballero
                return (int) (d * pScreen.x);
152
        }
153 234 fjp
154 956 vcaballero
        /**
155 1036 vcaballero
         * Devuelve un punto en pixels a partir de una coordenada X e Y real.
156 956 vcaballero
         *
157 1036 vcaballero
         * @param x Coordenada X real.
158
         * @param y Coordenada Y real.
159 956 vcaballero
         *
160 1036 vcaballero
         * @return Punto en pixels.
161 956 vcaballero
         */
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 234 fjp
166 956 vcaballero
                try {
167
                        trans.transform(pWorld, pScreen);
168
                } catch (Exception e) {
169
                        System.err.print(e.getMessage());
170
                }
171 213 fernando
172 956 vcaballero
                return pScreen;
173
        }
174 1036 vcaballero
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 1004 vcaballero
        public Point2D fromMapPoint(Point2D point) {
183 1036 vcaballero
                return fromMapPoint(point.getX(), point.getY());
184 1004 vcaballero
        }
185 213 fernando
186 956 vcaballero
        /**
187 1036 vcaballero
         * Devuelve un punto real a partir de una coordenada X e Y en pixels.
188 956 vcaballero
         *
189 1036 vcaballero
         * @param x Coordenada X en pixels.
190
         * @param y Coordenada Y en pixels.
191 956 vcaballero
         *
192 1036 vcaballero
         * @return Punto real.
193 956 vcaballero
         */
194
        public Point2D toMapPoint(int x, int y) {
195
                Point pScreen = new Point(x, y);
196 213 fernando
197 956 vcaballero
                return toMapPoint(pScreen);
198
        }
199 234 fjp
200 4213 caballero
        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 956 vcaballero
        /**
207 1036 vcaballero
         * Devuelve la distancia real a partir de la distancia en pixels.
208 956 vcaballero
         *
209 1036 vcaballero
         * @param d Distancia en pixels.
210 956 vcaballero
         *
211 1036 vcaballero
         * @return Distancia real.
212 956 vcaballero
         */
213
        public double toMapDistance(int d) {
214
                double dist = d / trans.getScaleX();
215 234 fjp
216 956 vcaballero
                return dist;
217
        }
218 213 fernando
219 956 vcaballero
        /**
220 1036 vcaballero
         * Devuelve un punto real a partir de un punto en pixels.
221 956 vcaballero
         *
222 1036 vcaballero
         * @param pScreen Punto en pixels.
223 956 vcaballero
         *
224 1036 vcaballero
         * @return Punto real.
225 956 vcaballero
         *
226 1036 vcaballero
         * @throws RuntimeException
227 956 vcaballero
         */
228
        public Point2D toMapPoint(Point2D pScreen) {
229
                Point2D.Double pWorld = new Point2D.Double();
230
                AffineTransform at;
231 213 fernando
232 956 vcaballero
                try {
233
                        at = trans.createInverse();
234
                        at.transform(pScreen, pWorld);
235
                } catch (NoninvertibleTransformException e) {
236
                        throw new RuntimeException(e);
237
                }
238 213 fernando
239 956 vcaballero
                return pWorld;
240
        }
241 1036 vcaballero
242 999 luisw
        /**
243 1036 vcaballero
         * 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 999 luisw
         * @param pt1
249
         * @param pt2
250 1036 vcaballero
         *
251
         * @return distancia real.
252 999 luisw
         */
253
        public double distanceWorld(Point2D pt1, Point2D pt2) {
254
                double dist = -1;
255
                dist = pt1.distance(pt2);
256 1036 vcaballero
257
                if ((proj != null) && !(proj instanceof CSUTM)) {
258
                        dist = new GeoCalc(proj).distanceVincenty(proj.toGeo(pt1),
259
                                        proj.toGeo(pt2));
260
                }
261
262 999 luisw
                return dist;
263
        }
264 213 fernando
265 956 vcaballero
        /**
266 1036 vcaballero
         * Rellena el extent anterior como actual.
267 956 vcaballero
         */
268
        public void setPreviousExtent() {
269
                extent = extents.removePrev();
270 918 fernando
271 956 vcaballero
                //Calcula la transformaci?n af?n
272
                calculateAffineTransform();
273 680 fernando
274 956 vcaballero
                // Lanzamos los eventos de extent cambiado
275 3683 caballero
                callExtentChanged(getAdjustedExtent());
276 956 vcaballero
        }
277 213 fernando
278 956 vcaballero
        /**
279 4910 jaume
         * <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 1036 vcaballero
         * @return Extent.
293 956 vcaballero
         */
294
        public Rectangle2D getExtent() {
295
                return extent;
296
        }
297 213 fernando
298
        /**
299 1036 vcaballero
         * Inserta el extent.
300 956 vcaballero
         *
301 1036 vcaballero
         * @param r Extent.
302 956 vcaballero
         */
303
        public void setExtent(Rectangle2D r) {
304
                if (extent != null) {
305
                        extents.put(extent);
306
                }
307
308 3884 caballero
                //Esto comprueba que el extent no es de anchura o altura = "0"
309 956 vcaballero
                //y si es as? lo redimensiona.
310 2409 caballero
                if (r!=null &&((r.getWidth() == 0) || (r.getHeight() == 0))) {
311 956 vcaballero
                        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 3683 caballero
                callExtentChanged(getAdjustedExtent());
322 956 vcaballero
        }
323
324
        /**
325 1036 vcaballero
         * Inserta la escala.
326 2970 fjp
     * TODO: (FJP) Que alguien me explique qu? sentido tiene esto
327 956 vcaballero
         *
328 1036 vcaballero
         * @param scale escala.
329 956 vcaballero
         */
330 1135 vcaballero
        public void setScale() {
331
                //this.scale = scale;
332 956 vcaballero
333
                //Calcula la transformaci?n af?n
334
                calculateAffineTransform();
335
336
                // Lanzamos los eventos de extent cambiado
337 3683 caballero
                callExtentChanged(getAdjustedExtent());
338 956 vcaballero
        }
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 1036 vcaballero
         * @return Escala.
345 213 fernando
         */
346 956 vcaballero
        public double getScale() {
347
                return proj.getScale(extent.getMinX(), extent.getMaxX(),
348
                        imageSize.getWidth(), dpi);
349
        }
350
351
        /**
352 1036 vcaballero
         * Devuelve la matriz de transformaci?n.
353 956 vcaballero
         *
354 1036 vcaballero
         * @return Matriz de transformaci?n.
355 956 vcaballero
         */
356 213 fernando
        public AffineTransform getAffineTransform() {
357
                return trans;
358
        }
359 956 vcaballero
360 213 fernando
        /**
361 1036 vcaballero
         * Devuelve las dimensiones de la imagen.
362 956 vcaballero
         *
363 213 fernando
         * @return Returns the imageSize.
364
         */
365
        public Dimension getImageSize() {
366
                return imageSize;
367
        }
368 956 vcaballero
369 213 fernando
        /**
370 1036 vcaballero
         * Inserta las dimensiones de la imagen.
371 956 vcaballero
         *
372 213 fernando
         * @param imageSize The imageSize to set.
373
         */
374
        public void setImageSize(Dimension imageSize) {
375
                this.imageSize = imageSize;
376
                calculateAffineTransform();
377
        }
378
379 956 vcaballero
        /**
380 1036 vcaballero
         * Llamada a los listeners tras el cambio de extent.
381 956 vcaballero
         *
382 1036 vcaballero
         * @param newRect Extent.
383 956 vcaballero
         */
384 1197 fernando
        private void callExtentChanged(Rectangle2D newRect) {
385 1082 fernando
                ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
386 655 fjp
387 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
388
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
389 956 vcaballero
                        listener.extentChanged(ev);
390
                }
391
        }
392
393 213 fernando
        /**
394 1036 vcaballero
         * Llamada a los listeners tras el cambio de color.
395 956 vcaballero
         *
396 1036 vcaballero
         * @param c Color.
397 213 fernando
         */
398 1197 fernando
        private void callColorChanged(Color c) {
399 1082 fernando
                ColorEvent ce = ColorEvent.createColorEvent(c);
400 956 vcaballero
401 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
402
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
403 956 vcaballero
                        listener.backColorChanged(ce);
404
                }
405
        }
406 5943 jmvivo
        /**
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 956 vcaballero
414 5943 jmvivo
                for (int i = 0; i < listeners.size(); i++) {
415
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
416
                        listener.projectionChanged(ev);
417
                }
418
        }
419
420
421 956 vcaballero
        /**
422 1036 vcaballero
         * C?lculo de la matriz de transformaci?n.
423 956 vcaballero
         *
424 3884 caballero
         * @throws RuntimeException
425 956 vcaballero
         */
426 213 fernando
        private void calculateAffineTransform() {
427 956 vcaballero
                if ((imageSize == null) || (extent == null) ||
428
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
429
                        return;
430
                }
431 213 fernando
432 956 vcaballero
                AffineTransform escalado = new AffineTransform();
433
                AffineTransform translacion = new AffineTransform();
434 213 fernando
435 956 vcaballero
                double escalaX;
436
                double escalaY;
437 213 fernando
438 956 vcaballero
                escalaX = imageSize.getWidth() / extent.getWidth();
439
                escalaY = imageSize.getHeight() / extent.getHeight();
440 213 fernando
441 956 vcaballero
                double xCenter = extent.getCenterX();
442
                double yCenter = extent.getCenterY();
443
                double newHeight;
444
                double newWidth;
445 213 fernando
446 956 vcaballero
                adjustedExtent = new Rectangle2D.Double();
447 213 fernando
448 956 vcaballero
                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 213 fernando
461 3683 caballero
                translacion.setToTranslation(-getAdjustedExtent().getX(),
462
                        -getAdjustedExtent().getY() - getAdjustedExtent().getHeight());
463 956 vcaballero
                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 3884 caballero
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
475 956 vcaballero
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
476
                AffineTransform at;
477
478 237 fjp
                try {
479
                        at = trans.createInverse();
480 956 vcaballero
481 237 fjp
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
482 956 vcaballero
                        Point2D.Float pProv = new Point2D.Float();
483
                        at.deltaTransform(pPixel, pProv);
484 237 fjp
485 956 vcaballero
                        dist1pixel = pProv.x;
486
                        dist3pixel = 3 * pProv.x;
487 237 fjp
                } catch (NoninvertibleTransformException e) {
488 867 fjp
                        System.err.println("transformada afin = " + trans.toString());
489 956 vcaballero
                        System.err.println("extent = " + extent.toString() +
490
                                " imageSize= " + imageSize.toString());
491 918 fernando
                        throw new RuntimeException(e);
492 237 fjp
                }
493 213 fernando
        }
494 956 vcaballero
495
        /**
496 1036 vcaballero
         * Inserta la desviaci?n.
497 956 vcaballero
         *
498 1036 vcaballero
         * @param p Punto.
499 956 vcaballero
         */
500
        public void setOffset(Point2D p) {
501
                offset = p;
502 351 vcaballero
        }
503 956 vcaballero
        /**
504 5369 jaume
         * 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 1317 vcaballero
         *
508 5369 jaume
         * @param p Point, in pixels, where the map starts.
509 1317 vcaballero
         */
510
        public Point2D getOffset() {
511
                return offset;
512
        }
513
        /**
514 1036 vcaballero
         * Inserta el color de fondo.
515 956 vcaballero
         *
516 1036 vcaballero
         * @param c Color de fondo.
517 956 vcaballero
         */
518 213 fernando
        public void setBackColor(Color c) {
519
                backColor = c;
520 1197 fernando
                callColorChanged(backColor);
521 213 fernando
        }
522
523 956 vcaballero
        /**
524 1036 vcaballero
         * Devuelve el color de fondo.
525 956 vcaballero
         *
526 1036 vcaballero
         * @return Color de fondo.
527 956 vcaballero
         */
528 213 fernando
        public Color getBackColor() {
529
                return backColor;
530
        }
531 956 vcaballero
532 213 fernando
        /**
533 4910 jaume
         * Returns the extent currently covered by the view.
534 956 vcaballero
         *
535 213 fernando
         * @return Returns the adjustedExtent.
536
         */
537
        public Rectangle2D getAdjustedExtent() {
538 3683 caballero
                if (cliprect!=null){
539
                        return adjustedExtent.createIntersection(cliprect);
540
                }
541 213 fernando
                return adjustedExtent;
542
        }
543 956 vcaballero
544 213 fernando
        /**
545 1036 vcaballero
         * Devuelve la unidad de medida.
546 956 vcaballero
         *
547 213 fernando
         * @return Returns the distanceUnits.
548
         */
549
        public int getDistanceUnits() {
550
                return distanceUnits;
551
        }
552 956 vcaballero
553 213 fernando
        /**
554 1036 vcaballero
         * Inserta la unidad de medida.
555 956 vcaballero
         *
556 213 fernando
         * @param distanceUnits The distanceUnits to set.
557
         */
558
        public void setDistanceUnits(int distanceUnits) {
559
                this.distanceUnits = distanceUnits;
560
        }
561 956 vcaballero
562 213 fernando
        /**
563 1036 vcaballero
         * Devuelve la unidad de medida del mapa.
564 956 vcaballero
         *
565 213 fernando
         * @return Returns the mapUnits.
566
         */
567
        public int getMapUnits() {
568
                return mapUnits;
569
        }
570 956 vcaballero
571 213 fernando
        /**
572 1036 vcaballero
         * Inserta la unidad de medida del mapa.
573 956 vcaballero
         *
574 213 fernando
         * @param mapUnits The mapUnits to set.
575
         */
576
        public void setMapUnits(int mapUnits) {
577
                this.mapUnits = mapUnits;
578 956 vcaballero
        }
579
580
        /**
581 1036 vcaballero
         * Devuelve la anchura de la imagen.
582 956 vcaballero
         *
583 1036 vcaballero
         * @return anchura en pixels de la imagen.
584 956 vcaballero
         */
585
        public int getImageWidth() {
586 214 fernando
                return imageSize.width;
587 213 fernando
        }
588 956 vcaballero
589
        /**
590 1036 vcaballero
         * Devuelve la altura de la imagen.
591 956 vcaballero
         *
592 1036 vcaballero
         * @return altura de la imagen.
593 956 vcaballero
         */
594
        public int getImageHeight() {
595 214 fernando
                return imageSize.height;
596
        }
597 956 vcaballero
598
        /**
599 1036 vcaballero
         * Devuelve la distancia real de un pixel.
600 956 vcaballero
         *
601 1036 vcaballero
         * @return Distancia real de un pixel.
602 956 vcaballero
         */
603 237 fjp
        public double getDist1pixel() {
604
                return dist1pixel;
605
        }
606 956 vcaballero
607
        /**
608 1036 vcaballero
         * Inserta la distancia real de un pixel.
609 956 vcaballero
         *
610 1036 vcaballero
         * @param dist1pixel Distancia real de un pixel.
611 956 vcaballero
         */
612 237 fjp
        public void setDist1pixel(double dist1pixel) {
613
                this.dist1pixel = dist1pixel;
614
        }
615 956 vcaballero
616
        /**
617 1036 vcaballero
         * Devuelve la distancia real de tres pixel.
618 956 vcaballero
         *
619 1036 vcaballero
         * @return Distancia real de tres pixel.
620 956 vcaballero
         */
621 237 fjp
        public double getDist3pixel() {
622
                return dist3pixel;
623
        }
624 956 vcaballero
625
        /**
626 1036 vcaballero
         * Inserta la distancia real de tres pixels.
627 956 vcaballero
         *
628 1036 vcaballero
         * @param dist3pixel Distancia real de tres pixels.
629 956 vcaballero
         */
630 237 fjp
        public void setDist3pixel(double dist3pixel) {
631
                this.dist3pixel = dist3pixel;
632
        }
633 956 vcaballero
634 277 fernando
        /**
635 1036 vcaballero
         * Devuelve los Extents anteriores almacenados.
636 956 vcaballero
         *
637 277 fernando
         * @return Returns the extents.
638
         */
639
        public ExtentHistory getExtents() {
640
                return extents;
641
        }
642
643
        /**
644 1036 vcaballero
         * Devuelve la proyecci?n.
645 956 vcaballero
         *
646 346 fernando
         * @return Returns the proj.
647
         */
648
        public IProjection getProjection() {
649
                return proj;
650
        }
651 956 vcaballero
652 346 fernando
        /**
653 1036 vcaballero
         * Inserta la proyecci?n.
654 956 vcaballero
         *
655 346 fernando
         * @param proj The proj to set.
656
         */
657
        public void setProjection(IProjection proj) {
658 6187 jmvivo
                if(this.proj == null || !this.proj.getAbrev().equals(proj.getAbrev())) {
659
                        this.proj = proj;
660
                        callProjectionChanged(proj);
661
                }
662 346 fernando
        }
663 3884 caballero
664 1152 fjp
        /**
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 3884 caballero
         * RasterAdapter, y que recibiera un ViewPortData.
669 1152 fjp
         * @param at
670
         */
671 3884 caballero
        public void setAffineTransform(AffineTransform at)
672 1152 fjp
        {
673
            this.trans = at;
674
        }
675 956 vcaballero
676
        /**
677 1036 vcaballero
         * Devuelve el XMLEntity.
678 956 vcaballero
         *
679 1036 vcaballero
         * @return XMLEntity.
680 956 vcaballero
         */
681
        public XMLEntity getXMLEntity() {
682
                XMLEntity xml = new XMLEntity();
683 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
684 956 vcaballero
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 741 vcaballero
                }
691 956 vcaballero
692 1282 fjp
                if (backColor != null)
693
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
694 956 vcaballero
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 435 vcaballero
                }
701 956 vcaballero
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 741 vcaballero
                }
712 956 vcaballero
713 449 vcaballero
                xml.addChild(extents.getXMLEntity());
714 956 vcaballero
                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 442 vcaballero
                }
721 956 vcaballero
722
                xml.putProperty("scale", scale);
723
724 435 vcaballero
                return xml;
725
        }
726 956 vcaballero
727
        /**
728 1036 vcaballero
         * Crea un nuevo ViewPort a partir del XMLEntity.
729 956 vcaballero
         *
730 1036 vcaballero
         * @param xml XMLEntity.
731 956 vcaballero
         *
732 1036 vcaballero
         * @return Nuevo ViewPort.
733 956 vcaballero
         */
734 2183 fernando
        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 6164 luisw2
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
781 2183 fernando
                }
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 956 vcaballero
        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 741 vcaballero
                }
805 956 vcaballero
806
                if (xml.contains("backColor")) {
807
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
808
                                                "backColor")));
809 435 vcaballero
                }
810 956 vcaballero
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 741 vcaballero
                }
817 956 vcaballero
818 442 vcaballero
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
819
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
820
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
821 956 vcaballero
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
822 918 fernando
823 956 vcaballero
                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 1040 fjp
                        // vp.callExtentListeners(vp.adjustedExtent);
834 741 vcaballero
                }
835 956 vcaballero
836 442 vcaballero
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
837 956 vcaballero
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
838
                                xml.getDoubleProperty("offsetY")));
839
840
                if (xml.contains("proj")) {
841 6164 luisw2
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
842 442 vcaballero
                }
843 956 vcaballero
844 1135 vcaballero
                //vp.setScale(xml.getDoubleProperty("scale"));
845
                vp.setScale();
846 435 vcaballero
                return vp;
847
        }
848 956 vcaballero
849
        /**
850 1036 vcaballero
         * Clona el ViewPort.
851 956 vcaballero
         *
852 1036 vcaballero
         * @return ViewPort clonado.
853 956 vcaballero
         */
854
        public ViewPort cloneViewPort() {
855
                return createFromXML(getXMLEntity());
856 798 vcaballero
        }
857 956 vcaballero
858
        /**
859 1036 vcaballero
         * Devuelve el String con datos del ViewPort.
860 956 vcaballero
         *
861 1036 vcaballero
         * @return Cadena con datos del ViewPort.
862 956 vcaballero
         */
863
        public String toString() {
864 875 fjp
                String str;
865 956 vcaballero
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
866
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
867 875 fjp
                        "\ntrans=" + trans;
868 956 vcaballero
869 875 fjp
                return str;
870
        }
871 3683 caballero
872
        public void setClipRect(Rectangle2D rectView) {
873
                cliprect=rectView;
874 3884 caballero
875 3683 caballero
        }
876
877 4213 caballero
        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 3884 caballero
884 4213 caballero
885 213 fernando
}