Statistics
| Revision:

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

History | View | Annotate | Download (23.2 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 8053 sbayarri
        private boolean adjustableExtent=true;
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 8053 sbayarri
         * Sets the adjustable option, so the extent is automatically adjusted to view aspect.
114
         *
115
         * @param boolean Adjustable.
116
         */
117
        public void setAdjustable(boolean adjustable) {
118
                adjustableExtent = adjustable;
119
        }
120
121
        /**
122 1036 vcaballero
         * A?ade un ViewPortListener al extentListener.
123 956 vcaballero
         *
124 1036 vcaballero
         * @param arg0 ViewPortListener.
125 956 vcaballero
         *
126 1036 vcaballero
         * @return True si ha sido a?adida correctamente.
127 956 vcaballero
         */
128 1197 fernando
        public boolean addViewPortListener(ViewPortListener arg0) {
129 7675 caballero
                if (!listeners.contains(arg0))
130
                        return listeners.add(arg0);
131
                return false;
132 213 fernando
        }
133
134 7675 caballero
135 956 vcaballero
        /**
136 1036 vcaballero
         * Borra el ViewPortListener que se pasa como par?metro delos
137
         * extentListener.
138 956 vcaballero
         *
139 1036 vcaballero
         * @param arg0 ViewPortListener.
140 956 vcaballero
         *
141 1036 vcaballero
         * @return True si ha sido borrado correctamente.
142 956 vcaballero
         */
143 1197 fernando
        public boolean removeViewPortListener(ViewPortListener arg0) {
144
                return listeners.remove(arg0);
145 213 fernando
        }
146
147 956 vcaballero
        /**
148 1036 vcaballero
         * Devuelve la distancia en pixels a partir de una distancia real.
149 956 vcaballero
         *
150 1036 vcaballero
         * @param d Distancia real.
151 956 vcaballero
         *
152 1036 vcaballero
         * @return Distancia en pixels.
153 956 vcaballero
         */
154
        public int fromMapDistance(double d) {
155
                Point2D.Double pWorld = new Point2D.Double(1, 1);
156
                Point2D.Double pScreen = new Point2D.Double();
157 234 fjp
158 956 vcaballero
                try {
159
                        trans.deltaTransform(pWorld, pScreen);
160
                } catch (Exception e) {
161
                        System.err.print(e.getMessage());
162
                }
163 213 fernando
164 956 vcaballero
                return (int) (d * pScreen.x);
165
        }
166 234 fjp
167 956 vcaballero
        /**
168 1036 vcaballero
         * Devuelve un punto en pixels a partir de una coordenada X e Y real.
169 956 vcaballero
         *
170 1036 vcaballero
         * @param x Coordenada X real.
171
         * @param y Coordenada Y real.
172 956 vcaballero
         *
173 1036 vcaballero
         * @return Punto en pixels.
174 956 vcaballero
         */
175
        public Point2D fromMapPoint(double x, double y) {
176
                Point2D.Double pWorld = new Point2D.Double(x, y);
177
                Point2D.Double pScreen = new Point2D.Double();
178 234 fjp
179 956 vcaballero
                try {
180
                        trans.transform(pWorld, pScreen);
181
                } catch (Exception e) {
182
                        System.err.print(e.getMessage());
183
                }
184 213 fernando
185 956 vcaballero
                return pScreen;
186
        }
187 1036 vcaballero
188
        /**
189
         * Devuelve el punto en pixels a partir de un punto real.
190
         *
191
         * @param point Punto real.
192
         *
193
         * @return Punto en pixels.
194
         */
195 1004 vcaballero
        public Point2D fromMapPoint(Point2D point) {
196 1036 vcaballero
                return fromMapPoint(point.getX(), point.getY());
197 1004 vcaballero
        }
198 213 fernando
199 956 vcaballero
        /**
200 1036 vcaballero
         * Devuelve un punto real a partir de una coordenada X e Y en pixels.
201 956 vcaballero
         *
202 1036 vcaballero
         * @param x Coordenada X en pixels.
203
         * @param y Coordenada Y en pixels.
204 956 vcaballero
         *
205 1036 vcaballero
         * @return Punto real.
206 956 vcaballero
         */
207
        public Point2D toMapPoint(int x, int y) {
208
                Point pScreen = new Point(x, y);
209 213 fernando
210 956 vcaballero
                return toMapPoint(pScreen);
211
        }
212 234 fjp
213 4213 caballero
        public Rectangle2D toMapRectangle(Rectangle2D r){
214 9515 caballero
                Rectangle2D rect=new Rectangle2D.Double();
215 4213 caballero
                Point2D p1=toMapPoint((int)r.getX(),(int)r.getY());
216 9515 caballero
                Point2D p2=toMapPoint((int)r.getMaxX(),(int)r.getMaxY());
217
                rect.setFrameFromDiagonal(p1,p2);
218
                return rect;
219 4213 caballero
        }
220 956 vcaballero
        /**
221 1036 vcaballero
         * Devuelve la distancia real a partir de la distancia en pixels.
222 956 vcaballero
         *
223 1036 vcaballero
         * @param d Distancia en pixels.
224 956 vcaballero
         *
225 1036 vcaballero
         * @return Distancia real.
226 956 vcaballero
         */
227
        public double toMapDistance(int d) {
228
                double dist = d / trans.getScaleX();
229 234 fjp
230 956 vcaballero
                return dist;
231
        }
232 213 fernando
233 956 vcaballero
        /**
234 1036 vcaballero
         * Devuelve un punto real a partir de un punto en pixels.
235 956 vcaballero
         *
236 1036 vcaballero
         * @param pScreen Punto en pixels.
237 956 vcaballero
         *
238 1036 vcaballero
         * @return Punto real.
239 956 vcaballero
         *
240 1036 vcaballero
         * @throws RuntimeException
241 956 vcaballero
         */
242
        public Point2D toMapPoint(Point2D pScreen) {
243
                Point2D.Double pWorld = new Point2D.Double();
244
                AffineTransform at;
245 213 fernando
246 956 vcaballero
                try {
247
                        at = trans.createInverse();
248
                        at.transform(pScreen, pWorld);
249
                } catch (NoninvertibleTransformException e) {
250
                        throw new RuntimeException(e);
251
                }
252 213 fernando
253 956 vcaballero
                return pWorld;
254
        }
255 1036 vcaballero
256 999 luisw
        /**
257 1036 vcaballero
         * Calcula la distancia entre dos puntos en unidades de usuario. Los puntos
258
         * est?n en unidades de usuario. Se tiene en cuenta la proyecci?n, con lo
259
         * que es INDISPENSABLE que la variable proj contenga el valor correcto de
260
         * la proyecci?n.
261
         *
262 999 luisw
         * @param pt1
263
         * @param pt2
264 1036 vcaballero
         *
265
         * @return distancia real.
266 999 luisw
         */
267
        public double distanceWorld(Point2D pt1, Point2D pt2) {
268
                double dist = -1;
269
                dist = pt1.distance(pt2);
270 1036 vcaballero
271
                if ((proj != null) && !(proj instanceof CSUTM)) {
272
                        dist = new GeoCalc(proj).distanceVincenty(proj.toGeo(pt1),
273
                                        proj.toGeo(pt2));
274 9819 caballero
                        return dist;
275 1036 vcaballero
                }
276 9819 caballero
                return (dist*MapContext.CHANGEM[getMapUnits()]);
277 999 luisw
        }
278 213 fernando
279 956 vcaballero
        /**
280 1036 vcaballero
         * Rellena el extent anterior como actual.
281 956 vcaballero
         */
282
        public void setPreviousExtent() {
283
                extent = extents.removePrev();
284 918 fernando
285 956 vcaballero
                //Calcula la transformaci?n af?n
286
                calculateAffineTransform();
287 680 fernando
288 956 vcaballero
                // Lanzamos los eventos de extent cambiado
289 3683 caballero
                callExtentChanged(getAdjustedExtent());
290 956 vcaballero
        }
291 213 fernando
292 956 vcaballero
        /**
293 4910 jaume
         * <p>
294
         * When the zoom changes (for instance when using the zoom in or zoom out tools,
295
         * but also zooming to a selected feature or shape) the extent that covers that
296
         * area is the value returned by this method. It is not the actual area shown
297
         * in the view because it does not care about the aspect ratio. However, any
298
         * part of the real world contained in this extent is shown in the view.<br>
299
         * </p>
300
         * <p>
301
         * Probably <b>this is not what you are looking for</b>. If you are looking for
302
         * the complete extent currently shown, you must use getAdjustedExtent() method
303
         * which returns the extent that contains this one but regarding the current
304
         * view's aspect ratio.
305
         * </p>
306 1036 vcaballero
         * @return Extent.
307 956 vcaballero
         */
308
        public Rectangle2D getExtent() {
309
                return extent;
310
        }
311 213 fernando
312
        /**
313 1036 vcaballero
         * Inserta el extent.
314 956 vcaballero
         *
315 1036 vcaballero
         * @param r Extent.
316 956 vcaballero
         */
317
        public void setExtent(Rectangle2D r) {
318
                if (extent != null) {
319
                        extents.put(extent);
320
                }
321
322 3884 caballero
                //Esto comprueba que el extent no es de anchura o altura = "0"
323 956 vcaballero
                //y si es as? lo redimensiona.
324 2409 caballero
                if (r!=null &&((r.getWidth() == 0) || (r.getHeight() == 0))) {
325 956 vcaballero
                        extent = new Rectangle2D.Double(r.getMinX() - 0.1,
326
                                        r.getMinY() - 0.1, r.getWidth() + 0.2, r.getHeight() + 0.2);
327
                } else {
328
                        extent = r;
329
                }
330
331
                //Calcula la transformaci?n af?n
332
                calculateAffineTransform();
333
334
                // Lanzamos los eventos de extent cambiado
335 3683 caballero
                callExtentChanged(getAdjustedExtent());
336 956 vcaballero
        }
337
338
        /**
339 6630 caballero
         * Refresca el extent.
340
     *
341 956 vcaballero
         */
342 6630 caballero
        public void refreshExtent() {
343 1135 vcaballero
                //this.scale = scale;
344 956 vcaballero
345
                //Calcula la transformaci?n af?n
346
                calculateAffineTransform();
347
348
                // Lanzamos los eventos de extent cambiado
349 3683 caballero
                callExtentChanged(getAdjustedExtent());
350 956 vcaballero
        }
351
352
        /**
353
         * Devuelve la escala. Debe estar siempre actualizada y no calcularse nunca
354
         * aqu? pues se utiliza en el dibujado para cada geometr?a
355
         *
356 1036 vcaballero
         * @return Escala.
357 213 fernando
         */
358 956 vcaballero
        public double getScale() {
359
                return proj.getScale(extent.getMinX(), extent.getMaxX(),
360
                        imageSize.getWidth(), dpi);
361
        }
362
363
        /**
364 1036 vcaballero
         * Devuelve la matriz de transformaci?n.
365 956 vcaballero
         *
366 1036 vcaballero
         * @return Matriz de transformaci?n.
367 956 vcaballero
         */
368 213 fernando
        public AffineTransform getAffineTransform() {
369
                return trans;
370
        }
371 956 vcaballero
372 213 fernando
        /**
373 1036 vcaballero
         * Devuelve las dimensiones de la imagen.
374 956 vcaballero
         *
375 213 fernando
         * @return Returns the imageSize.
376
         */
377
        public Dimension getImageSize() {
378
                return imageSize;
379
        }
380 956 vcaballero
381 213 fernando
        /**
382 1036 vcaballero
         * Inserta las dimensiones de la imagen.
383 956 vcaballero
         *
384 213 fernando
         * @param imageSize The imageSize to set.
385
         */
386
        public void setImageSize(Dimension imageSize) {
387
                this.imageSize = imageSize;
388
                calculateAffineTransform();
389
        }
390
391 956 vcaballero
        /**
392 1036 vcaballero
         * Llamada a los listeners tras el cambio de extent.
393 956 vcaballero
         *
394 1036 vcaballero
         * @param newRect Extent.
395 956 vcaballero
         */
396 1197 fernando
        private void callExtentChanged(Rectangle2D newRect) {
397 1082 fernando
                ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
398 655 fjp
399 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
400
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
401 956 vcaballero
                        listener.extentChanged(ev);
402
                }
403
        }
