1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.awt.geom.Area;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Objects;
|
---|
8 | import java.util.Set;
|
---|
9 | import java.util.TreeSet;
|
---|
10 | import java.util.function.Predicate;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
|
---|
16 | import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
|
---|
17 | import org.openstreetmap.josm.data.projection.Projecting;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * One node data, consisting of one world coordinate waypoint.
|
---|
23 | *
|
---|
24 | * @author imi
|
---|
25 | */
|
---|
26 | public final class Node extends OsmPrimitive implements INode {
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * We "inline" lat/lon rather than using a LatLon-object => reduces memory footprint
|
---|
30 | */
|
---|
31 | private double lat = Double.NaN;
|
---|
32 | private double lon = Double.NaN;
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * the cached projected coordinates
|
---|
36 | */
|
---|
37 | private double east = Double.NaN;
|
---|
38 | private double north = Double.NaN;
|
---|
39 | /**
|
---|
40 | * The cache key to use for {@link #east} and {@link #north}.
|
---|
41 | */
|
---|
42 | private Object eastNorthCacheKey;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Determines if this node has valid coordinates.
|
---|
46 | * @return {@code true} if this node has valid coordinates
|
---|
47 | * @since 7828
|
---|
48 | */
|
---|
49 | @Override
|
---|
50 | public boolean isLatLonKnown() {
|
---|
51 | // We cannot use default implementation - if we remove this implementation, we will break binary compatibility.
|
---|
52 | return !Double.isNaN(lat) && !Double.isNaN(lon);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void setCoor(LatLon coor) {
|
---|
57 | updateCoor(coor, null);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void setEastNorth(EastNorth eastNorth) {
|
---|
62 | updateCoor(null, eastNorth);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void updateCoor(LatLon coor, EastNorth eastNorth) {
|
---|
66 | if (getDataSet() != null) {
|
---|
67 | boolean locked = writeLock();
|
---|
68 | try {
|
---|
69 | getDataSet().fireNodeMoved(this, coor, eastNorth);
|
---|
70 | } finally {
|
---|
71 | writeUnlock(locked);
|
---|
72 | }
|
---|
73 | } else {
|
---|
74 | setCoorInternal(coor, eastNorth);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Returns lat/lon coordinates of this node, or {@code null} unless {@link #isLatLonKnown()}
|
---|
80 | * @return lat/lon coordinates of this node, or {@code null} unless {@link #isLatLonKnown()}
|
---|
81 | */
|
---|
82 | @Override
|
---|
83 | public LatLon getCoor() {
|
---|
84 | if (!isLatLonKnown()) {
|
---|
85 | return null;
|
---|
86 | } else {
|
---|
87 | return new LatLon(lat, lon);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public double lat() {
|
---|
93 | return lat;
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public double lon() {
|
---|
98 | return lon;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Replies the projected east/north coordinates.
|
---|
103 | * <p>
|
---|
104 | * Uses the {@link Main#getProjection() global projection} to project the lat/lon-coordinates.
|
---|
105 | * <p>
|
---|
106 | * Method {@link org.openstreetmap.josm.data.coor.ILatLon#getEastNorth()} of
|
---|
107 | * implemented interface <code>ILatLon</code> is deprecated, but this method is not.
|
---|
108 | * @return the east north coordinates or {@code null} if {@link #isLatLonKnown()}
|
---|
109 | * is false.
|
---|
110 | */
|
---|
111 | @SuppressWarnings("deprecation")
|
---|
112 | @Override
|
---|
113 | public EastNorth getEastNorth() {
|
---|
114 | return getEastNorth(Main.getProjection());
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public EastNorth getEastNorth(Projecting projection) {
|
---|
119 | if (!isLatLonKnown()) return null;
|
---|
120 |
|
---|
121 | if (Double.isNaN(east) || Double.isNaN(north) || !Objects.equals(projection.getCacheKey(), eastNorthCacheKey)) {
|
---|
122 | // projected coordinates haven't been calculated yet,
|
---|
123 | // so fill the cache of the projected node coordinates
|
---|
124 | EastNorth en = projection.latlon2eastNorth(this);
|
---|
125 | this.east = en.east();
|
---|
126 | this.north = en.north();
|
---|
127 | this.eastNorthCacheKey = projection.getCacheKey();
|
---|
128 | }
|
---|
129 | return new EastNorth(east, north);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * To be used only by Dataset.reindexNode
|
---|
134 | * @param coor lat/lon
|
---|
135 | * @param eastNorth east/north
|
---|
136 | */
|
---|
137 | void setCoorInternal(LatLon coor, EastNorth eastNorth) {
|
---|
138 | if (coor != null) {
|
---|
139 | this.lat = coor.lat();
|
---|
140 | this.lon = coor.lon();
|
---|
141 | invalidateEastNorthCache();
|
---|
142 | } else if (eastNorth != null) {
|
---|
143 | LatLon ll = Main.getProjection().eastNorth2latlon(eastNorth);
|
---|
144 | this.lat = ll.lat();
|
---|
145 | this.lon = ll.lon();
|
---|
146 | this.east = eastNorth.east();
|
---|
147 | this.north = eastNorth.north();
|
---|
148 | this.eastNorthCacheKey = Main.getProjection().getCacheKey();
|
---|
149 | } else {
|
---|
150 | this.lat = Double.NaN;
|
---|
151 | this.lon = Double.NaN;
|
---|
152 | invalidateEastNorthCache();
|
---|
153 | if (isVisible()) {
|
---|
154 | setIncomplete(true);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | protected Node(long id, boolean allowNegative) {
|
---|
160 | super(id, allowNegative);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Constructs a new local {@code Node} with id 0.
|
---|
165 | */
|
---|
166 | public Node() {
|
---|
167 | this(0, false);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Constructs an incomplete {@code Node} object with the given id.
|
---|
172 | * @param id The id. Must be >= 0
|
---|
173 | * @throws IllegalArgumentException if id < 0
|
---|
174 | */
|
---|
175 | public Node(long id) {
|
---|
176 | super(id, false);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Constructs a new {@code Node} with the given id and version.
|
---|
181 | * @param id The id. Must be >= 0
|
---|
182 | * @param version The version
|
---|
183 | * @throws IllegalArgumentException if id < 0
|
---|
184 | */
|
---|
185 | public Node(long id, int version) {
|
---|
186 | super(id, version, false);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Constructs an identical clone of the argument.
|
---|
191 | * @param clone The node to clone
|
---|
192 | * @param clearMetadata If {@code true}, clears the OSM id and other metadata as defined by {@link #clearOsmMetadata}.
|
---|
193 | * If {@code false}, does nothing
|
---|
194 | */
|
---|
195 | public Node(Node clone, boolean clearMetadata) {
|
---|
196 | super(clone.getUniqueId(), true /* allow negative IDs */);
|
---|
197 | cloneFrom(clone);
|
---|
198 | if (clearMetadata) {
|
---|
199 | clearOsmMetadata();
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Constructs an identical clone of the argument (including the id).
|
---|
205 | * @param clone The node to clone, including its id
|
---|
206 | */
|
---|
207 | public Node(Node clone) {
|
---|
208 | this(clone, false);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Constructs a new {@code Node} with the given lat/lon with id 0.
|
---|
213 | * @param latlon The {@link LatLon} coordinates
|
---|
214 | */
|
---|
215 | public Node(LatLon latlon) {
|
---|
216 | super(0, false);
|
---|
217 | setCoor(latlon);
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Constructs a new {@code Node} with the given east/north with id 0.
|
---|
222 | * @param eastNorth The {@link EastNorth} coordinates
|
---|
223 | */
|
---|
224 | public Node(EastNorth eastNorth) {
|
---|
225 | super(0, false);
|
---|
226 | setEastNorth(eastNorth);
|
---|
227 | }
|
---|
228 |
|
---|
229 | @Override
|
---|
230 | void setDataset(DataSet dataSet) {
|
---|
231 | super.setDataset(dataSet);
|
---|
232 | if (!isIncomplete() && isVisible() && !isLatLonKnown())
|
---|
233 | throw new DataIntegrityProblemException("Complete node with null coordinates: " + toString());
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * @deprecated no longer supported
|
---|
238 | */
|
---|
239 | @Override
|
---|
240 | @Deprecated
|
---|
241 | public void accept(org.openstreetmap.josm.data.osm.visitor.Visitor visitor) {
|
---|
242 | visitor.visit(this);
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Override
|
---|
246 | public void accept(OsmPrimitiveVisitor visitor) {
|
---|
247 | visitor.visit(this);
|
---|
248 | }
|
---|
249 |
|
---|
250 | @Override
|
---|
251 | public void accept(PrimitiveVisitor visitor) {
|
---|
252 | visitor.visit(this);
|
---|
253 | }
|
---|
254 |
|
---|
255 | @Override
|
---|
256 | public void cloneFrom(OsmPrimitive osm) {
|
---|
257 | if (!(osm instanceof Node))
|
---|
258 | throw new IllegalArgumentException("Not a node: " + osm);
|
---|
259 | boolean locked = writeLock();
|
---|
260 | try {
|
---|
261 | super.cloneFrom(osm);
|
---|
262 | setCoor(((Node) osm).getCoor());
|
---|
263 | } finally {
|
---|
264 | writeUnlock(locked);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Merges the technical and semantical attributes from <code>other</code> onto this.
|
---|
270 | *
|
---|
271 | * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
|
---|
272 | * have an assigend OSM id, the IDs have to be the same.
|
---|
273 | *
|
---|
274 | * @param other the other primitive. Must not be null.
|
---|
275 | * @throws IllegalArgumentException if other is null.
|
---|
276 | * @throws DataIntegrityProblemException if either this is new and other is not, or other is new and this is not
|
---|
277 | * @throws DataIntegrityProblemException if other is new and other.getId() != this.getId()
|
---|
278 | */
|
---|
279 | @Override
|
---|
280 | public void mergeFrom(OsmPrimitive other) {
|
---|
281 | if (!(other instanceof Node))
|
---|
282 | throw new IllegalArgumentException("Not a node: " + other);
|
---|
283 | boolean locked = writeLock();
|
---|
284 | try {
|
---|
285 | super.mergeFrom(other);
|
---|
286 | if (!other.isIncomplete()) {
|
---|
287 | setCoor(((Node) other).getCoor());
|
---|
288 | }
|
---|
289 | } finally {
|
---|
290 | writeUnlock(locked);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void load(PrimitiveData data) {
|
---|
296 | if (!(data instanceof NodeData))
|
---|
297 | throw new IllegalArgumentException("Not a node data: " + data);
|
---|
298 | boolean locked = writeLock();
|
---|
299 | try {
|
---|
300 | super.load(data);
|
---|
301 | setCoor(((NodeData) data).getCoor());
|
---|
302 | } finally {
|
---|
303 | writeUnlock(locked);
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | @Override
|
---|
308 | public NodeData save() {
|
---|
309 | NodeData data = new NodeData();
|
---|
310 | saveCommonAttributes(data);
|
---|
311 | if (!isIncomplete()) {
|
---|
312 | data.setCoor(getCoor());
|
---|
313 | }
|
---|
314 | return data;
|
---|
315 | }
|
---|
316 |
|
---|
317 | @Override
|
---|
318 | public String toString() {
|
---|
319 | String coorDesc = isLatLonKnown() ? "lat="+lat+",lon="+lon : "";
|
---|
320 | return "{Node id=" + getUniqueId() + " version=" + getVersion() + ' ' + getFlagsAsString() + ' ' + coorDesc+'}';
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Override
|
---|
324 | public boolean hasEqualSemanticAttributes(OsmPrimitive other, boolean testInterestingTagsOnly) {
|
---|
325 | return (other instanceof Node)
|
---|
326 | && hasEqualSemanticFlags(other)
|
---|
327 | && hasEqualCoordinates((Node) other)
|
---|
328 | && super.hasEqualSemanticAttributes(other, testInterestingTagsOnly);
|
---|
329 | }
|
---|
330 |
|
---|
331 | private boolean hasEqualCoordinates(Node other) {
|
---|
332 | final LatLon c1 = getCoor();
|
---|
333 | final LatLon c2 = other.getCoor();
|
---|
334 | return (c1 == null && c2 == null) || (c1 != null && c2 != null && c1.equalsEpsilon(c2));
|
---|
335 | }
|
---|
336 |
|
---|
337 | @Override
|
---|
338 | public int compareTo(OsmPrimitive o) {
|
---|
339 | return o instanceof Node ? Long.compare(getUniqueId(), o.getUniqueId()) : 1;
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Override
|
---|
343 | public String getDisplayName(NameFormatter formatter) {
|
---|
344 | return formatter.format(this);
|
---|
345 | }
|
---|
346 |
|
---|
347 | @Override
|
---|
348 | public OsmPrimitiveType getType() {
|
---|
349 | return OsmPrimitiveType.NODE;
|
---|
350 | }
|
---|
351 |
|
---|
352 | @Override
|
---|
353 | public BBox getBBox() {
|
---|
354 | return new BBox(lon, lat);
|
---|
355 | }
|
---|
356 |
|
---|
357 | @Override
|
---|
358 | protected void addToBBox(BBox box, Set<PrimitiveId> visited) {
|
---|
359 | box.add(lon, lat);
|
---|
360 | }
|
---|
361 |
|
---|
362 | @Override
|
---|
363 | public void updatePosition() {
|
---|
364 | // Do nothing
|
---|
365 | }
|
---|
366 |
|
---|
367 | @Override
|
---|
368 | public boolean isDrawable() {
|
---|
369 | // Not possible to draw a node without coordinates.
|
---|
370 | return super.isDrawable() && isLatLonKnown();
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Check whether this node connects 2 ways.
|
---|
375 | *
|
---|
376 | * @return true if isReferredByWays(2) returns true
|
---|
377 | * @see #isReferredByWays(int)
|
---|
378 | */
|
---|
379 | public boolean isConnectionNode() {
|
---|
380 | return isReferredByWays(2);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Invoke to invalidate the internal cache of projected east/north coordinates.
|
---|
385 | * Coordinates are reprojected on demand when the {@link #getEastNorth()} is invoked
|
---|
386 | * next time.
|
---|
387 | */
|
---|
388 | public void invalidateEastNorthCache() {
|
---|
389 | this.east = Double.NaN;
|
---|
390 | this.north = Double.NaN;
|
---|
391 | this.eastNorthCacheKey = null;
|
---|
392 | }
|
---|
393 |
|
---|
394 | @Override
|
---|
395 | public boolean concernsArea() {
|
---|
396 | // A node cannot be an area
|
---|
397 | return false;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Tests whether {@code this} node is connected to {@code otherNode} via at most {@code hops} nodes
|
---|
402 | * matching the {@code predicate} (which may be {@code null} to consider all nodes).
|
---|
403 | * @param otherNodes other nodes
|
---|
404 | * @param hops number of hops
|
---|
405 | * @param predicate predicate to match
|
---|
406 | * @return {@code true} if {@code this} node mets the conditions
|
---|
407 | */
|
---|
408 | public boolean isConnectedTo(final Collection<Node> otherNodes, final int hops, Predicate<Node> predicate) {
|
---|
409 | CheckParameterUtil.ensureParameterNotNull(otherNodes);
|
---|
410 | CheckParameterUtil.ensureThat(!otherNodes.isEmpty(), "otherNodes must not be empty!");
|
---|
411 | CheckParameterUtil.ensureThat(hops >= 0, "hops must be non-negative!");
|
---|
412 | return hops == 0
|
---|
413 | ? isConnectedTo(otherNodes, hops, predicate, null)
|
---|
414 | : isConnectedTo(otherNodes, hops, predicate, new TreeSet<>());
|
---|
415 | }
|
---|
416 |
|
---|
417 | private boolean isConnectedTo(final Collection<Node> otherNodes, final int hops, Predicate<Node> predicate, Set<Node> visited) {
|
---|
418 | if (otherNodes.contains(this)) {
|
---|
419 | return true;
|
---|
420 | }
|
---|
421 | if (hops > 0 && visited != null) {
|
---|
422 | visited.add(this);
|
---|
423 | for (final Way w : Utils.filteredCollection(this.getReferrers(), Way.class)) {
|
---|
424 | for (final Node n : w.getNodes()) {
|
---|
425 | final boolean containsN = visited.contains(n);
|
---|
426 | visited.add(n);
|
---|
427 | if (!containsN && (predicate == null || predicate.test(n))
|
---|
428 | && n.isConnectedTo(otherNodes, hops - 1, predicate, visited)) {
|
---|
429 | return true;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 | return false;
|
---|
435 | }
|
---|
436 |
|
---|
437 | @Override
|
---|
438 | public boolean isOutsideDownloadArea() {
|
---|
439 | if (isNewOrUndeleted() || getDataSet() == null)
|
---|
440 | return false;
|
---|
441 | Area area = getDataSet().getDataSourceArea();
|
---|
442 | if (area == null)
|
---|
443 | return false;
|
---|
444 | LatLon coor = getCoor();
|
---|
445 | return coor != null && !coor.isIn(area);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * Replies the set of referring ways.
|
---|
450 | * @return the set of referring ways
|
---|
451 | * @since 12031
|
---|
452 | */
|
---|
453 | public List<Way> getParentWays() {
|
---|
454 | return getFilteredList(getReferrers(), Way.class);
|
---|
455 | }
|
---|
456 | }
|
---|