Revision 43418
tags/org.gvsig.desktop-2.0.196/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/ViewPort.java | ||
---|---|---|
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.gvsig.compat.CompatLocator; |
|
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 |
* <p> |
|
69 |
* Includes an affine transformation, between the rectangular area selected of |
|
70 |
* the external map, in its own <i>map coordinates</i>, to the rectangular area |
|
71 |
* available of a view in <i>screen coordinates</i>. |
|
72 |
* </p> |
|
73 |
* <p> |
|
74 |
* Elements: |
|
75 |
* <ul> |
|
76 |
* <li><i>extent</i>: the area selected of the map, in <i>map coordinates</i>. |
|
77 |
* <li><i>imageSize</i>: width and height in pixels (<i>screen coordinates</i>) |
|
78 |
* of the area available in screen to display the area selected of the map. |
|
79 |
* <li><i>adjustedExtent</i>: the area selected must be an scale of |
|
80 |
* <i>imageSize</i>.<br> |
|
81 |
* This implies adapt the extent, preserving and centering it, and adding around |
|
82 |
* the needed area to fill all the image size. That added area will be extracted |
|
83 |
* from the original map, wherever exists, and filled with the background color |
|
84 |
* wherever not. |
|
85 |
* <li><i>scale</i>: the scale between the adjusted extent and the image size. |
|
86 |
* <li><i>backColor</i>: the default background color in the view, if there is |
|
87 |
* no map. |
|
88 |
* <li><i>trans</i>: the affine transformation. |
|
89 |
* <li><i>proj</i>: map projection used in this view. |
|
90 |
* <li><i>distanceUnits</i>: distance measurement units, of data in screen. |
|
91 |
* <li><i>mapUnits</i>: measurement units, of data in map. |
|
92 |
* <li><i>extents</i>: an {@link ExtentHistory ExtentHistory} with the last |
|
93 |
* previous extents. |
|
94 |
* <li><i>offset</i>: position in pixels of the available rectangular area, |
|
95 |
* where start drawing the map. |
|
96 |
* <li><i>dist1pixel</i>: the distance in <i>world coordinates</i> equivalent to |
|
97 |
* 1 pixel in the view with the current extent. |
|
98 |
* <li><i>dist3pixel</i>: the distance in <i>world coordinates</i> equivalent to |
|
99 |
* 3 pixels in the view with the current extent. |
|
100 |
* <li><i>listeners</i>: list with the {@link ViewPortListener ViewPortListener} |
|
101 |
* registered. |
|
102 |
* </ul> |
|
103 |
* </p> |
|
104 |
* |
|
105 |
* @author Vicente Caballero Navarro |
|
106 |
*/ |
|
107 |
public class ViewPort implements Persistent, Cloneable { |
|
108 |
|
|
109 |
private static final String FIELD_DISTANCE_AREA = "distanceArea"; |
|
110 |
|
|
111 |
private static final String FIELD_IMAGE_SIZE = "imageSize"; |
|
112 |
|
|
113 |
private static final String FIELD_PROJ = "proj"; |
|
114 |
|
|
115 |
private static final String FIELD_OFFSET = "offset"; |
|
116 |
|
|
117 |
private static final String FIELD_MAP_UNITS = "mapUnits"; |
|
118 |
|
|
119 |
private static final String FIELD_EXTENT = "extent"; |
|
120 |
|
|
121 |
private static final String FIELD_EXTENTS = "extents"; |
|
122 |
|
|
123 |
private static final String FIELD_DISTANCE_UNITS = "distanceUnits"; |
|
124 |
|
|
125 |
private static final String FIELD_DIST3PIXEL = "dist3pixel"; |
|
126 |
|
|
127 |
private static final String FIELD_DIST1PIXEL = "dist1pixel"; |
|
128 |
|
|
129 |
private static final String FIELD_CLIP = "clip"; |
|
130 |
|
|
131 |
private static final String FIELD_BACK_COLOR = "backColor"; |
|
132 |
|
|
133 |
private static final String FIELD_ADJUSTED_EXTENT = "adjustedExtent"; |
|
134 |
|
|
135 |
private static final GeometryManager geomManager = GeometryLocator |
|
136 |
.getGeometryManager(); |
|
137 |
|
|
138 |
private static final Logger logger = LoggerFactory.getLogger(ViewPort.class); |
|
139 |
|
|
140 |
/** |
|
141 |
* <p> |
|
142 |
* Area selected by user using some tool. |
|
143 |
* </p> |
|
144 |
* <p> |
|
145 |
* When the zoom changes (for instance when using the zoom in or zoom out |
|
146 |
* tools, but also zooming to a selected feature or shape) the extent that |
|
147 |
* covers that area is the value returned by this method. It is not the actual |
|
148 |
* area shown in the view because it does not care about the aspect ratio of |
|
149 |
* the available area. However, any part of the real world contained in this |
|
150 |
* extent is shown in the view. |
|
151 |
* </p> |
|
152 |
* <p> |
|
153 |
* Probably this is not what you are looking for. If you are looking for the |
|
154 |
* complete extent currently shown, you must use |
|
155 |
* {@linkplain #getAdjustedExtent()} method which returns the extent that |
|
156 |
* contains this one but regarding the current view's aspect ratio. |
|
157 |
* </p> |
|
158 |
* |
|
159 |
* @see #getExtent() |
|
160 |
* @see #setEnvelope(Envelope) |
|
161 |
*/ |
|
162 |
protected Rectangle2D extent; |
|
163 |
|
|
164 |
protected Time time; |
|
165 |
|
|
166 |
/** |
|
167 |
* <p> |
|
168 |
* Location and dimensions of the extent adjusted to the image size. |
|
169 |
* </p> |
|
170 |
* |
|
171 |
* @see #getAdjustedExtent() |
|
172 |
*/ |
|
173 |
protected Rectangle2D adjustedExtent; |
|
174 |
|
|
175 |
/** |
|
176 |
* Draw version of the context. It's used for know when de componend has |
|
177 |
* changed any visualization property |
|
178 |
* |
|
179 |
* @see getDrawVersion |
|
180 |
* @see updateDrawVersion |
|
181 |
*/ |
|
182 |
private long drawVersion = 0L; |
|
183 |
|
|
184 |
/** |
|
185 |
* <p> |
|
186 |
* History with the last extents of the view. |
|
187 |
* </p> |
|
188 |
* |
|
189 |
* @see #setPreviousExtent() |
|
190 |
* @see #getExtents() |
|
191 |
*/ |
|
192 |
protected ExtentHistory extentsHistory = new ExtentHistory(); |
|
193 |
|
|
194 |
/** |
|
195 |
* <p> |
|
196 |
* Size in <i>screen coordinates</i> of the rectangle where the image is |
|
197 |
* displayed. |
|
198 |
* </p> |
|
199 |
* <p> |
|
200 |
* Used by {@linkplain #calculateAffineTransform()} to calculate:<br> |
|
201 |
* <ul> |
|
202 |
* <li>The new {@link #scale scale} . |
|
203 |
* <li>The new {@link #adjustedExtent adjustableExtent} . |
|
204 |
* <li>The new {@link #trans trans} . |
|
205 |
* <li>The new real world coordinates equivalent to 1 pixel ( |
|
206 |
* {@link #dist1pixel dist1pixel}) . |
|
207 |
* <li>The new real world coordinates equivalent to 3 pixels ( |
|
208 |
* {@link #dist3pixel dist3pixel}) . |
|
209 |
* </ul> |
|
210 |
* </p> |
|
211 |
* |
|
212 |
* @see #getImageSize() |
|
213 |
* @see #getImageHeight() |
|
214 |
* @see #getImageWidth() |
|
215 |
* @see #setImageSize(Dimension) |
|
216 |
*/ |
|
217 |
private Dimension imageSize; |
|
218 |
|
|
219 |
/** |
|
220 |
* <p> |
|
221 |
* the affine transformation between the {@link #extent extent} in <i>map 2D |
|
222 |
* coordinates</i> to the image area in the screen, in <i>screen 2D |
|
223 |
* coordinates</i> (pixels). |
|
224 |
* </p> |
|
225 |
* |
|
226 |
* @see AffineTransform |
|
227 |
* @see #getAffineTransform() |
|
228 |
* @see #setAffineTransform(AffineTransform) |
|
229 |
* @see #calculateAffineTransform() |
|
230 |
*/ |
|
231 |
private AffineTransform trans = new AffineTransform(); |
|
232 |
|
|
233 |
/** |
|
234 |
* <p> |
|
235 |
* Measurement unit used for measuring distances and displaying information. |
|
236 |
* </p> |
|
237 |
* |
|
238 |
* @see #getDistanceUnits() |
|
239 |
* @see #setDistanceUnits(int) |
|
240 |
*/ |
|
241 |
private int distanceUnits = 1; |
|
242 |
|
|
243 |
/** |
|
244 |
* <p> |
|
245 |
* Measurement unit used for measuring areas and displaying information. |
|
246 |
* </p> |
|
247 |
* |
|
248 |
* @see #getDistanceArea() |
|
249 |
* @see #setDistanceArea(int) |
|
250 |
*/ |
|
251 |
private int distanceArea = 1; |
|
252 |
|
|
253 |
/** |
|
254 |
* <p> |
|
255 |
* Measurement unit used by this view port for the map. |
|
256 |
* </p> |
|
257 |
* |
|
258 |
* @see #getMapUnits() |
|
259 |
* @see #setMapUnits(int) |
|
260 |
*/ |
|
261 |
private int mapUnits = 1; |
|
262 |
|
|
263 |
/** |
|
264 |
* <p> |
|
265 |
* Array with the {@link ViewPortListener ViewPortListener}s registered to |
|
266 |
* this view port. |
|
267 |
* </p> |
|
268 |
* |
|
269 |
* @see #addViewPortListener(ViewPortListener) |
|
270 |
* @see #removeViewPortListener(ViewPortListener) |
|
271 |
*/ |
|
272 |
private ArrayList listeners = new ArrayList(); |
|
273 |
|
|
274 |
/** |
|
275 |
* <p> |
|
276 |
* The offset is the position where start drawing the map. |
|
277 |
* </p> |
|
278 |
* <p> |
|
279 |
* The offset of a <a href="http://www.gvsig.gva.es/">gvSIG</a>'s <i>View</i> |
|
280 |
* is always (0, 0) because the drawing area fits with the full window area. |
|
281 |
* But in a <a href="http://www.gvsig.gva.es/">gvSIG</a>'s <i>Layout</i> it's |
|
282 |
* up to the place where the <code>FFrameView</code> is located. |
|
283 |
* </p> |
|
284 |
* |
|
285 |
* @see #getOffset() |
|
286 |
* @see #setOffset(Point2D) |
|
287 |
*/ |
|
288 |
private Point2D offset = new Point2D.Double(0, 0); |
|
289 |
|
|
290 |
/** |
|
291 |
* <p> |
|
292 |
* Clipping area. |
|
293 |
* </p> |
|
294 |
*/ |
|
295 |
// private Rectangle2D clip; |
|
296 |
|
|
297 |
/** |
|
298 |
* <p> |
|
299 |
* Background color of this view. |
|
300 |
* </p> |
|
301 |
* |
|
302 |
* @see #getBackColor() |
|
303 |
* @see #setBackColor(Color) |
|
304 |
*/ |
|
305 |
private Color backColor = null; // Color.WHITE; |
|
306 |
|
|
307 |
/** |
|
308 |
* <p> |
|
309 |
* Information about the map projection used in this view. |
|
310 |
* </p> |
|
311 |
* |
|
312 |
* @see #getProjection() |
|
313 |
* @see #setProjection(IProjection) |
|
314 |
*/ |
|
315 |
private IProjection proj; |
|
316 |
|
|
317 |
/** |
|
318 |
* <p> |
|
319 |
* Represents the distance in <i>world coordinates</i> equivalent to 1 pixel |
|
320 |
* in the view with the current extent. |
|
321 |
* </p> |
|
322 |
* |
|
323 |
* @see #getDist1pixel() |
|
324 |
* @see #setDist1pixel(double) |
|
325 |
*/ |
|
326 |
private double dist1pixel; |
|
327 |
|
|
328 |
/** |
|
329 |
* <p> |
|
330 |
* Represents the distance in <i>world coordinates</i> equivalent to 3 pixels |
|
331 |
* in the view with the current extent. |
|
332 |
* </p> |
|
333 |
* |
|
334 |
* @see #getDist3pixel() |
|
335 |
* @see #setDist3pixel(double) |
|
336 |
*/ |
|
337 |
private double dist3pixel; |
|
338 |
|
|
339 |
/** |
|
340 |
* <p> |
|
341 |
* Ratio between the size of <code>imageSize</code> and <code>extent</code>: <br> |
|
342 |
* <i> |
|
343 |
* |
|
344 |
* <pre> |
|
345 |
* min{(imageSize.getHeight()/extent.getHeight(), imageSize.getWidth()/extent.getWidth())} |
|
346 |
* </pre> |
|
347 |
* |
|
348 |
* </i> |
|
349 |
* </p> |
|
350 |
*/ |
|
351 |
private double scale; |
|
352 |
|
|
353 |
/** |
|
354 |
* <p> |
|
355 |
* Clipping area. |
|
356 |
* </p> |
|
357 |
* |
|
358 |
* @see #setClipRect(Rectangle2D) |
|
359 |
*/ |
|
360 |
private Rectangle2D cliprect; |
|
361 |
|
|
362 |
/** |
|
363 |
* <p> |
|
364 |
* Enables or disables the <i>"adjustable extent"</i> mode. |
|
365 |
* </p> |
|
366 |
* <p> |
|
367 |
* When calculates the affine transform, if |
|
368 |
* <ul> |
|
369 |
* <li><i>enabled</i>: the new <code>adjustedExtent</code> will have the (X, |
|
370 |
* Y) coordinates of the <code>extent</code> and an area that will be an scale |
|
371 |
* of the image size. That area will have different height or width (not both) |
|
372 |
* of the extent according the least ratio (height or width) in |
|
373 |
* |
|
374 |
* <pre> |
|
375 |
* image.size/extent.size" |
|
376 |
* </pre>. |
|
377 |
* <li><i>disabled</i>: the new <code>adjustedExtent</code> will be like |
|
378 |
* <code>extent</code>. |
|
379 |
* </ul> |
|
380 |
* </p> |
|
381 |
* |
|
382 |
* @see #setAdjustable(boolean) |
|
383 |
*/ |
|
384 |
private boolean adjustableExtent = true; |
|
385 |
|
|
386 |
/** |
|
387 |
* <p> |
|
388 |
* ViewPort resolution in <i>dots-per-inch</i>. Useful to calculate the |
|
389 |
* geographic scale of the view. |
|
390 |
* </p> |
|
391 |
* |
|
392 |
* @see Toolkit#getScreenResolution() |
|
393 |
* @see MapContext#getScaleView() |
|
394 |
*/ |
|
395 |
private Double dpi = null; |
|
396 |
|
|
397 |
public ViewPort() { |
|
398 |
|
|
399 |
} |
|
400 |
|
|
401 |
/** |
|
402 |
* <p> |
|
403 |
* Creates a new view port with the information of the projection in |
|
404 |
* <code>proj</code> argument, and default configuration: |
|
405 |
* </p> |
|
406 |
* <p> |
|
407 |
* <ul> |
|
408 |
* <li><i><code>distanceUnits</code></i> = meters |
|
409 |
* <li><i><code>mapUnits</code></i> = meters |
|
410 |
* <li><i><code>backColor</code></i> = <i>undefined</i> |
|
411 |
* <li><i><code>offset</code></i> = <code>new Point2D.Double(0, 0);</code> |
|
412 |
* </ul> |
|
413 |
* </p> |
|
414 |
* |
|
415 |
* @param proj information of the projection for this view port |
|
416 |
*/ |
|
417 |
public ViewPort(IProjection proj) { |
|
418 |
// Por defecto |
|
419 |
this.proj = proj; |
|
420 |
} |
|
421 |
|
|
422 |
/** |
|
423 |
* <p> |
|
424 |
* Changes the status of the <i>"adjustable extent"</i> option to enabled or |
|
425 |
* disabled. |
|
426 |
* </p> |
|
427 |
* <p> |
|
428 |
* If view port isn't adjustable, won't bear in mind the aspect ratio of the |
|
429 |
* available rectangular area to calculate the affine transform from the |
|
430 |
* original map in real coordinates. (Won't scale the image to adapt it to the |
|
431 |
* available rectangular area). |
|
432 |
* </p> |
|
433 |
* |
|
434 |
* @param boolean the boolean to be set |
|
435 |
*/ |
|
436 |
public void setAdjustable(boolean adjustable) { |
|
437 |
if (adjustable == adjustableExtent) { |
|
438 |
return; |
|
439 |
} |
|
440 |
adjustableExtent = adjustable; |
|
441 |
this.updateDrawVersion(); |
|
442 |
} |
|
443 |
|
|
444 |
/** |
|
445 |
* <p> |
|
446 |
* Appends the specified {@link ViewPortListener ViewPortListener} listener if |
|
447 |
* weren't. |
|
448 |
* </p> |
|
449 |
* |
|
450 |
* @param arg0 the listener to add |
|
451 |
* @return <code>true</code> if has been added successfully |
|
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 the listener to remove |
|
468 |
* @return <code>true</code> if the contained the specified listener. |
|
469 |
* @see #addViewPortListener(ViewPortListener) |
|
470 |
*/ |
|
471 |
public boolean removeViewPortListener(ViewPortListener arg0) { |
|
472 |
return listeners.remove(arg0); |
|
473 |
} |
|
474 |
|
|
475 |
/** |
|
476 |
* <p> |
|
477 |
* Converts and returns the distance <code>d</code>, that is in <i>map |
|
478 |
* coordinates</i> to <i>screen coordinates</i> using a <i>delta transform</i> |
|
479 |
* with the transformation affine information in the {@link #trans #trans} |
|
480 |
* attribute. |
|
481 |
* </p> |
|
482 |
* |
|
483 |
* @param d distance in <i>map coordinates</i> |
|
484 |
* @return distance equivalent in <i>screen coordinates</i> |
|
485 |
* @see #toMapDistance(int) |
|
486 |
* @see AffineTransform#deltaTransform(Point2D, Point2D)S |
|
487 |
*/ |
|
488 |
public int fromMapDistance(double d) { |
|
489 |
Point2D.Double pWorld = new Point2D.Double(1, 1); |
|
490 |
Point2D.Double pScreen = new Point2D.Double(); |
|
491 |
|
|
492 |
try { |
|
493 |
trans.deltaTransform(pWorld, pScreen); |
|
494 |
} |
|
495 |
catch (Exception e) { |
|
496 |
System.err.print(e.getMessage()); |
|
497 |
} |
|
498 |
|
|
499 |
return (int) (d * pScreen.x); |
|
500 |
} |
|
501 |
|
|
502 |
/** |
|
503 |
* <p> |
|
504 |
* Converts and returns the 2D point <code>(x,y)</code>, that is in <i>map |
|
505 |
* coordinates</i> to <i>screen coordinates</i> (pixels) using the affine |
|
506 |
* transformation in the {@link #trans #trans} attribute. |
|
507 |
* </p> |
|
508 |
* |
|
509 |
* @param x the <code>x</code> <i>map coordinate</i> of a 2D point |
|
510 |
* @param y the <code>y</code> <i>map coordinate</i> of a 2D point |
|
511 |
* @return 2D point equivalent in <i>screen coordinates</i> (pixels) |
|
512 |
* @see #fromMapPoint(Point2D) |
|
513 |
* @see AffineTransform#transform(Point2D, Point2D) |
|
514 |
*/ |
|
515 |
public Point2D fromMapPoint(double x, double y) { |
|
516 |
Point2D.Double pWorld = new Point2D.Double(x, y); |
|
517 |
Point2D.Double pScreen = new Point2D.Double(); |
|
518 |
|
|
519 |
try { |
|
520 |
trans.transform(pWorld, pScreen); |
|
521 |
} |
|
522 |
catch (Exception e) { |
|
523 |
System.err.print(e.getMessage()); |
|
524 |
} |
|
525 |
|
|
526 |
return pScreen; |
|
527 |
} |
|
528 |
|
|
529 |
/** |
|
530 |
* <p> |
|
531 |
* Converts and returns the 2D point argument, that is in <i>map |
|
532 |
* coordinates</i> to <i>screen coordinates</i> (pixels) using the affine |
|
533 |
* transformation in the {@link #trans #trans} attribute. |
|
534 |
* </p> |
|
535 |
* |
|
536 |
* @param point the 2D point in <i>map coordinates</i> |
|
537 |
* @return 2D point equivalent in <i>screen coordinates</i> (pixels) |
|
538 |
* @see #toMapPoint(Point2D) |
|
539 |
* @see #fromMapPoint(double, double) |
|
540 |
*/ |
|
541 |
public Point2D fromMapPoint(Point2D point) { |
|
542 |
return fromMapPoint(point.getX(), point.getY()); |
|
543 |
} |
|
544 |
|
|
545 |
/** |
|
546 |
* <p> |
|
547 |
* Converts and returns the 2D point <code>(x,y)</code>, that is in <i>screen |
|
548 |
* coordinates</i> (pixels) to <i>map coordinates</i> using the affine |
|
549 |
* transformation in the {@link #trans #trans} attribute. |
|
550 |
* </p> |
|
551 |
* |
|
552 |
* @param x the <code>x</code> <i>screen coordinate</i> of a 2D point |
|
553 |
* @param y the <code>y</code> <i>screen coordinate</i> of a 2D point |
|
554 |
* @return 2D point equivalent in <i>map coordinates</i> |
|
555 |
* @see #toMapPoint(Point2D) |
|
556 |
* @see #fromMapPoint(double, double) |
|
557 |
* @deprecated use {@link #convertToMapPoint(int, int)} |
|
558 |
*/ |
|
559 |
public Point2D toMapPoint(int x, int y) { |
|
560 |
Point2D pScreen = new Point2D.Double(x, y); |
|
561 |
|
|
562 |
return toMapPoint(pScreen); |
|
563 |
} |
|
564 |
|
|
565 |
/** |
|
566 |
* <p> |
|
567 |
* Converts and returns the {@link Rectangle2D Rectangle2D}, that is in |
|
568 |
* <i>screen coordinates</i> (pixels) to <i>map coordinates</i> using |
|
569 |
* {@linkplain #toMapDistance(int)}, and {@linkplain #toMapPoint(int, int)}. |
|
570 |
* </p> |
|
571 |
* |
|
572 |
* @param r the 2D rectangle in <i>screen coordinates</i> (pixels) |
|
573 |
* @return 2D rectangle equivalent in <i>map coordinates</i> |
|
574 |
* @see #fromMapRectangle(Rectangle2D) |
|
575 |
* @see #toMapDistance(int) |
|
576 |
* @see #toMapPoint(int, int) |
|
577 |
*/ |
|
578 |
public Rectangle2D toMapRectangle(Rectangle2D r) { |
|
579 |
Rectangle2D rect = new Rectangle2D.Double(); |
|
580 |
Point2D p1 = toMapPoint((int) r.getX(), (int) r.getY()); |
|
581 |
Point2D p2 = toMapPoint((int) r.getMaxX(), (int) r.getMaxY()); |
|
582 |
rect.setFrameFromDiagonal(p1, p2); |
|
583 |
return rect; |
|
584 |
} |
|
585 |
|
|
586 |
/** |
|
587 |
* <p> |
|
588 |
* Converts and returns the distance <code>d</code>, that is in <i>screen |
|
589 |
* coordinates</i> to <i>map coordinates</i> using the transformation affine |
|
590 |
* information in the {@link #trans #trans} attribute. |
|
591 |
* </p> |
|
592 |
* |
|
593 |
* @param d distance in pixels |
|
594 |
* @return distance equivalent in <i>map coordinates</i> |
|
595 |
* @see #fromMapDistance(double) |
|
596 |
* @see AffineTransform |
|
597 |
*/ |
|
598 |
public double toMapDistance(int d) { |
|
599 |
double dist = d / trans.getScaleX(); |
|
600 |
|
|
601 |
return dist; |
|
602 |
} |
|
603 |
|
|
604 |
/** |
|
605 |
* <p> |
|
606 |
* Converts and returns the 2D point argument, that is in <i>screen |
|
607 |
* coordinates</i> (pixels) to <i>map coordinates</i> using the inverse affine |
|
608 |
* transformation of the {@link #trans #trans} attribute. |
|
609 |
* </p> |
|
610 |
* |
|
611 |
* @param pScreen the 2D point in <i>screen coordinates</i> (pixels) |
|
612 |
* @return 2D point equivalent in <i>map coordinates</i> |
|
613 |
* @see #toMapPoint(int, int) |
|
614 |
* @see AffineTransform#createInverse() |
|
615 |
* @see AffineTransform#transform(Point2D, Point2D) |
|
616 |
* @deprecated use {@link #convertToMapPoint(Point2D)} |
|
617 |
*/ |
|
618 |
public Point2D toMapPoint(Point2D pScreen) { |
|
619 |
Point2D.Double pWorld = new Point2D.Double(); |
|
620 |
AffineTransform at; |
|
621 |
|
|
622 |
try { |
|
623 |
at = trans.createInverse(); |
|
624 |
at.transform(pScreen, pWorld); |
|
625 |
} |
|
626 |
catch (NoninvertibleTransformException e) { |
|
627 |
throw new RuntimeException("Non invertible transform Exception", e); |
|
628 |
} |
|
629 |
|
|
630 |
return pWorld; |
|
631 |
} |
|
632 |
|
|
633 |
public Point convertToMapPoint(Point2D pScreen) { |
|
634 |
Point2D p = toMapPoint(pScreen); |
|
635 |
try { |
|
636 |
return geomManager.createPoint(p.getX(), p.getY(), |
|
637 |
Geometry.SUBTYPES.GEOM2D); |
|
638 |
} |
|
639 |
catch (CreateGeometryException e) { |
|
640 |
// FIXME: Use a most especific exception. |
|
641 |
throw new RuntimeException(e); |
|
642 |
} |
|
643 |
} |
|
644 |
|
|
645 |
public Point convertToMapPoint(int x, int y) { |
|
646 |
Point2D pScreen = new Point2D.Double(x, y); |
|
647 |
|
|
648 |
return convertToMapPoint(pScreen); |
|
649 |
} |
|
650 |
|
|
651 |
/** |
|
652 |
* <p> |
|
653 |
* Returns the real distance (in <i>world coordinates</i>) at the graphic |
|
654 |
* layers of two 2D points (in <i>map coordinates</i>) of the plane where is |
|
655 |
* selected the <i>extent</i>. |
|
656 |
* </p> |
|
657 |
* <p> |
|
658 |
* If the projection of this view is UTM, considers the Earth curvature. |
|
659 |
* </p> |
|
660 |
* |
|
661 |
* @param pt1 a 2D point in <i>map coordinates</i> |
|
662 |
* @param pt2 another 2D point in <i>map coordinates</i> |
|
663 |
* @return the distance in meters between the two points 2D |
|
664 |
* @see GeoCalcImpl#distanceVincenty(Point2D, Point2D) |
|
665 |
*/ |
|
666 |
public double distanceWorld(Point2D pt1, Point2D pt2) { |
|
667 |
|
|
668 |
double dist = 0; |
|
669 |
if (proj.isProjected()) { |
|
670 |
dist = pt1.distance(pt2); |
|
671 |
dist = dist * MapContext.getDistanceTrans2Meter()[getMapUnits()]; |
|
672 |
} |
|
673 |
else { |
|
674 |
GeoCalc geocalc = new GeoCalc(proj); |
|
675 |
dist = geocalc.distanceVincenty(pt1, pt2); |
|
676 |
} |
|
677 |
return dist; |
|
678 |
} |
|
679 |
|
|
680 |
/** |
|
681 |
* <p> |
|
682 |
* Sets as extent and adjusted extent of this view port, the previous. |
|
683 |
* Recalculating its parameters. |
|
684 |
* </p> |
|
685 |
* |
|
686 |
* @see #getExtents() |
|
687 |
* @see #calculateAffineTransform() |
|
688 |
* @deprecated use {@link ViewPort#setPreviousEnvelope()} |
|
689 |
*/ |
|
690 |
public void setPreviousExtent() { |
|
691 |
setPreviousEnvelope(); |
|
692 |
} |
|
693 |
|
|
694 |
/** |
|
695 |
* <p> |
|
696 |
* Sets as envelope and adjusted envelope of this view port, the previous. |
|
697 |
* Recalculating its parameters. |
|
698 |
* Stores the current extent in the next extents of the history. |
|
699 |
* </p> |
|
700 |
* |
|
701 |
* @see #getExtents() |
|
702 |
* @see #calculateAffineTransform() |
|
703 |
*/ |
|
704 |
public void setPreviousEnvelope() { |
|
705 |
this.updateDrawVersion(); |
|
706 |
|
|
707 |
// extentsHistory.putNext(extent); |
|
708 |
// extent = extentsHistory.removePrev(); |
|
709 |
extent = extentsHistory.setPreviousExtent(); |
|
710 |
|
|
711 |
// Calcula la transformaci�n af�n |
|
712 |
calculateAffineTransform(); |
|
713 |
|
|
714 |
// Lanzamos los eventos de extent cambiado |
|
715 |
callExtentChanged(getAdjustedExtent()); |
|
716 |
} |
|
717 |
|
|
718 |
/** |
|
719 |
* <p> |
|
720 |
* Sets as envelope and adjusted envelope of this view port, the next. |
|
721 |
* Recalculating its parameters. |
|
722 |
* Stores the current extent in the previous extents of the history. |
|
723 |
* </p> |
|
724 |
* |
|
725 |
* @see #getExtents() |
|
726 |
* @see #calculateAffineTransform() |
|
727 |
*/ |
|
728 |
public void setNextEnvelope() { |
|
729 |
this.updateDrawVersion(); |
|
730 |
|
|
731 |
extent = extentsHistory.setNextExtent(); |
|
732 |
|
|
733 |
// Calcula la transformaci�n af�n |
|
734 |
calculateAffineTransform(); |
|
735 |
|
|
736 |
// Lanzamos los eventos de extent cambiado |
|
737 |
callExtentChanged(getAdjustedExtent()); |
|
738 |
} |
|
739 |
|
|
740 |
/** |
|
741 |
* <p> |
|
742 |
* Gets the area selected by user using some tool. |
|
743 |
* </p> |
|
744 |
* <p> |
|
745 |
* When the zoom changes (for instance using the <i>zoom in</i> or <i>zoom |
|
746 |
* out</i> tools, but also zooming to a selected feature or shape) the extent |
|
747 |
* that covers that area is the value returned by this method. It is not the |
|
748 |
* actual area shown because it doesn't care about the aspect ratio of the |
|
749 |
* image size of the view. However, any part of the real world contained in |
|
750 |
* this extent is shown in the view. |
|
751 |
* </p> |
|
752 |
* <p> |
|
753 |
* If you are looking for the complete extent currently shown, you must use |
|
754 |
* the {@linkplain #getAdjustedExtent()} method. |
|
755 |
* </p> |
|
756 |
* |
|
757 |
* @return the current extent |
|
758 |
* @see #setEnvelope(Envelope) |
|
759 |
* @see #getAdjustedExtent() |
|
760 |
* @see #setPreviousExtent() |
|
761 |
* @see #getExtents() |
|
762 |
* @deprecated use {@link ViewPort#getEnvelope()} |
|
763 |
*/ |
|
764 |
public Rectangle2D getExtent() { |
|
765 |
return extent; |
|
766 |
} |
|
767 |
|
|
768 |
/** |
|
769 |
* <p> |
|
770 |
* Gets the envelope selected by user using some tool. |
|
771 |
* </p> |
|
772 |
* <p> |
|
773 |
* When the zoom changes (for instance using the <i>zoom in</i> or <i>zoom |
|
774 |
* out</i> tools, but also zooming to a selected feature or shape) the |
|
775 |
* envelope that covers that area is the value returned by this method. It is |
|
776 |
* not the actual envelope shown because it doesn't care about the aspect |
|
777 |
* ratio of the image size of the view. However, any part of the real world |
|
778 |
* contained in this envelope is shown in the view. |
|
779 |
* </p> |
|
780 |
* <p> |
|
781 |
* If you are looking for the complete extent currently shown, you must use |
|
782 |
* the {@linkplain #getAdjustedEnvelope()} method. |
|
783 |
* </p> |
|
784 |
* |
|
785 |
* @return the current envelope |
|
786 |
* @see #setEnvelope(Envelope) |
|
787 |
* @see #getAdjustedEnvelope() |
|
788 |
* @see #setPreviousEnvelope() |
|
789 |
* @see #getEnvelopes() |
|
790 |
*/ |
|
791 |
public Envelope getEnvelope() { |
|
792 |
if (this.extent == null) { |
|
793 |
return null; |
|
794 |
} |
|
795 |
try { |
|
796 |
return geomManager.createEnvelope(extent.getMinX(), extent.getMinY(), |
|
797 |
extent.getMaxX(), extent.getMaxY(), SUBTYPES.GEOM2D); |
|
798 |
// This class has to use Envelope instead of Rectangle2D. This catch |
|
799 |
// will disappear |
|
800 |
} |
|
801 |
catch (CreateEnvelopeException e) { |
|
802 |
logger.error("Error creating the envelope"); |
|
803 |
} |
|
804 |
return null; |
|
805 |
} |
|
806 |
|
|
807 |
/** |
|
808 |
* <p> |
|
809 |
* Changes the <i>extent</i> and <i>adjusted extent</i> of this view port:<br> |
|
810 |
* <ul> |
|
811 |
* <li>Stores the previous extent. |
|
812 |
* <li>Calculates the new extent using <code>r</code>: |
|
813 |
* |
|
814 |
* <pre> |
|
815 |
* extent = new Rectangle2D.Double(r.getMinX() - 0.1, r.getMinY() - 0.1, |
|
816 |
* r.getWidth() + 0.2, r.getHeight() + 0.2); |
|
817 |
* </pre> |
|
818 |
* <li>Executes {@linkplain #calculateAffineTransform()}: getting the new |
|
819 |
* scale, adjusted extent, affine transformation between map and screen |
|
820 |
* coordinates, the real world coordinates equivalent to 1 pixel, and the real |
|
821 |
* world coordinates equivalent to 3 pixels. |
|
822 |
* <li>Notifies all {@link ViewPortListener ViewPortListener} registered that |
|
823 |
* the extent has changed. |
|
824 |
* </ul> |
|
825 |
* </p> |
|
826 |
* |
|
827 |
* @param r the new extent |
|
828 |
* @see #getExtent() |
|
829 |
* @see #getExtents() |
|
830 |
* @see #calculateAffineTransform() |
|
831 |
* @see #setPreviousExtent() |
|
832 |
* @see #clear() |
|
833 |
*/ |
|
834 |
public void setEnvelope(Envelope r) { |
|
835 |
Rectangle2D newExtent = null; |
|
836 |
// Esto comprueba que el extent no es de anchura o altura = "0" |
|
837 |
// y si es as? lo redimensiona. |
|
838 |
if (r != null) { |
|
839 |
if ((r.getMaximum(0) - r.getMinimum(0) == 0) |
|
840 |
|| (r.getMaximum(1) - r.getMinimum(1) == 0)) { |
|
841 |
newExtent = new Rectangle2D.Double(r.getMinimum(0) - 0.1, |
|
842 |
r.getMinimum(1) - 0.1, r.getMaximum(0) - r.getMinimum(0) + 0.2, |
|
843 |
r.getMaximum(1) - r.getMinimum(1) + 0.2); |
|
844 |
} |
|
845 |
else { |
|
846 |
newExtent = new Rectangle2D.Double(r.getMinimum(0), r.getMinimum(1), |
|
847 |
Math.abs(r.getMaximum(0) - r.getMinimum(0)), Math.abs(r |
|
848 |
.getMaximum(1) - r.getMinimum(1))); |
|
849 |
} |
|
850 |
} |
|
851 |
|
|
852 |
if (this.extent != null && this.extent.equals(newExtent)) { |
|
853 |
return; |
|
854 |
} |
|
855 |
|
|
856 |
this.updateDrawVersion(); |
|
857 |
this.extent = newExtent; |
|
858 |
extentsHistory.put(extent); |
|
859 |
|
|
860 |
// Calcula la transformaci�n af�n |
|
861 |
calculateAffineTransform(); |
|
862 |
|
|
863 |
|
|
864 |
// Lanzamos los eventos de extent cambiado |
|
865 |
callExtentChanged(getAdjustedExtent()); |
|
866 |
} |
|
867 |
|
|
868 |
/** |
|
869 |
* <p> |
|
870 |
* Changes the <i>extent</i> and <i>adjusted extent</i> of this view port:<br> |
|
871 |
* <ul> |
|
872 |
* <li>Executes {@linkplain #calculateAffineTransform()}: getting the new |
|
873 |
* scale, adjusted extent, affine transformation between map and screen |
|
874 |
* coordinates, the real world coordinates equivalent to 1 pixel, and the real |
|
875 |
* world coordinates equivalent to 3 pixels. |
|
876 |
* <li>Notifies to all {@link ViewPortListener ViewPortListener} registered |
|
877 |
* that the extent has changed. |
|
878 |
* </ul> |
|
879 |
* </p> |
|
880 |
* |
|
881 |
* @see #setEnvelope(Envelope) |
|
882 |
* @see #calculateAffineTransform() |
|
883 |
*/ |
|
884 |
public void refreshExtent() { |
|
885 |
//Por compatibilidad con versiones anteriores a la introducci?n de las lista de zooms siguientes |
|
886 |
if (extentsHistory.getCurrent() == null) { |
|
887 |
extentsHistory.put(extent); |
|
888 |
} else { |
|
889 |
extent = extentsHistory.getCurrent(); |
|
890 |
} |
|
891 |
|
|
892 |
// Calcula la transformaci?n af?n |
|
893 |
calculateAffineTransform(); |
|
894 |
|
|
895 |
// Lanzamos los eventos de extent cambiado |
|
896 |
callExtentChanged(getAdjustedExtent()); |
|
897 |
} |
|
898 |
|
|
899 |
/** |
|
900 |
* <p> |
|
901 |
* Calculates and returns using the current projection of this view port, the |
|
902 |
* scale that is the extent in <i>screen coordinates</i> from the image in |
|
903 |
* <i>map coordinates</i>. |
|
904 |
* </p> |
|
905 |
* |
|
906 |
* @return the scale <i>extent / image size</i> projected by this view port |
|
907 |
* @deprecated since 07/09/07, use {@linkplain MapContext#getScaleView()} |
|
908 |
*/ |
|
909 |
private double getScale() { |
|
910 |
|
|
911 |
double[] trans2Meter = MapContext.getDistanceTrans2Meter(); |
|
912 |
if (proj == null) { |
|
913 |
double wmeters = ((getImageSize().width / this.getDPI()) * 0.0254); |
|
914 |
return (long) ((trans2Meter[getMapUnits()] * getAdjustedEnvelope() |
|
915 |
.getLength(0)) / wmeters); |
|
916 |
} |
|
917 |
else { |
|
918 |
return Math.round(proj.getScale(getAdjustedEnvelope().getMinimum(0) |
|
919 |
* trans2Meter[getMapUnits()], getAdjustedEnvelope().getMaximum(0) |
|
920 |
* trans2Meter[getMapUnits()], getImageSize().width, this.getDPI())); |
|
921 |
} |
|
922 |
|
|
923 |
/* |
|
924 |
* return proj.getScale(extent.getMinX(), extent.getMaxX(), imageSize.width, |
|
925 |
* dpi); |
|
926 |
*/ |
|
927 |
} |
|
928 |
|
|
929 |
/** |
|
930 |
* <p> |
|
931 |
* Affine transformation between <i>map 2D coordinates</i> to <i>screen 2D |
|
932 |
* coordinates</i> (pixels), preserving the "straightness" and "parallelism" |
|
933 |
* of the lines. |
|
934 |
* </p> |
|
935 |
* |
|
936 |
* @return the affine transformation |
|
937 |
* @see #setAffineTransform(AffineTransform) |
|
938 |
* @see #calculateAffineTransform() |
|
939 |
*/ |
|
940 |
public AffineTransform getAffineTransform() { |
|
941 |
return trans; |
|
942 |
} |
|
943 |
|
|
944 |
/** |
|
945 |
* <p> |
|
946 |
* Returns the size of the image projected. |
|
947 |
* </p> |
|
948 |
* |
|
949 |
* @return the image size |
|
950 |
* @see #setImageSize(Dimension) |
|
951 |
* @see #getImageHeight() |
|
952 |
* @see #getImageWidth() |
|
953 |
*/ |
|
954 |
public Dimension getImageSize() { |
|
955 |
return imageSize; |
|
956 |
} |
|
957 |
|
|
958 |
/** |
|
959 |
* <p> |
|
960 |
* Sets the size of the image projected, recalculating the parameters of this |
|
961 |
* view port. |
|
962 |
* </p> |
|
963 |
* |
|
964 |
* @param imageSize the image size |
|
965 |
* @see #getImageSize() |
|
966 |
* @see #calculateAffineTransform() |
|
967 |
*/ |
|
968 |
public void setImageSize(Dimension imageSize) { |
|
969 |
|
|
970 |
if (this.imageSize == null || (!this.imageSize.equals(imageSize))) { |
|
971 |
this.updateDrawVersion(); |
|
972 |
this.imageSize = imageSize; |
|
973 |
calculateAffineTransform(); |
|
974 |
} |
|
975 |
} |
|
976 |
|
|
977 |
/** |
|
978 |
* <p> |
|
979 |
* Notifies to all view port listeners registered, that the adjusted extent of |
|
980 |
* this view port has changed. |
|
981 |
* </p> |
|
982 |
* |
|
983 |
* @param newRect the new adjusted extend |
|
984 |
* @see #refreshExtent() |
|
985 |
* @see #setEnvelope(Envelope) |
|
986 |
* @see #setPreviousExtent() |
|
987 |
* @see ExtentEvent |
|
988 |
* @see ViewPortListener |
|
989 |
*/ |
|
990 |
protected void callExtentChanged(Envelope newRect) { |
|
991 |
ExtentEvent ev = ExtentEvent.createExtentEvent(newRect); |
|
992 |
|
|
993 |
for (int i = 0; i < listeners.size(); i++) { |
|
994 |
ViewPortListener listener = (ViewPortListener) listeners.get(i); |
|
995 |
listener.extentChanged(ev); |
|
996 |
} |
|
997 |
} |
|
998 |
|
|
999 |
/** |
|
1000 |
* <p> |
|
1001 |
* Notifies to all view port listeners registered, that the time of this view |
|
1002 |
* port has changed. |
|
1003 |
* </p> |
|
1004 |
* |
|
1005 |
* @param newTime the new time |
|
1006 |
* @see #refreshExtent() |
|
1007 |
* @see #setTime(Time) |
|
1008 |
* @see ExtentEvent |
|
1009 |
* @see ViewPortListener |
|
1010 |
*/ |
|
1011 |
protected void callTimeChanged(Time newTime) { |
|
1012 |
ExtentEvent viewPortEvent = new ExtentEvent(newTime); |
|
1013 |
|
|
1014 |
for (int i = 0; i < listeners.size(); i++) { |
|
1015 |
ViewPortListener listener = (ViewPortListener) listeners.get(i); |
|
1016 |
listener.extentChanged(viewPortEvent); |
|
1017 |
} |
|
1018 |
} |
|
1019 |
|
|
1020 |
/** |
|
1021 |
* <p> |
|
1022 |
* Notifies to all view port listeners registered, that the background color |
|
1023 |
* of this view port has changed. |
|
1024 |
* </p> |
|
1025 |
* |
|
1026 |
* @param c the new background color |
|
1027 |
* @see #setBackColor(Color) |
|
1028 |
* @see ColorEvent |
|
1029 |
* @see ViewPortListener |
|
1030 |
*/ |
|
1031 |
private void callColorChanged(Color c) { |
|
1032 |
ColorEvent ce = ColorEvent.createColorEvent(c); |
|
1033 |
|
|
1034 |
for (int i = 0; i < listeners.size(); i++) { |
|
1035 |
ViewPortListener listener = (ViewPortListener) listeners.get(i); |
|
1036 |
listener.backColorChanged(ce); |
|
1037 |
} |
|
1038 |
} |
|
1039 |
|
|
1040 |
/** |
|
1041 |
* <p> |
|
1042 |
* Notifies to all view port listeners registered, that the projection of this |
|
1043 |
* view port has changed. |
|
1044 |
* </p> |
|
1045 |
* |
|
1046 |
* @param projection the new projection |
|
1047 |
* @see #setProjection(IProjection) |
|
1048 |
* @see ProjectionEvent |
|
1049 |
* @see ViewPortListener |
|
1050 |
*/ |
|
1051 |
private void callProjectionChanged(IProjection projection) { |
|
1052 |
ProjectionEvent ev = ProjectionEvent.createProjectionEvent(projection); |
|
1053 |
|
|
1054 |
for (int i = 0; i < listeners.size(); i++) { |
|
1055 |
ViewPortListener listener = (ViewPortListener) listeners.get(i); |
|
1056 |
listener.projectionChanged(ev); |
|
1057 |
} |
|
1058 |
} |
|
1059 |
|
|
1060 |
/** |
|
1061 |
* <p> |
|
1062 |
* Calculates the affine transformation between the {@link #extent extent} in |
|
1063 |
* <i>map 2D coordinates</i> to the image area in the screen, in <i>screen 2D |
|
1064 |
* coordinates</i> (pixels). |
|
1065 |
* </p> |
|
1066 |
* <p> |
|
1067 |
* This process recalculates some parameters of this view port:<br> |
|
1068 |
* <ul> |
|
1069 |
* <li>The new {@link #scale scale} . |
|
1070 |
* <li>The new {@link #adjustedExtent adjustedExtent} . |
|
1071 |
* <li>The new {@link #trans trans} . |
|
1072 |
* <li>The new real world coordinates equivalent to 1 pixel ( |
|
1073 |
* {@link #dist1pixel dist1pixel}) . |
|
1074 |
* <li>The new real world coordinates equivalent to 3 pixels ( |
|
1075 |
* {@link #dist3pixel dist3pixel}) . |
|
1076 |
* </ul> |
|
1077 |
* </p> |
|
1078 |
* |
|
1079 |
* @see #getAffineTransform() |
|
1080 |
* @see #setAffineTransform(AffineTransform) |
|
1081 |
* @see #refreshExtent() |
|
1082 |
* @see #setEnvelope(Envelope) |
|
1083 |
* @see #setImageSize(Dimension) |
|
1084 |
* @see #setPreviousExtent() |
|
1085 |
* @see #createFromXML(XMLEntity) |
|
1086 |
* @see AffineTransform |
|
1087 |
*/ |
|
1088 |
private void calculateAffineTransform() { |
|
1089 |
if ((imageSize == null) || (extent == null) || (imageSize.width <= 0) |
|
1090 |
|| (imageSize.height <= 0)) { |
|
1091 |
return; |
|
1092 |
} |
|
1093 |
|
|
1094 |
AffineTransform escalado = new AffineTransform(); |
|
1095 |
AffineTransform translacion = new AffineTransform(); |
|
1096 |
|
|
1097 |
double escalaX; |
|
1098 |
double escalaY; |
|
1099 |
|
|
1100 |
escalaX = imageSize.width / extent.getWidth(); |
|
1101 |
escalaY = imageSize.height / extent.getHeight(); |
|
1102 |
|
|
1103 |
double xCenter = extent.getCenterX(); |
|
1104 |
double yCenter = extent.getCenterY(); |
|
1105 |
double newHeight; |
|
1106 |
double newWidth; |
|
1107 |
|
|
1108 |
adjustedExtent = new Rectangle2D.Double(); |
|
1109 |
|
|
1110 |
if (adjustableExtent) { |
|
1111 |
if (escalaX < escalaY) { |
|
1112 |
scale = escalaX; |
|
1113 |
newHeight = imageSize.height / scale; |
|
1114 |
adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0), yCenter |
|
1115 |
- (newHeight / 2.0), extent.getWidth(), newHeight); |
|
1116 |
} |
|
1117 |
else { |
|
1118 |
scale = escalaY; |
|
1119 |
newWidth = imageSize.width / scale; |
|
1120 |
adjustedExtent.setRect(xCenter - (newWidth / 2.0), |
|
1121 |
yCenter - (extent.getHeight() / 2.0), newWidth, extent.getHeight()); |
|
1122 |
} |
|
1123 |
escalado.setToScale(scale, -scale); |
|
1124 |
} |
|
1125 |
else { // adjusted is same as extent |
|
1126 |
scale = escalaX; |
|
1127 |
adjustedExtent.setFrame(extent); |
|
1128 |
escalado.setToScale(escalaX, -escalaY); |
|
1129 |
} |
|
1130 |
Envelope env = getAdjustedExtent(); |
|
1131 |
if (env == null) { |
|
1132 |
return; |
|
1133 |
} |
|
1134 |
translacion.setToTranslation(-env.getMinimum(0), -env.getMinimum(1) |
|
1135 |
- getAdjustedExtent().getLength(1)); |
|
1136 |
|
|
1137 |
AffineTransform offsetTrans = new AffineTransform(); |
|
1138 |
offsetTrans.setToTranslation(offset.getX(), offset.getY()); |
|
1139 |
|
|
1140 |
trans.setToIdentity(); |
|
1141 |
trans.concatenate(offsetTrans); |
|
1142 |
trans.concatenate(escalado); |
|
1143 |
|
|
1144 |
trans.concatenate(translacion); |
|
1145 |
|
|
1146 |
// Calculamos las distancias de 1 pixel y 3 pixel con esa |
|
1147 |
// transformaci�n |
|
1148 |
// de coordenadas, de forma que est�n precalculadas para cuando las |
|
1149 |
// necesitemos |
|
1150 |
AffineTransform at; |
|
1151 |
|
|
1152 |
try { |
|
1153 |
at = trans.createInverse(); |
|
1154 |
|
|
1155 |
Point2D pPixel = new Point2D.Float(1, 1); |
|
1156 |
|
|
1157 |
Point2D.Float pProv = new Point2D.Float(); |
|
1158 |
at.deltaTransform(pPixel, pProv); |
|
1159 |
|
|
1160 |
dist1pixel = pProv.x; |
|
1161 |
dist3pixel = 3 * pProv.x; |
|
1162 |
} |
|
1163 |
catch (NoninvertibleTransformException e) { |
|
1164 |
System.err.println("transformada afin = " + trans.toString()); |
|
1165 |
System.err.println("extent = " + extent.toString() + " imageSize= " |
|
1166 |
+ imageSize.toString()); |
|
1167 |
throw new RuntimeException("Non invertible transform Exception", e); |
|
1168 |
} |
|
1169 |
} |
|
1170 |
|
|
1171 |
/** |
|
1172 |
* <p> |
|
1173 |
* Sets the offset. |
|
1174 |
* </p> |
|
1175 |
* <p> |
|
1176 |
* The offset is the position where start drawing the map. |
|
1177 |
* </p> |
|
1178 |
* |
|
1179 |
* @param p 2D point that represents the offset in pixels |
|
1180 |
* @see #getOffset() |
|
1181 |
*/ |
|
1182 |
public void setOffset(Point2D p) { |
|
1183 |
if (!offset.equals(p)) { |
|
1184 |
this.updateDrawVersion(); |
|
1185 |
offset = p; |
|
1186 |
} |
|
1187 |
} |
|
1188 |
|
|
1189 |
/** |
|
1190 |
* <p> |
|
1191 |
* Gets the offset. |
|
1192 |
* </p> |
|
1193 |
* <p> |
|
1194 |
* The offset is the position where start drawing the map. |
|
1195 |
* </p> |
|
1196 |
* |
|
1197 |
* @return 2D point that represents the offset in pixels |
|
1198 |
* @see #setOffset(Point2D) |
|
1199 |
*/ |
|
1200 |
public Point2D getOffset() { |
|
1201 |
return offset; |
|
1202 |
} |
|
1203 |
|
|
1204 |
/** |
|
1205 |
* <p> |
|
1206 |
* Sets the background color. |
|
1207 |
* </p> |
|
1208 |
* |
|
1209 |
* @param c the new background color |
|
1210 |
* @see #getBackColor() |
|
1211 |
*/ |
|
1212 |
public void setBackColor(Color c) { |
|
1213 |
if (!c.equals(this.backColor)) { |
|
1214 |
this.updateDrawVersion(); |
|
1215 |
backColor = c; |
|
1216 |
callColorChanged(backColor); |
|
1217 |
} |
|
1218 |
} |
|
1219 |
|
|
1220 |
/** |
|
1221 |
* <p> |
|
1222 |
* Gets the background color. |
|
1223 |
* </p> |
|
1224 |
* |
|
1225 |
* @return the background color of the view |
|
1226 |
* @see #setBackColor(Color) |
|
1227 |
*/ |
|
1228 |
public Color getBackColor() { |
|
1229 |
return backColor; |
|
1230 |
} |
|
1231 |
|
|
1232 |
/** |
|
1233 |
* <p> |
|
1234 |
* Returns the extent currently covered by the view adjusted (scaled) to the |
|
1235 |
* image size aspect. |
|
1236 |
* </p> |
|
1237 |
* |
|
1238 |
* @return extent of the view adjusted to the image size aspect |
|
1239 |
* @see #setAdjustable(boolean) |
|
1240 |
* @deprecated use {@link ViewPort#getAdjustedEnvelope()} instead |
|
1241 |
*/ |
|
1242 |
public Envelope getAdjustedExtent() { |
|
1243 |
return getAdjustedEnvelope(); |
|
1244 |
} |
|
1245 |
|
|
1246 |
/** |
|
1247 |
* <p> |
|
1248 |
* Returns the envelope currently covered by the view adjusted (scaled) to the |
|
1249 |
* image size aspect. |
|
1250 |
* </p> |
|
1251 |
* |
|
1252 |
* @return envelope of the view adjusted to the image size aspect |
|
1253 |
* @see #setAdjustable(boolean) |
|
1254 |
*/ |
|
1255 |
public Envelope getAdjustedEnvelope() { |
|
1256 |
if (adjustedExtent == null) { |
|
1257 |
calculateAffineTransform(); |
|
1258 |
} |
|
1259 |
if (cliprect != null) { |
|
1260 |
Rectangle2D r = adjustedExtent.createIntersection(cliprect); |
|
1261 |
try { |
|
1262 |
return geomManager.createEnvelope(r.getX(), r.getY(), r.getMaxX(), r.getMaxY(), SUBTYPES.GEOM2D); |
|
1263 |
} catch (CreateEnvelopeException e) { |
|
1264 |
e.printStackTrace(); |
|
1265 |
logger.error("Error adjusting the extent", e); |
|
1266 |
} |
|
1267 |
} |
|
1268 |
if (adjustedExtent != null) { |
|
1269 |
try { |
|
1270 |
return geomManager.createEnvelope(adjustedExtent.getX(), adjustedExtent.getY(), |
|
1271 |
adjustedExtent.getMaxX(), adjustedExtent.getMaxY(), SUBTYPES.GEOM2D); |
|
1272 |
} catch (CreateEnvelopeException e) { |
|
1273 |
e.printStackTrace(); |
|
1274 |
logger.error("Error adjusting the extent", e); |
|
1275 |
} |
|
1276 |
} |
|
1277 |
return null; |
|
1278 |
} |
|
1279 |
|
|
1280 |
/** |
|
1281 |
* <p> |
|
1282 |
* Returns the measurement unit of this view port used for measuring distances |
|
1283 |
* and displaying information. |
|
1284 |
* </p> |
|
1285 |
* |
|
1286 |
* @return the measurement unit of this view used for measuring distances and |
|
1287 |
* displaying information |
|
1288 |
* @see #setDistanceUnits(int) |
|
1289 |
*/ |
|
1290 |
public int getDistanceUnits() { |
|
1291 |
return distanceUnits; |
|
1292 |
} |
|
1293 |
|
|
1294 |
/** |
|
1295 |
* <p> |
|
1296 |
* Returns the measurement unit of this view port used for measuring areas and |
|
1297 |
* displaying information. |
|
1298 |
* </p> |
|
1299 |
* |
|
1300 |
* @return the measurement unit of this view used for measuring areas and |
|
1301 |
* displaying information |
|
1302 |
* @see #setDistanceUnits(int) |
|
1303 |
*/ |
|
1304 |
public int getDistanceArea() { |
|
1305 |
return distanceArea; |
|
1306 |
} |
|
1307 |
|
|
1308 |
/** |
|
1309 |
* <p> |
|
1310 |
* Sets the measurement unit of this view port used for measuring distances |
|
1311 |
* and displaying information. |
|
1312 |
* </p> |
|
1313 |
* |
|
1314 |
* @param distanceUnits the measurement unit of this view used for measuring |
|
1315 |
* distances and displaying information |
|
1316 |
* @see #getDistanceUnits() |
|
1317 |
*/ |
|
1318 |
public void setDistanceUnits(int distanceUnits) { |
|
1319 |
this.distanceUnits = distanceUnits; |
|
1320 |
} |
|
1321 |
|
|
1322 |
/** |
|
1323 |
* <p> |
|
1324 |
* Sets the measurement unit of this view port used for measuring areas and |
|
1325 |
* displaying information. |
|
1326 |
* </p> |
|
1327 |
* |
|
1328 |
* @param distanceUnits the measurement unit of this view used for measuring |
|
1329 |
* areas and displaying information |
|
1330 |
* @see #getDistanceUnits() |
|
1331 |
*/ |
|
1332 |
public void setDistanceArea(int distanceArea) { |
|
1333 |
this.distanceArea = distanceArea; |
|
1334 |
} |
|
1335 |
|
|
1336 |
/** |
|
1337 |
* <p> |
|
1338 |
* Gets the measurement unit used by this view port for the map. |
|
1339 |
* </p> |
|
1340 |
* |
|
1341 |
* @return Returns the current map measure unit |
|
1342 |
* @see #setMapUnits(int) |
|
1343 |
*/ |
|
1344 |
public int getMapUnits() { |
|
1345 |
return mapUnits; |
|
1346 |
} |
|
1347 |
|
|
1348 |
/** |
|
1349 |
* <p> |
|
1350 |
* Sets the measurement unit used by this view port for the map. |
|
1351 |
* </p> |
|
1352 |
* |
|
1353 |
* @param mapUnits the new map measure unit |
|
1354 |
* @see #getMapUnits() |
|
1355 |
*/ |
|
1356 |
public void setMapUnits(int mapUnits) { |
|
1357 |
this.mapUnits = mapUnits; |
|
1358 |
} |
|
1359 |
|
|
1360 |
/** |
|
1361 |
* <p> |
|
1362 |
* Gets the width in <i>screen coordinates</i> of the rectangle where the |
|
1363 |
* image is displayed. |
|
1364 |
* </p> |
|
1365 |
* <p> |
|
1366 |
* Used by {@linkplain #calculateAffineTransform()} to calculate:<br> |
|
1367 |
* <ul> |
|
1368 |
* <li>The new {@link #scale scale} . |
|
1369 |
* <li>The new {@link #adjustedExtent adjustableExtent} . |
|
1370 |
* <li>The new {@link #trans trans} . |
|
1371 |
* <li>The new real world coordinates equivalent to 1 pixel ( |
|
1372 |
* {@link #dist1pixel dist1pixel}) . |
|
1373 |
* <li>The new real world coordinates equivalent to 3 pixels ( |
|
1374 |
* {@link #dist3pixel dist3pixel}) . |
|
1375 |
* </ul> |
|
1376 |
* </p> |
|
1377 |
* |
|
1378 |
* @see #getImageHeight() |
|
1379 |
* @see #getImageSize() |
|
1380 |
* @see #setImageSize(Dimension) |
|
1381 |
*/ |
|
1382 |
public int getImageWidth() { |
|
1383 |
return imageSize.width; |
|
1384 |
} |
|
1385 |
|
|
1386 |
/** |
|
1387 |
* <p> |
|
1388 |
* Gets the height in <i>screen coordinates</i> of the rectangle where the |
|
1389 |
* image is displayed. |
|
1390 |
* </p> |
|
1391 |
* <p> |
|
1392 |
* Used by {@linkplain #calculateAffineTransform()} to calculate:<br> |
|
1393 |
* <ul> |
|
1394 |
* <li>The new {@link #scale scale} . |
|
1395 |
* <li>The new {@link #adjustedExtent adjustableExtent} . |
|
1396 |
* <li>The new {@link #trans trans} . |
|
1397 |
* <li>The new real world coordinates equivalent to 1 pixel ( |
|
1398 |
* {@link #dist1pixel dist1pixel}) . |
|
1399 |
* <li>The new real world coordinates equivalent to 3 pixels ( |
|
1400 |
* {@link #dist3pixel dist3pixel}) . |
|
1401 |
* </ul> |
|
1402 |
* </p> |
|
1403 |
* |
|
1404 |
* @see #getImageWidth() |
|
1405 |
* @see #getImageSize() |
|
1406 |
* @see #setImageSize(Dimension) |
|
1407 |
*/ |
|
1408 |
public int getImageHeight() { |
|
1409 |
return imageSize.height; |
|
1410 |
} |
|
1411 |
|
|
1412 |
/** |
|
1413 |
* <p> |
|
1414 |
* Gets the distance in <i>world coordinates</i> equivalent to 1 pixel in the |
|
1415 |
* view with the current extent. |
|
1416 |
* </p> |
|
1417 |
* |
|
1418 |
* @return the distance |
|
1419 |
* @see #setDist1pixel(double) |
|
1420 |
*/ |
|
1421 |
public double getDist1pixel() { |
|
1422 |
return dist1pixel; |
|
1423 |
} |
|
1424 |
|
|
1425 |
/** |
|
1426 |
* <p> |
|
1427 |
* Sets the distance in <i>world coordinates</i> equivalent to 1 pixel in the |
|
1428 |
* view with the current extent. |
|
1429 |
* </p> |
|
1430 |
* |
|
1431 |
* @param dist1pixel the distance |
|
1432 |
* @see #getDist1pixel() |
|
1433 |
*/ |
|
1434 |
public void setDist1pixel(double dist1pixel) { |
|
1435 |
if (dist1pixel == this.dist1pixel) { |
|
1436 |
return; |
|
1437 |
} |
|
1438 |
this.updateDrawVersion(); |
|
1439 |
this.dist1pixel = dist1pixel; |
|
1440 |
} |
|
1441 |
|
|
1442 |
/** |
|
1443 |
* <p> |
|
1444 |
* Gets the distance in <i>world coordinates</i> equivalent to 3 pixels in the |
|
1445 |
* view with the current extent. |
|
1446 |
* </p> |
|
1447 |
* |
|
1448 |
* @return the distance |
|
1449 |
* @see #setDist3pixel(double) |
|
1450 |
*/ |
|
1451 |
public double getDist3pixel() { |
|
1452 |
return dist3pixel; |
|
1453 |
} |
|
1454 |
|
|
1455 |
/** |
|
1456 |
* <p> |
|
1457 |
* Sets the distance in <i>world coordinates</i> equivalent to 3 pixels in the |
|
1458 |
* view with the current extent. |
|
1459 |
* </p> |
|
1460 |
* |
|
1461 |
* @param dist3pixel the distance |
|
1462 |
* @see #getDist3pixel() |
|
1463 |
*/ |
|
1464 |
public void setDist3pixel(double dist3pixel) { |
|
1465 |
if (this.dist3pixel == dist3pixel) { |
|
1466 |
return; |
|
1467 |
} |
|
1468 |
this.updateDrawVersion(); |
|
1469 |
this.dist3pixel = dist3pixel; |
|
1470 |
} |
|
1471 |
|
|
1472 |
/** |
|
1473 |
* <p> |
|
1474 |
* Returns the last previous extents of this view port. |
|
1475 |
* </p> |
|
1476 |
* |
|
1477 |
* @return the last previous extents of this view port |
|
1478 |
* @see #setPreviousExtent() |
|
1479 |
* @deprecated use {@link ViewPort#getEnvelopes()} |
|
1480 |
*/ |
|
1481 |
public ExtentHistory getExtents() { |
|
1482 |
return getEnvelopes(); |
|
1483 |
} |
|
1484 |
|
|
1485 |
/** |
|
1486 |
* <p> |
|
1487 |
* Returns the last previous extents of this view port. |
|
1488 |
* </p> |
|
1489 |
* |
|
1490 |
* @return the last previous extents of this view port |
|
1491 |
* @see #setPreviousExtent() |
|
1492 |
*/ |
|
1493 |
public ExtentHistory getEnvelopes() { |
|
1494 |
return extentsHistory; |
|
1495 |
} |
|
1496 |
|
Also available in: Unified diff