404
405 213 fernando
        /**
406 1036 vcaballero
         * Llamada a los listeners tras el cambio de color.
407 956 vcaballero
         *
408 1036 vcaballero
         * @param c Color.
409 213 fernando
         */
410 1197 fernando
        private void callColorChanged(Color c) {
411 1082 fernando
                ColorEvent ce = ColorEvent.createColorEvent(c);
412 956 vcaballero
413 1197 fernando
                for (int i = 0; i < listeners.size(); i++) {
414
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
415 956 vcaballero
                        listener.backColorChanged(ce);
416
                }
417
        }
418 5943 jmvivo
        /**
419
         * Llamada a los listeners tras el cambio de extent.
420
         *
421
         * @param newRect Extent.
422
         */
423
        private void callProjectionChanged(IProjection projection) {
424
                ProjectionEvent ev = ProjectionEvent.createProjectionEvent(projection);
425 956 vcaballero
426 5943 jmvivo
                for (int i = 0; i < listeners.size(); i++) {
427
                        ViewPortListener listener = (ViewPortListener) listeners.get(i);
428
                        listener.projectionChanged(ev);
429
                }
430
        }
431
432 6630 caballero
433 956 vcaballero
        /**
434 1036 vcaballero
         * C?lculo de la matriz de transformaci?n.
435 956 vcaballero
         *
436 3884 caballero
         * @throws RuntimeException
437 956 vcaballero
         */
