Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / ViewPort.java @ 41051

History | View | Annotate | Download (59.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontext;
25

    
26
import java.awt.Color;
27
import java.awt.Dimension;
28
import java.awt.Toolkit;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.NoninvertibleTransformException;
31
import java.awt.geom.Point2D;
32
import java.awt.geom.Rectangle2D;
33
import java.util.ArrayList;
34

    
35
import org.cresques.cts.GeoCalc;
36
import org.cresques.cts.IProjection;
37
import org.cresques.cts.UTM;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.GeometryManager;
45
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
46
import org.gvsig.fmap.geom.exception.CreateGeometryException;
47
import org.gvsig.fmap.geom.primitive.Envelope;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.fmap.mapcontext.events.ColorEvent;
50
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
51
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
52
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener;
53
import org.gvsig.timesupport.Time;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dynobject.DynStruct;
56
import org.gvsig.tools.lang.Cloneable;
57
import org.gvsig.tools.persistence.PersistenceManager;
58
import org.gvsig.tools.persistence.Persistent;
59
import org.gvsig.tools.persistence.PersistentState;
60
import org.gvsig.tools.persistence.exception.PersistenceException;
61
import org.gvsig.tools.util.Callable;
62

    
63
/**
64
 * <p>
65
 * <code>ViewPort</code> class represents the logic needed to transform a
66
 * rectangular area of a map to the available area in screen to display it.
67
 * </p>
68
 * 
69
 * <p>
70
 * Includes an affine transformation, between the rectangular area selected of
71
 * the external map, in its own <i>map coordinates</i>, to the rectangular area
72
 * available of a view in <i>screen coordinates</i>.
73
 * </p>
74
 * 
75
 * <p>
76
 * Elements:
77
 * <ul>
78
 * <li><i>extent</i>: the area selected of the map, in <i>map coordinates</i>.
79
 * <li><i>imageSize</i>: width and height in pixels (<i>screen coordinates</i>)
80
 * of the area available in screen to display the area selected of the map.
81
 * <li><i>adjustedExtent</i>: the area selected must be an scale of
82
 * <i>imageSize</i>.<br>
83
 * This implies adapt the extent, preserving and centering it, and adding around
84
 * the needed area to fill all the image size. That added area will be extracted
85
 * from the original map, wherever exists, and filled with the background color
86
 * wherever not.
87
 * <li><i>scale</i>: the scale between the adjusted extent and the image size.
88
 * <li><i>backColor</i>: the default background color in the view, if there is
89
 * no map.
90
 * <li><i>trans</i>: the affine transformation.
91
 * <li><i>proj</i>: map projection used in this view.
92
 * <li><i>distanceUnits</i>: distance measurement units, of data in screen.
93
 * <li><i>mapUnits</i>: measurement units, of data in map.
94
 * <li><i>extents</i>: an {@link ExtentHistory ExtentHistory} with the last
95
 * previous extents.
96
 * <li><i>offset</i>: position in pixels of the available rectangular area,
97
 * where start drawing the map.
98
 * <li><i>dist1pixel</i>: the distance in <i>world coordinates</i> equivalent to
99
 * 1 pixel in the view with the current extent.
100
 * <li><i>dist3pixel</i>: the distance in <i>world coordinates</i> equivalent to
101
 * 3 pixels in the view with the current extent.
102
 * <li><i>listeners</i>: list with the {@link ViewPortListener ViewPortListener}
103
 * registered.
104
 * </ul>
105
 * </p>
106
 * 
107
 * @author Vicente Caballero Navarro
108
 */
109
public class ViewPort implements Persistent, Cloneable {
110

    
111
    private static final String FIELD_DISTANCE_AREA = "distanceArea";
112
    private static final String FIELD_IMAGE_SIZE = "imageSize";
113
    private static final String FIELD_PROJ = "proj";
114
    private static final String FIELD_OFFSET = "offset";
115
    private static final String FIELD_MAP_UNITS = "mapUnits";
116
    private static final String FIELD_EXTENT = "extent";
117
    private static final String FIELD_EXTENTS = "extents";
118
    private static final String FIELD_DISTANCE_UNITS = "distanceUnits";
119
    private static final String FIELD_DIST3PIXEL = "dist3pixel";
120
    private static final String FIELD_DIST1PIXEL = "dist1pixel";
121
    private static final String FIELD_CLIP = "clip";
122
    private static final String FIELD_BACK_COLOR = "backColor";
123
    private static final String FIELD_ADJUSTED_EXTENT = "adjustedExtent";
124

    
125
    private static final GeometryManager geomManager =
126
        GeometryLocator.getGeometryManager();
127
    private static final Logger logger =
128
        LoggerFactory.getLogger(ViewPort.class);
129

    
130
    /**
131
     * <p>
132
     * Screen resolution in <i>dots-per-inch</i>. Useful to calculate the
133
     * geographic scale of the view.
134
     * </p>
135
     * 
136
     * @see Toolkit#getScreenResolution()
137
     * @see #getScale()
138
     */
139
    private static int dpi = java.awt.Toolkit.getDefaultToolkit()
140
        .getScreenResolution();
141

    
142
    /**
143
     * <p>
144
     * Area selected by user using some tool.
145
     * </p>
146
     * 
147
     * <p>
148
     * When the zoom changes (for instance when using the zoom in or zoom out
149
     * tools, but also zooming to a selected feature or shape) the extent that
150
     * covers that area is the value returned by this method. It is not the
151
     * actual area shown in the view because it does not care about the aspect
152
     * ratio of the available area. However, any part of the real world
153
     * contained in this extent is shown in the view.
154
     * </p>
155
     * <p>
156
     * Probably this is not what you are looking for. If you are looking for the
157
     * complete extent currently shown, you must use
158
     * {@linkplain #getAdjustedExtent()} method which returns the extent that
159
     * contains this one but regarding the current view's aspect ratio.
160
     * </p>
161
     * 
162
     * @see #getExtent()
163
     * @see #setEnvelope(Envelope)
164
     */
165
    protected Rectangle2D extent;
166
    
167
    protected Time time;
168

    
169
    /**
170
     * <p>
171
     * Location and dimensions of the extent adjusted to the image size.
172
     * </p>
173
     * 
174
     * @see #getAdjustedExtent()
175
     */
176
    protected Rectangle2D adjustedExtent;
177

    
178
    /**
179
     * Draw version of the context. It's used for know when de componend has
180
     * changed any visualization property
181
     * 
182
     * @see getDrawVersion
183
     * @see updateDrawVersion
184
     */
185
    private long drawVersion = 0L;
186

    
187
    /**
188
     * <p>
189
     * History with the last extents of the view.
190
     * </p>
191
     * 
192
     * @see #setPreviousExtent()
193
     * @see #getExtents()
194
     */
195
    protected ExtentHistory extents = new ExtentHistory();
196

    
197
    /**
198
     * <p>
199
     * Size in <i>screen coordinates</i> of the rectangle where the image is
200
     * displayed.
201
     * </p>
202
     * <p>
203
     * Used by {@linkplain #calculateAffineTransform()} to calculate:<br>
204
     * 
205
     * <ul>
206
     * <li>The new {@link #scale scale} .
207
     * <li>The new {@link #adjustedExtent adjustableExtent} .
208
     * <li>The new {@link #trans trans} .
209
     * <li>The new real world coordinates equivalent to 1 pixel (
210
     * {@link #dist1pixel dist1pixel}) .
211
     * <li>The new real world coordinates equivalent to 3 pixels (
212
     * {@link #dist3pixel dist3pixel}) .
213
     * </ul>
214
     * </p>
215
     * 
216
     * @see #getImageSize()
217
     * @see #getImageHeight()
218
     * @see #getImageWidth()
219
     * @see #setImageSize(Dimension)
220
     */
221
    private Dimension imageSize;
222

    
223
    /**
224
     * <p>
225
     * the affine transformation between the {@link #extent extent} in <i>map 2D
226
     * coordinates</i> to the image area in the screen, in <i>screen 2D
227
     * coordinates</i> (pixels).
228
     * </p>
229
     * 
230
     * @see AffineTransform
231
     * 
232
     * @see #getAffineTransform()
233
     * @see #setAffineTransform(AffineTransform)
234
     * @see #calculateAffineTransform()
235
     */
236
    private AffineTransform trans = new AffineTransform();
237

    
238
    /**
239
     * <p>
240
     * Measurement unit used for measuring distances and displaying information.
241
     * </p>
242
     * 
243
     * @see #getDistanceUnits()
244
     * @see #setDistanceUnits(int)
245
     */
246
    private int distanceUnits = 1;
247
    /**
248
     * <p>
249
     * Measurement unit used for measuring areas and displaying information.
250
     * </p>
251
     * 
252
     * @see #getDistanceArea()
253
     * @see #setDistanceArea(int)
254
     */
255
    private int distanceArea = 1;
256
    /**
257
     * <p>
258
     * Measurement unit used by this view port for the map.
259
     * </p>
260
     * 
261
     * @see #getMapUnits()
262
     * @see #setMapUnits(int)
263
     */
264
    private int mapUnits = 1;
265

    
266
    /**
267
     * <p>
268
     * Array with the {@link ViewPortListener ViewPortListener}s registered to
269
     * this view port.
270
     * </p>
271
     * 
272
     * @see #addViewPortListener(ViewPortListener)
273
     * @see #removeViewPortListener(ViewPortListener)
274
     */
275
    private ArrayList listeners = new ArrayList();
276

    
277
    /**
278
     * <p>
279
     * The offset is the position where start drawing the map.
280
     * </p>
281
     * <p>
282
     * The offset of a <a href="http://www.gvsig.gva.es/">gvSIG</a>'s
283
     * <i>View</i> is always (0, 0) because the drawing area fits with the full
284
     * window area. But in a <a href="http://www.gvsig.gva.es/">gvSIG</a>'s
285
     * <i>Layout</i> it's up to the place where the <code>FFrameView</code> is
286
     * located.
287
     * </p>
288
     * 
289
     * @see #getOffset()
290
     * @see #setOffset(Point2D)
291
     */
292
    private Point2D offset = new Point2D.Double(0, 0);
293

    
294
    /**
295
     * <p>
296
     * Clipping area.
297
     * </p>
298
     */
299
    // private Rectangle2D clip;
300

    
301
    /**
302
     * <p>
303
     * Background color of this view.
304
     * </p>
305
     * 
306
     * @see #getBackColor()
307
     * @see #setBackColor(Color)
308
     */
309
    private Color backColor = null; // Color.WHITE;
310

    
311
    /**
312
     * <p>
313
     * Information about the map projection used in this view.
314
     * </p>
315
     * 
316
     * @see #getProjection()
317
     * @see #setProjection(IProjection)
318
     */
319
    private IProjection proj;
320

    
321
    /**
322
     * <p>
323
     * Represents the distance in <i>world coordinates</i> equivalent to 1 pixel
324
     * in the view with the current extent.
325
     * </p>
326
     * 
327
     * @see #getDist1pixel()
328
     * @see #setDist1pixel(double)
329
     */
330
    private double dist1pixel;
331

    
332
    /**
333
     * <p>
334
     * Represents the distance in <i>world coordinates</i> equivalent to 3
335
     * pixels in the view with the current extent.
336
     * </p>
337
     * 
338
     * @see #getDist3pixel()
339
     * @see #setDist3pixel(double)
340
     */
341
    private double dist3pixel;
342

    
343
    /**
344
     * <p>
345
     * Ratio between the size of <code>imageSize</code> and <code>extent</code>:
346
     * <br>
347
     * <i>
348
     * 
349
     * <pre>
350
     * min{(imageSize.getHeight()/extent.getHeight(), imageSize.getWidth()/extent.getWidth())}
351
     * </pre>
352
     * 
353
     * </i>
354
     * </p>
355
     */
356
    private double scale;
357

    
358
    /**
359
     * <p>
360
     * Clipping area.
361
     * </p>
362
     * 
363
     * @see #setClipRect(Rectangle2D)
364
     */
365
    private Rectangle2D cliprect;
366

    
367
    /**
368
     * <p>
369
     * Enables or disables the <i>"adjustable extent"</i> mode.
370
     * </p>
371
     * 
372
     * <p>
373
     * When calculates the affine transform, if
374
     * <ul>
375
     * <li><i>enabled</i>: the new <code>adjustedExtent</code> will have the (X,
376
     * Y) coordinates of the <code>extent</code> and an area that will be an
377
     * scale of the image size. That area will have different height or width
378
     * (not both) of the extent according the least ratio (height or width) in
379
     * 
380
     * <pre>
381
     * image.size/extent.size&quot;
382
     * </pre>.
383
     * <li><i>disabled</i>: the new <code>adjustedExtent</code> will be like
384
     * <code>extent</code>.
385
     * </ul>
386
     * </p>
387
     * 
388
     * @see #setAdjustable(boolean)
389
     */
390
    private boolean adjustableExtent = true;
391

    
392
    public ViewPort() {
393

    
394
    }
395

    
396
    /**
397
     * <p>
398
     * Creates a new view port with the information of the projection in
399
     * <code>proj</code> argument, and default configuration:
400
     * </p>
401
     * <p>
402
     * <ul>
403
     * <li><i><code>distanceUnits</code></i> = meters
404
     * <li><i><code>mapUnits</code></i> = meters
405
     * <li><i><code>backColor</code></i> = <i>undefined</i>
406
     * <li><i><code>offset</code></i> = <code>new Point2D.Double(0, 0);</code>
407
     * </ul>
408
     * </p>
409
     * 
410
     * @param proj
411
     *            information of the projection for this view port
412
     */
413
    public ViewPort(IProjection proj) {
414
        // Por defecto
415
        this.proj = proj;
416
    }
417

    
418
    /**
419
     * <p>
420
     * Changes the status of the <i>"adjustable extent"</i> option to enabled or
421
     * disabled.
422
     * </p>
423
     * 
424
     * <p>
425
     * If view port isn't adjustable, won't bear in mind the aspect ratio of the
426
     * available rectangular area to calculate the affine transform from the
427
     * original map in real coordinates. (Won't scale the image to adapt it to
428
     * the available rectangular area).
429
     * </p>
430
     * 
431
     * @param boolean the boolean to be set
432
     */
433
    public void setAdjustable(boolean adjustable) {
434
        if (adjustable == adjustableExtent) {
435
            return;
436
        }
437
        adjustableExtent = adjustable;
438
        this.updateDrawVersion();
439
    }
440

    
441
    /**
442
     * <p>
443
     * Appends the specified {@link ViewPortListener ViewPortListener} listener
444
     * if weren't.
445
     * </p>
446
     * 
447
     * @param arg0
448
     *            the listener to add
449
     * 
450
     * @return <code>true</code> if has been added successfully
451
     * 
452
     * @see #removeViewPortListener(ViewPortListener)
453
     */
454
    public boolean addViewPortListener(ViewPortListener arg0) {
455
        if (!listeners.contains(arg0)) {
456
            return listeners.add(arg0);
457
        }
458
        return false;
459
    }
460

    
461
    /**
462
     * <p>
463
     * Removes the specified {@link ViewPortListener ViewPortListener} listener,
464
     * if existed.
465
     * </p>
466
     * 
467
     * @param arg0
468
     *            the listener to remove
469
     * 
470
     * @return <code>true</code> if the contained the specified listener.
471
     * 
472
     * @see #addViewPortListener(ViewPortListener)
473
     */
474
    public boolean removeViewPortListener(ViewPortListener arg0) {
475
        return listeners.remove(arg0);
476
    }
477

    
478
    /**
479
     * <p>
480
     * Converts and returns the distance <code>d</code>, that is in <i>map
481
     * coordinates</i> to <i>screen coordinates</i> using a <i>delta
482
     * transform</i> with the transformation affine information in the
483
     * {@link #trans #trans} attribute.
484
     * </p>
485
     * 
486
     * @param d
487
     *            distance in <i>map coordinates</i>
488
     * 
489
     * @return distance equivalent in <i>screen coordinates</i>
490
     * 
491
     * @see #toMapDistance(int)
492
     * @see AffineTransform#deltaTransform(Point2D, Point2D)S
493
     */
494
    public int fromMapDistance(double d) {
495
        Point2D.Double pWorld = new Point2D.Double(1, 1);
496
        Point2D.Double pScreen = new Point2D.Double();
497

    
498
        try {
499
            trans.deltaTransform(pWorld, pScreen);
500
        } catch (Exception e) {
501
            System.err.print(e.getMessage());
502
        }
503

    
504
        return (int) (d * pScreen.x);
505
    }
506

    
507
    /**
508
     * <p>
509
     * Converts and returns the 2D point <code>(x,y)</code>, that is in <i>map
510
     * coordinates</i> to <i>screen coordinates</i> (pixels) using the affine
511
     * transformation in the {@link #trans #trans} attribute.
512
     * </p>
513
     * 
514
     * @param x
515
     *            the <code>x</code> <i>map coordinate</i> of a 2D point
516
     * @param y
517
     *            the <code>y</code> <i>map coordinate</i> of a 2D point
518
     * 
519
     * @return 2D point equivalent in <i>screen coordinates</i> (pixels)
520
     * 
521
     * @see #fromMapPoint(Point2D)
522
     * @see AffineTransform#transform(Point2D, Point2D)
523
     */
524
    public Point2D fromMapPoint(double x, double y) {
525
        Point2D.Double pWorld = new Point2D.Double(x, y);
526
        Point2D.Double pScreen = new Point2D.Double();
527

    
528
        try {
529
            trans.transform(pWorld, pScreen);
530
        } catch (Exception e) {
531
            System.err.print(e.getMessage());
532
        }
533

    
534
        return pScreen;
535
    }
536

    
537
    /**
538
     * <p>
539
     * Converts and returns the 2D point argument, that is in <i>map
540
     * coordinates</i> to <i>screen coordinates</i> (pixels) using the affine
541
     * transformation in the {@link #trans #trans} attribute.
542
     * </p>
543
     * 
544
     * @param point
545
     *            the 2D point in <i>map coordinates</i>
546
     * 
547
     * @return 2D point equivalent in <i>screen coordinates</i> (pixels)
548
     * 
549
     * @see #toMapPoint(Point2D)
550
     * @see #fromMapPoint(double, double)
551
     */
552
    public Point2D fromMapPoint(Point2D point) {
553
        return fromMapPoint(point.getX(), point.getY());
554
    }
555

    
556
    /**
557
     * <p>
558
     * Converts and returns the 2D point <code>(x,y)</code>, that is in
559
     * <i>screen coordinates</i> (pixels) to <i>map coordinates</i> using the
560
     * affine transformation in the {@link #trans #trans} attribute.
561
     * </p>
562
     * 
563
     * @param x
564
     *            the <code>x</code> <i>screen coordinate</i> of a 2D point
565
     * @param y
566
     *            the <code>y</code> <i>screen coordinate</i> of a 2D point
567
     * 
568
     * @return 2D point equivalent in <i>map coordinates</i>
569
     * 
570
     * @see #toMapPoint(Point2D)
571
     * @see #fromMapPoint(double, double)
572
     * @deprecated use {@link #convertToMapPoint(int, int)}
573
     */
574
    public Point2D toMapPoint(int x, int y) {
575
        Point2D pScreen = new Point2D.Double(x, y);
576

    
577
        return toMapPoint(pScreen);
578
    }
579

    
580
    /**
581
     * <p>
582
     * Converts and returns the {@link Rectangle2D Rectangle2D}, that is in
583
     * <i>screen coordinates</i> (pixels) to <i>map coordinates</i> using
584
     * {@linkplain #toMapDistance(int)}, and {@linkplain #toMapPoint(int, int)}.
585
     * </p>
586
     * 
587
     * @param r
588
     *            the 2D rectangle in <i>screen coordinates</i> (pixels)
589
     * @return 2D rectangle equivalent in <i>map coordinates</i>
590
     * 
591
     * @see #fromMapRectangle(Rectangle2D)
592
     * @see #toMapDistance(int)
593
     * @see #toMapPoint(int, int)
594
     */
595
    public Rectangle2D toMapRectangle(Rectangle2D r) {
596
        Rectangle2D rect = new Rectangle2D.Double();
597
        Point2D p1 = toMapPoint((int) r.getX(), (int) r.getY());
598
        Point2D p2 = toMapPoint((int) r.getMaxX(), (int) r.getMaxY());
599
        rect.setFrameFromDiagonal(p1, p2);
600
        return rect;
601
    }
602

    
603
    /**
604
     * <p>
605
     * Converts and returns the distance <code>d</code>, that is in <i>screen
606
     * coordinates</i> to <i>map coordinates</i> using the transformation affine
607
     * information in the {@link #trans #trans} attribute.
608
     * </p>
609
     * 
610
     * @param d
611
     *            distance in pixels
612
     * 
613
     * @return distance equivalent in <i>map coordinates</i>
614
     * 
615
     * @see #fromMapDistance(double)
616
     * @see AffineTransform
617
     */
618
    public double toMapDistance(int d) {
619
        double dist = d / trans.getScaleX();
620

    
621
        return dist;
622
    }
623

    
624
    /**
625
     * <p>
626
     * Converts and returns the 2D point argument, that is in <i>screen
627
     * coordinates</i> (pixels) to <i>map coordinates</i> using the inverse
628
     * affine transformation of the {@link #trans #trans} attribute.
629
     * </p>
630
     * 
631
     * @param pScreen
632
     *            the 2D point in <i>screen coordinates</i> (pixels)
633
     * 
634
     * @return 2D point equivalent in <i>map coordinates</i>
635
     * 
636
     * @see #toMapPoint(int, int)
637
     * @see AffineTransform#createInverse()
638
     * @see AffineTransform#transform(Point2D, Point2D)
639
     * @deprecated use {@link #convertToMapPoint(Point2D)}
640
     */
641
    public Point2D toMapPoint(Point2D pScreen) {
642
        Point2D.Double pWorld = new Point2D.Double();
643
        AffineTransform at;
644

    
645
        try {
646
            at = trans.createInverse();
647
            at.transform(pScreen, pWorld);
648
        } catch (NoninvertibleTransformException e) {
649
            throw new RuntimeException("Non invertible transform Exception", e);
650
        }
651

    
652
        return pWorld;
653
    }
654

    
655
    public Point convertToMapPoint(Point2D pScreen) {
656
            Point2D p = toMapPoint(pScreen);
657
                try {
658
                        return geomManager.createPoint(
659
                                p.getX(), 
660
                                p.getY(), 
661
                                Geometry.SUBTYPES.GEOM2D
662
                        );
663
                } catch (CreateGeometryException e) {
664
                        // FIXME: Use a most especific exception.
665
                        throw new RuntimeException(e);
666
                }
667
    }
668
    
669
    public Point convertToMapPoint(int x, int y) {
670
        Point2D pScreen = new Point2D.Double(x, y);
671

    
672
        return convertToMapPoint(pScreen);
673
    }
674

    
675
    
676
    /**
677
     * <p>
678
     * Returns the real distance (in <i>world coordinates</i>) at the graphic
679
     * layers of two 2D points (in <i>map coordinates</i>) of the plane where is
680
     * selected the <i>extent</i>.
681
     * </p>
682
     * <p>
683
     * If the projection of this view is UTM, considers the Earth curvature.
684
     * </p>
685
     * 
686
     * @param pt1
687
     *            a 2D point in <i>map coordinates</i>
688
     * @param pt2
689
     *            another 2D point in <i>map coordinates</i>
690
     * 
691
     * @return the distance in meters between the two points 2D
692
     * 
693
     * @see GeoCalcImpl#distanceVincenty(Point2D, Point2D)
694
     */
695
    public double distanceWorld(Point2D pt1, Point2D pt2) {
696
        
697
        double dist = 0;
698
        if (proj.isProjected()) {
699
            dist = pt1.distance(pt2);
700
            dist = dist * MapContext.getDistanceTrans2Meter()[getMapUnits()];
701
        } else {
702
            GeoCalc geocalc = new GeoCalc(proj);
703
            dist = geocalc.distanceVincenty(pt1, pt2);
704
        }
705
        return dist;
706
    }
707

    
708
    /**
709
     * <p>
710
     * Sets as extent and adjusted extent of this view port, the previous.
711
     * Recalculating its parameters.
712
     * </p>
713
     * 
714
     * @see #getExtents()
715
     * @see #calculateAffineTransform()
716
     * @deprecated use {@link ViewPort#setPreviousEnvelope()}
717
     */
718
    public void setPreviousExtent() {
719
        setPreviousEnvelope();
720
    }
721

    
722
    /**
723
     * <p>
724
     * Sets as envelope and adjusted envelope of this view port, the previous.
725
     * Recalculating its parameters.
726
     * </p>
727
     * 
728
     * @see #getExtents()
729
     * @see #calculateAffineTransform()
730
     */
731
    public void setPreviousEnvelope() {
732
        this.updateDrawVersion();
733
        extent = extents.removePrev();
734

    
735
        // Calcula la transformaci�n af�n
736
        calculateAffineTransform();
737

    
738
        // Lanzamos los eventos de extent cambiado
739
        callExtentChanged(getAdjustedExtent());
740
    }
741

    
742
    /**
743
     * <p>
744
     * Gets the area selected by user using some tool.
745
     * </p>
746
     * 
747
     * <p>
748
     * When the zoom changes (for instance using the <i>zoom in</i> or <i>zoom
749
     * out</i> tools, but also zooming to a selected feature or shape) the
750
     * extent that covers that area is the value returned by this method. It is
751
     * not the actual area shown because it doesn't care about the aspect ratio
752
     * of the image size of the view. However, any part of the real world
753
     * contained in this extent is shown in the view.
754
     * </p>
755
     * 
756
     * <p>
757
     * If you are looking for the complete extent currently shown, you must use
758
     * the {@linkplain #getAdjustedExtent()} method.
759
     * </p>
760
     * 
761
     * @return the current extent
762
     * 
763
     * @see #setEnvelope(Envelope)
764
     * @see #getAdjustedExtent()
765
     * @see #setPreviousExtent()
766
     * @see #getExtents()
767
     * 
768
     * @deprecated use {@link ViewPort#getEnvelope()}
769
     */
770
    public Rectangle2D getExtent() {
771
        return extent;
772
    }
773

    
774
    /**
775
     * <p>
776
     * Gets the envelope selected by user using some tool.
777
     * </p>
778
     * 
779
     * <p>
780
     * When the zoom changes (for instance using the <i>zoom in</i> or <i>zoom
781
     * out</i> tools, but also zooming to a selected feature or shape) the
782
     * envelope that covers that area is the value returned by this method. It
783
     * is not the actual envelope shown because it doesn't care about the aspect
784
     * ratio of the image size of the view. However, any part of the real world
785
     * contained in this envelope is shown in the view.
786
     * </p>
787
     * 
788
     * <p>
789
     * If you are looking for the complete extent currently shown, you must use
790
     * the {@linkplain #getAdjustedEnvelope()} method.
791
     * </p>
792
     * 
793
     * @return the current envelope
794
     * 
795
     * @see #setEnvelope(Envelope)
796
     * @see #getAdjustedEnvelope()
797
     * @see #setPreviousEnvelope()
798
     * @see #getEnvelopes()
799
     */
800
    public Envelope getEnvelope() {
801
        if (this.extent == null) {
802
            return null;
803
        }
804
        try {
805
            return geomManager.createEnvelope(extent.getMinX(),
806
                extent.getMinY(),
807
                extent.getMaxX(),
808
                extent.getMaxY(),
809
                SUBTYPES.GEOM2D);
810
            // This class has to use Envelope instead of Rectangle2D. This catch
811
            // will disappear
812
        } catch (CreateEnvelopeException e) {
813
            logger.error("Error creating the envelope");
814
        }
815
        return null;
816
    }
817

    
818
    /**
819
     * <p>
820
     * Changes the <i>extent</i> and <i>adjusted extent</i> of this view port:<br>
821
     * <ul>
822
     * <li>Stores the previous extent.
823
     * <li>Calculates the new extent using <code>r</code>:
824
     * 
825
     * <pre>
826
     * extent =
827
     *     new Rectangle2D.Double(r.getMinX() - 0.1,
828
     *         r.getMinY() - 0.1,
829
     *         r.getWidth() + 0.2,
830
     *         r.getHeight() + 0.2);
831
     * </pre>
832
     * 
833
     * <li>Executes {@linkplain #calculateAffineTransform()}: getting the new
834
     * scale, adjusted extent, affine transformation between map and screen
835
     * coordinates, the real world coordinates equivalent to 1 pixel, and the
836
     * real world coordinates equivalent to 3 pixels.
837
     * <li>Notifies all {@link ViewPortListener ViewPortListener} registered
838
     * that the extent has changed.
839
     * </ul>
840
     * </p>
841
     * 
842
     * @param r
843
     *            the new extent
844
     * 
845
     * @see #getExtent()
846
     * @see #getExtents()
847
     * @see #calculateAffineTransform()
848
     * @see #setPreviousExtent()
849
     */
850
    public void setEnvelope(Envelope r) {
851
        Rectangle2D newExtent = null;
852
        // Esto comprueba que el extent no es de anchura o altura = "0"
853
        // y si es as� lo redimensiona.
854
        if (r != null) {
855
            if ((r.getMaximum(0) - r.getMinimum(0) == 0)
856
                || (r.getMaximum(1) - r.getMinimum(1) == 0)) {
857
                newExtent =
858
                    new Rectangle2D.Double(r.getMinimum(0) - 0.1,
859
                        r.getMinimum(1) - 0.1,
860
                        r.getMaximum(0) - r.getMinimum(0) + 0.2,
861
                        r.getMaximum(1) - r.getMinimum(1) + 0.2);
862
            } else {
863
                newExtent =
864
                    new Rectangle2D.Double(r.getMinimum(0),
865
                        r.getMinimum(1),
866
                        Math.abs(r.getMaximum(0) - r.getMinimum(0)),
867
                        Math.abs(r.getMaximum(1) - r.getMinimum(1)));
868
            }
869
        }
870

    
871
        if (this.extent != null && this.extent.equals(newExtent)) {
872
            return;
873
        }
874
        if (extent != null) {
875
            extents.put(extent);
876
        }
877
        this.updateDrawVersion();
878
        this.extent = newExtent;
879

    
880
        // Calcula la transformaci�n af�n
881
        calculateAffineTransform();
882

    
883
        // Lanzamos los eventos de extent cambiado
884
        callExtentChanged(getAdjustedExtent());
885
    }
886

    
887
    /**
888
     * <p>
889
     * Changes the <i>extent</i> and <i>adjusted extent</i> of this view port:<br>
890
     * <ul>
891
     * <li>Executes {@linkplain #calculateAffineTransform()}: getting the new
892
     * scale, adjusted extent, affine transformation between map and screen
893
     * coordinates, the real world coordinates equivalent to 1 pixel, and the
894
     * real world coordinates equivalent to 3 pixels.
895
     * <li>Notifies to all {@link ViewPortListener ViewPortListener} registered
896
     * that the extent has changed.
897
     * </ul>
898
     * </p>
899
     * 
900
     * @see #setEnvelope(Envelope)
901
     * @see #calculateAffineTransform()
902
     */
903
    public void refreshExtent() {
904
        // this.scale = scale;
905

    
906
        // Calcula la transformaci�n af�n
907
        calculateAffineTransform();
908

    
909
        // Lanzamos los eventos de extent cambiado
910
        callExtentChanged(getAdjustedExtent());
911
    }
912

    
913
    /**
914
     * <p>
915
     * Calculates and returns using the current projection of this view port,
916
     * the scale that is the extent in <i>screen coordinates</i> from the image
917
     * in <i>map coordinates</i>.
918
     * </p>
919
     * 
920
     * @return the scale <i>extent / image size</i> projected by this view port
921
     * 
922
     * @deprecated since 07/09/07, use {@linkplain MapContext#getScaleView()}
923
     */
924
    public double getScale() {
925
        
926
        double[] trans2Meter = MapContext.getDistanceTrans2Meter();
927
        if (proj == null) {
928
            double wmeters = ((getImageSize().width / dpi) * 0.0254);
929
            return (long) (
930
                (trans2Meter[getMapUnits()] * getAdjustedEnvelope().getLength(0))
931
                / wmeters);
932
        } else {
933
            return Math.round(proj.getScale(
934
                getAdjustedEnvelope().getMinimum(0)*trans2Meter[getMapUnits()],
935
                getAdjustedEnvelope().getMaximum(0)*trans2Meter[getMapUnits()],
936
                getImageSize().width,
937
                dpi));
938
        }
939

    
940
        /*
941
        return proj.getScale(extent.getMinX(),
942
            extent.getMaxX(),
943
            imageSize.width,
944
            dpi);
945
        */
946
    }
947

    
948
    /**
949
     * <p>
950
     * Affine transformation between <i>map 2D coordinates</i> to <i>screen 2D
951
     * coordinates</i> (pixels), preserving the "straightness" and "parallelism"
952
     * of the lines.
953
     * </p>
954
     * 
955
     * @return the affine transformation
956
     * 
957
     * @see #setAffineTransform(AffineTransform)
958
     * @see #calculateAffineTransform()
959
     */
960
    public AffineTransform getAffineTransform() {
961
        return trans;
962
    }
963

    
964
    /**
965
     * <p>
966
     * Returns the size of the image projected.
967
     * </p>
968
     * 
969
     * @return the image size
970
     * 
971
     * @see #setImageSize(Dimension)
972
     * @see #getImageHeight()
973
     * @see #getImageWidth()
974
     */
975
    public Dimension getImageSize() {
976
        return imageSize;
977
    }
978

    
979
    /**
980
     * <p>
981
     * Sets the size of the image projected, recalculating the parameters of
982
     * this view port.
983
     * </p>
984
     * 
985
     * @param imageSize
986
     *            the image size
987
     * 
988
     * @see #getImageSize()
989
     * @see #calculateAffineTransform()
990
     */
991
    public void setImageSize(Dimension imageSize) {
992

    
993
        if (this.imageSize == null || (!this.imageSize.equals(imageSize))) {
994
            this.updateDrawVersion();
995
            this.imageSize = imageSize;
996
            calculateAffineTransform();
997
        }
998
    }
999

    
1000
    /**
1001
     * <p>
1002
     * Notifies to all view port listeners registered, that the adjusted extent
1003
     * of this view port has changed.
1004
     * </p>
1005
     * 
1006
     * @param newRect
1007
     *            the new adjusted extend
1008
     * 
1009
     * @see #refreshExtent()
1010
     * @see #setEnvelope(Envelope)
1011
     * @see #setPreviousExtent()
1012
     * @see ExtentEvent
1013
     * @see ViewPortListener
1014
     */
1015
    protected void callExtentChanged(Envelope newRect) {
1016
        ExtentEvent ev = ExtentEvent.createExtentEvent(newRect);
1017

    
1018
        for (int i = 0; i < listeners.size(); i++) {
1019
            ViewPortListener listener = (ViewPortListener) listeners.get(i);
1020
            listener.extentChanged(ev);
1021
        }
1022
    }
1023
    
1024
    /**
1025
     * <p>
1026
     * Notifies to all view port listeners registered, that the time
1027
     * of this view port has changed.
1028
     * </p>
1029
     * 
1030
     * @param newTime
1031
     *            the new time
1032
     * 
1033
     * @see #refreshExtent()
1034
     * @see #setTime(Time)   
1035
     * @see ExtentEvent
1036
     * @see ViewPortListener
1037
     */
1038
    protected void callTimeChanged(Time newTime) {
1039
        ExtentEvent viewPortEvent = new ExtentEvent(newTime);
1040

    
1041
        for (int i = 0; i < listeners.size(); i++) {
1042
            ViewPortListener listener = (ViewPortListener) listeners.get(i);
1043
            listener.extentChanged(viewPortEvent);
1044
        }
1045
    }
1046

    
1047
    /**
1048
     * <p>
1049
     * Notifies to all view port listeners registered, that the background color
1050
     * of this view port has changed.
1051
     * </p>
1052
     * 
1053
     * @param c
1054
     *            the new background color
1055
     * 
1056
     * @see #setBackColor(Color)
1057
     * @see ColorEvent
1058
     * @see ViewPortListener
1059
     */
1060
    private void callColorChanged(Color c) {
1061
        ColorEvent ce = ColorEvent.createColorEvent(c);
1062

    
1063
        for (int i = 0; i < listeners.size(); i++) {
1064
            ViewPortListener listener = (ViewPortListener) listeners.get(i);
1065
            listener.backColorChanged(ce);
1066
        }
1067
    }
1068

    
1069
    /**
1070
     * <p>
1071
     * Notifies to all view port listeners registered, that the projection of
1072
     * this view port has changed.
1073
     * </p>
1074
     * 
1075
     * @param projection
1076
     *            the new projection
1077
     * 
1078
     * @see #setProjection(IProjection)
1079
     * @see ProjectionEvent
1080
     * @see ViewPortListener
1081
     */
1082
    private void callProjectionChanged(IProjection projection) {
1083
        ProjectionEvent ev = ProjectionEvent.createProjectionEvent(projection);
1084

    
1085
        for (int i = 0; i < listeners.size(); i++) {
1086
            ViewPortListener listener = (ViewPortListener) listeners.get(i);
1087
            listener.projectionChanged(ev);
1088
        }
1089
    }
1090

    
1091
    /**
1092
     * <p>
1093
     * Calculates the affine transformation between the {@link #extent extent}
1094
     * in <i>map 2D coordinates</i> to the image area in the screen, in
1095
     * <i>screen 2D coordinates</i> (pixels).
1096
     * </p>
1097
     * 
1098
     * <p>
1099
     * This process recalculates some parameters of this view port:<br>
1100
     * 
1101
     * <ul>
1102
     * <li>The new {@link #scale scale} .
1103
     * <li>The new {@link #adjustedExtent adjustedExtent} .
1104
     * <li>The new {@link #trans trans} .
1105
     * <li>The new real world coordinates equivalent to 1 pixel (
1106
     * {@link #dist1pixel dist1pixel}) .
1107
     * <li>The new real world coordinates equivalent to 3 pixels (
1108
     * {@link #dist3pixel dist3pixel}) .
1109
     * </ul>
1110
     * </p>
1111
     * 
1112
     * @see #getAffineTransform()
1113
     * @see #setAffineTransform(AffineTransform)
1114
     * @see #refreshExtent()
1115
     * @see #setEnvelope(Envelope)
1116
     * @see #setImageSize(Dimension)
1117
     * @see #setPreviousExtent()
1118
     * @see #createFromXML(XMLEntity)
1119
     * @see AffineTransform
1120
     */
1121
    private void calculateAffineTransform() {
1122
        if ((imageSize == null) || (extent == null) || (imageSize.width <= 0)
1123
            || (imageSize.height <= 0)) {
1124
            return;
1125
        }
1126

    
1127
        AffineTransform escalado = new AffineTransform();
1128
        AffineTransform translacion = new AffineTransform();
1129

    
1130
        double escalaX;
1131
        double escalaY;
1132

    
1133
        escalaX = imageSize.width / extent.getWidth();
1134
        escalaY = imageSize.height / extent.getHeight();
1135

    
1136
        double xCenter = extent.getCenterX();
1137
        double yCenter = extent.getCenterY();
1138
        double newHeight;
1139
        double newWidth;
1140

    
1141
        adjustedExtent = new Rectangle2D.Double();
1142

    
1143
        if (adjustableExtent) {
1144
            if (escalaX < escalaY) {
1145
                scale = escalaX;
1146
                newHeight = imageSize.height / scale;
1147
                adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
1148
                    yCenter - (newHeight / 2.0),
1149
                    extent.getWidth(),
1150
                    newHeight);
1151
            } else {
1152
                scale = escalaY;
1153
                newWidth = imageSize.width / scale;
1154
                adjustedExtent.setRect(xCenter - (newWidth / 2.0), yCenter
1155
                    - (extent.getHeight() / 2.0), newWidth, extent.getHeight());
1156
            }
1157
            escalado.setToScale(scale, -scale);
1158
        } else { // adjusted is same as extent
1159
            scale = escalaX;
1160
            adjustedExtent.setFrame(extent);
1161
            escalado.setToScale(escalaX, -escalaY);
1162
        }
1163
        Envelope env = getAdjustedExtent();
1164
        if (env == null) {
1165
            return;
1166
        }
1167
        translacion.setToTranslation(-env.getMinimum(0), -env.getMinimum(1)
1168
            - getAdjustedExtent().getLength(1));
1169

    
1170
        AffineTransform offsetTrans = new AffineTransform();
1171
        offsetTrans.setToTranslation(offset.getX(), offset.getY());
1172

    
1173
        trans.setToIdentity();
1174
        trans.concatenate(offsetTrans);
1175
        trans.concatenate(escalado);
1176

    
1177
        trans.concatenate(translacion);
1178

    
1179
        // Calculamos las distancias de 1 pixel y 3 pixel con esa
1180
        // transformaci�n
1181
        // de coordenadas, de forma que est�n precalculadas para cuando las
1182
        // necesitemos
1183
        AffineTransform at;
1184

    
1185
        try {
1186
            at = trans.createInverse();
1187

    
1188
            Point2D pPixel = new Point2D.Float(1, 1);
1189

    
1190
            Point2D.Float pProv = new Point2D.Float();
1191
            at.deltaTransform(pPixel, pProv);
1192

    
1193
            dist1pixel = pProv.x;
1194
            dist3pixel = 3 * pProv.x;
1195
        } catch (NoninvertibleTransformException e) {
1196
            System.err.println("transformada afin = " + trans.toString());
1197
            System.err.println("extent = " + extent.toString() + " imageSize= "
1198
                + imageSize.toString());
1199
            throw new RuntimeException("Non invertible transform Exception", e);
1200
        }
1201
    }
1202

    
1203
    /**
1204
     * <p>
1205
     * Sets the offset.
1206
     * </p>
1207
     * <p>
1208
     * The offset is the position where start drawing the map.
1209
     * </p>
1210
     * 
1211
     * @param p
1212
     *            2D point that represents the offset in pixels
1213
     * 
1214
     * @see #getOffset()
1215
     */
1216
    public void setOffset(Point2D p) {
1217
        if (!offset.equals(p)) {
1218
            this.updateDrawVersion();
1219
            offset = p;
1220
        }
1221
    }
1222

    
1223
    /**
1224
     * <p>
1225
     * Gets the offset.
1226
     * </p>
1227
     * <p>
1228
     * The offset is the position where start drawing the map.
1229
     * </p>
1230
     * 
1231
     * @return 2D point that represents the offset in pixels
1232
     * 
1233
     * @see #setOffset(Point2D)
1234
     */
1235
    public Point2D getOffset() {
1236
        return offset;
1237
    }
1238

    
1239
    /**
1240
     * <p>
1241
     * Sets the background color.
1242
     * </p>
1243
     * 
1244
     * @param c
1245
     *            the new background color
1246
     * 
1247
     * @see #getBackColor()
1248
     */
1249
    public void setBackColor(Color c) {
1250
        if (!c.equals(this.backColor)) {
1251
            this.updateDrawVersion();
1252
            backColor = c;
1253
            callColorChanged(backColor);
1254
        }
1255
    }
1256

    
1257
    /**
1258
     * <p>
1259
     * Gets the background color.
1260
     * </p>
1261
     * 
1262
     * @return the background color of the view
1263
     * 
1264
     * @see #setBackColor(Color)
1265
     */
1266
    public Color getBackColor() {
1267
        return backColor;
1268
    }
1269

    
1270
    /**
1271
     * <p>
1272
     * Returns the extent currently covered by the view adjusted (scaled) to the
1273
     * image size aspect.
1274
     * </p>
1275
     * 
1276
     * @return extent of the view adjusted to the image size aspect
1277
     * 
1278
     * @see #setAdjustable(boolean)
1279
     * @deprecated use {@link ViewPort#getAdjustedEnvelope()} instead
1280
     */
1281
    public Envelope getAdjustedExtent() {
1282
        return getAdjustedEnvelope();
1283
    }
1284

    
1285
    /**
1286
     * <p>
1287
     * Returns the envelope currently covered by the view adjusted (scaled) to
1288
     * the image size aspect.
1289
     * </p>
1290
     * 
1291
     * @return envelope of the view adjusted to the image size aspect
1292
     * 
1293
     * @see #setAdjustable(boolean)
1294
     */
1295
    public Envelope getAdjustedEnvelope() {
1296
        if (cliprect != null) {
1297
            Rectangle2D r = adjustedExtent.createIntersection(cliprect);
1298
            try {
1299
                return geomManager.createEnvelope(r.getX(),
1300
                    r.getY(),
1301
                    r.getMaxX(),
1302
                    r.getMaxY(),
1303
                    SUBTYPES.GEOM2D);
1304
            } catch (CreateEnvelopeException e) {
1305
                e.printStackTrace();
1306
                logger.error("Error adjusting the extent", e);
1307
            }
1308
        }
1309
        if (adjustedExtent != null) {
1310
            try {
1311
                return geomManager.createEnvelope(adjustedExtent.getX(),
1312
                    adjustedExtent.getY(),
1313
                    adjustedExtent.getMaxX(),
1314
                    adjustedExtent.getMaxY(),
1315
                    SUBTYPES.GEOM2D);
1316
            } catch (CreateEnvelopeException e) {
1317
                e.printStackTrace();
1318
                logger.error("Error adjusting the extent", e);
1319
            }
1320
        }
1321
        return null;
1322
    }
1323

    
1324
    /**
1325
     * <p>
1326
     * Returns the measurement unit of this view port used for measuring
1327
     * distances and displaying information.
1328
     * </p>
1329
     * 
1330
     * @return the measurement unit of this view used for measuring distances
1331
     *         and displaying information
1332
     * 
1333
     * @see #setDistanceUnits(int)
1334
     */
1335
    public int getDistanceUnits() {
1336
        return distanceUnits;
1337
    }
1338

    
1339
    /**
1340
     * <p>
1341
     * Returns the measurement unit of this view port used for measuring areas
1342
     * and displaying information.
1343
     * </p>
1344
     * 
1345
     * @return the measurement unit of this view used for measuring areas and
1346
     *         displaying information
1347
     * 
1348
     * @see #setDistanceUnits(int)
1349
     */
1350
    public int getDistanceArea() {
1351
        return distanceArea;
1352
    }
1353

    
1354
    /**
1355
     * <p>
1356
     * Sets the measurement unit of this view port used for measuring distances
1357
     * and displaying information.
1358
     * </p>
1359
     * 
1360
     * @param distanceUnits
1361
     *            the measurement unit of this view used for measuring distances
1362
     *            and displaying information
1363
     * 
1364
     * @see #getDistanceUnits()
1365
     */
1366
    public void setDistanceUnits(int distanceUnits) {
1367
        this.distanceUnits = distanceUnits;
1368
    }
1369

    
1370
    /**
1371
     * <p>
1372
     * Sets the measurement unit of this view port used for measuring areas and
1373
     * displaying information.
1374
     * </p>
1375
     * 
1376
     * @param distanceUnits
1377
     *            the measurement unit of this view used for measuring areas and
1378
     *            displaying information
1379
     * 
1380
     * @see #getDistanceUnits()
1381
     */
1382
    public void setDistanceArea(int distanceArea) {
1383
        this.distanceArea = distanceArea;
1384
    }
1385

    
1386
    /**
1387
     * <p>
1388
     * Gets the measurement unit used by this view port for the map.
1389
     * </p>
1390
     * 
1391
     * @return Returns the current map measure unit
1392
     * 
1393
     * @see #setMapUnits(int)
1394
     */
1395
    public int getMapUnits() {
1396
        return mapUnits;
1397
    }
1398

    
1399
    /**
1400
     * <p>
1401
     * Sets the measurement unit used by this view port for the map.
1402
     * </p>
1403
     * 
1404
     * @param mapUnits
1405
     *            the new map measure unit
1406
     * 
1407
     * @see #getMapUnits()
1408
     */
1409
    public void setMapUnits(int mapUnits) {
1410
        this.mapUnits = mapUnits;
1411
    }
1412

    
1413
    /**
1414
     * <p>
1415
     * Gets the width in <i>screen coordinates</i> of the rectangle where the
1416
     * image is displayed.
1417
     * </p>
1418
     * <p>
1419
     * Used by {@linkplain #calculateAffineTransform()} to calculate:<br>
1420
     * 
1421
     * <ul>
1422
     * <li>The new {@link #scale scale} .
1423
     * <li>The new {@link #adjustedExtent adjustableExtent} .
1424
     * <li>The new {@link #trans trans} .
1425
     * <li>The new real world coordinates equivalent to 1 pixel (
1426
     * {@link #dist1pixel dist1pixel}) .
1427
     * <li>The new real world coordinates equivalent to 3 pixels (
1428
     * {@link #dist3pixel dist3pixel}) .
1429
     * </ul>
1430
     * </p>
1431
     * 
1432
     * @see #getImageHeight()
1433
     * @see #getImageSize()
1434
     * @see #setImageSize(Dimension)
1435
     */
1436
    public int getImageWidth() {
1437
        return imageSize.width;
1438
    }
1439

    
1440
    /**
1441
     * <p>
1442
     * Gets the height in <i>screen coordinates</i> of the rectangle where the
1443
     * image is displayed.
1444
     * </p>
1445
     * <p>
1446
     * Used by {@linkplain #calculateAffineTransform()} to calculate:<br>
1447
     * 
1448
     * <ul>
1449
     * <li>The new {@link #scale scale} .
1450
     * <li>The new {@link #adjustedExtent adjustableExtent} .
1451
     * <li>The new {@link #trans trans} .
1452
     * <li>The new real world coordinates equivalent to 1 pixel (
1453
     * {@link #dist1pixel dist1pixel}) .
1454
     * <li>The new real world coordinates equivalent to 3 pixels (
1455
     * {@link #dist3pixel dist3pixel}) .
1456
     * </ul>
1457
     * </p>
1458
     * 
1459
     * @see #getImageWidth()
1460
     * @see #getImageSize()
1461
     * @see #setImageSize(Dimension)
1462
     */
1463
    public int getImageHeight() {
1464
        return imageSize.height;
1465
    }
1466

    
1467
    /**
1468
     * <p>
1469
     * Gets the distance in <i>world coordinates</i> equivalent to 1 pixel in
1470
     * the view with the current extent.
1471
     * </p>
1472
     * 
1473
     * @return the distance
1474
     * 
1475
     * @see #setDist1pixel(double)
1476
     */
1477
    public double getDist1pixel() {
1478
        return dist1pixel;
1479
    }
1480

    
1481
    /**
1482
     * <p>
1483
     * Sets the distance in <i>world coordinates</i> equivalent to 1 pixel in
1484
     * the view with the current extent.
1485
     * </p>
1486
     * 
1487
     * @param dist1pixel
1488
     *            the distance
1489
     * 
1490
     * @see #getDist1pixel()
1491
     */
1492
    public void setDist1pixel(double dist1pixel) {
1493
        if (dist1pixel == this.dist1pixel) {
1494
            return;
1495
        }
1496
        this.updateDrawVersion();
1497
        this.dist1pixel = dist1pixel;
1498
    }
1499

    
1500
    /**
1501
     * <p>
1502
     * Gets the distance in <i>world coordinates</i> equivalent to 3 pixels in
1503
     * the view with the current extent.
1504
     * </p>
1505
     * 
1506
     * @return the distance
1507
     * 
1508
     * @see #setDist3pixel(double)
1509
     */
1510
    public double getDist3pixel() {
1511
        return dist3pixel;
1512
    }
1513

    
1514
    /**
1515
     * <p>
1516
     * Sets the distance in <i>world coordinates</i> equivalent to 3 pixels in
1517
     * the view with the current extent.
1518
     * </p>
1519
     * 
1520
     * @param dist3pixel
1521
     *            the distance
1522
     * 
1523
     * @see #getDist3pixel()
1524
     */
1525
    public void setDist3pixel(double dist3pixel) {
1526
        if (this.dist3pixel == dist3pixel) {
1527
            return;
1528
        }
1529
        this.updateDrawVersion();
1530
        this.dist3pixel = dist3pixel;
1531
    }
1532

    
1533
    /**
1534
     * <p>
1535
     * Returns the last previous extents of this view port.
1536
     * </p>
1537
     * 
1538
     * @return the last previous extents of this view port
1539
     * 
1540
     * @see #setPreviousExtent()
1541
     * @deprecated use {@link ViewPort#getEnvelopes()}
1542
     */
1543
    public ExtentHistory getExtents() {
1544
        return getEnvelopes();
1545
    }
1546

    
1547
    /**
1548
     * <p>
1549
     * Returns the last previous extents of this view port.
1550
     * </p>
1551
     * 
1552
     * @return the last previous extents of this view port
1553
     * 
1554
     * @see #setPreviousExtent()
1555
     */
1556
    public ExtentHistory getEnvelopes() {
1557
        return extents;
1558
    }
1559

    
1560
    /**
1561
     * <p>
1562
     * Gets the projection used in this view port.
1563
     * </p>
1564
     * 
1565
     * @return projection used in this view port
1566
     * 
1567
     * @see #setProjection(IProjection)
1568
     */
1569
    public IProjection getProjection() {
1570
        return proj;
1571
    }
1572

    
1573
    /**
1574
     * <p>
1575
     * Sets the projection to this view port.
1576
     * </p>
1577
     * 
1578
     * @param proj
1579
     *            the new projection
1580
     * 
1581
     * @see #getProjection()
1582
     */
1583
    public void setProjection(IProjection proj) {
1584
        if (this.proj == null || !this.proj.getAbrev().equals(proj.getAbrev())) {
1585
            this.updateDrawVersion();
1586
            this.proj = proj;
1587
            callProjectionChanged(proj);
1588
        }
1589
    }
1590

    
1591
    // -----------------------------------------------------------------------------------------------------------
1592
    // NOTA PARA DESARROLLADORES SOBRE EL M�TODO
1593
    // "public void setAffineTransform(AffineTransform at)"
1594
    // ==============================================================================================
1595
    // Only used for print, should be removed, redefining the {@link
1596
    // RasterAdapter RasterAdapter} interface,
1597
    // allowing it to receive a {@link ViewPortData ViewPortData} .
1598
    // -----------------------------------------------------------------------------------------------------------
1599

    
1600
    /**
1601
     * <p>
1602
     * Sets only the affine transform to this view port, without updating
1603
     * dependent attributes.
1604
     * </p>
1605
     * <p>
1606
     * <b><i>This method could be problematic!</i></b>
1607
     * </p>
1608
     * 
1609
     * @param at
1610
     *            the affine transform to set
1611
     * 
1612
     * @see #getAffineTransform()
1613
     * @see #calculateAffineTransform()
1614
     */
1615
    public void setAffineTransform(AffineTransform at) {
1616
        this.trans = at;
1617
    }
1618

    
1619
    /**
1620
     * <p>
1621
     * Returns an XML entity that represents this view port instance:<br>
1622
     * <ul>
1623
     * <li>Properties:
1624
     * <ul>
1625
     * <li><i>className</i>: name of this class.
1626
     * <li>If defined, the adjusted extent:
1627
     * <ul>
1628
     * <li><i>adjustedExtentX</i>: X coordinate of the adjusted extent.
1629
     * <li><i>adjustedExtentY</i>: Y coordinate of the adjusted extent.
1630
     * <li><i>adjustedExtentW</i>: width of the adjusted extent.
1631
     * <li><i>adjustedExtentH</i>: height of the adjusted extent.
1632
     * </ul>
1633
     * <li>If defined, the background color:
1634
     * <ul>
1635
     * <li><i>backColor</i>: background color.
1636
     * </ul>
1637
     * <li>If defined, the clip:
1638
     * <ul>
1639
     * <li><i>clipX</i>: X coordinate of the clip.
1640
     * <li><i>clipY</i>: Y coordinate of clip.
1641
     * <li><i>clipW</i>: width of the clip.
1642
     * <li><i>clipH</i>: height of the clip.
1643
     * </ul>
1644
     * <li><i>dist1pixel</i>: the distance in world coordinates equivalent to 1
1645
     * pixel in the view.
1646
     * <li><i>dist3pixel</i>: the distance in world coordinates equivalent to 3
1647
     * pixels in the view.
1648
     * <li><i>distanceUnits</i>: the distance measurement unit.
1649
     * <li>If defined, the extent:
1650
     * <ul>
1651
     * <li><i>extentX</i>: X coordinate of the extent.
1652
     * <li><i>extentY</i>: Y coordinate of the extent.
1653
     * <li><i>extentW</i>: width of the extent.
1654
     * <li><i>extentH</i>: height of the extent.
1655
     * </ul>
1656
     * <li><i>mapUnits</i>: the map measurement unit.
1657
     * <li><i>offsetX</i>: X coordinate of the offset.
1658
     * <li><i>offsetY</i>: Y coordinate of the offset.
1659
     * <li>If defined, the projection:
1660
     * <ul>
1661
     * <li>If its defined, the projection:
1662
     * <ul>
1663
     * <li><i>proj</i>: the projection.</li>
1664
     * </ul>
1665
     * </ul>
1666
     * <li><i>scale</i>: ratio between the size of <code>imageSize</code> and
1667
     * <code>extent</code>.
1668
     * </ul>
1669
     * <li>Child branches:
1670
     * <ul>
1671
     * <li>XML entity of the internal {@link ExtentHistory ExtentHistory} .
1672
     * </ul>
1673
     * </ul>
1674
     * 
1675
     * @return the XML entity
1676
     * 
1677
     * @see #createFromXML(XMLEntity)
1678
     */
1679
    public void saveToState(PersistentState state) throws PersistenceException {
1680

    
1681
        state.set(FIELD_ADJUSTED_EXTENT, adjustedExtent);
1682
        state.set(FIELD_BACK_COLOR, backColor);
1683
        state.set(FIELD_CLIP, cliprect);
1684
        state.set(FIELD_DIST1PIXEL, dist1pixel);
1685
        state.set(FIELD_DIST3PIXEL, dist3pixel);
1686
        state.set(FIELD_DISTANCE_UNITS, distanceUnits);
1687
        state.set(FIELD_DISTANCE_AREA, distanceArea);
1688

    
1689
        state.set(FIELD_EXTENT, extent);
1690
        state.set(FIELD_EXTENTS, extents);
1691

    
1692
        state.set(FIELD_MAP_UNITS, mapUnits);
1693
        state.set(FIELD_OFFSET, offset);
1694

    
1695
        state.set(FIELD_PROJ, proj);
1696

    
1697
        state.set(FIELD_IMAGE_SIZE, imageSize);
1698
    }
1699

    
1700
    public void loadFromState(PersistentState state) throws PersistenceException {
1701

    
1702
        adjustedExtent = (Rectangle2D) state.get(FIELD_ADJUSTED_EXTENT);
1703
        backColor = (Color) state.get(FIELD_BACK_COLOR);
1704
        cliprect = (Rectangle2D) state.get(FIELD_CLIP);
1705
        dist1pixel = state.getDouble(FIELD_DIST1PIXEL);
1706
        dist3pixel = state.getDouble(FIELD_DIST3PIXEL);
1707
        distanceUnits = state.getInt(FIELD_DISTANCE_UNITS);
1708
        extents = (ExtentHistory) state.get(FIELD_EXTENTS);
1709
        extent = (Rectangle2D) state.get(FIELD_EXTENT);
1710
        mapUnits = state.getInt(FIELD_MAP_UNITS);
1711
        offset = (Point2D) state.get(FIELD_OFFSET);
1712
        proj = (IProjection) state.get(FIELD_PROJ);
1713
        imageSize = (Dimension) state.get(FIELD_IMAGE_SIZE);
1714
        distanceArea = state.getInt(FIELD_DISTANCE_AREA);
1715

    
1716
        refreshExtent();
1717
    }
1718

    
1719
        public static class RegisterPersistence implements Callable {
1720

    
1721
                public Object call() throws Exception {
1722
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1723
                        if( manager.getDefinition("ViewPort")==null ) {
1724
                        DynStruct definition =
1725
                            manager.addDefinition(ViewPort.class,
1726
                                "ViewPort",
1727
                                "ViewPort Persistence definition",
1728
                                null,
1729
                                null);
1730

    
1731
                        definition.addDynFieldObject(FIELD_ADJUSTED_EXTENT)
1732
                            .setClassOfValue(Rectangle2D.class)
1733
                            .setMandatory(false);
1734

    
1735
                        definition.addDynFieldObject(FIELD_BACK_COLOR)
1736
                            .setClassOfValue(Color.class)
1737
                            .setMandatory(false);
1738

    
1739
                        definition.addDynFieldObject(FIELD_CLIP)
1740
                            .setClassOfValue(Rectangle2D.class)
1741
                            .setMandatory(false);
1742

    
1743
                        definition.addDynFieldDouble(FIELD_DIST1PIXEL).setMandatory(true);
1744

    
1745
                        definition.addDynFieldDouble(FIELD_DIST3PIXEL).setMandatory(true);
1746

    
1747
                        definition.addDynFieldInt(FIELD_DISTANCE_UNITS).setMandatory(true);
1748

    
1749
                        definition.addDynFieldInt(FIELD_DISTANCE_AREA).setMandatory(false);
1750

    
1751
                        definition.addDynFieldObject(FIELD_EXTENT)
1752
                            .setClassOfValue(Rectangle2D.class)
1753
                            .setMandatory(false);
1754

    
1755
                        definition.addDynFieldObject(FIELD_EXTENTS)
1756
                            .setClassOfValue(ExtentHistory.class)
1757
                            .setMandatory(true);
1758

    
1759
                        definition.addDynFieldInt(FIELD_MAP_UNITS).setMandatory(true);
1760

    
1761
                        definition.addDynFieldObject(FIELD_OFFSET)
1762
                            .setClassOfValue(Point2D.class)
1763
                            .setMandatory(false);
1764

    
1765
                        definition.addDynFieldObject(FIELD_PROJ)
1766
                            .setClassOfValue(IProjection.class)
1767
                            .setMandatory(true);
1768

    
1769
                        definition.addDynFieldObject(FIELD_IMAGE_SIZE)
1770
                            .setClassOfValue(Dimension.class)
1771
                            .setMandatory(false);
1772
                        }
1773
                        return Boolean.TRUE;
1774
                }
1775
                
1776
        }
1777

    
1778
        /**
1779
     * Clone the view port without clone the listeners nor the extent history.
1780
     * 
1781
     * @return the cloned view port
1782
     */
1783
    public Object clone() throws CloneNotSupportedException {
1784

    
1785
        ViewPort clonedViewPort = (ViewPort) super.clone();
1786
        clonedViewPort.listeners = new ArrayList();
1787
        clonedViewPort.extents = new ExtentHistory();
1788

    
1789
        if (this.adjustedExtent!=null){
1790
            clonedViewPort.adjustedExtent =
1791
                (Rectangle2D) this.adjustedExtent.clone();
1792
        }
1793

    
1794
        if (this.cliprect!=null){
1795
            clonedViewPort.cliprect = (Rectangle2D) this.cliprect.clone();
1796
        }
1797
        
1798
        if (this.extent!=null) {
1799
            clonedViewPort.extent = (Rectangle2D) this.extent.clone();
1800
        }
1801
        if (this.imageSize!=null){
1802
            clonedViewPort.imageSize = (Dimension) this.imageSize.clone();
1803
        }
1804
        
1805
        if (this.offset!=null){
1806
            clonedViewPort.offset = (Point2D) this.offset.clone();
1807
        }
1808
        if (proj!=null){
1809
            clonedViewPort.proj = (IProjection) this.proj.clone();
1810
        }
1811
        
1812
        clonedViewPort.trans = (AffineTransform) this.trans.clone();
1813

    
1814
        return clonedViewPort;
1815
    }
1816

    
1817
    /**
1818
     * <p>
1819
     * Returns a <code>String</code> representation of the main values of this
1820
     * view port: <code>{@linkplain #extent}</code>,
1821
     * <code>{@linkplain #adjustedExtent}</code>,
1822
     * <code>{@linkplain #imageSize}</code>, <code>{@linkplain #scale}</code>,
1823
     * and <code>{@linkplain #trans}</code>.
1824
     * </p>
1825
     * 
1826
     * @return a <code>string</code> representation of the main values of this
1827
     *         view port
1828
     */
1829
    public String toString() {
1830

    
1831
        String str;
1832
        str =
1833
            "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent="
1834
                + adjustedExtent + "\nimageSize=" + imageSize + "\nescale="
1835
                + scale + "\ntrans=" + trans;
1836

    
1837
        return str;
1838
    }
1839

    
1840
    /**
1841
     * <p>
1842
     * Sets the position and size of the clipping rectangle.
1843
     * </p>
1844
     * 
1845
     * @param rectView
1846
     *            the clipping rectangle to set
1847
     */
1848
    public void setClipRect(Rectangle2D rectView) {
1849
        this.updateDrawVersion();
1850
        cliprect = rectView;
1851
    }
1852

    
1853
    /**
1854
     * <p>
1855
     * Converts and returns the {@link Rectangle2D Rectangle2D}, that is in
1856
     * <i>map coordinates</i> to <i>screen coordinates</i> (pixels) using an
1857
     * <i>inverse transform</i> with the transformation affine information in
1858
     * the {@link #trans #trans} attribute.
1859
     * </p>
1860
     * 
1861
     * @param r
1862
     *            the 2D rectangle in <i>map coordinates</i>
1863
     * @return 2D rectangle equivalent in <i>screen coordinates</i> (pixels)
1864
     * 
1865
     * @see #toMapRectangle(Rectangle2D)
1866
     * @see #fromMapDistance(double)
1867
     * @see #fromMapPoint(Point2D)
1868
     */
1869
    public Rectangle2D fromMapRectangle(Rectangle2D r) {
1870
        Rectangle2D rect = new Rectangle2D.Double();
1871
        Point2D p1 = fromMapPoint((int) r.getX(), (int) r.getY());
1872
        Point2D p2 = fromMapPoint((int) r.getMaxX(), (int) r.getMaxY());
1873
        rect.setFrameFromDiagonal(p1, p2);
1874
        return rect;
1875
    }
1876

    
1877
    /**
1878
     * <p>
1879
     * Recalculates the current <code>{@linkplain #extent}</code> using an
1880
     * scale. It's necessary execute {@linkplain #refreshExtent()} after.
1881
     * </p>
1882
     * 
1883
     * @param s
1884
     *            the scale to set
1885
     * 
1886
     * @deprecated since 07/09/07, use
1887
     *             {@linkplain MapContext#setScaleView(long)}
1888
     */
1889
    public void setScale(long s) {
1890
        double x = extent.getX();
1891
        double y = extent.getY();
1892
        double escalaX = imageSize.width / extent.getWidth();
1893
        // double w = imageSize.width / s;
1894
        // double h = imageSize.height / s;
1895
        double difw = escalaX / s;
1896

    
1897
        double x1 = (-x * difw) - x + extent.getWidth() / 2;
1898
        double y1 = (-y * difw) - y + extent.getHeight() / 2;
1899
        double w1 = extent.getWidth() * difw;
1900
        double h1 = extent.getHeight() * difw;
1901
        extent.setRect(-x1, -y1, w1, h1);
1902
    }
1903

    
1904
    public long getDrawVersion() {
1905
        return this.drawVersion;
1906
    }
1907

    
1908
    protected void updateDrawVersion() {
1909
        this.drawVersion++;
1910
    }
1911

    
1912
    public Time getTime() {
1913
        return time;
1914
    }
1915
    
1916
    public void setTime(Time time) {
1917
        this.time = time;
1918
        this.updateDrawVersion();
1919
        callTimeChanged(time);
1920
    }
1921
}