1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.awt.Container;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.geom.AffineTransform;
|
---|
8 | import java.awt.geom.Area;
|
---|
9 | import java.awt.geom.Path2D;
|
---|
10 | import java.awt.geom.Point2D;
|
---|
11 | import java.awt.geom.Point2D.Double;
|
---|
12 | import java.awt.geom.Rectangle2D;
|
---|
13 |
|
---|
14 | import javax.swing.JComponent;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.data.Bounds;
|
---|
18 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.projection.Projecting;
|
---|
22 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
23 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
---|
24 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This class represents a state of the {@link MapView}.
|
---|
28 | * @author Michael Zangl
|
---|
29 | * @since 10343
|
---|
30 | */
|
---|
31 | public final class MapViewState {
|
---|
32 |
|
---|
33 | private final Projecting projecting;
|
---|
34 |
|
---|
35 | private final int viewWidth;
|
---|
36 | private final int viewHeight;
|
---|
37 |
|
---|
38 | private final double scale;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Top left {@link EastNorth} coordinate of the view.
|
---|
42 | */
|
---|
43 | private final EastNorth topLeft;
|
---|
44 |
|
---|
45 | private final Point topLeftOnScreen;
|
---|
46 | private final Point topLeftInWindow;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Create a new {@link MapViewState}
|
---|
50 | * @param projection The projection to use.
|
---|
51 | * @param viewWidth The view width
|
---|
52 | * @param viewHeight The view height
|
---|
53 | * @param scale The scale to use
|
---|
54 | * @param topLeft The top left corner in east/north space.
|
---|
55 | */
|
---|
56 | private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
|
---|
57 | this.projecting = projection;
|
---|
58 | this.scale = scale;
|
---|
59 | this.topLeft = topLeft;
|
---|
60 |
|
---|
61 | this.viewWidth = viewWidth;
|
---|
62 | this.viewHeight = viewHeight;
|
---|
63 | topLeftInWindow = new Point(0, 0);
|
---|
64 | topLeftOnScreen = new Point(0, 0);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private MapViewState(EastNorth topLeft, MapViewState mapViewState) {
|
---|
68 | this.projecting = mapViewState.projecting;
|
---|
69 | this.scale = mapViewState.scale;
|
---|
70 | this.topLeft = topLeft;
|
---|
71 |
|
---|
72 | viewWidth = mapViewState.viewWidth;
|
---|
73 | viewHeight = mapViewState.viewHeight;
|
---|
74 | topLeftInWindow = mapViewState.topLeftInWindow;
|
---|
75 | topLeftOnScreen = mapViewState.topLeftOnScreen;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private MapViewState(double scale, MapViewState mapViewState) {
|
---|
79 | this.projecting = mapViewState.projecting;
|
---|
80 | this.scale = scale;
|
---|
81 | this.topLeft = mapViewState.topLeft;
|
---|
82 |
|
---|
83 | viewWidth = mapViewState.viewWidth;
|
---|
84 | viewHeight = mapViewState.viewHeight;
|
---|
85 | topLeftInWindow = mapViewState.topLeftInWindow;
|
---|
86 | topLeftOnScreen = mapViewState.topLeftOnScreen;
|
---|
87 | }
|
---|
88 |
|
---|
89 | private MapViewState(JComponent position, MapViewState mapViewState) {
|
---|
90 | this.projecting = mapViewState.projecting;
|
---|
91 | this.scale = mapViewState.scale;
|
---|
92 | this.topLeft = mapViewState.topLeft;
|
---|
93 |
|
---|
94 | viewWidth = position.getWidth();
|
---|
95 | viewHeight = position.getHeight();
|
---|
96 | topLeftInWindow = new Point();
|
---|
97 | // better than using swing utils, since this allows us to use the mehtod if no screen is present.
|
---|
98 | Container component = position;
|
---|
99 | while (component != null) {
|
---|
100 | topLeftInWindow.x += component.getX();
|
---|
101 | topLeftInWindow.y += component.getY();
|
---|
102 | component = component.getParent();
|
---|
103 | }
|
---|
104 | try {
|
---|
105 | topLeftOnScreen = position.getLocationOnScreen();
|
---|
106 | } catch (RuntimeException e) {
|
---|
107 | throw BugReport.intercept(e).put("position", position).put("parent", position::getParent);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | private MapViewState(Projecting projecting, MapViewState mapViewState) {
|
---|
112 | this.projecting = projecting;
|
---|
113 | this.scale = mapViewState.scale;
|
---|
114 | this.topLeft = mapViewState.topLeft;
|
---|
115 |
|
---|
116 | viewWidth = mapViewState.viewWidth;
|
---|
117 | viewHeight = mapViewState.viewHeight;
|
---|
118 | topLeftInWindow = mapViewState.topLeftInWindow;
|
---|
119 | topLeftOnScreen = mapViewState.topLeftOnScreen;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * The scale in east/north units per pixel.
|
---|
124 | * @return The scale.
|
---|
125 | */
|
---|
126 | public double getScale() {
|
---|
127 | return scale;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Gets the MapViewPoint representation for a position in view coordinates.
|
---|
132 | * @param x The x coordinate inside the view.
|
---|
133 | * @param y The y coordinate inside the view.
|
---|
134 | * @return The MapViewPoint.
|
---|
135 | */
|
---|
136 | public MapViewPoint getForView(double x, double y) {
|
---|
137 | return new MapViewViewPoint(x, y);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Gets the {@link MapViewPoint} for the given {@link EastNorth} coordinate.
|
---|
142 | * @param eastNorth the position.
|
---|
143 | * @return The point for that position.
|
---|
144 | */
|
---|
145 | public MapViewPoint getPointFor(EastNorth eastNorth) {
|
---|
146 | return new MapViewEastNorthPoint(eastNorth);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Gets the {@link MapViewPoint} for the given {@link LatLon} coordinate.
|
---|
151 | * @param latlon the position
|
---|
152 | * @return The point for that position.
|
---|
153 | * @since 10651
|
---|
154 | */
|
---|
155 | public MapViewPoint getPointFor(LatLon latlon) {
|
---|
156 | return getPointFor(getProjection().latlon2eastNorth(latlon));
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Gets a rectangle representing the whole view area.
|
---|
161 | * @return The rectangle.
|
---|
162 | */
|
---|
163 | public MapViewRectangle getViewArea() {
|
---|
164 | return getForView(0, 0).rectTo(getForView(viewWidth, viewHeight));
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Gets a rectangle of the view as map view area.
|
---|
169 | * @param rectangle The rectangle to get.
|
---|
170 | * @return The view area.
|
---|
171 | * @since 10458
|
---|
172 | */
|
---|
173 | public MapViewRectangle getViewArea(Rectangle rectangle) {
|
---|
174 | return getForView(rectangle.getMinX(), rectangle.getMinY()).rectTo(getForView(rectangle.getMaxX(), rectangle.getMaxY()));
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Gets the center of the view.
|
---|
179 | * @return The center position.
|
---|
180 | */
|
---|
181 | public MapViewPoint getCenter() {
|
---|
182 | return getForView(viewWidth / 2.0, viewHeight / 2.0);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Gets the width of the view on the Screen;
|
---|
187 | * @return The width of the view component in screen pixel.
|
---|
188 | */
|
---|
189 | public double getViewWidth() {
|
---|
190 | return viewWidth;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Gets the height of the view on the Screen;
|
---|
195 | * @return The height of the view component in screen pixel.
|
---|
196 | */
|
---|
197 | public double getViewHeight() {
|
---|
198 | return viewHeight;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Gets the current projection used for the MapView.
|
---|
203 | * @return The projection.
|
---|
204 | */
|
---|
205 | public Projection getProjection() {
|
---|
206 | return projecting.getBaseProjection();
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
|
---|
211 | * @return The affine transform. It should not be changed.
|
---|
212 | * @since 10375
|
---|
213 | */
|
---|
214 | public AffineTransform getAffineTransform() {
|
---|
215 | return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, -topLeft.east() / scale,
|
---|
216 | topLeft.north() / scale);
|
---|
217 | }
|
---|
218 |
|
---|
219 | public Area getArea(Bounds bounds) {
|
---|
220 | Path2D area = new Path2D.Double();
|
---|
221 | bounds.visitEdge(getProjection(), latlon -> {
|
---|
222 | MapViewPoint point = getPointFor(latlon);
|
---|
223 | if (area.getCurrentPoint() == null) {
|
---|
224 | area.moveTo(point.getInViewX(), point.getInViewY());
|
---|
225 | } else {
|
---|
226 | area.lineTo(point.getInViewX(), point.getInViewY());
|
---|
227 | }
|
---|
228 | });
|
---|
229 | area.closePath();
|
---|
230 | return new Area(area);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Creates a new state that is the same as the current state except for that it is using a new center.
|
---|
235 | * @param newCenter The new center coordinate.
|
---|
236 | * @return The new state.
|
---|
237 | * @since 10375
|
---|
238 | */
|
---|
239 | public MapViewState usingCenter(EastNorth newCenter) {
|
---|
240 | return movedTo(getCenter(), newCenter);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * @param mapViewPoint The reference point.
|
---|
245 | * @param newEastNorthThere The east/north coordinate that should be there.
|
---|
246 | * @return The new state.
|
---|
247 | * @since 10375
|
---|
248 | */
|
---|
249 | public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
|
---|
250 | EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
|
---|
251 | if (delta.distanceSq(0, 0) < .1e-20) {
|
---|
252 | return this;
|
---|
253 | } else {
|
---|
254 | return new MapViewState(topLeft.add(delta), this);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Creates a new state that is the same as the current state except for that it is using a new scale.
|
---|
260 | * @param newScale The new scale to use.
|
---|
261 | * @return The new state.
|
---|
262 | * @since 10375
|
---|
263 | */
|
---|
264 | public MapViewState usingScale(double newScale) {
|
---|
265 | return new MapViewState(newScale, this);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Creates a new state that is the same as the current state except for that it is using the location of the given component.
|
---|
270 | * <p>
|
---|
271 | * The view is moved so that the center is the same as the old center.
|
---|
272 | * @param positon The new location to use.
|
---|
273 | * @return The new state.
|
---|
274 | * @since 10375
|
---|
275 | */
|
---|
276 | public MapViewState usingLocation(JComponent positon) {
|
---|
277 | EastNorth center = this.getCenter().getEastNorth();
|
---|
278 | return new MapViewState(positon, this).usingCenter(center);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Creates a state that uses the projection.
|
---|
283 | * @param projection The projection to use.
|
---|
284 | * @return The new state.
|
---|
285 | * @since 10486
|
---|
286 | */
|
---|
287 | public MapViewState usingProjection(Projection projection) {
|
---|
288 | if (projection.equals(this.projecting)) {
|
---|
289 | return this;
|
---|
290 | } else {
|
---|
291 | return new MapViewState(projection, this);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Create the default {@link MapViewState} object for the given map view. The screen position won't be set so that this method can be used
|
---|
297 | * before the view was added to the hirarchy.
|
---|
298 | * @param width The view width
|
---|
299 | * @param height The view height
|
---|
300 | * @return The state
|
---|
301 | * @since 10375
|
---|
302 | */
|
---|
303 | public static MapViewState createDefaultState(int width, int height) {
|
---|
304 | Projection projection = Main.getProjection();
|
---|
305 | double scale = projection.getDefaultZoomInPPD();
|
---|
306 | MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
|
---|
307 | EastNorth center = calculateDefaultCenter();
|
---|
308 | return state.movedTo(state.getCenter(), center);
|
---|
309 | }
|
---|
310 |
|
---|
311 | private static EastNorth calculateDefaultCenter() {
|
---|
312 | Bounds b = DownloadDialog.getSavedDownloadBounds();
|
---|
313 | if (b == null) {
|
---|
314 | b = Main.getProjection().getWorldBoundsLatLon();
|
---|
315 | }
|
---|
316 | return Main.getProjection().latlon2eastNorth(b.getCenter());
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * A class representing a point in the map view. It allows to convert between the different coordinate systems.
|
---|
321 | * @author Michael Zangl
|
---|
322 | */
|
---|
323 | public abstract class MapViewPoint {
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Get this point in view coordinates.
|
---|
327 | * @return The point in view coordinates.
|
---|
328 | */
|
---|
329 | public Point2D getInView() {
|
---|
330 | return new Point2D.Double(getInViewX(), getInViewY());
|
---|
331 | }
|
---|
332 |
|
---|
333 | protected abstract double getInViewX();
|
---|
334 |
|
---|
335 | protected abstract double getInViewY();
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Convert this point to window coordinates.
|
---|
339 | * @return The point in window coordinates.
|
---|
340 | */
|
---|
341 | public Point2D getInWindow() {
|
---|
342 | return getUsingCorner(topLeftInWindow);
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Convert this point to screen coordinates.
|
---|
347 | * @return The point in screen coordinates.
|
---|
348 | */
|
---|
349 | public Point2D getOnScreen() {
|
---|
350 | return getUsingCorner(topLeftOnScreen);
|
---|
351 | }
|
---|
352 |
|
---|
353 | private Double getUsingCorner(Point corner) {
|
---|
354 | return new Point2D.Double(corner.getX() + getInViewX(), corner.getY() + getInViewY());
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Gets the {@link EastNorth} coordinate of this point.
|
---|
359 | * @return The east/north coordinate.
|
---|
360 | */
|
---|
361 | public EastNorth getEastNorth() {
|
---|
362 | return new EastNorth(topLeft.east() + getInViewX() * scale, topLeft.north() - getInViewY() * scale);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Create a rectangle from this to the other point.
|
---|
367 | * @param other The other point. Needs to be of the same {@link MapViewState}
|
---|
368 | * @return A rectangle.
|
---|
369 | */
|
---|
370 | public MapViewRectangle rectTo(MapViewPoint other) {
|
---|
371 | return new MapViewRectangle(this, other);
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Gets the current position in LatLon coordinates according to the current projection.
|
---|
376 | * @return The positon as LatLon.
|
---|
377 | * @see #getLatLonClamped()
|
---|
378 | */
|
---|
379 | public LatLon getLatLon() {
|
---|
380 | return projecting.getBaseProjection().eastNorth2latlon(getEastNorth());
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Gets the latlon coordinate clamped to the current world area.
|
---|
385 | * @return The lat/lon coordinate
|
---|
386 | * @since 10805
|
---|
387 | */
|
---|
388 | public LatLon getLatLonClamped() {
|
---|
389 | return projecting.eastNorth2latlonClamped(getEastNorth());
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Add the given offset to this point
|
---|
394 | * @param en The offset in east/north space.
|
---|
395 | * @return The new point
|
---|
396 | * @since 10651
|
---|
397 | */
|
---|
398 | public MapViewPoint add(EastNorth en) {
|
---|
399 | return new MapViewEastNorthPoint(getEastNorth().add(en));
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | private class MapViewViewPoint extends MapViewPoint {
|
---|
404 | private final double x;
|
---|
405 | private final double y;
|
---|
406 |
|
---|
407 | MapViewViewPoint(double x, double y) {
|
---|
408 | this.x = x;
|
---|
409 | this.y = y;
|
---|
410 | }
|
---|
411 |
|
---|
412 | @Override
|
---|
413 | protected double getInViewX() {
|
---|
414 | return x;
|
---|
415 | }
|
---|
416 |
|
---|
417 | @Override
|
---|
418 | protected double getInViewY() {
|
---|
419 | return y;
|
---|
420 | }
|
---|
421 |
|
---|
422 | @Override
|
---|
423 | public String toString() {
|
---|
424 | return "MapViewViewPoint [x=" + x + ", y=" + y + ']';
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | private class MapViewEastNorthPoint extends MapViewPoint {
|
---|
429 |
|
---|
430 | private final EastNorth eastNorth;
|
---|
431 |
|
---|
432 | MapViewEastNorthPoint(EastNorth eastNorth) {
|
---|
433 | this.eastNorth = eastNorth;
|
---|
434 | }
|
---|
435 |
|
---|
436 | @Override
|
---|
437 | protected double getInViewX() {
|
---|
438 | return (eastNorth.east() - topLeft.east()) / scale;
|
---|
439 | }
|
---|
440 |
|
---|
441 | @Override
|
---|
442 | protected double getInViewY() {
|
---|
443 | return (topLeft.north() - eastNorth.north()) / scale;
|
---|
444 | }
|
---|
445 |
|
---|
446 | @Override
|
---|
447 | public EastNorth getEastNorth() {
|
---|
448 | return eastNorth;
|
---|
449 | }
|
---|
450 |
|
---|
451 | @Override
|
---|
452 | public String toString() {
|
---|
453 | return "MapViewEastNorthPoint [eastNorth=" + eastNorth + ']';
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * A rectangle on the MapView. It is rectangular in screen / EastNorth space.
|
---|
459 | * @author Michael Zangl
|
---|
460 | */
|
---|
461 | public class MapViewRectangle {
|
---|
462 | private final MapViewPoint p1;
|
---|
463 | private final MapViewPoint p2;
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Create a new MapViewRectangle
|
---|
467 | * @param p1 The first point to use
|
---|
468 | * @param p2 The second point to use.
|
---|
469 | */
|
---|
470 | MapViewRectangle(MapViewPoint p1, MapViewPoint p2) {
|
---|
471 | this.p1 = p1;
|
---|
472 | this.p2 = p2;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Gets the projection bounds for this rectangle.
|
---|
477 | * @return The projection bounds.
|
---|
478 | */
|
---|
479 | public ProjectionBounds getProjectionBounds() {
|
---|
480 | ProjectionBounds b = new ProjectionBounds(p1.getEastNorth());
|
---|
481 | b.extend(p2.getEastNorth());
|
---|
482 | return b;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /**
|
---|
486 | * Gets a rough estimate of the bounds by assuming lat/lon are parallel to x/y.
|
---|
487 | * @return The bounds computed by converting the corners of this rectangle.
|
---|
488 | * @see #getLatLonBoundsBox()
|
---|
489 | */
|
---|
490 | public Bounds getCornerBounds() {
|
---|
491 | Bounds b = new Bounds(p1.getLatLon());
|
---|
492 | b.extend(p2.getLatLon());
|
---|
493 | return b;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /**
|
---|
497 | * Gets the real bounds that enclose this rectangle.
|
---|
498 | * This is computed respecting that the borders of this rectangle may not be a straignt line in latlon coordinates.
|
---|
499 | * @return The bounds.
|
---|
500 | * @since 10458
|
---|
501 | */
|
---|
502 | public Bounds getLatLonBoundsBox() {
|
---|
503 | // TODO @michael2402: Use hillclimb.
|
---|
504 | return projecting.getBaseProjection().getLatLonBoundsBox(getProjectionBounds());
|
---|
505 | }
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Gets this rectangle on the screen.
|
---|
509 | * @return The rectangle.
|
---|
510 | * @since 10651
|
---|
511 | */
|
---|
512 | public Rectangle2D getInView() {
|
---|
513 | double x1 = p1.getInViewX();
|
---|
514 | double y1 = p1.getInViewY();
|
---|
515 | double x2 = p2.getInViewX();
|
---|
516 | double y2 = p2.getInViewY();
|
---|
517 | return new Rectangle2D.Double(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | }
|
---|