438 213 fernando
        private void calculateAffineTransform() {
439 956 vcaballero
                if ((imageSize == null) || (extent == null) ||
440
                                (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) {
441
                        return;
442
                }
443 213 fernando
444 956 vcaballero
                AffineTransform escalado = new AffineTransform();
445
                AffineTransform translacion = new AffineTransform();
446 213 fernando
447 956 vcaballero
                double escalaX;
448
                double escalaY;
449 213 fernando
450 956 vcaballero
                escalaX = imageSize.getWidth() / extent.getWidth();
451
                escalaY = imageSize.getHeight() / extent.getHeight();
452 213 fernando
453 956 vcaballero
                double xCenter = extent.getCenterX();
454
                double yCenter = extent.getCenterY();
455
                double newHeight;
456
                double newWidth;
457 213 fernando
458 956 vcaballero
                adjustedExtent = new Rectangle2D.Double();
459 213 fernando
460 8053 sbayarri
                if (adjustableExtent) {
461
                        if (escalaX < escalaY) {
462
                                scale = escalaX;
463
                                newHeight = imageSize.getHeight() / scale;
464
                                adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
465
                                        yCenter - (newHeight / 2.0), extent.getWidth(), newHeight);
466
                        } else {
467
                                scale = escalaY;
468
                                newWidth = imageSize.getWidth() / scale;
469
                                adjustedExtent.setRect(xCenter - (newWidth / 2.0),
470
                                        yCenter - (extent.getHeight() / 2.0), newWidth,
471
                                        extent.getHeight());
472
                        }
473
                        escalado.setToScale(scale, -scale);
474
                }
475
                else { // adjusted is same as extent
476 956 vcaballero
                        scale = escalaX;
477 8053 sbayarri
                        adjustedExtent.setFrame(extent);
478
                        escalado.setToScale(escalaX, -escalaY);
479 956 vcaballero
                }
480 213 fernando
481 3683 caballero
                translacion.setToTranslation(-getAdjustedExtent().getX(),
482
                        -getAdjustedExtent().getY() - getAdjustedExtent().getHeight());
483 956 vcaballero
484
                AffineTransform offsetTrans = new AffineTransform();
485
                offsetTrans.setToTranslation(offset.getX(), offset.getY());
486
487
                trans.setToIdentity();
488
                trans.concatenate(offsetTrans);
489
                trans.concatenate(escalado);
490
491
                trans.concatenate(translacion);
492
493 3884 caballero
                // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
494 956 vcaballero
                // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
495
                AffineTransform at;
496
497 237 fjp
                try {
498
                        at = trans.createInverse();
499 956 vcaballero
500 237 fjp
                        java.awt.Point pPixel = new java.awt.Point(1, 1);
501 956 vcaballero
                        Point2D.Float pProv = new Point2D.Float();
502
                        at.deltaTransform(pPixel, pProv);
503 237 fjp
504 956 vcaballero
                        dist1pixel = pProv.x;
505
                        dist3pixel = 3 * pProv.x;
506 237 fjp
                } catch (NoninvertibleTransformException e) {
507 867 fjp
                        System.err.println("transformada afin = " + trans.toString());
508 956 vcaballero
                        System.err.println("extent = " + extent.toString() +
509
                                " imageSize= " + imageSize.toString());
510 918 fernando
                        throw new RuntimeException(e);
511 237 fjp
                }
512 213 fernando
        }
