1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.event.ComponentAdapter;
|
---|
8 | import java.awt.event.ComponentEvent;
|
---|
9 | import java.awt.event.HierarchyEvent;
|
---|
10 | import java.awt.event.HierarchyListener;
|
---|
11 | import java.awt.geom.AffineTransform;
|
---|
12 | import java.awt.geom.Point2D;
|
---|
13 | import java.nio.charset.StandardCharsets;
|
---|
14 | import java.text.NumberFormat;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.Date;
|
---|
19 | import java.util.HashSet;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Map;
|
---|
23 | import java.util.Map.Entry;
|
---|
24 | import java.util.Set;
|
---|
25 | import java.util.Stack;
|
---|
26 | import java.util.TreeMap;
|
---|
27 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
28 | import java.util.function.Predicate;
|
---|
29 | import java.util.zip.CRC32;
|
---|
30 |
|
---|
31 | import javax.swing.JComponent;
|
---|
32 | import javax.swing.SwingUtilities;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.data.Bounds;
|
---|
36 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
37 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
---|
38 | import org.openstreetmap.josm.data.ViewportData;
|
---|
39 | import org.openstreetmap.josm.data.coor.CachedLatLon;
|
---|
40 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
41 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
42 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
43 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
44 | import org.openstreetmap.josm.data.osm.Node;
|
---|
45 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
46 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
47 | import org.openstreetmap.josm.data.osm.Way;
|
---|
48 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
49 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
50 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
51 | import org.openstreetmap.josm.data.preferences.DoubleProperty;
|
---|
52 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
53 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
54 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
55 | import org.openstreetmap.josm.gui.help.Helpful;
|
---|
56 | import org.openstreetmap.josm.gui.layer.NativeScaleLayer;
|
---|
57 | import org.openstreetmap.josm.gui.layer.NativeScaleLayer.Scale;
|
---|
58 | import org.openstreetmap.josm.gui.layer.NativeScaleLayer.ScaleList;
|
---|
59 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
60 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
61 | import org.openstreetmap.josm.gui.util.CursorManager;
|
---|
62 | import org.openstreetmap.josm.tools.Utils;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * A component that can be navigated by a {@link MapMover}. Used as map view and for the
|
---|
66 | * zoomer in the download dialog.
|
---|
67 | *
|
---|
68 | * @author imi
|
---|
69 | * @since 41
|
---|
70 | */
|
---|
71 | public class NavigatableComponent extends JComponent implements Helpful {
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Interface to notify listeners of the change of the zoom area.
|
---|
75 | * @since 10600 (functional interface)
|
---|
76 | */
|
---|
77 | @FunctionalInterface
|
---|
78 | public interface ZoomChangeListener {
|
---|
79 | /**
|
---|
80 | * Method called when the zoom area has changed.
|
---|
81 | */
|
---|
82 | void zoomChanged();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * To determine if a primitive is currently selectable.
|
---|
87 | */
|
---|
88 | public transient Predicate<OsmPrimitive> isSelectablePredicate = prim -> {
|
---|
89 | if (!prim.isSelectable()) return false;
|
---|
90 | // if it isn't displayed on screen, you cannot click on it
|
---|
91 | MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock();
|
---|
92 | try {
|
---|
93 | return !MapPaintStyles.getStyles().get(prim, getDist100Pixel(), this).isEmpty();
|
---|
94 | } finally {
|
---|
95 | MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock();
|
---|
96 | }
|
---|
97 | };
|
---|
98 |
|
---|
99 | /** Snap distance */
|
---|
100 | public static final IntegerProperty PROP_SNAP_DISTANCE = new IntegerProperty("mappaint.node.snap-distance", 10);
|
---|
101 | /** Zoom steps to get double scale */
|
---|
102 | public static final DoubleProperty PROP_ZOOM_RATIO = new DoubleProperty("zoom.ratio", 2.0);
|
---|
103 | /** Divide intervals between native resolution levels to smaller steps if they are much larger than zoom ratio */
|
---|
104 | public static final BooleanProperty PROP_ZOOM_INTERMEDIATE_STEPS = new BooleanProperty("zoom.intermediate-steps", true);
|
---|
105 |
|
---|
106 | /** Property name for center change events */
|
---|
107 | public static final String PROPNAME_CENTER = "center";
|
---|
108 | /** Property name for scale change events */
|
---|
109 | public static final String PROPNAME_SCALE = "scale";
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * The layer which scale is set to.
|
---|
113 | */
|
---|
114 | private transient NativeScaleLayer nativeScaleLayer;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * the zoom listeners
|
---|
118 | */
|
---|
119 | private static final CopyOnWriteArrayList<ZoomChangeListener> zoomChangeListeners = new CopyOnWriteArrayList<>();
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Removes a zoom change listener
|
---|
123 | *
|
---|
124 | * @param listener the listener. Ignored if null or already absent
|
---|
125 | */
|
---|
126 | public static void removeZoomChangeListener(NavigatableComponent.ZoomChangeListener listener) {
|
---|
127 | zoomChangeListeners.remove(listener);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Adds a zoom change listener
|
---|
132 | *
|
---|
133 | * @param listener the listener. Ignored if null or already registered.
|
---|
134 | */
|
---|
135 | public static void addZoomChangeListener(NavigatableComponent.ZoomChangeListener listener) {
|
---|
136 | if (listener != null) {
|
---|
137 | zoomChangeListeners.addIfAbsent(listener);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected static void fireZoomChanged() {
|
---|
142 | for (ZoomChangeListener l : zoomChangeListeners) {
|
---|
143 | l.zoomChanged();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | // The only events that may move/resize this map view are window movements or changes to the map view size.
|
---|
148 | // We can clean this up more by only recalculating the state on repaint.
|
---|
149 | private final transient HierarchyListener hierarchyListener = e -> {
|
---|
150 | long interestingFlags = HierarchyEvent.ANCESTOR_MOVED | HierarchyEvent.SHOWING_CHANGED;
|
---|
151 | if ((e.getChangeFlags() & interestingFlags) != 0) {
|
---|
152 | updateLocationState();
|
---|
153 | }
|
---|
154 | };
|
---|
155 |
|
---|
156 | private final transient ComponentAdapter componentListener = new ComponentAdapter() {
|
---|
157 | @Override
|
---|
158 | public void componentShown(ComponentEvent e) {
|
---|
159 | updateLocationState();
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public void componentResized(ComponentEvent e) {
|
---|
164 | updateLocationState();
|
---|
165 | }
|
---|
166 | };
|
---|
167 |
|
---|
168 | protected transient ViewportData initialViewport;
|
---|
169 |
|
---|
170 | protected final transient CursorManager cursorManager = new CursorManager(this);
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * The current state (scale, center, ...) of this map view.
|
---|
174 | */
|
---|
175 | private transient MapViewState state;
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Constructs a new {@code NavigatableComponent}.
|
---|
179 | */
|
---|
180 | public NavigatableComponent() {
|
---|
181 | setLayout(null);
|
---|
182 | state = MapViewState.createDefaultState(getWidth(), getHeight());
|
---|
183 | // uses weak link.
|
---|
184 | Main.addProjectionChangeListener((oldValue, newValue) -> fixProjection());
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public void addNotify() {
|
---|
189 | updateLocationState();
|
---|
190 | addHierarchyListener(hierarchyListener);
|
---|
191 | addComponentListener(componentListener);
|
---|
192 | super.addNotify();
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public void removeNotify() {
|
---|
197 | removeHierarchyListener(hierarchyListener);
|
---|
198 | removeComponentListener(componentListener);
|
---|
199 | super.removeNotify();
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Choose a layer that scale will be snap to its native scales.
|
---|
204 | * @param nativeScaleLayer layer to which scale will be snapped
|
---|
205 | */
|
---|
206 | public void setNativeScaleLayer(NativeScaleLayer nativeScaleLayer) {
|
---|
207 | this.nativeScaleLayer = nativeScaleLayer;
|
---|
208 | zoomTo(getCenter(), scaleRound(getScale()));
|
---|
209 | repaint();
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Replies the layer which scale is set to.
|
---|
214 | * @return the current scale layer (may be null)
|
---|
215 | */
|
---|
216 | public NativeScaleLayer getNativeScaleLayer() {
|
---|
217 | return nativeScaleLayer;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Get a new scale that is zoomed in from previous scale
|
---|
222 | * and snapped to selected native scale layer.
|
---|
223 | * @return new scale
|
---|
224 | */
|
---|
225 | public double scaleZoomIn() {
|
---|
226 | return scaleZoomManyTimes(-1);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Get a new scale that is zoomed out from previous scale
|
---|
231 | * and snapped to selected native scale layer.
|
---|
232 | * @return new scale
|
---|
233 | */
|
---|
234 | public double scaleZoomOut() {
|
---|
235 | return scaleZoomManyTimes(1);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Get a new scale that is zoomed in/out a number of times
|
---|
240 | * from previous scale and snapped to selected native scale layer.
|
---|
241 | * @param times count of zoom operations, negative means zoom in
|
---|
242 | * @return new scale
|
---|
243 | */
|
---|
244 | public double scaleZoomManyTimes(int times) {
|
---|
245 | if (nativeScaleLayer != null) {
|
---|
246 | ScaleList scaleList = nativeScaleLayer.getNativeScales();
|
---|
247 | if (scaleList != null) {
|
---|
248 | if (PROP_ZOOM_INTERMEDIATE_STEPS.get()) {
|
---|
249 | scaleList = scaleList.withIntermediateSteps(PROP_ZOOM_RATIO.get());
|
---|
250 | }
|
---|
251 | Scale s = scaleList.scaleZoomTimes(getScale(), PROP_ZOOM_RATIO.get(), times);
|
---|
252 | return s != null ? s.getScale() : 0;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | return getScale() * Math.pow(PROP_ZOOM_RATIO.get(), times);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Get a scale snapped to native resolutions, use round method.
|
---|
260 | * It gives nearest step from scale list.
|
---|
261 | * Use round method.
|
---|
262 | * @param scale to snap
|
---|
263 | * @return snapped scale
|
---|
264 | */
|
---|
265 | public double scaleRound(double scale) {
|
---|
266 | return scaleSnap(scale, false);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Get a scale snapped to native resolutions.
|
---|
271 | * It gives nearest lower step from scale list, usable to fit objects.
|
---|
272 | * @param scale to snap
|
---|
273 | * @return snapped scale
|
---|
274 | */
|
---|
275 | public double scaleFloor(double scale) {
|
---|
276 | return scaleSnap(scale, true);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Get a scale snapped to native resolutions.
|
---|
281 | * It gives nearest lower step from scale list, usable to fit objects.
|
---|
282 | * @param scale to snap
|
---|
283 | * @param floor use floor instead of round, set true when fitting view to objects
|
---|
284 | * @return new scale
|
---|
285 | */
|
---|
286 | public double scaleSnap(double scale, boolean floor) {
|
---|
287 | if (nativeScaleLayer != null) {
|
---|
288 | ScaleList scaleList = nativeScaleLayer.getNativeScales();
|
---|
289 | if (scaleList != null) {
|
---|
290 | if (PROP_ZOOM_INTERMEDIATE_STEPS.get()) {
|
---|
291 | scaleList = scaleList.withIntermediateSteps(PROP_ZOOM_RATIO.get());
|
---|
292 | }
|
---|
293 | Scale snapscale = scaleList.getSnapScale(scale, PROP_ZOOM_RATIO.get(), floor);
|
---|
294 | return snapscale != null ? snapscale.getScale() : scale;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | return scale;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Zoom in current view. Use configured zoom step and scaling settings.
|
---|
302 | */
|
---|
303 | public void zoomIn() {
|
---|
304 | zoomTo(state.getCenterAtPixel().getEastNorth(), scaleZoomIn());
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Zoom out current view. Use configured zoom step and scaling settings.
|
---|
309 | */
|
---|
310 | public void zoomOut() {
|
---|
311 | zoomTo(state.getCenterAtPixel().getEastNorth(), scaleZoomOut());
|
---|
312 | }
|
---|
313 |
|
---|
314 | protected void updateLocationState() {
|
---|
315 | if (isVisibleOnScreen()) {
|
---|
316 | state = state.usingLocation(this);
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | protected boolean isVisibleOnScreen() {
|
---|
321 | return SwingUtilities.getWindowAncestor(this) != null && isShowing();
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Changes the projection settings used for this map view.
|
---|
326 | * <p>
|
---|
327 | * Made public temporarily, will be made private later.
|
---|
328 | */
|
---|
329 | public void fixProjection() {
|
---|
330 | state = state.usingProjection(Main.getProjection());
|
---|
331 | repaint();
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Gets the current view state. This includes the scale, the current view area and the position.
|
---|
336 | * @return The current state.
|
---|
337 | */
|
---|
338 | public MapViewState getState() {
|
---|
339 | return state;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Returns the text describing the given distance in the current system of measurement.
|
---|
344 | * @param dist The distance in metres.
|
---|
345 | * @return the text describing the given distance in the current system of measurement.
|
---|
346 | * @since 3406
|
---|
347 | */
|
---|
348 | public static String getDistText(double dist) {
|
---|
349 | return SystemOfMeasurement.getSystemOfMeasurement().getDistText(dist);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Returns the text describing the given distance in the current system of measurement.
|
---|
354 | * @param dist The distance in metres
|
---|
355 | * @param format A {@link NumberFormat} to format the area value
|
---|
356 | * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
|
---|
357 | * @return the text describing the given distance in the current system of measurement.
|
---|
358 | * @since 7135
|
---|
359 | */
|
---|
360 | public static String getDistText(final double dist, final NumberFormat format, final double threshold) {
|
---|
361 | return SystemOfMeasurement.getSystemOfMeasurement().getDistText(dist, format, threshold);
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Returns the text describing the distance in meter that correspond to 100 px on screen.
|
---|
366 | * @return the text describing the distance in meter that correspond to 100 px on screen
|
---|
367 | */
|
---|
368 | public String getDist100PixelText() {
|
---|
369 | return getDistText(getDist100Pixel());
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Get the distance in meter that correspond to 100 px on screen.
|
---|
374 | *
|
---|
375 | * @return the distance in meter that correspond to 100 px on screen
|
---|
376 | */
|
---|
377 | public double getDist100Pixel() {
|
---|
378 | return getDist100Pixel(true);
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Get the distance in meter that correspond to 100 px on screen.
|
---|
383 | *
|
---|
384 | * @param alwaysPositive if true, makes sure the return value is always
|
---|
385 | * > 0. (Two points 100 px apart can appear to be identical if the user
|
---|
386 | * has zoomed out a lot and the projection code does something funny.)
|
---|
387 | * @return the distance in meter that correspond to 100 px on screen
|
---|
388 | */
|
---|
389 | public double getDist100Pixel(boolean alwaysPositive) {
|
---|
390 | int w = getWidth()/2;
|
---|
391 | int h = getHeight()/2;
|
---|
392 | LatLon ll1 = getLatLon(w-50, h);
|
---|
393 | LatLon ll2 = getLatLon(w+50, h);
|
---|
394 | double gcd = ll1.greatCircleDistance(ll2);
|
---|
395 | if (alwaysPositive && gcd <= 0)
|
---|
396 | return 0.1;
|
---|
397 | return gcd;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Returns the current center of the viewport.
|
---|
402 | *
|
---|
403 | * (Use {@link #zoomTo(EastNorth)} to the change the center.)
|
---|
404 | *
|
---|
405 | * @return the current center of the viewport
|
---|
406 | */
|
---|
407 | public EastNorth getCenter() {
|
---|
408 | return state.getCenterAtPixel().getEastNorth();
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Returns the current scale.
|
---|
413 | *
|
---|
414 | * In east/north units per pixel.
|
---|
415 | *
|
---|
416 | * @return the current scale
|
---|
417 | */
|
---|
418 | public double getScale() {
|
---|
419 | return state.getScale();
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * @param x X-Pixelposition to get coordinate from
|
---|
424 | * @param y Y-Pixelposition to get coordinate from
|
---|
425 | *
|
---|
426 | * @return Geographic coordinates from a specific pixel coordination on the screen.
|
---|
427 | */
|
---|
428 | public EastNorth getEastNorth(int x, int y) {
|
---|
429 | return state.getForView(x, y).getEastNorth();
|
---|
430 | }
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * Determines the projection bounds of view area.
|
---|
434 | * @return the projection bounds of view area
|
---|
435 | */
|
---|
436 | public ProjectionBounds getProjectionBounds() {
|
---|
437 | return getState().getViewArea().getProjectionBounds();
|
---|
438 | }
|
---|
439 |
|
---|
440 | /* FIXME: replace with better method - used by MapSlider */
|
---|
441 | public ProjectionBounds getMaxProjectionBounds() {
|
---|
442 | Bounds b = getProjection().getWorldBoundsLatLon();
|
---|
443 | return new ProjectionBounds(getProjection().latlon2eastNorth(b.getMin()),
|
---|
444 | getProjection().latlon2eastNorth(b.getMax()));
|
---|
445 | }
|
---|
446 |
|
---|
447 | /* FIXME: replace with better method - used by Main to reset Bounds when projection changes, don't use otherwise */
|
---|
448 | public Bounds getRealBounds() {
|
---|
449 | return getState().getViewArea().getCornerBounds();
|
---|
450 | }
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Returns unprojected geographic coordinates for a specific pixel position on the screen.
|
---|
454 | * @param x X-Pixelposition to get coordinate from
|
---|
455 | * @param y Y-Pixelposition to get coordinate from
|
---|
456 | *
|
---|
457 | * @return Geographic unprojected coordinates from a specific pixel position on the screen.
|
---|
458 | */
|
---|
459 | public LatLon getLatLon(int x, int y) {
|
---|
460 | return getProjection().eastNorth2latlon(getEastNorth(x, y));
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Returns unprojected geographic coordinates for a specific pixel position on the screen.
|
---|
465 | * @param x X-Pixelposition to get coordinate from
|
---|
466 | * @param y Y-Pixelposition to get coordinate from
|
---|
467 | *
|
---|
468 | * @return Geographic unprojected coordinates from a specific pixel position on the screen.
|
---|
469 | */
|
---|
470 | public LatLon getLatLon(double x, double y) {
|
---|
471 | return getLatLon((int) x, (int) y);
|
---|
472 | }
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Determines the projection bounds of given rectangle.
|
---|
476 | * @param r rectangle
|
---|
477 | * @return the projection bounds of {@code r}
|
---|
478 | */
|
---|
479 | public ProjectionBounds getProjectionBounds(Rectangle r) {
|
---|
480 | return getState().getViewArea(r).getProjectionBounds();
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * @param r rectangle
|
---|
485 | * @return Minimum bounds that will cover rectangle
|
---|
486 | */
|
---|
487 | public Bounds getLatLonBounds(Rectangle r) {
|
---|
488 | return Main.getProjection().getLatLonBoundsBox(getProjectionBounds(r));
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
|
---|
493 | * @return The affine transform.
|
---|
494 | */
|
---|
495 | public AffineTransform getAffineTransform() {
|
---|
496 | return getState().getAffineTransform();
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | * Return the point on the screen where this Coordinate would be.
|
---|
501 | * @param p The point, where this geopoint would be drawn.
|
---|
502 | * @return The point on screen where "point" would be drawn, relative to the own top/left.
|
---|
503 | */
|
---|
504 | public Point2D getPoint2D(EastNorth p) {
|
---|
505 | if (null == p)
|
---|
506 | return new Point();
|
---|
507 | return getState().getPointFor(p).getInView();
|
---|
508 | }
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Return the point on the screen where this Coordinate would be.
|
---|
512 | * @param latlon The point, where this geopoint would be drawn.
|
---|
513 | * @return The point on screen where "point" would be drawn, relative to the own top/left.
|
---|
514 | */
|
---|
515 | public Point2D getPoint2D(LatLon latlon) {
|
---|
516 | if (latlon == null)
|
---|
517 | return new Point();
|
---|
518 | else if (latlon instanceof CachedLatLon)
|
---|
519 | return getPoint2D(((CachedLatLon) latlon).getEastNorth());
|
---|
520 | else
|
---|
521 | return getPoint2D(getProjection().latlon2eastNorth(latlon));
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Return the point on the screen where this Node would be.
|
---|
526 | * @param n The node, where this geopoint would be drawn.
|
---|
527 | * @return The point on screen where "node" would be drawn, relative to the own top/left.
|
---|
528 | */
|
---|
529 | public Point2D getPoint2D(Node n) {
|
---|
530 | return getPoint2D(n.getEastNorth());
|
---|
531 | }
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * looses precision, may overflow (depends on p and current scale)
|
---|
535 | * @param p east/north
|
---|
536 | * @return point
|
---|
537 | * @see #getPoint2D(EastNorth)
|
---|
538 | */
|
---|
539 | public Point getPoint(EastNorth p) {
|
---|
540 | Point2D d = getPoint2D(p);
|
---|
541 | return new Point((int) d.getX(), (int) d.getY());
|
---|
542 | }
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * looses precision, may overflow (depends on p and current scale)
|
---|
546 | * @param latlon lat/lon
|
---|
547 | * @return point
|
---|
548 | * @see #getPoint2D(LatLon)
|
---|
549 | */
|
---|
550 | public Point getPoint(LatLon latlon) {
|
---|
551 | Point2D d = getPoint2D(latlon);
|
---|
552 | return new Point((int) d.getX(), (int) d.getY());
|
---|
553 | }
|
---|
554 |
|
---|
555 | /**
|
---|
556 | * looses precision, may overflow (depends on p and current scale)
|
---|
557 | * @param n node
|
---|
558 | * @return point
|
---|
559 | * @see #getPoint2D(Node)
|
---|
560 | */
|
---|
561 | public Point getPoint(Node n) {
|
---|
562 | Point2D d = getPoint2D(n);
|
---|
563 | return new Point((int) d.getX(), (int) d.getY());
|
---|
564 | }
|
---|
565 |
|
---|
566 | /**
|
---|
567 | * Zoom to the given coordinate and scale.
|
---|
568 | *
|
---|
569 | * @param newCenter The center x-value (easting) to zoom to.
|
---|
570 | * @param newScale The scale to use.
|
---|
571 | */
|
---|
572 | public void zoomTo(EastNorth newCenter, double newScale) {
|
---|
573 | zoomTo(newCenter, newScale, false);
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Zoom to the given coordinate and scale.
|
---|
578 | *
|
---|
579 | * @param center The center x-value (easting) to zoom to.
|
---|
580 | * @param scale The scale to use.
|
---|
581 | * @param initial true if this call initializes the viewport.
|
---|
582 | */
|
---|
583 | public void zoomTo(EastNorth center, double scale, boolean initial) {
|
---|
584 | Bounds b = getProjection().getWorldBoundsLatLon();
|
---|
585 | ProjectionBounds pb = getProjection().getWorldBoundsBoxEastNorth();
|
---|
586 | double newScale = scale;
|
---|
587 | int width = getWidth();
|
---|
588 | int height = getHeight();
|
---|
589 |
|
---|
590 | // make sure, the center of the screen is within projection bounds
|
---|
591 | double east = center.east();
|
---|
592 | double north = center.north();
|
---|
593 | east = Math.max(east, pb.minEast);
|
---|
594 | east = Math.min(east, pb.maxEast);
|
---|
595 | north = Math.max(north, pb.minNorth);
|
---|
596 | north = Math.min(north, pb.maxNorth);
|
---|
597 | EastNorth newCenter = new EastNorth(east, north);
|
---|
598 |
|
---|
599 | // don't zoom out too much, the world bounds should be at least
|
---|
600 | // half the size of the screen
|
---|
601 | double pbHeight = pb.maxNorth - pb.minNorth;
|
---|
602 | if (height > 0 && 2 * pbHeight < height * newScale) {
|
---|
603 | double newScaleH = 2 * pbHeight / height;
|
---|
604 | double pbWidth = pb.maxEast - pb.minEast;
|
---|
605 | if (width > 0 && 2 * pbWidth < width * newScale) {
|
---|
606 | double newScaleW = 2 * pbWidth / width;
|
---|
607 | newScale = Math.max(newScaleH, newScaleW);
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | // don't zoom in too much, minimum: 100 px = 1 cm
|
---|
612 | LatLon ll1 = getLatLon(width / 2 - 50, height / 2);
|
---|
613 | LatLon ll2 = getLatLon(width / 2 + 50, height / 2);
|
---|
614 | if (ll1.isValid() && ll2.isValid() && b.contains(ll1) && b.contains(ll2)) {
|
---|
615 | double dm = ll1.greatCircleDistance(ll2);
|
---|
616 | double den = 100 * getScale();
|
---|
617 | double scaleMin = 0.01 * den / dm / 100;
|
---|
618 | if (newScale < scaleMin && !Double.isInfinite(scaleMin)) {
|
---|
619 | newScale = scaleMin;
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | // snap scale to imagery if needed
|
---|
624 | newScale = scaleRound(newScale);
|
---|
625 |
|
---|
626 | // Align to the pixel grid:
|
---|
627 | // This is a sub-pixel correction to ensure consistent drawing at a certain scale.
|
---|
628 | // For example take 2 nodes, that have a distance of exactly 2.6 pixels.
|
---|
629 | // Depending on the offset, the distance in rounded or truncated integer
|
---|
630 | // pixels will be 2 or 3. It is preferable to have a consistent distance
|
---|
631 | // and not switch back and forth as the viewport moves. This can be achieved by
|
---|
632 | // locking an arbitrary point to integer pixel coordinates. (Here the EastNorth
|
---|
633 | // origin is used as reference point.)
|
---|
634 | // Note that the normal right mouse button drag moves the map by integer pixel
|
---|
635 | // values, so it is not an issue in this case. It only shows when zooming
|
---|
636 | // in & back out, etc.
|
---|
637 | MapViewState mvs = getState().usingScale(newScale);
|
---|
638 | mvs = mvs.movedTo(mvs.getCenter(), newCenter);
|
---|
639 | Point2D enOrigin = mvs.getPointFor(new EastNorth(0, 0)).getInView();
|
---|
640 | // as a result of the alignment, it is common to round "half integer" values
|
---|
641 | // like 1.49999, which is numerically unstable; add small epsilon to resolve this
|
---|
642 | final double epsilon = 1e-3;
|
---|
643 | Point2D enOriginAligned = new Point2D.Double(
|
---|
644 | Math.round(enOrigin.getX()) + epsilon,
|
---|
645 | Math.round(enOrigin.getY()) + epsilon);
|
---|
646 | EastNorth enShift = mvs.getForView(enOriginAligned.getX(), enOriginAligned.getY()).getEastNorth();
|
---|
647 | newCenter = newCenter.subtract(enShift);
|
---|
648 |
|
---|
649 | if (!newCenter.equals(getCenter()) || !Utils.equalsEpsilon(getScale(), newScale)) {
|
---|
650 | if (!initial) {
|
---|
651 | pushZoomUndo(getCenter(), getScale());
|
---|
652 | }
|
---|
653 | zoomNoUndoTo(newCenter, newScale, initial);
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Zoom to the given coordinate without adding to the zoom undo buffer.
|
---|
659 | *
|
---|
660 | * @param newCenter The center x-value (easting) to zoom to.
|
---|
661 | * @param newScale The scale to use.
|
---|
662 | * @param initial true if this call initializes the viewport.
|
---|
663 | */
|
---|
664 | private void zoomNoUndoTo(EastNorth newCenter, double newScale, boolean initial) {
|
---|
665 | if (!Utils.equalsEpsilon(getScale(), newScale)) {
|
---|
666 | double oldScale = getScale();
|
---|
667 | state = state.usingScale(newScale);
|
---|
668 | if (!initial) {
|
---|
669 | firePropertyChange(PROPNAME_SCALE, oldScale, newScale);
|
---|
670 | }
|
---|
671 | }
|
---|
672 | if (!newCenter.equals(getCenter())) {
|
---|
673 | EastNorth oldCenter = getCenter();
|
---|
674 | state = state.movedTo(state.getCenter(), newCenter);
|
---|
675 | if (!initial) {
|
---|
676 | firePropertyChange(PROPNAME_CENTER, oldCenter, newCenter);
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | if (!initial) {
|
---|
681 | repaint();
|
---|
682 | fireZoomChanged();
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Zoom to given east/north.
|
---|
688 | * @param newCenter new center coordinates
|
---|
689 | */
|
---|
690 | public void zoomTo(EastNorth newCenter) {
|
---|
691 | zoomTo(newCenter, getScale());
|
---|
692 | }
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * Zoom to given lat/lon.
|
---|
696 | * @param newCenter new center coordinates
|
---|
697 | */
|
---|
698 | public void zoomTo(LatLon newCenter) {
|
---|
699 | zoomTo(Projections.project(newCenter));
|
---|
700 | }
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Create a thread that moves the viewport to the given center in an animated fashion.
|
---|
704 | * @param newCenter new east/north center
|
---|
705 | */
|
---|
706 | public void smoothScrollTo(EastNorth newCenter) {
|
---|
707 | // FIXME make these configurable.
|
---|
708 | final int fps = 20; // animation frames per second
|
---|
709 | final int speed = 1500; // milliseconds for full-screen-width pan
|
---|
710 | if (!newCenter.equals(getCenter())) {
|
---|
711 | final EastNorth oldCenter = getCenter();
|
---|
712 | final double distance = newCenter.distance(oldCenter) / getScale();
|
---|
713 | final double milliseconds = distance / getWidth() * speed;
|
---|
714 | final double frames = milliseconds * fps / 1000;
|
---|
715 | final EastNorth finalNewCenter = newCenter;
|
---|
716 |
|
---|
717 | new Thread("smooth-scroller") {
|
---|
718 | @Override
|
---|
719 | public void run() {
|
---|
720 | for (int i = 0; i < frames; i++) {
|
---|
721 | // FIXME - not use zoom history here
|
---|
722 | zoomTo(oldCenter.interpolate(finalNewCenter, (i+1) / frames));
|
---|
723 | try {
|
---|
724 | Thread.sleep(1000L / fps);
|
---|
725 | } catch (InterruptedException ex) {
|
---|
726 | Main.warn("InterruptedException in "+NavigatableComponent.class.getSimpleName()+" during smooth scrolling");
|
---|
727 | Thread.currentThread().interrupt();
|
---|
728 | }
|
---|
729 | }
|
---|
730 | }
|
---|
731 | }.start();
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | public void zoomManyTimes(double x, double y, int times) {
|
---|
736 | double oldScale = getScale();
|
---|
737 | double newScale = scaleZoomManyTimes(times);
|
---|
738 | zoomToFactor(x, y, newScale / oldScale);
|
---|
739 | }
|
---|
740 |
|
---|
741 | public void zoomToFactor(double x, double y, double factor) {
|
---|
742 | double newScale = getScale()*factor;
|
---|
743 | EastNorth oldUnderMouse = getState().getForView(x, y).getEastNorth();
|
---|
744 | MapViewState newState = getState().usingScale(newScale);
|
---|
745 | newState = newState.movedTo(newState.getForView(x, y), oldUnderMouse);
|
---|
746 | zoomTo(newState.getCenter().getEastNorth(), newScale);
|
---|
747 | }
|
---|
748 |
|
---|
749 | public void zoomToFactor(EastNorth newCenter, double factor) {
|
---|
750 | zoomTo(newCenter, getScale()*factor);
|
---|
751 | }
|
---|
752 |
|
---|
753 | public void zoomToFactor(double factor) {
|
---|
754 | zoomTo(getCenter(), getScale()*factor);
|
---|
755 | }
|
---|
756 |
|
---|
757 | /**
|
---|
758 | * Zoom to given projection bounds.
|
---|
759 | * @param box new projection bounds
|
---|
760 | */
|
---|
761 | public void zoomTo(ProjectionBounds box) {
|
---|
762 | // -20 to leave some border
|
---|
763 | int w = getWidth()-20;
|
---|
764 | if (w < 20) {
|
---|
765 | w = 20;
|
---|
766 | }
|
---|
767 | int h = getHeight()-20;
|
---|
768 | if (h < 20) {
|
---|
769 | h = 20;
|
---|
770 | }
|
---|
771 |
|
---|
772 | double scaleX = (box.maxEast-box.minEast)/w;
|
---|
773 | double scaleY = (box.maxNorth-box.minNorth)/h;
|
---|
774 | double newScale = Math.max(scaleX, scaleY);
|
---|
775 |
|
---|
776 | newScale = scaleFloor(newScale);
|
---|
777 | zoomTo(box.getCenter(), newScale);
|
---|
778 | }
|
---|
779 |
|
---|
780 | /**
|
---|
781 | * Zoom to given bounds.
|
---|
782 | * @param box new bounds
|
---|
783 | */
|
---|
784 | public void zoomTo(Bounds box) {
|
---|
785 | zoomTo(new ProjectionBounds(getProjection().latlon2eastNorth(box.getMin()),
|
---|
786 | getProjection().latlon2eastNorth(box.getMax())));
|
---|
787 | }
|
---|
788 |
|
---|
789 | /**
|
---|
790 | * Zoom to given viewport data.
|
---|
791 | * @param viewport new viewport data
|
---|
792 | */
|
---|
793 | public void zoomTo(ViewportData viewport) {
|
---|
794 | if (viewport == null) return;
|
---|
795 | if (viewport.getBounds() != null) {
|
---|
796 | BoundingXYVisitor box = new BoundingXYVisitor();
|
---|
797 | box.visit(viewport.getBounds());
|
---|
798 | zoomTo(box);
|
---|
799 | } else {
|
---|
800 | zoomTo(viewport.getCenter(), viewport.getScale(), true);
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 | /**
|
---|
805 | * Set the new dimension to the view.
|
---|
806 | * @param box box to zoom to
|
---|
807 | */
|
---|
808 | public void zoomTo(BoundingXYVisitor box) {
|
---|
809 | if (box == null) {
|
---|
810 | box = new BoundingXYVisitor();
|
---|
811 | }
|
---|
812 | if (box.getBounds() == null) {
|
---|
813 | box.visit(getProjection().getWorldBoundsLatLon());
|
---|
814 | }
|
---|
815 | if (!box.hasExtend()) {
|
---|
816 | box.enlargeBoundingBox();
|
---|
817 | }
|
---|
818 |
|
---|
819 | zoomTo(box.getBounds());
|
---|
820 | }
|
---|
821 |
|
---|
822 | private static class ZoomData {
|
---|
823 | private final EastNorth center;
|
---|
824 | private final double scale;
|
---|
825 |
|
---|
826 | ZoomData(EastNorth center, double scale) {
|
---|
827 | this.center = center;
|
---|
828 | this.scale = scale;
|
---|
829 | }
|
---|
830 |
|
---|
831 | public EastNorth getCenterEastNorth() {
|
---|
832 | return center;
|
---|
833 | }
|
---|
834 |
|
---|
835 | public double getScale() {
|
---|
836 | return scale;
|
---|
837 | }
|
---|
838 | }
|
---|
839 |
|
---|
840 | private final transient Stack<ZoomData> zoomUndoBuffer = new Stack<>();
|
---|
841 | private final transient Stack<ZoomData> zoomRedoBuffer = new Stack<>();
|
---|
842 | private Date zoomTimestamp = new Date();
|
---|
843 |
|
---|
844 | private void pushZoomUndo(EastNorth center, double scale) {
|
---|
845 | Date now = new Date();
|
---|
846 | if ((now.getTime() - zoomTimestamp.getTime()) > (Main.pref.getDouble("zoom.undo.delay", 1.0) * 1000)) {
|
---|
847 | zoomUndoBuffer.push(new ZoomData(center, scale));
|
---|
848 | if (zoomUndoBuffer.size() > Main.pref.getInteger("zoom.undo.max", 50)) {
|
---|
849 | zoomUndoBuffer.remove(0);
|
---|
850 | }
|
---|
851 | zoomRedoBuffer.clear();
|
---|
852 | }
|
---|
853 | zoomTimestamp = now;
|
---|
854 | }
|
---|
855 |
|
---|
856 | /**
|
---|
857 | * Zoom to previous location.
|
---|
858 | */
|
---|
859 | public void zoomPrevious() {
|
---|
860 | if (!zoomUndoBuffer.isEmpty()) {
|
---|
861 | ZoomData zoom = zoomUndoBuffer.pop();
|
---|
862 | zoomRedoBuffer.push(new ZoomData(getCenter(), getScale()));
|
---|
863 | zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale(), false);
|
---|
864 | }
|
---|
865 | }
|
---|
866 |
|
---|
867 | /**
|
---|
868 | * Zoom to next location.
|
---|
869 | */
|
---|
870 | public void zoomNext() {
|
---|
871 | if (!zoomRedoBuffer.isEmpty()) {
|
---|
872 | ZoomData zoom = zoomRedoBuffer.pop();
|
---|
873 | zoomUndoBuffer.push(new ZoomData(getCenter(), getScale()));
|
---|
874 | zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale(), false);
|
---|
875 | }
|
---|
876 | }
|
---|
877 |
|
---|
878 | /**
|
---|
879 | * Determines if zoom history contains "undo" entries.
|
---|
880 | * @return {@code true} if zoom history contains "undo" entries
|
---|
881 | */
|
---|
882 | public boolean hasZoomUndoEntries() {
|
---|
883 | return !zoomUndoBuffer.isEmpty();
|
---|
884 | }
|
---|
885 |
|
---|
886 | /**
|
---|
887 | * Determines if zoom history contains "redo" entries.
|
---|
888 | * @return {@code true} if zoom history contains "redo" entries
|
---|
889 | */
|
---|
890 | public boolean hasZoomRedoEntries() {
|
---|
891 | return !zoomRedoBuffer.isEmpty();
|
---|
892 | }
|
---|
893 |
|
---|
894 | private BBox getBBox(Point p, int snapDistance) {
|
---|
895 | return new BBox(getLatLon(p.x - snapDistance, p.y - snapDistance),
|
---|
896 | getLatLon(p.x + snapDistance, p.y + snapDistance));
|
---|
897 | }
|
---|
898 |
|
---|
899 | /**
|
---|
900 | * The *result* does not depend on the current map selection state, neither does the result *order*.
|
---|
901 | * It solely depends on the distance to point p.
|
---|
902 | * @param p point
|
---|
903 | * @param predicate predicate to match
|
---|
904 | *
|
---|
905 | * @return a sorted map with the keys representing the distance of their associated nodes to point p.
|
---|
906 | */
|
---|
907 | private Map<Double, List<Node>> getNearestNodesImpl(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
908 | Map<Double, List<Node>> nearestMap = new TreeMap<>();
|
---|
909 | DataSet ds = Main.getLayerManager().getEditDataSet();
|
---|
910 |
|
---|
911 | if (ds != null) {
|
---|
912 | double dist, snapDistanceSq = PROP_SNAP_DISTANCE.get();
|
---|
913 | snapDistanceSq *= snapDistanceSq;
|
---|
914 |
|
---|
915 | for (Node n : ds.searchNodes(getBBox(p, PROP_SNAP_DISTANCE.get()))) {
|
---|
916 | if (predicate.test(n)
|
---|
917 | && (dist = getPoint2D(n).distanceSq(p)) < snapDistanceSq) {
|
---|
918 | List<Node> nlist;
|
---|
919 | if (nearestMap.containsKey(dist)) {
|
---|
920 | nlist = nearestMap.get(dist);
|
---|
921 | } else {
|
---|
922 | nlist = new LinkedList<>();
|
---|
923 | nearestMap.put(dist, nlist);
|
---|
924 | }
|
---|
925 | nlist.add(n);
|
---|
926 | }
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | return nearestMap;
|
---|
931 | }
|
---|
932 |
|
---|
933 | /**
|
---|
934 | * The *result* does not depend on the current map selection state,
|
---|
935 | * neither does the result *order*.
|
---|
936 | * It solely depends on the distance to point p.
|
---|
937 | *
|
---|
938 | * @param p the point for which to search the nearest segment.
|
---|
939 | * @param ignore a collection of nodes which are not to be returned.
|
---|
940 | * @param predicate the returned objects have to fulfill certain properties.
|
---|
941 | *
|
---|
942 | * @return All nodes nearest to point p that are in a belt from
|
---|
943 | * dist(nearest) to dist(nearest)+4px around p and
|
---|
944 | * that are not in ignore.
|
---|
945 | */
|
---|
946 | public final List<Node> getNearestNodes(Point p,
|
---|
947 | Collection<Node> ignore, Predicate<OsmPrimitive> predicate) {
|
---|
948 | List<Node> nearestList = Collections.emptyList();
|
---|
949 |
|
---|
950 | if (ignore == null) {
|
---|
951 | ignore = Collections.emptySet();
|
---|
952 | }
|
---|
953 |
|
---|
954 | Map<Double, List<Node>> nlists = getNearestNodesImpl(p, predicate);
|
---|
955 | if (!nlists.isEmpty()) {
|
---|
956 | Double minDistSq = null;
|
---|
957 | for (Entry<Double, List<Node>> entry : nlists.entrySet()) {
|
---|
958 | Double distSq = entry.getKey();
|
---|
959 | List<Node> nlist = entry.getValue();
|
---|
960 |
|
---|
961 | // filter nodes to be ignored before determining minDistSq..
|
---|
962 | nlist.removeAll(ignore);
|
---|
963 | if (minDistSq == null) {
|
---|
964 | if (!nlist.isEmpty()) {
|
---|
965 | minDistSq = distSq;
|
---|
966 | nearestList = new ArrayList<>();
|
---|
967 | nearestList.addAll(nlist);
|
---|
968 | }
|
---|
969 | } else {
|
---|
970 | if (distSq-minDistSq < (4)*(4)) {
|
---|
971 | nearestList.addAll(nlist);
|
---|
972 | }
|
---|
973 | }
|
---|
974 | }
|
---|
975 | }
|
---|
976 |
|
---|
977 | return nearestList;
|
---|
978 | }
|
---|
979 |
|
---|
980 | /**
|
---|
981 | * The *result* does not depend on the current map selection state,
|
---|
982 | * neither does the result *order*.
|
---|
983 | * It solely depends on the distance to point p.
|
---|
984 | *
|
---|
985 | * @param p the point for which to search the nearest segment.
|
---|
986 | * @param predicate the returned objects have to fulfill certain properties.
|
---|
987 | *
|
---|
988 | * @return All nodes nearest to point p that are in a belt from
|
---|
989 | * dist(nearest) to dist(nearest)+4px around p.
|
---|
990 | * @see #getNearestNodes(Point, Collection, Predicate)
|
---|
991 | */
|
---|
992 | public final List<Node> getNearestNodes(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
993 | return getNearestNodes(p, null, predicate);
|
---|
994 | }
|
---|
995 |
|
---|
996 | /**
|
---|
997 | * The *result* depends on the current map selection state IF use_selected is true.
|
---|
998 | *
|
---|
999 | * If more than one node within node.snap-distance pixels is found,
|
---|
1000 | * the nearest node selected is returned IF use_selected is true.
|
---|
1001 | *
|
---|
1002 | * Else the nearest new/id=0 node within about the same distance
|
---|
1003 | * as the true nearest node is returned.
|
---|
1004 | *
|
---|
1005 | * If no such node is found either, the true nearest node to p is returned.
|
---|
1006 | *
|
---|
1007 | * Finally, if a node is not found at all, null is returned.
|
---|
1008 | *
|
---|
1009 | * @param p the screen point
|
---|
1010 | * @param predicate this parameter imposes a condition on the returned object, e.g.
|
---|
1011 | * give the nearest node that is tagged.
|
---|
1012 | * @param useSelected make search depend on selection
|
---|
1013 | *
|
---|
1014 | * @return A node within snap-distance to point p, that is chosen by the algorithm described.
|
---|
1015 | */
|
---|
1016 | public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate, boolean useSelected) {
|
---|
1017 | return getNearestNode(p, predicate, useSelected, null);
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | /**
|
---|
1021 | * The *result* depends on the current map selection state IF use_selected is true
|
---|
1022 | *
|
---|
1023 | * If more than one node within node.snap-distance pixels is found,
|
---|
1024 | * the nearest node selected is returned IF use_selected is true.
|
---|
1025 | *
|
---|
1026 | * If there are no selected nodes near that point, the node that is related to some of the preferredRefs
|
---|
1027 | *
|
---|
1028 | * Else the nearest new/id=0 node within about the same distance
|
---|
1029 | * as the true nearest node is returned.
|
---|
1030 | *
|
---|
1031 | * If no such node is found either, the true nearest node to p is returned.
|
---|
1032 | *
|
---|
1033 | * Finally, if a node is not found at all, null is returned.
|
---|
1034 | *
|
---|
1035 | * @param p the screen point
|
---|
1036 | * @param predicate this parameter imposes a condition on the returned object, e.g.
|
---|
1037 | * give the nearest node that is tagged.
|
---|
1038 | * @param useSelected make search depend on selection
|
---|
1039 | * @param preferredRefs primitives, whose nodes we prefer
|
---|
1040 | *
|
---|
1041 | * @return A node within snap-distance to point p, that is chosen by the algorithm described.
|
---|
1042 | * @since 6065
|
---|
1043 | */
|
---|
1044 | public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate,
|
---|
1045 | boolean useSelected, Collection<OsmPrimitive> preferredRefs) {
|
---|
1046 |
|
---|
1047 | Map<Double, List<Node>> nlists = getNearestNodesImpl(p, predicate);
|
---|
1048 | if (nlists.isEmpty()) return null;
|
---|
1049 |
|
---|
1050 | if (preferredRefs != null && preferredRefs.isEmpty()) preferredRefs = null;
|
---|
1051 | Node ntsel = null, ntnew = null, ntref = null;
|
---|
1052 | boolean useNtsel = useSelected;
|
---|
1053 | double minDistSq = nlists.keySet().iterator().next();
|
---|
1054 |
|
---|
1055 | for (Entry<Double, List<Node>> entry : nlists.entrySet()) {
|
---|
1056 | Double distSq = entry.getKey();
|
---|
1057 | for (Node nd : entry.getValue()) {
|
---|
1058 | // find the nearest selected node
|
---|
1059 | if (ntsel == null && nd.isSelected()) {
|
---|
1060 | ntsel = nd;
|
---|
1061 | // if there are multiple nearest nodes, prefer the one
|
---|
1062 | // that is selected. This is required in order to drag
|
---|
1063 | // the selected node if multiple nodes have the same
|
---|
1064 | // coordinates (e.g. after unglue)
|
---|
1065 | useNtsel |= Utils.equalsEpsilon(distSq, minDistSq);
|
---|
1066 | }
|
---|
1067 | if (ntref == null && preferredRefs != null && Utils.equalsEpsilon(distSq, minDistSq)) {
|
---|
1068 | List<OsmPrimitive> ndRefs = nd.getReferrers();
|
---|
1069 | for (OsmPrimitive ref: preferredRefs) {
|
---|
1070 | if (ndRefs.contains(ref)) {
|
---|
1071 | ntref = nd;
|
---|
1072 | break;
|
---|
1073 | }
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | // find the nearest newest node that is within about the same
|
---|
1077 | // distance as the true nearest node
|
---|
1078 | if (ntnew == null && nd.isNew() && (distSq-minDistSq < 1)) {
|
---|
1079 | ntnew = nd;
|
---|
1080 | }
|
---|
1081 | }
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | // take nearest selected, nearest new or true nearest node to p, in that order
|
---|
1085 | if (ntsel != null && useNtsel)
|
---|
1086 | return ntsel;
|
---|
1087 | if (ntref != null)
|
---|
1088 | return ntref;
|
---|
1089 | if (ntnew != null)
|
---|
1090 | return ntnew;
|
---|
1091 | return nlists.values().iterator().next().get(0);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | /**
|
---|
1095 | * Convenience method to {@link #getNearestNode(Point, Predicate, boolean)}.
|
---|
1096 | * @param p the screen point
|
---|
1097 | * @param predicate this parameter imposes a condition on the returned object, e.g.
|
---|
1098 | * give the nearest node that is tagged.
|
---|
1099 | *
|
---|
1100 | * @return The nearest node to point p.
|
---|
1101 | */
|
---|
1102 | public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1103 | return getNearestNode(p, predicate, true);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | /**
|
---|
1107 | * The *result* does not depend on the current map selection state, neither does the result *order*.
|
---|
1108 | * It solely depends on the distance to point p.
|
---|
1109 | * @param p the screen point
|
---|
1110 | * @param predicate this parameter imposes a condition on the returned object, e.g.
|
---|
1111 | * give the nearest node that is tagged.
|
---|
1112 | *
|
---|
1113 | * @return a sorted map with the keys representing the perpendicular
|
---|
1114 | * distance of their associated way segments to point p.
|
---|
1115 | */
|
---|
1116 | private Map<Double, List<WaySegment>> getNearestWaySegmentsImpl(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1117 | Map<Double, List<WaySegment>> nearestMap = new TreeMap<>();
|
---|
1118 | DataSet ds = Main.getLayerManager().getEditDataSet();
|
---|
1119 |
|
---|
1120 | if (ds != null) {
|
---|
1121 | double snapDistanceSq = Main.pref.getInteger("mappaint.segment.snap-distance", 10);
|
---|
1122 | snapDistanceSq *= snapDistanceSq;
|
---|
1123 |
|
---|
1124 | for (Way w : ds.searchWays(getBBox(p, Main.pref.getInteger("mappaint.segment.snap-distance", 10)))) {
|
---|
1125 | if (!predicate.test(w)) {
|
---|
1126 | continue;
|
---|
1127 | }
|
---|
1128 | Node lastN = null;
|
---|
1129 | int i = -2;
|
---|
1130 | for (Node n : w.getNodes()) {
|
---|
1131 | i++;
|
---|
1132 | if (n.isDeleted() || n.isIncomplete()) { //FIXME: This shouldn't happen, raise exception?
|
---|
1133 | continue;
|
---|
1134 | }
|
---|
1135 | if (lastN == null) {
|
---|
1136 | lastN = n;
|
---|
1137 | continue;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | Point2D pA = getPoint2D(lastN);
|
---|
1141 | Point2D pB = getPoint2D(n);
|
---|
1142 | double c = pA.distanceSq(pB);
|
---|
1143 | double a = p.distanceSq(pB);
|
---|
1144 | double b = p.distanceSq(pA);
|
---|
1145 |
|
---|
1146 | /* perpendicular distance squared
|
---|
1147 | * loose some precision to account for possible deviations in the calculation above
|
---|
1148 | * e.g. if identical (A and B) come about reversed in another way, values may differ
|
---|
1149 | * -- zero out least significant 32 dual digits of mantissa..
|
---|
1150 | */
|
---|
1151 | double perDistSq = Double.longBitsToDouble(
|
---|
1152 | Double.doubleToLongBits(a - (a - b + c) * (a - b + c) / 4 / c)
|
---|
1153 | >> 32 << 32); // resolution in numbers with large exponent not needed here..
|
---|
1154 |
|
---|
1155 | if (perDistSq < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
|
---|
1156 | List<WaySegment> wslist;
|
---|
1157 | if (nearestMap.containsKey(perDistSq)) {
|
---|
1158 | wslist = nearestMap.get(perDistSq);
|
---|
1159 | } else {
|
---|
1160 | wslist = new LinkedList<>();
|
---|
1161 | nearestMap.put(perDistSq, wslist);
|
---|
1162 | }
|
---|
1163 | wslist.add(new WaySegment(w, i));
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | lastN = n;
|
---|
1167 | }
|
---|
1168 | }
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | return nearestMap;
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | /**
|
---|
1175 | * The result *order* depends on the current map selection state.
|
---|
1176 | * Segments within 10px of p are searched and sorted by their distance to @param p,
|
---|
1177 | * then, within groups of equally distant segments, prefer those that are selected.
|
---|
1178 | *
|
---|
1179 | * @param p the point for which to search the nearest segments.
|
---|
1180 | * @param ignore a collection of segments which are not to be returned.
|
---|
1181 | * @param predicate the returned objects have to fulfill certain properties.
|
---|
1182 | *
|
---|
1183 | * @return all segments within 10px of p that are not in ignore,
|
---|
1184 | * sorted by their perpendicular distance.
|
---|
1185 | */
|
---|
1186 | public final List<WaySegment> getNearestWaySegments(Point p,
|
---|
1187 | Collection<WaySegment> ignore, Predicate<OsmPrimitive> predicate) {
|
---|
1188 | List<WaySegment> nearestList = new ArrayList<>();
|
---|
1189 | List<WaySegment> unselected = new LinkedList<>();
|
---|
1190 |
|
---|
1191 | for (List<WaySegment> wss : getNearestWaySegmentsImpl(p, predicate).values()) {
|
---|
1192 | // put selected waysegs within each distance group first
|
---|
1193 | // makes the order of nearestList dependent on current selection state
|
---|
1194 | for (WaySegment ws : wss) {
|
---|
1195 | (ws.way.isSelected() ? nearestList : unselected).add(ws);
|
---|
1196 | }
|
---|
1197 | nearestList.addAll(unselected);
|
---|
1198 | unselected.clear();
|
---|
1199 | }
|
---|
1200 | if (ignore != null) {
|
---|
1201 | nearestList.removeAll(ignore);
|
---|
1202 | }
|
---|
1203 |
|
---|
1204 | return nearestList;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | /**
|
---|
1208 | * The result *order* depends on the current map selection state.
|
---|
1209 | *
|
---|
1210 | * @param p the point for which to search the nearest segments.
|
---|
1211 | * @param predicate the returned objects have to fulfill certain properties.
|
---|
1212 | *
|
---|
1213 | * @return all segments within 10px of p, sorted by their perpendicular distance.
|
---|
1214 | * @see #getNearestWaySegments(Point, Collection, Predicate)
|
---|
1215 | */
|
---|
1216 | public final List<WaySegment> getNearestWaySegments(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1217 | return getNearestWaySegments(p, null, predicate);
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | /**
|
---|
1221 | * The *result* depends on the current map selection state IF use_selected is true.
|
---|
1222 | *
|
---|
1223 | * @param p the point for which to search the nearest segment.
|
---|
1224 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1225 | * @param useSelected whether selected way segments should be preferred.
|
---|
1226 | *
|
---|
1227 | * @return The nearest way segment to point p,
|
---|
1228 | * and, depending on use_selected, prefers a selected way segment, if found.
|
---|
1229 | * @see #getNearestWaySegments(Point, Collection, Predicate)
|
---|
1230 | */
|
---|
1231 | public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate, boolean useSelected) {
|
---|
1232 | WaySegment wayseg = null;
|
---|
1233 | WaySegment ntsel = null;
|
---|
1234 |
|
---|
1235 | for (List<WaySegment> wslist : getNearestWaySegmentsImpl(p, predicate).values()) {
|
---|
1236 | if (wayseg != null && ntsel != null) {
|
---|
1237 | break;
|
---|
1238 | }
|
---|
1239 | for (WaySegment ws : wslist) {
|
---|
1240 | if (wayseg == null) {
|
---|
1241 | wayseg = ws;
|
---|
1242 | }
|
---|
1243 | if (ntsel == null && ws.way.isSelected()) {
|
---|
1244 | ntsel = ws;
|
---|
1245 | }
|
---|
1246 | }
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | return (ntsel != null && useSelected) ? ntsel : wayseg;
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | /**
|
---|
1253 | * The *result* depends on the current map selection state IF use_selected is true.
|
---|
1254 | *
|
---|
1255 | * @param p the point for which to search the nearest segment.
|
---|
1256 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1257 | * @param useSelected whether selected way segments should be preferred.
|
---|
1258 | * @param preferredRefs - prefer segments related to these primitives, may be null
|
---|
1259 | *
|
---|
1260 | * @return The nearest way segment to point p,
|
---|
1261 | * and, depending on use_selected, prefers a selected way segment, if found.
|
---|
1262 | * Also prefers segments of ways that are related to one of preferredRefs primitives
|
---|
1263 | *
|
---|
1264 | * @see #getNearestWaySegments(Point, Collection, Predicate)
|
---|
1265 | * @since 6065
|
---|
1266 | */
|
---|
1267 | public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate,
|
---|
1268 | boolean useSelected, Collection<OsmPrimitive> preferredRefs) {
|
---|
1269 | WaySegment wayseg = null;
|
---|
1270 | WaySegment ntsel = null;
|
---|
1271 | WaySegment ntref = null;
|
---|
1272 | if (preferredRefs != null && preferredRefs.isEmpty())
|
---|
1273 | preferredRefs = null;
|
---|
1274 |
|
---|
1275 | searchLoop: for (List<WaySegment> wslist : getNearestWaySegmentsImpl(p, predicate).values()) {
|
---|
1276 | for (WaySegment ws : wslist) {
|
---|
1277 | if (wayseg == null) {
|
---|
1278 | wayseg = ws;
|
---|
1279 | }
|
---|
1280 | if (ntsel == null && ws.way.isSelected()) {
|
---|
1281 | ntsel = ws;
|
---|
1282 | break searchLoop;
|
---|
1283 | }
|
---|
1284 | if (ntref == null && preferredRefs != null) {
|
---|
1285 | // prefer ways containing given nodes
|
---|
1286 | for (Node nd: ws.way.getNodes()) {
|
---|
1287 | if (preferredRefs.contains(nd)) {
|
---|
1288 | ntref = ws;
|
---|
1289 | break searchLoop;
|
---|
1290 | }
|
---|
1291 | }
|
---|
1292 | Collection<OsmPrimitive> wayRefs = ws.way.getReferrers();
|
---|
1293 | // prefer member of the given relations
|
---|
1294 | for (OsmPrimitive ref: preferredRefs) {
|
---|
1295 | if (ref instanceof Relation && wayRefs.contains(ref)) {
|
---|
1296 | ntref = ws;
|
---|
1297 | break searchLoop;
|
---|
1298 | }
|
---|
1299 | }
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 | if (ntsel != null && useSelected)
|
---|
1304 | return ntsel;
|
---|
1305 | if (ntref != null)
|
---|
1306 | return ntref;
|
---|
1307 | return wayseg;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | /**
|
---|
1311 | * Convenience method to {@link #getNearestWaySegment(Point, Predicate, boolean)}.
|
---|
1312 | * @param p the point for which to search the nearest segment.
|
---|
1313 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1314 | *
|
---|
1315 | * @return The nearest way segment to point p.
|
---|
1316 | */
|
---|
1317 | public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1318 | return getNearestWaySegment(p, predicate, true);
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | /**
|
---|
1322 | * The *result* does not depend on the current map selection state,
|
---|
1323 | * neither does the result *order*.
|
---|
1324 | * It solely depends on the perpendicular distance to point p.
|
---|
1325 | *
|
---|
1326 | * @param p the point for which to search the nearest ways.
|
---|
1327 | * @param ignore a collection of ways which are not to be returned.
|
---|
1328 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1329 | *
|
---|
1330 | * @return all nearest ways to the screen point given that are not in ignore.
|
---|
1331 | * @see #getNearestWaySegments(Point, Collection, Predicate)
|
---|
1332 | */
|
---|
1333 | public final List<Way> getNearestWays(Point p,
|
---|
1334 | Collection<Way> ignore, Predicate<OsmPrimitive> predicate) {
|
---|
1335 | List<Way> nearestList = new ArrayList<>();
|
---|
1336 | Set<Way> wset = new HashSet<>();
|
---|
1337 |
|
---|
1338 | for (List<WaySegment> wss : getNearestWaySegmentsImpl(p, predicate).values()) {
|
---|
1339 | for (WaySegment ws : wss) {
|
---|
1340 | if (wset.add(ws.way)) {
|
---|
1341 | nearestList.add(ws.way);
|
---|
1342 | }
|
---|
1343 | }
|
---|
1344 | }
|
---|
1345 | if (ignore != null) {
|
---|
1346 | nearestList.removeAll(ignore);
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | return nearestList;
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | /**
|
---|
1353 | * The *result* does not depend on the current map selection state,
|
---|
1354 | * neither does the result *order*.
|
---|
1355 | * It solely depends on the perpendicular distance to point p.
|
---|
1356 | *
|
---|
1357 | * @param p the point for which to search the nearest ways.
|
---|
1358 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1359 | *
|
---|
1360 | * @return all nearest ways to the screen point given.
|
---|
1361 | * @see #getNearestWays(Point, Collection, Predicate)
|
---|
1362 | */
|
---|
1363 | public final List<Way> getNearestWays(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1364 | return getNearestWays(p, null, predicate);
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | /**
|
---|
1368 | * The *result* depends on the current map selection state.
|
---|
1369 | *
|
---|
1370 | * @param p the point for which to search the nearest segment.
|
---|
1371 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1372 | *
|
---|
1373 | * @return The nearest way to point p, prefer a selected way if there are multiple nearest.
|
---|
1374 | * @see #getNearestWaySegment(Point, Predicate)
|
---|
1375 | */
|
---|
1376 | public final Way getNearestWay(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1377 | WaySegment nearestWaySeg = getNearestWaySegment(p, predicate);
|
---|
1378 | return (nearestWaySeg == null) ? null : nearestWaySeg.way;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | /**
|
---|
1382 | * The *result* does not depend on the current map selection state,
|
---|
1383 | * neither does the result *order*.
|
---|
1384 | * It solely depends on the distance to point p.
|
---|
1385 | *
|
---|
1386 | * First, nodes will be searched. If there are nodes within BBox found,
|
---|
1387 | * return a collection of those nodes only.
|
---|
1388 | *
|
---|
1389 | * If no nodes are found, search for nearest ways. If there are ways
|
---|
1390 | * within BBox found, return a collection of those ways only.
|
---|
1391 | *
|
---|
1392 | * If nothing is found, return an empty collection.
|
---|
1393 | *
|
---|
1394 | * @param p The point on screen.
|
---|
1395 | * @param ignore a collection of ways which are not to be returned.
|
---|
1396 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1397 | *
|
---|
1398 | * @return Primitives nearest to the given screen point that are not in ignore.
|
---|
1399 | * @see #getNearestNodes(Point, Collection, Predicate)
|
---|
1400 | * @see #getNearestWays(Point, Collection, Predicate)
|
---|
1401 | */
|
---|
1402 | public final List<OsmPrimitive> getNearestNodesOrWays(Point p,
|
---|
1403 | Collection<OsmPrimitive> ignore, Predicate<OsmPrimitive> predicate) {
|
---|
1404 | List<OsmPrimitive> nearestList = Collections.emptyList();
|
---|
1405 | OsmPrimitive osm = getNearestNodeOrWay(p, predicate, false);
|
---|
1406 |
|
---|
1407 | if (osm != null) {
|
---|
1408 | if (osm instanceof Node) {
|
---|
1409 | nearestList = new ArrayList<>(getNearestNodes(p, predicate));
|
---|
1410 | } else if (osm instanceof Way) {
|
---|
1411 | nearestList = new ArrayList<>(getNearestWays(p, predicate));
|
---|
1412 | }
|
---|
1413 | if (ignore != null) {
|
---|
1414 | nearestList.removeAll(ignore);
|
---|
1415 | }
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | return nearestList;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | /**
|
---|
1422 | * The *result* does not depend on the current map selection state,
|
---|
1423 | * neither does the result *order*.
|
---|
1424 | * It solely depends on the distance to point p.
|
---|
1425 | *
|
---|
1426 | * @param p The point on screen.
|
---|
1427 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1428 | * @return Primitives nearest to the given screen point.
|
---|
1429 | * @see #getNearestNodesOrWays(Point, Collection, Predicate)
|
---|
1430 | */
|
---|
1431 | public final List<OsmPrimitive> getNearestNodesOrWays(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1432 | return getNearestNodesOrWays(p, null, predicate);
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | /**
|
---|
1436 | * This is used as a helper routine to {@link #getNearestNodeOrWay(Point, Predicate, boolean)}
|
---|
1437 | * It decides, whether to yield the node to be tested or look for further (way) candidates.
|
---|
1438 | *
|
---|
1439 | * @param osm node to check
|
---|
1440 | * @param p point clicked
|
---|
1441 | * @param useSelected whether to prefer selected nodes
|
---|
1442 | * @return true, if the node fulfills the properties of the function body
|
---|
1443 | */
|
---|
1444 | private boolean isPrecedenceNode(Node osm, Point p, boolean useSelected) {
|
---|
1445 | if (osm != null) {
|
---|
1446 | if (p.distanceSq(getPoint2D(osm)) <= (4*4)) return true;
|
---|
1447 | if (osm.isTagged()) return true;
|
---|
1448 | if (useSelected && osm.isSelected()) return true;
|
---|
1449 | }
|
---|
1450 | return false;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | /**
|
---|
1454 | * The *result* depends on the current map selection state IF use_selected is true.
|
---|
1455 | *
|
---|
1456 | * IF use_selected is true, use {@link #getNearestNode(Point, Predicate)} to find
|
---|
1457 | * the nearest, selected node. If not found, try {@link #getNearestWaySegment(Point, Predicate)}
|
---|
1458 | * to find the nearest selected way.
|
---|
1459 | *
|
---|
1460 | * IF use_selected is false, or if no selected primitive was found, do the following.
|
---|
1461 | *
|
---|
1462 | * If the nearest node found is within 4px of p, simply take it.
|
---|
1463 | * Else, find the nearest way segment. Then, if p is closer to its
|
---|
1464 | * middle than to the node, take the way segment, else take the node.
|
---|
1465 | *
|
---|
1466 | * Finally, if no nearest primitive is found at all, return null.
|
---|
1467 | *
|
---|
1468 | * @param p The point on screen.
|
---|
1469 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1470 | * @param useSelected whether to prefer primitives that are currently selected or referred by selected primitives
|
---|
1471 | *
|
---|
1472 | * @return A primitive within snap-distance to point p,
|
---|
1473 | * that is chosen by the algorithm described.
|
---|
1474 | * @see #getNearestNode(Point, Predicate)
|
---|
1475 | * @see #getNearestWay(Point, Predicate)
|
---|
1476 | */
|
---|
1477 | public final OsmPrimitive getNearestNodeOrWay(Point p, Predicate<OsmPrimitive> predicate, boolean useSelected) {
|
---|
1478 | Collection<OsmPrimitive> sel;
|
---|
1479 | DataSet ds = Main.getLayerManager().getEditDataSet();
|
---|
1480 | if (useSelected && ds != null) {
|
---|
1481 | sel = ds.getSelected();
|
---|
1482 | } else {
|
---|
1483 | sel = null;
|
---|
1484 | }
|
---|
1485 | OsmPrimitive osm = getNearestNode(p, predicate, useSelected, sel);
|
---|
1486 |
|
---|
1487 | if (isPrecedenceNode((Node) osm, p, useSelected)) return osm;
|
---|
1488 | WaySegment ws;
|
---|
1489 | if (useSelected) {
|
---|
1490 | ws = getNearestWaySegment(p, predicate, useSelected, sel);
|
---|
1491 | } else {
|
---|
1492 | ws = getNearestWaySegment(p, predicate, useSelected);
|
---|
1493 | }
|
---|
1494 | if (ws == null) return osm;
|
---|
1495 |
|
---|
1496 | if ((ws.way.isSelected() && useSelected) || osm == null) {
|
---|
1497 | // either (no _selected_ nearest node found, if desired) or no nearest node was found
|
---|
1498 | osm = ws.way;
|
---|
1499 | } else {
|
---|
1500 | int maxWaySegLenSq = 3*PROP_SNAP_DISTANCE.get();
|
---|
1501 | maxWaySegLenSq *= maxWaySegLenSq;
|
---|
1502 |
|
---|
1503 | Point2D wp1 = getPoint2D(ws.way.getNode(ws.lowerIndex));
|
---|
1504 | Point2D wp2 = getPoint2D(ws.way.getNode(ws.lowerIndex+1));
|
---|
1505 |
|
---|
1506 | // is wayseg shorter than maxWaySegLenSq and
|
---|
1507 | // is p closer to the middle of wayseg than to the nearest node?
|
---|
1508 | if (wp1.distanceSq(wp2) < maxWaySegLenSq &&
|
---|
1509 | p.distanceSq(project(0.5, wp1, wp2)) < p.distanceSq(getPoint2D((Node) osm))) {
|
---|
1510 | osm = ws.way;
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 | return osm;
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 | /**
|
---|
1517 | * if r = 0 returns a, if r=1 returns b,
|
---|
1518 | * if r = 0.5 returns center between a and b, etc..
|
---|
1519 | *
|
---|
1520 | * @param r scale value
|
---|
1521 | * @param a root of vector
|
---|
1522 | * @param b vector
|
---|
1523 | * @return new point at a + r*(ab)
|
---|
1524 | */
|
---|
1525 | public static Point2D project(double r, Point2D a, Point2D b) {
|
---|
1526 | Point2D ret = null;
|
---|
1527 |
|
---|
1528 | if (a != null && b != null) {
|
---|
1529 | ret = new Point2D.Double(a.getX() + r*(b.getX()-a.getX()),
|
---|
1530 | a.getY() + r*(b.getY()-a.getY()));
|
---|
1531 | }
|
---|
1532 | return ret;
|
---|
1533 | }
|
---|
1534 |
|
---|
1535 | /**
|
---|
1536 | * The *result* does not depend on the current map selection state, neither does the result *order*.
|
---|
1537 | * It solely depends on the distance to point p.
|
---|
1538 | *
|
---|
1539 | * @param p The point on screen.
|
---|
1540 | * @param ignore a collection of ways which are not to be returned.
|
---|
1541 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1542 | *
|
---|
1543 | * @return a list of all objects that are nearest to point p and
|
---|
1544 | * not in ignore or an empty list if nothing was found.
|
---|
1545 | */
|
---|
1546 | public final List<OsmPrimitive> getAllNearest(Point p,
|
---|
1547 | Collection<OsmPrimitive> ignore, Predicate<OsmPrimitive> predicate) {
|
---|
1548 | List<OsmPrimitive> nearestList = new ArrayList<>();
|
---|
1549 | Set<Way> wset = new HashSet<>();
|
---|
1550 |
|
---|
1551 | // add nearby ways
|
---|
1552 | for (List<WaySegment> wss : getNearestWaySegmentsImpl(p, predicate).values()) {
|
---|
1553 | for (WaySegment ws : wss) {
|
---|
1554 | if (wset.add(ws.way)) {
|
---|
1555 | nearestList.add(ws.way);
|
---|
1556 | }
|
---|
1557 | }
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | // add nearby nodes
|
---|
1561 | for (List<Node> nlist : getNearestNodesImpl(p, predicate).values()) {
|
---|
1562 | nearestList.addAll(nlist);
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | // add parent relations of nearby nodes and ways
|
---|
1566 | Set<OsmPrimitive> parentRelations = new HashSet<>();
|
---|
1567 | for (OsmPrimitive o : nearestList) {
|
---|
1568 | for (OsmPrimitive r : o.getReferrers()) {
|
---|
1569 | if (r instanceof Relation && predicate.test(r)) {
|
---|
1570 | parentRelations.add(r);
|
---|
1571 | }
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 | nearestList.addAll(parentRelations);
|
---|
1575 |
|
---|
1576 | if (ignore != null) {
|
---|
1577 | nearestList.removeAll(ignore);
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | return nearestList;
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | /**
|
---|
1584 | * The *result* does not depend on the current map selection state, neither does the result *order*.
|
---|
1585 | * It solely depends on the distance to point p.
|
---|
1586 | *
|
---|
1587 | * @param p The point on screen.
|
---|
1588 | * @param predicate the returned object has to fulfill certain properties.
|
---|
1589 | *
|
---|
1590 | * @return a list of all objects that are nearest to point p
|
---|
1591 | * or an empty list if nothing was found.
|
---|
1592 | * @see #getAllNearest(Point, Collection, Predicate)
|
---|
1593 | */
|
---|
1594 | public final List<OsmPrimitive> getAllNearest(Point p, Predicate<OsmPrimitive> predicate) {
|
---|
1595 | return getAllNearest(p, null, predicate);
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | /**
|
---|
1599 | * @return The projection to be used in calculating stuff.
|
---|
1600 | */
|
---|
1601 | public Projection getProjection() {
|
---|
1602 | return state.getProjection();
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 | @Override
|
---|
1606 | public String helpTopic() {
|
---|
1607 | String n = getClass().getName();
|
---|
1608 | return n.substring(n.lastIndexOf('.')+1);
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | /**
|
---|
1612 | * Return a ID which is unique as long as viewport dimensions are the same
|
---|
1613 | * @return A unique ID, as long as viewport dimensions are the same
|
---|
1614 | */
|
---|
1615 | public int getViewID() {
|
---|
1616 | EastNorth center = getCenter();
|
---|
1617 | String x = new StringBuilder().append(center.east())
|
---|
1618 | .append('_').append(center.north())
|
---|
1619 | .append('_').append(getScale())
|
---|
1620 | .append('_').append(getWidth())
|
---|
1621 | .append('_').append(getHeight())
|
---|
1622 | .append('_').append(getProjection()).toString();
|
---|
1623 | CRC32 id = new CRC32();
|
---|
1624 | id.update(x.getBytes(StandardCharsets.UTF_8));
|
---|
1625 | return (int) id.getValue();
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | /**
|
---|
1629 | * Set new cursor.
|
---|
1630 | * @param cursor The new cursor to use.
|
---|
1631 | * @param reference A reference object that can be passed to the next set/reset calls to identify the caller.
|
---|
1632 | */
|
---|
1633 | public void setNewCursor(Cursor cursor, Object reference) {
|
---|
1634 | cursorManager.setNewCursor(cursor, reference);
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | /**
|
---|
1638 | * Set new cursor.
|
---|
1639 | * @param cursor the type of predefined cursor
|
---|
1640 | * @param reference A reference object that can be passed to the next set/reset calls to identify the caller.
|
---|
1641 | */
|
---|
1642 | public void setNewCursor(int cursor, Object reference) {
|
---|
1643 | setNewCursor(Cursor.getPredefinedCursor(cursor), reference);
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | /**
|
---|
1647 | * Remove the new cursor and reset to previous
|
---|
1648 | * @param reference Cursor reference
|
---|
1649 | */
|
---|
1650 | public void resetCursor(Object reference) {
|
---|
1651 | cursorManager.resetCursor(reference);
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | /**
|
---|
1655 | * Gets the cursor manager that is used for this NavigatableComponent.
|
---|
1656 | * @return The cursor manager.
|
---|
1657 | */
|
---|
1658 | public CursorManager getCursorManager() {
|
---|
1659 | return cursorManager;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | /**
|
---|
1663 | * Get a max scale for projection that describes world in 1/512 of the projection unit
|
---|
1664 | * @return max scale
|
---|
1665 | */
|
---|
1666 | public double getMaxScale() {
|
---|
1667 | ProjectionBounds world = getMaxProjectionBounds();
|
---|
1668 | return Math.max(
|
---|
1669 | world.maxNorth-world.minNorth,
|
---|
1670 | world.maxEast-world.minEast
|
---|
1671 | )/512;
|
---|
1672 | }
|
---|
1673 | }
|
---|