1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.text.MessageFormat;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.Date;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.LinkedHashSet;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Locale;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Objects;
|
---|
20 | import java.util.Set;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
---|
23 | import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
|
---|
24 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
---|
25 | import org.openstreetmap.josm.data.osm.visitor.OsmPrimitiveVisitor;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.StyleCache;
|
---|
27 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
28 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
29 | import org.openstreetmap.josm.tools.Logging;
|
---|
30 | import org.openstreetmap.josm.tools.Utils;
|
---|
31 | import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * The base class for OSM objects ({@link Node}, {@link Way}, {@link Relation}).
|
---|
35 | *
|
---|
36 | * It can be created, deleted and uploaded to the OSM-Server.
|
---|
37 | *
|
---|
38 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
39 | * it by any other than from the package {@link org.openstreetmap.josm.data.osm}. The available primitives are a fixed set that are given
|
---|
40 | * by the server environment and not an extendible data stuff.
|
---|
41 | *
|
---|
42 | * @author imi
|
---|
43 | */
|
---|
44 | public abstract class OsmPrimitive extends AbstractPrimitive implements Comparable<OsmPrimitive>, TemplateEngineDataProvider {
|
---|
45 | private static final String SPECIAL_VALUE_ID = "id";
|
---|
46 | private static final String SPECIAL_VALUE_LOCAL_NAME = "localname";
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * A tagged way that matches this pattern has a direction.
|
---|
50 | * @see #FLAG_HAS_DIRECTIONS
|
---|
51 | */
|
---|
52 | static volatile Match directionKeys;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * A tagged way that matches this pattern has a direction that is reversed.
|
---|
56 | * <p>
|
---|
57 | * This pattern should be a subset of {@link #directionKeys}
|
---|
58 | * @see #FLAG_DIRECTION_REVERSED
|
---|
59 | */
|
---|
60 | private static volatile Match reversedDirectionKeys;
|
---|
61 |
|
---|
62 | static {
|
---|
63 | String reversedDirectionDefault = "oneway=\"-1\"";
|
---|
64 |
|
---|
65 | String directionDefault = "oneway? | "+
|
---|
66 | "(aerialway=chair_lift & -oneway=no) | "+
|
---|
67 | "(aerialway=rope_tow & -oneway=no) | "+
|
---|
68 | "(aerialway=magic_carpet & -oneway=no) | "+
|
---|
69 | "(aerialway=zip_line & -oneway=no) | "+
|
---|
70 | "(aerialway=drag_lift & -oneway=no) | "+
|
---|
71 | "(aerialway=t-bar & -oneway=no) | "+
|
---|
72 | "(aerialway=j-bar & -oneway=no) | "+
|
---|
73 | "(aerialway=platter & -oneway=no) | "+
|
---|
74 | "waterway=stream | waterway=river | waterway=ditch | waterway=drain | "+
|
---|
75 | "(\"piste:type\"=downhill & -area=yes) | (\"piste:type\"=sled & -area=yes) | (man_made=\"piste:halfpipe\" & -area=yes) | "+
|
---|
76 | "junction=roundabout | (highway=motorway & -oneway=no & -oneway=reversible) | "+
|
---|
77 | "(highway=motorway_link & -oneway=no & -oneway=reversible)";
|
---|
78 |
|
---|
79 | reversedDirectionKeys = compileDirectionKeys("tags.reversed_direction", reversedDirectionDefault);
|
---|
80 | directionKeys = compileDirectionKeys("tags.direction", directionDefault);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Replies the sub-collection of {@link OsmPrimitive}s of type <code>type</code> present in
|
---|
85 | * another collection of {@link OsmPrimitive}s. The result collection is a list.
|
---|
86 | *
|
---|
87 | * If <code>list</code> is null, replies an empty list.
|
---|
88 | *
|
---|
89 | * @param <T> type of data (must be one of the {@link OsmPrimitive} types
|
---|
90 | * @param list the original list
|
---|
91 | * @param type the type to filter for
|
---|
92 | * @return the sub-list of OSM primitives of type <code>type</code>
|
---|
93 | */
|
---|
94 | public static <T extends OsmPrimitive> List<T> getFilteredList(Collection<OsmPrimitive> list, Class<T> type) {
|
---|
95 | if (list == null) return Collections.emptyList();
|
---|
96 | List<T> ret = new LinkedList<>();
|
---|
97 | for (OsmPrimitive p: list) {
|
---|
98 | if (type.isInstance(p)) {
|
---|
99 | ret.add(type.cast(p));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return ret;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Replies the sub-collection of {@link OsmPrimitive}s of type <code>type</code> present in
|
---|
107 | * another collection of {@link OsmPrimitive}s. The result collection is a set.
|
---|
108 | *
|
---|
109 | * If <code>list</code> is null, replies an empty set.
|
---|
110 | *
|
---|
111 | * @param <T> type of data (must be one of the {@link OsmPrimitive} types
|
---|
112 | * @param set the original collection
|
---|
113 | * @param type the type to filter for
|
---|
114 | * @return the sub-set of OSM primitives of type <code>type</code>
|
---|
115 | */
|
---|
116 | public static <T extends OsmPrimitive> Set<T> getFilteredSet(Collection<OsmPrimitive> set, Class<T> type) {
|
---|
117 | Set<T> ret = new LinkedHashSet<>();
|
---|
118 | if (set != null) {
|
---|
119 | for (OsmPrimitive p: set) {
|
---|
120 | if (type.isInstance(p)) {
|
---|
121 | ret.add(type.cast(p));
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return ret;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Replies the collection of referring primitives for the primitives in <code>primitives</code>.
|
---|
130 | *
|
---|
131 | * @param primitives the collection of primitives.
|
---|
132 | * @return the collection of referring primitives for the primitives in <code>primitives</code>;
|
---|
133 | * empty set if primitives is null or if there are no referring primitives
|
---|
134 | */
|
---|
135 | public static Set<OsmPrimitive> getReferrer(Collection<? extends OsmPrimitive> primitives) {
|
---|
136 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
137 | if (primitives == null || primitives.isEmpty()) return ret;
|
---|
138 | for (OsmPrimitive p: primitives) {
|
---|
139 | ret.addAll(p.getReferrers());
|
---|
140 | }
|
---|
141 | return ret;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Creates a new primitive for the given id.
|
---|
146 | *
|
---|
147 | * If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing.
|
---|
148 | * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
|
---|
149 | * positive number.
|
---|
150 | *
|
---|
151 | * @param id the id
|
---|
152 | * @param allowNegativeId {@code true} to allow negative id
|
---|
153 | * @throws IllegalArgumentException if id < 0 and allowNegativeId is false
|
---|
154 | */
|
---|
155 | protected OsmPrimitive(long id, boolean allowNegativeId) {
|
---|
156 | if (allowNegativeId) {
|
---|
157 | this.id = id;
|
---|
158 | } else {
|
---|
159 | if (id < 0)
|
---|
160 | throw new IllegalArgumentException(MessageFormat.format("Expected ID >= 0. Got {0}.", id));
|
---|
161 | else if (id == 0) {
|
---|
162 | this.id = generateUniqueId();
|
---|
163 | } else {
|
---|
164 | this.id = id;
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|
168 | this.version = 0;
|
---|
169 | this.setIncomplete(id > 0);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Creates a new primitive for the given id and version.
|
---|
174 | *
|
---|
175 | * If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing.
|
---|
176 | * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
|
---|
177 | * positive number.
|
---|
178 | *
|
---|
179 | * If id is not > 0 version is ignored and set to 0.
|
---|
180 | *
|
---|
181 | * @param id the id
|
---|
182 | * @param version the version (positive integer)
|
---|
183 | * @param allowNegativeId {@code true} to allow negative id
|
---|
184 | * @throws IllegalArgumentException if id < 0 and allowNegativeId is false
|
---|
185 | */
|
---|
186 | protected OsmPrimitive(long id, int version, boolean allowNegativeId) {
|
---|
187 | this(id, allowNegativeId);
|
---|
188 | this.version = id > 0 ? version : 0;
|
---|
189 | setIncomplete(id > 0 && version == 0);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /*----------
|
---|
193 | * MAPPAINT
|
---|
194 | *--------*/
|
---|
195 | public StyleCache mappaintStyle;
|
---|
196 | private short mappaintCacheIdx;
|
---|
197 |
|
---|
198 | /* This should not be called from outside. Fixing the UI to add relevant
|
---|
199 | get/set functions calling this implicitely is preferred, so we can have
|
---|
200 | transparent cache handling in the future. */
|
---|
201 | public void clearCachedStyle() {
|
---|
202 | mappaintStyle = null;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Check if the cached style for this primitive is up to date.
|
---|
207 | * @return true if the cached style for this primitive is up to date
|
---|
208 | * @since 13420
|
---|
209 | */
|
---|
210 | public final boolean isCachedStyleUpToDate() {
|
---|
211 | return mappaintStyle != null && mappaintCacheIdx == dataSet.getMappaintCacheIndex();
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Declare that the cached style for this primitive is up to date.
|
---|
216 | * @since 13420
|
---|
217 | */
|
---|
218 | public final void declareCachedStyleUpToDate() {
|
---|
219 | this.mappaintCacheIdx = dataSet.getMappaintCacheIndex();
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Returns mappaint cache index.
|
---|
224 | * @return mappaint cache index
|
---|
225 | * @deprecated no longer supported (see also {@link #isCachedStyleUpToDate()})
|
---|
226 | */
|
---|
227 | @Deprecated
|
---|
228 | public final short getMappaintCacheIdx() {
|
---|
229 | return mappaintCacheIdx;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Sets the mappaint cache index.
|
---|
234 | * @param mappaintCacheIdx mappaint cache index
|
---|
235 | * @deprecated no longer supported (see also {@link #declareCachedStyleUpToDate()})
|
---|
236 | */
|
---|
237 | @Deprecated
|
---|
238 | public final void setMappaintCacheIdx(short mappaintCacheIdx) {
|
---|
239 | this.mappaintCacheIdx = mappaintCacheIdx;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /* end of mappaint data */
|
---|
243 |
|
---|
244 | /*---------
|
---|
245 | * DATASET
|
---|
246 | *---------*/
|
---|
247 |
|
---|
248 | /** the parent dataset */
|
---|
249 | private DataSet dataSet;
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * This method should never ever by called from somewhere else than Dataset.addPrimitive or removePrimitive methods
|
---|
253 | * @param dataSet the parent dataset
|
---|
254 | */
|
---|
255 | void setDataset(DataSet dataSet) {
|
---|
256 | if (this.dataSet != null && dataSet != null && this.dataSet != dataSet)
|
---|
257 | throw new DataIntegrityProblemException("Primitive cannot be included in more than one Dataset");
|
---|
258 | this.dataSet = dataSet;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | *
|
---|
263 | * @return DataSet this primitive is part of.
|
---|
264 | */
|
---|
265 | public DataSet getDataSet() {
|
---|
266 | return dataSet;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Throws exception if primitive is not part of the dataset
|
---|
271 | */
|
---|
272 | public void checkDataset() {
|
---|
273 | if (dataSet == null)
|
---|
274 | throw new DataIntegrityProblemException("Primitive must be part of the dataset: " + toString());
|
---|
275 | }
|
---|
276 |
|
---|
277 | protected boolean writeLock() {
|
---|
278 | if (dataSet != null) {
|
---|
279 | dataSet.beginUpdate();
|
---|
280 | return true;
|
---|
281 | } else
|
---|
282 | return false;
|
---|
283 | }
|
---|
284 |
|
---|
285 | protected void writeUnlock(boolean locked) {
|
---|
286 | if (locked) {
|
---|
287 | // It shouldn't be possible for dataset to become null because
|
---|
288 | // method calling setDataset would need write lock which is owned by this thread
|
---|
289 | dataSet.endUpdate();
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Sets the id and the version of this primitive if it is known to the OSM API.
|
---|
295 | *
|
---|
296 | * Since we know the id and its version it can't be incomplete anymore. incomplete
|
---|
297 | * is set to false.
|
---|
298 | *
|
---|
299 | * @param id the id. > 0 required
|
---|
300 | * @param version the version > 0 required
|
---|
301 | * @throws IllegalArgumentException if id <= 0
|
---|
302 | * @throws IllegalArgumentException if version <= 0
|
---|
303 | * @throws DataIntegrityProblemException if id is changed and primitive was already added to the dataset
|
---|
304 | */
|
---|
305 | @Override
|
---|
306 | public void setOsmId(long id, int version) {
|
---|
307 | boolean locked = writeLock();
|
---|
308 | try {
|
---|
309 | if (id <= 0)
|
---|
310 | throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
|
---|
311 | if (version <= 0)
|
---|
312 | throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
|
---|
313 | if (dataSet != null && id != this.id) {
|
---|
314 | DataSet datasetCopy = dataSet;
|
---|
315 | // Reindex primitive
|
---|
316 | datasetCopy.removePrimitive(this);
|
---|
317 | this.id = id;
|
---|
318 | datasetCopy.addPrimitive(this);
|
---|
319 | }
|
---|
320 | super.setOsmId(id, version);
|
---|
321 | } finally {
|
---|
322 | writeUnlock(locked);
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Clears the metadata, including id and version known to the OSM API.
|
---|
328 | * The id is a new unique id. The version, changeset and timestamp are set to 0.
|
---|
329 | * incomplete and deleted are set to false. It's preferred to use copy constructor with clearMetadata set to true instead
|
---|
330 | *
|
---|
331 | * <strong>Caution</strong>: Do not use this method on primitives which are already added to a {@link DataSet}.
|
---|
332 | *
|
---|
333 | * @throws DataIntegrityProblemException If primitive was already added to the dataset
|
---|
334 | * @since 6140
|
---|
335 | */
|
---|
336 | @Override
|
---|
337 | public void clearOsmMetadata() {
|
---|
338 | if (dataSet != null)
|
---|
339 | throw new DataIntegrityProblemException("Method cannot be called after primitive was added to the dataset");
|
---|
340 | super.clearOsmMetadata();
|
---|
341 | }
|
---|
342 |
|
---|
343 | @Override
|
---|
344 | public void setUser(User user) {
|
---|
345 | boolean locked = writeLock();
|
---|
346 | try {
|
---|
347 | super.setUser(user);
|
---|
348 | } finally {
|
---|
349 | writeUnlock(locked);
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | @Override
|
---|
354 | public void setChangesetId(int changesetId) {
|
---|
355 | boolean locked = writeLock();
|
---|
356 | try {
|
---|
357 | int old = this.changesetId;
|
---|
358 | super.setChangesetId(changesetId);
|
---|
359 | if (dataSet != null) {
|
---|
360 | dataSet.fireChangesetIdChanged(this, old, changesetId);
|
---|
361 | }
|
---|
362 | } finally {
|
---|
363 | writeUnlock(locked);
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | @Override
|
---|
368 | public void setTimestamp(Date timestamp) {
|
---|
369 | boolean locked = writeLock();
|
---|
370 | try {
|
---|
371 | super.setTimestamp(timestamp);
|
---|
372 | } finally {
|
---|
373 | writeUnlock(locked);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | /* -------
|
---|
379 | /* FLAGS
|
---|
380 | /* ------*/
|
---|
381 |
|
---|
382 | private void updateFlagsNoLock(short flag, boolean value) {
|
---|
383 | super.updateFlags(flag, value);
|
---|
384 | }
|
---|
385 |
|
---|
386 | @Override
|
---|
387 | protected final void updateFlags(short flag, boolean value) {
|
---|
388 | boolean locked = writeLock();
|
---|
389 | try {
|
---|
390 | updateFlagsNoLock(flag, value);
|
---|
391 | } finally {
|
---|
392 | writeUnlock(locked);
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Make the primitive disabled (e.g. if a filter applies).
|
---|
398 | *
|
---|
399 | * To enable the primitive again, use unsetDisabledState.
|
---|
400 | * @param hidden if the primitive should be completely hidden from view or
|
---|
401 | * just shown in gray color.
|
---|
402 | * @return true, any flag has changed; false if you try to set the disabled
|
---|
403 | * state to the value that is already preset
|
---|
404 | */
|
---|
405 | public boolean setDisabledState(boolean hidden) {
|
---|
406 | boolean locked = writeLock();
|
---|
407 | try {
|
---|
408 | int oldFlags = flags;
|
---|
409 | updateFlagsNoLock(FLAG_DISABLED, true);
|
---|
410 | updateFlagsNoLock(FLAG_HIDE_IF_DISABLED, hidden);
|
---|
411 | return oldFlags != flags;
|
---|
412 | } finally {
|
---|
413 | writeUnlock(locked);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Remove the disabled flag from the primitive.
|
---|
419 | * Afterwards, the primitive is displayed normally and can be selected again.
|
---|
420 | * @return {@code true} if a change occurred
|
---|
421 | */
|
---|
422 | public boolean unsetDisabledState() {
|
---|
423 | boolean locked = writeLock();
|
---|
424 | try {
|
---|
425 | int oldFlags = flags;
|
---|
426 | updateFlagsNoLock(FLAG_DISABLED, false);
|
---|
427 | updateFlagsNoLock(FLAG_HIDE_IF_DISABLED, false);
|
---|
428 | return oldFlags != flags;
|
---|
429 | } finally {
|
---|
430 | writeUnlock(locked);
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Set binary property used internally by the filter mechanism.
|
---|
436 | * @param isExplicit new "disabled type" flag value
|
---|
437 | */
|
---|
438 | public void setDisabledType(boolean isExplicit) {
|
---|
439 | updateFlags(FLAG_DISABLED_TYPE, isExplicit);
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Set binary property used internally by the filter mechanism.
|
---|
444 | * @param isExplicit new "hidden type" flag value
|
---|
445 | */
|
---|
446 | public void setHiddenType(boolean isExplicit) {
|
---|
447 | updateFlags(FLAG_HIDDEN_TYPE, isExplicit);
|
---|
448 | }
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Set binary property used internally by the filter mechanism.
|
---|
452 | * @param isPreserved new "preserved" flag value
|
---|
453 | * @since 13309
|
---|
454 | */
|
---|
455 | public void setPreserved(boolean isPreserved) {
|
---|
456 | updateFlags(FLAG_PRESERVED, isPreserved);
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Replies true, if this primitive is disabled. (E.g. a filter applies)
|
---|
461 | * @return {@code true} if this object has the "disabled" flag enabled
|
---|
462 | */
|
---|
463 | public boolean isDisabled() {
|
---|
464 | return (flags & FLAG_DISABLED) != 0;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Replies true, if this primitive is disabled and marked as completely hidden on the map.
|
---|
469 | * @return {@code true} if this object has both the "disabled" and "hide if disabled" flags enabled
|
---|
470 | */
|
---|
471 | public boolean isDisabledAndHidden() {
|
---|
472 | return ((flags & FLAG_DISABLED) != 0) && ((flags & FLAG_HIDE_IF_DISABLED) != 0);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Get binary property used internally by the filter mechanism.
|
---|
477 | * @return {@code true} if this object has the "hidden type" flag enabled
|
---|
478 | */
|
---|
479 | public boolean getHiddenType() {
|
---|
480 | return (flags & FLAG_HIDDEN_TYPE) != 0;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Get binary property used internally by the filter mechanism.
|
---|
485 | * @return {@code true} if this object has the "disabled type" flag enabled
|
---|
486 | */
|
---|
487 | public boolean getDisabledType() {
|
---|
488 | return (flags & FLAG_DISABLED_TYPE) != 0;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Replies true, if this primitive is preserved from filtering.
|
---|
493 | * @return {@code true} if this object has the "preserved" flag enabled
|
---|
494 | * @since 13309
|
---|
495 | */
|
---|
496 | public boolean isPreserved() {
|
---|
497 | return (flags & FLAG_PRESERVED) != 0;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Determines if this object is selectable.
|
---|
502 | * <p>
|
---|
503 | * A primitive can be selected if all conditions are met:
|
---|
504 | * <ul>
|
---|
505 | * <li>it is drawable
|
---|
506 | * <li>it is not disabled (greyed out) by a filter.
|
---|
507 | * </ul>
|
---|
508 | * @return {@code true} if this object is selectable
|
---|
509 | */
|
---|
510 | public boolean isSelectable() {
|
---|
511 | // not synchronized -> check disabled twice just to be sure we did not have a race condition.
|
---|
512 | return !isDisabled() && isDrawable() && !isDisabled();
|
---|
513 | }
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Determines if this object is drawable.
|
---|
517 | * <p>
|
---|
518 | * A primitive is complete if all conditions are met:
|
---|
519 | * <ul>
|
---|
520 | * <li>type and id is known
|
---|
521 | * <li>tags are known
|
---|
522 | * <li>it is not deleted
|
---|
523 | * <li>it is not hidden by a filter
|
---|
524 | * <li>for nodes: lat/lon are known
|
---|
525 | * <li>for ways: all nodes are known and complete
|
---|
526 | * <li>for relations: all members are known and complete
|
---|
527 | * </ul>
|
---|
528 | * @return {@code true} if this object is drawable
|
---|
529 | */
|
---|
530 | public boolean isDrawable() {
|
---|
531 | return (flags & (FLAG_DELETED + FLAG_INCOMPLETE + FLAG_HIDE_IF_DISABLED)) == 0;
|
---|
532 | }
|
---|
533 |
|
---|
534 | @Override
|
---|
535 | public void setModified(boolean modified) {
|
---|
536 | boolean locked = writeLock();
|
---|
537 | try {
|
---|
538 | super.setModified(modified);
|
---|
539 | if (dataSet != null) {
|
---|
540 | dataSet.firePrimitiveFlagsChanged(this);
|
---|
541 | }
|
---|
542 | clearCachedStyle();
|
---|
543 | } finally {
|
---|
544 | writeUnlock(locked);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | @Override
|
---|
549 | public void setVisible(boolean visible) {
|
---|
550 | boolean locked = writeLock();
|
---|
551 | try {
|
---|
552 | super.setVisible(visible);
|
---|
553 | clearCachedStyle();
|
---|
554 | } finally {
|
---|
555 | writeUnlock(locked);
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|
559 | @Override
|
---|
560 | public void setDeleted(boolean deleted) {
|
---|
561 | boolean locked = writeLock();
|
---|
562 | try {
|
---|
563 | super.setDeleted(deleted);
|
---|
564 | if (dataSet != null) {
|
---|
565 | if (deleted) {
|
---|
566 | dataSet.firePrimitivesRemoved(Collections.singleton(this), false);
|
---|
567 | } else {
|
---|
568 | dataSet.firePrimitivesAdded(Collections.singleton(this), false);
|
---|
569 | }
|
---|
570 | }
|
---|
571 | clearCachedStyle();
|
---|
572 | } finally {
|
---|
573 | writeUnlock(locked);
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | @Override
|
---|
578 | protected final void setIncomplete(boolean incomplete) {
|
---|
579 | boolean locked = writeLock();
|
---|
580 | try {
|
---|
581 | if (dataSet != null && incomplete != this.isIncomplete()) {
|
---|
582 | if (incomplete) {
|
---|
583 | dataSet.firePrimitivesRemoved(Collections.singletonList(this), true);
|
---|
584 | } else {
|
---|
585 | dataSet.firePrimitivesAdded(Collections.singletonList(this), true);
|
---|
586 | }
|
---|
587 | }
|
---|
588 | super.setIncomplete(incomplete);
|
---|
589 | } finally {
|
---|
590 | writeUnlock(locked);
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Determines whether the primitive is selected
|
---|
596 | * @return whether the primitive is selected
|
---|
597 | * @see DataSet#isSelected(OsmPrimitive)
|
---|
598 | */
|
---|
599 | public boolean isSelected() {
|
---|
600 | return dataSet != null && dataSet.isSelected(this);
|
---|
601 | }
|
---|
602 |
|
---|
603 | /**
|
---|
604 | * Determines if this primitive is a member of a selected relation.
|
---|
605 | * @return {@code true} if this primitive is a member of a selected relation, {@code false} otherwise
|
---|
606 | */
|
---|
607 | public boolean isMemberOfSelected() {
|
---|
608 | if (referrers == null)
|
---|
609 | return false;
|
---|
610 | if (referrers instanceof OsmPrimitive)
|
---|
611 | return referrers instanceof Relation && ((OsmPrimitive) referrers).isSelected();
|
---|
612 | for (OsmPrimitive ref : (OsmPrimitive[]) referrers) {
|
---|
613 | if (ref instanceof Relation && ref.isSelected())
|
---|
614 | return true;
|
---|
615 | }
|
---|
616 | return false;
|
---|
617 | }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Determines if this primitive is an outer member of a selected multipolygon relation.
|
---|
621 | * @return {@code true} if this primitive is an outer member of a selected multipolygon relation, {@code false} otherwise
|
---|
622 | * @since 7621
|
---|
623 | */
|
---|
624 | public boolean isOuterMemberOfSelected() {
|
---|
625 | if (referrers == null)
|
---|
626 | return false;
|
---|
627 | if (referrers instanceof OsmPrimitive) {
|
---|
628 | return isOuterMemberOfMultipolygon((OsmPrimitive) referrers);
|
---|
629 | }
|
---|
630 | for (OsmPrimitive ref : (OsmPrimitive[]) referrers) {
|
---|
631 | if (isOuterMemberOfMultipolygon(ref))
|
---|
632 | return true;
|
---|
633 | }
|
---|
634 | return false;
|
---|
635 | }
|
---|
636 |
|
---|
637 | private boolean isOuterMemberOfMultipolygon(OsmPrimitive ref) {
|
---|
638 | if (ref instanceof Relation && ref.isSelected() && ((Relation) ref).isMultipolygon()) {
|
---|
639 | for (RelationMember rm : ((Relation) ref).getMembersFor(Collections.singleton(this))) {
|
---|
640 | if ("outer".equals(rm.getRole())) {
|
---|
641 | return true;
|
---|
642 | }
|
---|
643 | }
|
---|
644 | }
|
---|
645 | return false;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Updates the highlight flag for this primitive.
|
---|
650 | * @param highlighted The new highlight flag.
|
---|
651 | */
|
---|
652 | public void setHighlighted(boolean highlighted) {
|
---|
653 | if (isHighlighted() != highlighted) {
|
---|
654 | updateFlags(FLAG_HIGHLIGHTED, highlighted);
|
---|
655 | if (dataSet != null) {
|
---|
656 | dataSet.fireHighlightingChanged();
|
---|
657 | }
|
---|
658 | }
|
---|
659 | }
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Checks if the highlight flag for this primitive was set
|
---|
663 | * @return The highlight flag.
|
---|
664 | */
|
---|
665 | public boolean isHighlighted() {
|
---|
666 | return (flags & FLAG_HIGHLIGHTED) != 0;
|
---|
667 | }
|
---|
668 |
|
---|
669 | /*---------------------------------------------------
|
---|
670 | * WORK IN PROGRESS, UNINTERESTING AND DIRECTION KEYS
|
---|
671 | *--------------------------------------------------*/
|
---|
672 |
|
---|
673 | private static volatile Collection<String> workinprogress;
|
---|
674 | private static volatile Collection<String> uninteresting;
|
---|
675 | private static volatile Collection<String> discardable;
|
---|
676 |
|
---|
677 | /**
|
---|
678 | * Returns a list of "uninteresting" keys that do not make an object
|
---|
679 | * "tagged". Entries that end with ':' are causing a whole namespace to be considered
|
---|
680 | * "uninteresting". Only the first level namespace is considered.
|
---|
681 | * Initialized by isUninterestingKey()
|
---|
682 | * @return The list of uninteresting keys.
|
---|
683 | */
|
---|
684 | public static Collection<String> getUninterestingKeys() {
|
---|
685 | if (uninteresting == null) {
|
---|
686 | List<String> l = new LinkedList<>(Arrays.asList(
|
---|
687 | "source", "source_ref", "source:", "comment",
|
---|
688 | "watch", "watch:", "description", "attribution"));
|
---|
689 | l.addAll(getDiscardableKeys());
|
---|
690 | l.addAll(getWorkInProgressKeys());
|
---|
691 | uninteresting = new HashSet<>(Config.getPref().getList("tags.uninteresting", l));
|
---|
692 | }
|
---|
693 | return uninteresting;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Returns a list of keys which have been deemed uninteresting to the point
|
---|
698 | * that they can be silently removed from data which is being edited.
|
---|
699 | * @return The list of discardable keys.
|
---|
700 | */
|
---|
701 | public static Collection<String> getDiscardableKeys() {
|
---|
702 | if (discardable == null) {
|
---|
703 | discardable = new HashSet<>(Config.getPref().getList("tags.discardable",
|
---|
704 | Arrays.asList(
|
---|
705 | "created_by",
|
---|
706 | "converted_by",
|
---|
707 | "geobase:datasetName",
|
---|
708 | "geobase:uuid",
|
---|
709 | "KSJ2:ADS",
|
---|
710 | "KSJ2:ARE",
|
---|
711 | "KSJ2:AdminArea",
|
---|
712 | "KSJ2:COP_label",
|
---|
713 | "KSJ2:DFD",
|
---|
714 | "KSJ2:INT",
|
---|
715 | "KSJ2:INT_label",
|
---|
716 | "KSJ2:LOC",
|
---|
717 | "KSJ2:LPN",
|
---|
718 | "KSJ2:OPC",
|
---|
719 | "KSJ2:PubFacAdmin",
|
---|
720 | "KSJ2:RAC",
|
---|
721 | "KSJ2:RAC_label",
|
---|
722 | "KSJ2:RIC",
|
---|
723 | "KSJ2:RIN",
|
---|
724 | "KSJ2:WSC",
|
---|
725 | "KSJ2:coordinate",
|
---|
726 | "KSJ2:curve_id",
|
---|
727 | "KSJ2:curve_type",
|
---|
728 | "KSJ2:filename",
|
---|
729 | "KSJ2:lake_id",
|
---|
730 | "KSJ2:lat",
|
---|
731 | "KSJ2:long",
|
---|
732 | "KSJ2:river_id",
|
---|
733 | "odbl",
|
---|
734 | "odbl:note",
|
---|
735 | "SK53_bulk:load",
|
---|
736 | "sub_sea:type",
|
---|
737 | "tiger:source",
|
---|
738 | "tiger:separated",
|
---|
739 | "tiger:tlid",
|
---|
740 | "tiger:upload_uuid",
|
---|
741 | "yh:LINE_NAME",
|
---|
742 | "yh:LINE_NUM",
|
---|
743 | "yh:STRUCTURE",
|
---|
744 | "yh:TOTYUMONO",
|
---|
745 | "yh:TYPE",
|
---|
746 | "yh:WIDTH",
|
---|
747 | "yh:WIDTH_RANK"
|
---|
748 | )));
|
---|
749 | }
|
---|
750 | return discardable;
|
---|
751 | }
|
---|
752 |
|
---|
753 | /**
|
---|
754 | * Returns a list of "work in progress" keys that do not make an object
|
---|
755 | * "tagged" but "annotated".
|
---|
756 | * @return The list of work in progress keys.
|
---|
757 | * @since 5754
|
---|
758 | */
|
---|
759 | public static Collection<String> getWorkInProgressKeys() {
|
---|
760 | if (workinprogress == null) {
|
---|
761 | workinprogress = new HashSet<>(Config.getPref().getList("tags.workinprogress",
|
---|
762 | Arrays.asList("note", "fixme", "FIXME")));
|
---|
763 | }
|
---|
764 | return workinprogress;
|
---|
765 | }
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Determines if key is considered "uninteresting".
|
---|
769 | * @param key The key to check
|
---|
770 | * @return true if key is considered "uninteresting".
|
---|
771 | */
|
---|
772 | public static boolean isUninterestingKey(String key) {
|
---|
773 | getUninterestingKeys();
|
---|
774 | if (uninteresting.contains(key))
|
---|
775 | return true;
|
---|
776 | int pos = key.indexOf(':');
|
---|
777 | if (pos > 0)
|
---|
778 | return uninteresting.contains(key.substring(0, pos + 1));
|
---|
779 | return false;
|
---|
780 | }
|
---|
781 |
|
---|
782 | /**
|
---|
783 | * Returns {@link #getKeys()} for which {@code key} does not fulfill {@link #isUninterestingKey}.
|
---|
784 | * @return A map of interesting tags
|
---|
785 | */
|
---|
786 | public Map<String, String> getInterestingTags() {
|
---|
787 | Map<String, String> result = new HashMap<>();
|
---|
788 | String[] keys = this.keys;
|
---|
789 | if (keys != null) {
|
---|
790 | for (int i = 0; i < keys.length; i += 2) {
|
---|
791 | if (!isUninterestingKey(keys[i])) {
|
---|
792 | result.put(keys[i], keys[i + 1]);
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 | return result;
|
---|
797 | }
|
---|
798 |
|
---|
799 | private static Match compileDirectionKeys(String prefName, String defaultValue) throws AssertionError {
|
---|
800 | try {
|
---|
801 | return SearchCompiler.compile(Config.getPref().get(prefName, defaultValue));
|
---|
802 | } catch (SearchParseError e) {
|
---|
803 | Logging.log(Logging.LEVEL_ERROR, "Unable to compile pattern for " + prefName + ", trying default pattern:", e);
|
---|
804 | }
|
---|
805 |
|
---|
806 | try {
|
---|
807 | return SearchCompiler.compile(defaultValue);
|
---|
808 | } catch (SearchParseError e2) {
|
---|
809 | throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage(), e2);
|
---|
810 | }
|
---|
811 | }
|
---|
812 |
|
---|
813 | private void updateTagged() {
|
---|
814 | for (String key: keySet()) {
|
---|
815 | // 'area' is not really uninteresting (putting it in that list may have unpredictable side effects)
|
---|
816 | // but it's clearly not enough to consider an object as tagged (see #9261)
|
---|
817 | if (!isUninterestingKey(key) && !"area".equals(key)) {
|
---|
818 | updateFlagsNoLock(FLAG_TAGGED, true);
|
---|
819 | return;
|
---|
820 | }
|
---|
821 | }
|
---|
822 | updateFlagsNoLock(FLAG_TAGGED, false);
|
---|
823 | }
|
---|
824 |
|
---|
825 | private void updateAnnotated() {
|
---|
826 | for (String key: keySet()) {
|
---|
827 | if (getWorkInProgressKeys().contains(key)) {
|
---|
828 | updateFlagsNoLock(FLAG_ANNOTATED, true);
|
---|
829 | return;
|
---|
830 | }
|
---|
831 | }
|
---|
832 | updateFlagsNoLock(FLAG_ANNOTATED, false);
|
---|
833 | }
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * Determines if this object is considered "tagged". To be "tagged", an object
|
---|
837 | * must have one or more "interesting" tags. "created_by" and "source"
|
---|
838 | * are typically considered "uninteresting" and do not make an object
|
---|
839 | * "tagged".
|
---|
840 | * @return true if this object is considered "tagged"
|
---|
841 | */
|
---|
842 | public boolean isTagged() {
|
---|
843 | return (flags & FLAG_TAGGED) != 0;
|
---|
844 | }
|
---|
845 |
|
---|
846 | /**
|
---|
847 | * Determines if this object is considered "annotated". To be "annotated", an object
|
---|
848 | * must have one or more "work in progress" tags, such as "note" or "fixme".
|
---|
849 | * @return true if this object is considered "annotated"
|
---|
850 | * @since 5754
|
---|
851 | */
|
---|
852 | public boolean isAnnotated() {
|
---|
853 | return (flags & FLAG_ANNOTATED) != 0;
|
---|
854 | }
|
---|
855 |
|
---|
856 | private void updateDirectionFlags() {
|
---|
857 | boolean hasDirections = false;
|
---|
858 | boolean directionReversed = false;
|
---|
859 | if (reversedDirectionKeys.match(this)) {
|
---|
860 | hasDirections = true;
|
---|
861 | directionReversed = true;
|
---|
862 | }
|
---|
863 | if (directionKeys.match(this)) {
|
---|
864 | hasDirections = true;
|
---|
865 | }
|
---|
866 |
|
---|
867 | updateFlagsNoLock(FLAG_DIRECTION_REVERSED, directionReversed);
|
---|
868 | updateFlagsNoLock(FLAG_HAS_DIRECTIONS, hasDirections);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * true if this object has direction dependent tags (e.g. oneway)
|
---|
873 | * @return {@code true} if this object has direction dependent tags
|
---|
874 | */
|
---|
875 | public boolean hasDirectionKeys() {
|
---|
876 | return (flags & FLAG_HAS_DIRECTIONS) != 0;
|
---|
877 | }
|
---|
878 |
|
---|
879 | /**
|
---|
880 | * true if this object has the "reversed diretion" flag enabled
|
---|
881 | * @return {@code true} if this object has the "reversed diretion" flag enabled
|
---|
882 | */
|
---|
883 | public boolean reversedDirection() {
|
---|
884 | return (flags & FLAG_DIRECTION_REVERSED) != 0;
|
---|
885 | }
|
---|
886 |
|
---|
887 | /*------------
|
---|
888 | * Keys handling
|
---|
889 | ------------*/
|
---|
890 |
|
---|
891 | @Override
|
---|
892 | public final void setKeys(TagMap keys) {
|
---|
893 | boolean locked = writeLock();
|
---|
894 | try {
|
---|
895 | super.setKeys(keys);
|
---|
896 | } finally {
|
---|
897 | writeUnlock(locked);
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | @Override
|
---|
902 | public final void setKeys(Map<String, String> keys) {
|
---|
903 | boolean locked = writeLock();
|
---|
904 | try {
|
---|
905 | super.setKeys(keys);
|
---|
906 | } finally {
|
---|
907 | writeUnlock(locked);
|
---|
908 | }
|
---|
909 | }
|
---|
910 |
|
---|
911 | @Override
|
---|
912 | public final void put(String key, String value) {
|
---|
913 | boolean locked = writeLock();
|
---|
914 | try {
|
---|
915 | super.put(key, value);
|
---|
916 | } finally {
|
---|
917 | writeUnlock(locked);
|
---|
918 | }
|
---|
919 | }
|
---|
920 |
|
---|
921 | @Override
|
---|
922 | public final void remove(String key) {
|
---|
923 | boolean locked = writeLock();
|
---|
924 | try {
|
---|
925 | super.remove(key);
|
---|
926 | } finally {
|
---|
927 | writeUnlock(locked);
|
---|
928 | }
|
---|
929 | }
|
---|
930 |
|
---|
931 | @Override
|
---|
932 | public final void removeAll() {
|
---|
933 | boolean locked = writeLock();
|
---|
934 | try {
|
---|
935 | super.removeAll();
|
---|
936 | } finally {
|
---|
937 | writeUnlock(locked);
|
---|
938 | }
|
---|
939 | }
|
---|
940 |
|
---|
941 | @Override
|
---|
942 | protected void keysChangedImpl(Map<String, String> originalKeys) {
|
---|
943 | clearCachedStyle();
|
---|
944 | if (dataSet != null) {
|
---|
945 | for (OsmPrimitive ref : getReferrers()) {
|
---|
946 | ref.clearCachedStyle();
|
---|
947 | }
|
---|
948 | }
|
---|
949 | updateDirectionFlags();
|
---|
950 | updateTagged();
|
---|
951 | updateAnnotated();
|
---|
952 | if (dataSet != null) {
|
---|
953 | dataSet.fireTagsChanged(this, originalKeys);
|
---|
954 | }
|
---|
955 | }
|
---|
956 |
|
---|
957 | /*------------
|
---|
958 | * Referrers
|
---|
959 | ------------*/
|
---|
960 |
|
---|
961 | private Object referrers;
|
---|
962 |
|
---|
963 | /**
|
---|
964 | * Add new referrer. If referrer is already included then no action is taken
|
---|
965 | * @param referrer The referrer to add
|
---|
966 | */
|
---|
967 | protected void addReferrer(OsmPrimitive referrer) {
|
---|
968 | if (referrers == null) {
|
---|
969 | referrers = referrer;
|
---|
970 | } else if (referrers instanceof OsmPrimitive) {
|
---|
971 | if (referrers != referrer) {
|
---|
972 | referrers = new OsmPrimitive[] {(OsmPrimitive) referrers, referrer};
|
---|
973 | }
|
---|
974 | } else {
|
---|
975 | for (OsmPrimitive primitive:(OsmPrimitive[]) referrers) {
|
---|
976 | if (primitive == referrer)
|
---|
977 | return;
|
---|
978 | }
|
---|
979 | referrers = Utils.addInArrayCopy((OsmPrimitive[]) referrers, referrer);
|
---|
980 | }
|
---|
981 | }
|
---|
982 |
|
---|
983 | /**
|
---|
984 | * Remove referrer. No action is taken if referrer is not registered
|
---|
985 | * @param referrer The referrer to remove
|
---|
986 | */
|
---|
987 | protected void removeReferrer(OsmPrimitive referrer) {
|
---|
988 | if (referrers instanceof OsmPrimitive) {
|
---|
989 | if (referrers == referrer) {
|
---|
990 | referrers = null;
|
---|
991 | }
|
---|
992 | } else if (referrers instanceof OsmPrimitive[]) {
|
---|
993 | OsmPrimitive[] orig = (OsmPrimitive[]) referrers;
|
---|
994 | int idx = -1;
|
---|
995 | for (int i = 0; i < orig.length; i++) {
|
---|
996 | if (orig[i] == referrer) {
|
---|
997 | idx = i;
|
---|
998 | break;
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 | if (idx == -1)
|
---|
1002 | return;
|
---|
1003 |
|
---|
1004 | if (orig.length == 2) {
|
---|
1005 | referrers = orig[1-idx]; // idx is either 0 or 1, take the other
|
---|
1006 | } else { // downsize the array
|
---|
1007 | OsmPrimitive[] smaller = new OsmPrimitive[orig.length-1];
|
---|
1008 | System.arraycopy(orig, 0, smaller, 0, idx);
|
---|
1009 | System.arraycopy(orig, idx+1, smaller, idx, smaller.length-idx);
|
---|
1010 | referrers = smaller;
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * Find primitives that reference this primitive. Returns only primitives that are included in the same
|
---|
1017 | * dataset as this primitive. <br>
|
---|
1018 | *
|
---|
1019 | * For example following code will add wnew as referer to all nodes of existingWay, but this method will
|
---|
1020 | * not return wnew because it's not part of the dataset <br>
|
---|
1021 | *
|
---|
1022 | * <code>Way wnew = new Way(existingWay)</code>
|
---|
1023 | *
|
---|
1024 | * @param allowWithoutDataset If true, method will return empty list if primitive is not part of the dataset. If false,
|
---|
1025 | * exception will be thrown in this case
|
---|
1026 | *
|
---|
1027 | * @return a collection of all primitives that reference this primitive.
|
---|
1028 | */
|
---|
1029 | public final List<OsmPrimitive> getReferrers(boolean allowWithoutDataset) {
|
---|
1030 | // Returns only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
1031 | // when way is cloned
|
---|
1032 |
|
---|
1033 | if (dataSet == null && allowWithoutDataset)
|
---|
1034 | return Collections.emptyList();
|
---|
1035 |
|
---|
1036 | checkDataset();
|
---|
1037 | Object referrers = this.referrers;
|
---|
1038 | List<OsmPrimitive> result = new ArrayList<>();
|
---|
1039 | if (referrers != null) {
|
---|
1040 | if (referrers instanceof OsmPrimitive) {
|
---|
1041 | OsmPrimitive ref = (OsmPrimitive) referrers;
|
---|
1042 | if (ref.dataSet == dataSet) {
|
---|
1043 | result.add(ref);
|
---|
1044 | }
|
---|
1045 | } else {
|
---|
1046 | for (OsmPrimitive o:(OsmPrimitive[]) referrers) {
|
---|
1047 | if (dataSet == o.dataSet) {
|
---|
1048 | result.add(o);
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 | return result;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * Gets a list of all primitives in the current dataset that reference this primitive.
|
---|
1058 | * @return The referrers
|
---|
1059 | */
|
---|
1060 | public final List<OsmPrimitive> getReferrers() {
|
---|
1061 | return getReferrers(false);
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /**
|
---|
1065 | * <p>Visits {@code visitor} for all referrers.</p>
|
---|
1066 | *
|
---|
1067 | * @param visitor the visitor. Ignored, if null.
|
---|
1068 | * @since 12809
|
---|
1069 | */
|
---|
1070 | public void visitReferrers(OsmPrimitiveVisitor visitor) {
|
---|
1071 | if (visitor == null) return;
|
---|
1072 | if (this.referrers == null)
|
---|
1073 | return;
|
---|
1074 | else if (this.referrers instanceof OsmPrimitive) {
|
---|
1075 | OsmPrimitive ref = (OsmPrimitive) this.referrers;
|
---|
1076 | if (ref.dataSet == dataSet) {
|
---|
1077 | ref.accept(visitor);
|
---|
1078 | }
|
---|
1079 | } else if (this.referrers instanceof OsmPrimitive[]) {
|
---|
1080 | OsmPrimitive[] refs = (OsmPrimitive[]) this.referrers;
|
---|
1081 | for (OsmPrimitive ref: refs) {
|
---|
1082 | if (ref.dataSet == dataSet) {
|
---|
1083 | ref.accept(visitor);
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | /**
|
---|
1090 | Return true, if this primitive is referred by at least n ways
|
---|
1091 | @param n Minimal number of ways to return true. Must be positive
|
---|
1092 | * @return {@code true} if this primitive is referred by at least n ways
|
---|
1093 | */
|
---|
1094 | public final boolean isReferredByWays(int n) {
|
---|
1095 | // Count only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
1096 | // when way is cloned
|
---|
1097 | Object referrers = this.referrers;
|
---|
1098 | if (referrers == null) return false;
|
---|
1099 | checkDataset();
|
---|
1100 | if (referrers instanceof OsmPrimitive)
|
---|
1101 | return n <= 1 && referrers instanceof Way && ((OsmPrimitive) referrers).dataSet == dataSet;
|
---|
1102 | else {
|
---|
1103 | int counter = 0;
|
---|
1104 | for (OsmPrimitive o : (OsmPrimitive[]) referrers) {
|
---|
1105 | if (dataSet == o.dataSet && o instanceof Way && ++counter >= n)
|
---|
1106 | return true;
|
---|
1107 | }
|
---|
1108 | return false;
|
---|
1109 | }
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /*-----------------
|
---|
1113 | * OTHER METHODS
|
---|
1114 | *----------------*/
|
---|
1115 |
|
---|
1116 | /**
|
---|
1117 | * Implementation of the visitor scheme. Subclasses have to call the correct
|
---|
1118 | * visitor function.
|
---|
1119 | * @param visitor The visitor from which the visit() function must be called.
|
---|
1120 | * @since 12809
|
---|
1121 | */
|
---|
1122 | public abstract void accept(OsmPrimitiveVisitor visitor);
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
1126 | * use this only in the data initializing phase
|
---|
1127 | * @param other other primitive
|
---|
1128 | */
|
---|
1129 | public void cloneFrom(OsmPrimitive other) {
|
---|
1130 | // write lock is provided by subclasses
|
---|
1131 | if (id != other.id && dataSet != null)
|
---|
1132 | throw new DataIntegrityProblemException("Osm id cannot be changed after primitive was added to the dataset");
|
---|
1133 |
|
---|
1134 | super.cloneFrom(other);
|
---|
1135 | clearCachedStyle();
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | /**
|
---|
1139 | * Merges the technical and semantical attributes from <code>other</code> onto this.
|
---|
1140 | *
|
---|
1141 | * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
|
---|
1142 | * have an assigned OSM id, the IDs have to be the same.
|
---|
1143 | *
|
---|
1144 | * @param other the other primitive. Must not be null.
|
---|
1145 | * @throws IllegalArgumentException if other is null.
|
---|
1146 | * @throws DataIntegrityProblemException if either this is new and other is not, or other is new and this is not
|
---|
1147 | * @throws DataIntegrityProblemException if other isn't new and other.getId() != this.getId()
|
---|
1148 | */
|
---|
1149 | public void mergeFrom(OsmPrimitive other) {
|
---|
1150 | boolean locked = writeLock();
|
---|
1151 | try {
|
---|
1152 | CheckParameterUtil.ensureParameterNotNull(other, "other");
|
---|
1153 | if (other.isNew() ^ isNew())
|
---|
1154 | throw new DataIntegrityProblemException(
|
---|
1155 | tr("Cannot merge because either of the participating primitives is new and the other is not"));
|
---|
1156 | if (!other.isNew() && other.getId() != id)
|
---|
1157 | throw new DataIntegrityProblemException(
|
---|
1158 | tr("Cannot merge primitives with different ids. This id is {0}, the other is {1}", id, other.getId()));
|
---|
1159 |
|
---|
1160 | setKeys(other.hasKeys() ? other.getKeys() : null);
|
---|
1161 | timestamp = other.timestamp;
|
---|
1162 | version = other.version;
|
---|
1163 | setIncomplete(other.isIncomplete());
|
---|
1164 | flags = other.flags;
|
---|
1165 | user = other.user;
|
---|
1166 | changesetId = other.changesetId;
|
---|
1167 | } finally {
|
---|
1168 | writeUnlock(locked);
|
---|
1169 | }
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | /**
|
---|
1173 | * Replies true if other isn't null and has the same interesting tags (key/value-pairs) as this.
|
---|
1174 | *
|
---|
1175 | * @param other the other object primitive
|
---|
1176 | * @return true if other isn't null and has the same interesting tags (key/value-pairs) as this.
|
---|
1177 | */
|
---|
1178 | public boolean hasSameInterestingTags(OsmPrimitive other) {
|
---|
1179 | return (keys == null && other.keys == null)
|
---|
1180 | || getInterestingTags().equals(other.getInterestingTags());
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * Replies true if this primitive and other are equal with respect to their semantic attributes.
|
---|
1185 | * <ol>
|
---|
1186 | * <li>equal id</li>
|
---|
1187 | * <li>both are complete or both are incomplete</li>
|
---|
1188 | * <li>both have the same tags</li>
|
---|
1189 | * </ol>
|
---|
1190 | * @param other other primitive to compare
|
---|
1191 | * @return true if this primitive and other are equal with respect to their semantic attributes.
|
---|
1192 | */
|
---|
1193 | public final boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
1194 | return hasEqualSemanticAttributes(other, true);
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | boolean hasEqualSemanticFlags(final OsmPrimitive other) {
|
---|
1198 | if (!isNew() && id != other.id)
|
---|
1199 | return false;
|
---|
1200 | return !(isIncomplete() ^ other.isIncomplete()); // exclusive or operator for performance (see #7159)
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | boolean hasEqualSemanticAttributes(final OsmPrimitive other, final boolean testInterestingTagsOnly) {
|
---|
1204 | return hasEqualSemanticFlags(other)
|
---|
1205 | && (testInterestingTagsOnly ? hasSameInterestingTags(other) : getKeys().equals(other.getKeys()));
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | /**
|
---|
1209 | * Replies true if this primitive and other are equal with respect to their technical attributes.
|
---|
1210 | * The attributes:
|
---|
1211 | * <ol>
|
---|
1212 | * <li>deleted</li>
|
---|
1213 | * <li>modified</li>
|
---|
1214 | * <li>timestamp</li>
|
---|
1215 | * <li>version</li>
|
---|
1216 | * <li>visible</li>
|
---|
1217 | * <li>user</li>
|
---|
1218 | * </ol>
|
---|
1219 | * have to be equal
|
---|
1220 | * @param other the other primitive
|
---|
1221 | * @return true if this primitive and other are equal with respect to their technical attributes
|
---|
1222 | */
|
---|
1223 | public boolean hasEqualTechnicalAttributes(OsmPrimitive other) {
|
---|
1224 | // CHECKSTYLE.OFF: BooleanExpressionComplexity
|
---|
1225 | return other != null
|
---|
1226 | && timestamp == other.timestamp
|
---|
1227 | && version == other.version
|
---|
1228 | && changesetId == other.changesetId
|
---|
1229 | && isDeleted() == other.isDeleted()
|
---|
1230 | && isModified() == other.isModified()
|
---|
1231 | && isVisible() == other.isVisible()
|
---|
1232 | && Objects.equals(user, other.user);
|
---|
1233 | // CHECKSTYLE.ON: BooleanExpressionComplexity
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | /**
|
---|
1237 | * Loads (clone) this primitive from provided PrimitiveData
|
---|
1238 | * @param data The object which should be cloned
|
---|
1239 | */
|
---|
1240 | public void load(PrimitiveData data) {
|
---|
1241 | // Write lock is provided by subclasses
|
---|
1242 | setKeys(data.hasKeys() ? data.getKeys() : null);
|
---|
1243 | setRawTimestamp(data.getRawTimestamp());
|
---|
1244 | user = data.getUser();
|
---|
1245 | setChangesetId(data.getChangesetId());
|
---|
1246 | setDeleted(data.isDeleted());
|
---|
1247 | setModified(data.isModified());
|
---|
1248 | setVisible(data.isVisible());
|
---|
1249 | setIncomplete(data.isIncomplete());
|
---|
1250 | version = data.getVersion();
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | /**
|
---|
1254 | * Save parameters of this primitive to the transport object
|
---|
1255 | * @return The saved object data
|
---|
1256 | */
|
---|
1257 | public abstract PrimitiveData save();
|
---|
1258 |
|
---|
1259 | /**
|
---|
1260 | * Save common parameters of primitives to the transport object
|
---|
1261 | * @param data The object to save the data into
|
---|
1262 | */
|
---|
1263 | protected void saveCommonAttributes(PrimitiveData data) {
|
---|
1264 | data.setId(id);
|
---|
1265 | data.setKeys(hasKeys() ? getKeys() : null);
|
---|
1266 | data.setRawTimestamp(getRawTimestamp());
|
---|
1267 | data.setUser(user);
|
---|
1268 | data.setDeleted(isDeleted());
|
---|
1269 | data.setModified(isModified());
|
---|
1270 | data.setVisible(isVisible());
|
---|
1271 | data.setIncomplete(isIncomplete());
|
---|
1272 | data.setChangesetId(changesetId);
|
---|
1273 | data.setVersion(version);
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | /**
|
---|
1277 | * Fetch the bounding box of the primitive
|
---|
1278 | * @return Bounding box of the object
|
---|
1279 | */
|
---|
1280 | public abstract BBox getBBox();
|
---|
1281 |
|
---|
1282 | /**
|
---|
1283 | * Called by Dataset to update cached position information of primitive (bbox, cached EarthNorth, ...)
|
---|
1284 | */
|
---|
1285 | public abstract void updatePosition();
|
---|
1286 |
|
---|
1287 | /*----------------
|
---|
1288 | * OBJECT METHODS
|
---|
1289 | *---------------*/
|
---|
1290 |
|
---|
1291 | @Override
|
---|
1292 | protected String getFlagsAsString() {
|
---|
1293 | StringBuilder builder = new StringBuilder(super.getFlagsAsString());
|
---|
1294 |
|
---|
1295 | if (isDisabled()) {
|
---|
1296 | if (isDisabledAndHidden()) {
|
---|
1297 | builder.append('h');
|
---|
1298 | } else {
|
---|
1299 | builder.append('d');
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 | if (isTagged()) {
|
---|
1303 | builder.append('T');
|
---|
1304 | }
|
---|
1305 | if (hasDirectionKeys()) {
|
---|
1306 | if (reversedDirection()) {
|
---|
1307 | builder.append('<');
|
---|
1308 | } else {
|
---|
1309 | builder.append('>');
|
---|
1310 | }
|
---|
1311 | }
|
---|
1312 | return builder.toString();
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | /**
|
---|
1316 | * Equal, if the id (and class) is equal.
|
---|
1317 | *
|
---|
1318 | * An primitive is equal to its incomplete counter part.
|
---|
1319 | */
|
---|
1320 | @Override
|
---|
1321 | public boolean equals(Object obj) {
|
---|
1322 | if (this == obj) {
|
---|
1323 | return true;
|
---|
1324 | } else if (obj == null || getClass() != obj.getClass()) {
|
---|
1325 | return false;
|
---|
1326 | } else {
|
---|
1327 | OsmPrimitive that = (OsmPrimitive) obj;
|
---|
1328 | return id == that.id;
|
---|
1329 | }
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | /**
|
---|
1333 | * Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.
|
---|
1334 | *
|
---|
1335 | * An primitive has the same hashcode as its incomplete counterpart.
|
---|
1336 | */
|
---|
1337 | @Override
|
---|
1338 | public int hashCode() {
|
---|
1339 | return Long.hashCode(id);
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | /**
|
---|
1343 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
1344 | * @param formatter formatter to use
|
---|
1345 | *
|
---|
1346 | * @return the display name
|
---|
1347 | */
|
---|
1348 | public abstract String getDisplayName(NameFormatter formatter);
|
---|
1349 |
|
---|
1350 | @Override
|
---|
1351 | public Collection<String> getTemplateKeys() {
|
---|
1352 | Collection<String> keySet = keySet();
|
---|
1353 | List<String> result = new ArrayList<>(keySet.size() + 2);
|
---|
1354 | result.add(SPECIAL_VALUE_ID);
|
---|
1355 | result.add(SPECIAL_VALUE_LOCAL_NAME);
|
---|
1356 | result.addAll(keySet);
|
---|
1357 | return result;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | @Override
|
---|
1361 | public Object getTemplateValue(String name, boolean special) {
|
---|
1362 | if (special) {
|
---|
1363 | String lc = name.toLowerCase(Locale.ENGLISH);
|
---|
1364 | if (SPECIAL_VALUE_ID.equals(lc))
|
---|
1365 | return getId();
|
---|
1366 | else if (SPECIAL_VALUE_LOCAL_NAME.equals(lc))
|
---|
1367 | return getLocalName();
|
---|
1368 | else
|
---|
1369 | return null;
|
---|
1370 |
|
---|
1371 | } else
|
---|
1372 | return getIgnoreCase(name);
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 | @Override
|
---|
1376 | public boolean evaluateCondition(Match condition) {
|
---|
1377 | return condition.match(this);
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | /**
|
---|
1381 | * Replies the set of referring relations
|
---|
1382 | * @param primitives primitives to fetch relations from
|
---|
1383 | *
|
---|
1384 | * @return the set of referring relations
|
---|
1385 | */
|
---|
1386 | public static Set<Relation> getParentRelations(Collection<? extends OsmPrimitive> primitives) {
|
---|
1387 | Set<Relation> ret = new HashSet<>();
|
---|
1388 | for (OsmPrimitive w : primitives) {
|
---|
1389 | ret.addAll(OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class));
|
---|
1390 | }
|
---|
1391 | return ret;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | /**
|
---|
1395 | * Determines if this primitive has tags denoting an area.
|
---|
1396 | * @return {@code true} if this primitive has tags denoting an area, {@code false} otherwise.
|
---|
1397 | * @since 6491
|
---|
1398 | */
|
---|
1399 | public final boolean hasAreaTags() {
|
---|
1400 | return hasKey("landuse", "amenity", "building", "building:part")
|
---|
1401 | || hasTag("area", OsmUtils.TRUE_VALUE)
|
---|
1402 | || hasTag("waterway", "riverbank")
|
---|
1403 | || hasTagDifferent("leisure", "picnic_table", "slipway", "firepit")
|
---|
1404 | || hasTag("natural", "water", "wood", "scrub", "wetland", "grassland", "heath", "rock", "bare_rock",
|
---|
1405 | "sand", "beach", "scree", "bay", "glacier", "shingle", "fell", "reef", "stone",
|
---|
1406 | "mud", "landslide", "sinkhole", "crevasse", "desert");
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | /**
|
---|
1410 | * Determines if this primitive semantically concerns an area.
|
---|
1411 | * @return {@code true} if this primitive semantically concerns an area, according to its type, geometry and tags, {@code false} otherwise.
|
---|
1412 | * @since 6491
|
---|
1413 | */
|
---|
1414 | public abstract boolean concernsArea();
|
---|
1415 |
|
---|
1416 | /**
|
---|
1417 | * Tests if this primitive lies outside of the downloaded area of its {@link DataSet}.
|
---|
1418 | * @return {@code true} if this primitive lies outside of the downloaded area
|
---|
1419 | */
|
---|
1420 | public abstract boolean isOutsideDownloadArea();
|
---|
1421 |
|
---|
1422 | /**
|
---|
1423 | * Determines if this object is a relation and behaves as a multipolygon.
|
---|
1424 | * @return {@code true} if it is a real mutlipolygon or a boundary relation
|
---|
1425 | * @since 10716
|
---|
1426 | */
|
---|
1427 | public boolean isMultipolygon() {
|
---|
1428 | return false;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | /**
|
---|
1432 | * If necessary, extend the bbox to contain this primitive
|
---|
1433 | * @param box a bbox instance
|
---|
1434 | * @param visited a set of visited members or null
|
---|
1435 | * @since 11269
|
---|
1436 | */
|
---|
1437 | protected abstract void addToBBox(BBox box, Set<PrimitiveId> visited);
|
---|
1438 | }
|
---|