513 956 vcaballero
514
        /**
515 1036 vcaballero
         * Inserta la desviaci?n.
516 956 vcaballero
         *
517 1036 vcaballero
         * @param p Punto.
518 956 vcaballero
         */
519
        public void setOffset(Point2D p) {
520
                offset = p;
521 351 vcaballero
        }
522 956 vcaballero
        /**
523 5369 jaume
         * The offset is the position where to start drawing. The offset of a View is
524
         * always (0, 0) because the drawing area fits with the full window area. But in
525
         * a Layout it is up to the place where the FFrameView is located.
526 1317 vcaballero
         *
527 5369 jaume
         * @param p Point, in pixels, where the map starts.
528 1317 vcaballero
         */
529
        public Point2D getOffset() {
530
                return offset;
531
        }
532
        /**
533 1036 vcaballero
         * Inserta el color de fondo.
534 956 vcaballero
         *
535 1036 vcaballero
         * @param c Color de fondo.
536 956 vcaballero
         */
537 213 fernando
        public void setBackColor(Color c) {
538
                backColor = c;
539 1197 fernando
                callColorChanged(backColor);
540 213 fernando
        }
541
542 956 vcaballero
        /**
543 1036 vcaballero
         * Devuelve el color de fondo.
544 956 vcaballero
         *
545 1036 vcaballero
         * @return Color de fondo.
546 956 vcaballero
         */
