Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1012 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 12987

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