547 213 fernando
        public Color getBackColor() {
548
                return backColor;
549
        }
550 956 vcaballero
551 213 fernando
        /**
552 4910 jaume
         * Returns the extent currently covered by the view.
553 956 vcaballero
         *
554 213 fernando
         * @return Returns the adjustedExtent.
555
         */
556
        public Rectangle2D getAdjustedExtent() {
557 3683 caballero
                if (cliprect!=null){
558
                        return adjustedExtent.createIntersection(cliprect);
559
                }
560 213 fernando
                return adjustedExtent;
561
        }
562 956 vcaballero
563 213 fernando
        /**
564 7796 caballero
         * Devuelve la unidad de medida en la que queremos realizar nuestras mediciones y
565
         * en la que se nos mostrar? toda la informaci?n.
566 956 vcaballero
         *
567 213 fernando
         * @return Returns the distanceUnits.
568
         */
569
        public int getDistanceUnits() {
570
                return distanceUnits;
571
        }
572 956 vcaballero
573 213 fernando
        /**
574 7796 caballero
         * Inserta la unidad de medida en la que queremos realizar nuestras mediciones y
575
         * en la que se nos mostrar? toda la informaci?n.
576 956 vcaballero
         *
577 213 fernando
         * @param distanceUnits The distanceUnits to set.
578
         */
579
        public void setDistanceUnits(int distanceUnits) {
580
                this.distanceUnits = distanceUnits;
581
        }
582 956 vcaballero
583 213 fernando
        /**
584 7796 caballero
         * Devuelve la unidad de medida del mapa, es la unidad de medida en la que est? la cartogr?fia que cargamos,
585
         * normalmente en metros.
586 956 vcaballero
         *
587 213 fernando
         * @return Returns the mapUnits.
588
         */
589
        public int getMapUnits() {
590
                return mapUnits;
591
        }
592 956 vcaballero
593 213 fernando
        /**
594 7796 caballero
         * Inserta la unidad de medida del mapa, es la unidad de medida en la que est? la cartogr?fia que cargamos,
595
         * normalmente en metros.
596 956 vcaballero
         *
597 213 fernando
         * @param mapUnits The mapUnits to set.
598
         */
599
        public void setMapUnits(int mapUnits) {
600
                this.mapUnits = mapUnits;
601 956 vcaballero
        }
602
603
        /**
604 1036 vcaballero
         * Devuelve la anchura de la imagen.
605 956 vcaballero
         *
606 1036 vcaballero
         * @return anchura en pixels de la imagen.
607 956 vcaballero
         */
608
        public int getImageWidth() {
609 214 fernando
                return imageSize.width;
610 213 fernando
        }
611 956 vcaballero
612
        /**
613 1036 vcaballero
         * Devuelve la altura de la imagen.
614 956 vcaballero
         *
615 1036 vcaballero
         * @return altura de la imagen.
616 956 vcaballero
         */
617
        public int getImageHeight() {
618 214 fernando
                return imageSize.height;
619
        }
620 956 vcaballero
621
        /**
622 1036 vcaballero
         * Devuelve la distancia real de un pixel.
623 956 vcaballero
         *
624 1036 vcaballero
         * @return Distancia real de un pixel.
625 956 vcaballero
         */
626 237 fjp
        public double getDist1pixel() {
627
                return dist1pixel;
628
        }
629 956 vcaballero
630
        /**
631 1036 vcaballero
         * Inserta la distancia real de un pixel.
632 956 vcaballero
         *
633 1036 vcaballero
         * @param dist1pixel Distancia real de un pixel.
634 956 vcaballero
         */
635 237 fjp
        public void setDist1pixel(double dist1pixel) {
636
                this.dist1pixel = dist1pixel;
637
        }
638 956 vcaballero
639
        /**
640 1036 vcaballero
         * Devuelve la distancia real de tres pixel.
641 956 vcaballero
         *
642 1036 vcaballero
         * @return Distancia real de tres pixel.
643 956 vcaballero
         */
644 237 fjp
        public double getDist3pixel() {
645
                return dist3pixel;
646
        }
647 956 vcaballero
648
        /**
649 1036 vcaballero
         * Inserta la distancia real de tres pixels.
650 956 vcaballero
         *
651 1036 vcaballero
         * @param dist3pixel Distancia real de tres pixels.
652 956 vcaballero
         */
653 237 fjp
        public void setDist3pixel(double dist3pixel) {
654
                this.dist3pixel = dist3pixel;
655
        }
656 956 vcaballero
657 277 fernando
        /**
658 1036 vcaballero
         * Devuelve los Extents anteriores almacenados.
659 956 vcaballero
         *
660 277 fernando
         * @return Returns the extents.
661
         */
662
        public ExtentHistory getExtents() {
663
                return extents;
664
        }
665
666
        /**
667 1036 vcaballero
         * Devuelve la proyecci?n.
668 956 vcaballero
         *
669 346 fernando
         * @return Returns the proj.
670
         */
671
        public IProjection getProjection() {
672
                return proj;
673
        }
674 956 vcaballero
675 346 fernando
        /**
676 1036 vcaballero
         * Inserta la proyecci?n.
677 956 vcaballero
         *
678 346 fernando
         * @param proj The proj to set.
679
         */
680
        public void setProjection(IProjection proj) {
681 6187 jmvivo
                if(this.proj == null || !this.proj.getAbrev().equals(proj.getAbrev())) {
682 6630 caballero
                        this.proj = proj;
683 6187 jmvivo
                        callProjectionChanged(proj);
684
                }
685 346 fernando
        }
686 3884 caballero
687 1152 fjp
        /**
688
         * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
689
         * un calculateAffineTransform, ni recalcula el adjustedExtent.
690
         * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
691 3884 caballero
         * RasterAdapter, y que recibiera un ViewPortData.
692 1152 fjp
         * @param at
693
         */
694 3884 caballero
        public void setAffineTransform(AffineTransform at)
695 1152 fjp
        {
696
            this.trans = at;
697
        }
698 956 vcaballero
699
        /**
700 1036 vcaballero
         * Devuelve el XMLEntity.
701 956 vcaballero
         *
702 1036 vcaballero
         * @return XMLEntity.
703 956 vcaballero
         */
704
        public XMLEntity getXMLEntity() {
705
                XMLEntity xml = new XMLEntity();
706 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
707 956 vcaballero
708
                if (adjustedExtent != null) {
709
                        xml.putProperty("adjustedExtentX", adjustedExtent.getX());
710
                        xml.putProperty("adjustedExtentY", adjustedExtent.getY());
711
                        xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
712
                        xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
713 741 vcaballero
                }
714 956 vcaballero
715 1282 fjp
                if (backColor != null)
716
                    xml.putProperty("backColor", StringUtilities.color2String(backColor));
717 956 vcaballero
718
                if (clip != null) {
719
                        xml.putProperty("clipX", clip.getX());
720
                        xml.putProperty("clipY", clip.getY());
721
                        xml.putProperty("clipW", clip.getWidth());
722
                        xml.putProperty("clipH", clip.getHeight());
723 435 vcaballero
                }
724 956 vcaballero
725
                xml.putProperty("dist1pixel", dist1pixel);
726
                xml.putProperty("dist3pixel", dist3pixel);
727
                xml.putProperty("distanceUnits", distanceUnits);
728
729
                if (extent != null) {
730
                        xml.putProperty("extentX", extent.getX());
731
                        xml.putProperty("extentY", extent.getY());
732
                        xml.putProperty("extentW", extent.getWidth());
733
                        xml.putProperty("extentH", extent.getHeight());
734 741 vcaballero
                }
735 956 vcaballero
736 449 vcaballero
                xml.addChild(extents.getXMLEntity());
737 956 vcaballero
                xml.putProperty("mapUnits", mapUnits);
738
                xml.putProperty("offsetX", offset.getX());
739
                xml.putProperty("offsetY", offset.getY());
740
741
                if (proj != null) {
742
                        xml.putProperty("proj", proj.getAbrev());
743 442 vcaballero
                }
744 956 vcaballero
745 10739 jaume
                xml.putProperty("scale", scale, false);
746 956 vcaballero
747 435 vcaballero
                return xml;
748
        }
749 956 vcaballero
750
        /**
751 1036 vcaballero
         * Crea un nuevo ViewPort a partir del XMLEntity.
752 956 vcaballero
         *
753 1036 vcaballero
         * @param xml XMLEntity.
754 956 vcaballero
         *
755 1036 vcaballero
         * @return Nuevo ViewPort.
756 956 vcaballero
         */
757 2183 fernando
        public static ViewPort createFromXML03(XMLEntity xml) {
758
                ViewPort vp = new ViewPort(null);
759
760
                if (xml.contains("adjustedExtentX")) {
761
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
762
                                                "adjustedExtentX"),
763
                                        xml.getDoubleProperty("adjustedExtentY"),
764
                                        xml.getDoubleProperty("adjustedExtentW"),
765
                                        xml.getDoubleProperty("adjustedExtentH"));
766
                }
767
768
                if (xml.contains("backColor")) {
769
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
770
                                                "backColor")));
771
                }
772
773
                if (xml.contains("clipX")) {
774
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
775
                                        xml.getDoubleProperty("clipY"),
776
                                        xml.getDoubleProperty("clipW"),
777
                                        xml.getDoubleProperty("clipH"));
778
                }
779
780
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
781
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
782
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
783
                vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
784
785
                if (xml.contains("extentX")) {
786
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
787
                                        xml.getDoubleProperty("extentY"),
788
                                        xml.getDoubleProperty("extentW"),
789
                                        xml.getDoubleProperty("extentH")));
790
791
                        //Calcula la transformaci?n af?n
792
                        vp.calculateAffineTransform();
793
794
                        // Lanzamos los eventos de extent cambiado
795
                        // vp.callExtentListeners(vp.adjustedExtent);
796
                }
797
798
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
799
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
800
                                xml.getDoubleProperty("offsetY")));
801
802
                if (xml.contains("proj")) {
803 6164 luisw2
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
804 2183 fernando
                }
805
806
                //vp.setScale(xml.getDoubleProperty("scale"));
807 6630 caballero
                vp.refreshExtent();
808 2183 fernando
                return vp;
809
        }
810
811
        /**
812
         * Crea un nuevo ViewPort a partir del XMLEntity.
813
         *
814
         * @param xml XMLEntity.
815
         *
816
         * @return Nuevo ViewPort.
817
         */
818 956 vcaballero
        public static ViewPort createFromXML(XMLEntity xml) {
819
                ViewPort vp = new ViewPort(null);
820
821
                if (xml.contains("adjustedExtentX")) {
822
                        vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty(
823
                                                "adjustedExtentX"),
824
                                        xml.getDoubleProperty("adjustedExtentY"),
825
                                        xml.getDoubleProperty("adjustedExtentW"),
826
                                        xml.getDoubleProperty("adjustedExtentH"));
827 741 vcaballero
                }
828 956 vcaballero
829
                if (xml.contains("backColor")) {
830
                        vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty(
831
                                                "backColor")));
832 8242 caballero
                }else {
833
                        vp.setBackColor(Color.white);
834 435 vcaballero
                }
835 956 vcaballero
836
                if (xml.contains("clipX")) {
837
                        vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"),
838
                                        xml.getDoubleProperty("clipY"),
839
                                        xml.getDoubleProperty("clipW"),
840
                                        xml.getDoubleProperty("clipH"));
841 741 vcaballero
                }
842 956 vcaballero
843 442 vcaballero
                vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
844
                vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
845
                vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
846 956 vcaballero
                vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
847 918 fernando
848 956 vcaballero
                if (xml.contains("extentX")) {
849
                        vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),
850
                                        xml.getDoubleProperty("extentY"),
851
                                        xml.getDoubleProperty("extentW"),
852
                                        xml.getDoubleProperty("extentH")));
853
854
                        //Calcula la transformaci?n af?n
855
                        vp.calculateAffineTransform();
856
857
                        // Lanzamos los eventos de extent cambiado
858 1040 fjp
                        // vp.callExtentListeners(vp.adjustedExtent);
859 741 vcaballero
                }
860 956 vcaballero
861 442 vcaballero
                vp.setMapUnits(xml.getIntProperty("mapUnits"));
862 956 vcaballero
                vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),
863
                                xml.getDoubleProperty("offsetY")));
864
865
                if (xml.contains("proj")) {
866 6164 luisw2
                        vp.proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
867 442 vcaballero
                }
868 956 vcaballero
869 1135 vcaballero
                //vp.setScale(xml.getDoubleProperty("scale"));
870 6630 caballero
                vp.refreshExtent();
871 435 vcaballero
                return vp;
872
        }
873 956 vcaballero
874
        /**
875 1036 vcaballero
         * Clona el ViewPort.
876 956 vcaballero
         *
877 1036 vcaballero
         * @return ViewPort clonado.
878 956 vcaballero
         */
879
        public ViewPort cloneViewPort() {
880
                return createFromXML(getXMLEntity());
881 798 vcaballero
        }
882 956 vcaballero
883
        /**
884 1036 vcaballero
         * Devuelve el String con datos del ViewPort.
885 956 vcaballero
         *
886 1036 vcaballero
         * @return Cadena con datos del ViewPort.
887 956 vcaballero
         */
888
        public String toString() {
889 875 fjp
                String str;
890 956 vcaballero
                str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" +
891
                        adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale +
892 875 fjp
                        "\ntrans=" + trans;
893 956 vcaballero
894 875 fjp
                return str;
895
        }
896 3683 caballero
897
        public void setClipRect(Rectangle2D rectView) {
898
                cliprect=rectView;
899 3884 caballero
900 3683 caballero
        }
901
902 4213 caballero
        public Rectangle2D fromMapRectangle(Rectangle2D r) {
903 9515 caballero
                Rectangle2D rect=new Rectangle2D.Double();
904 4213 caballero
                Point2D p1=fromMapPoint((int)r.getX(),(int)r.getY());
905 9515 caballero
                Point2D p2=fromMapPoint((int)r.getMaxX(),(int)r.getMaxY());
906
                rect.setFrameFromDiagonal(p1,p2);
907
                return rect;
908 4213 caballero
        }
909 6630 caballero
        public void setScale(long s){
910
                double x=extent.getX();
911
                double y=extent.getY();
912
                double escalaX = imageSize.getWidth() / extent.getWidth();
913
                double w=imageSize.getWidth() / s;
914
                double h=imageSize.getHeight() / s;
915
                double difw = escalaX/s;
916 3884 caballero
917 6630 caballero
                double x1 = (-x * difw) -
918
            x+
919
            extent.getWidth()/2;
920
        double y1 = (-y * difw) -
921
            y +
922
            extent.getHeight()/2;
923
        double w1=extent.getWidth()*difw;
924
        double h1=extent.getHeight()*difw;
925
                extent.setRect(-x1,-y1,w1,h1);
926
        }
927 4213 caballero
928 213 fernando
}