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.awt.geom.Area;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.Iterator;
|
---|
14 | import java.util.LinkedHashSet;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Objects;
|
---|
19 | import java.util.Set;
|
---|
20 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
21 | import java.util.concurrent.locks.Lock;
|
---|
22 | import java.util.concurrent.locks.ReadWriteLock;
|
---|
23 | import java.util.concurrent.locks.ReentrantReadWriteLock;
|
---|
24 | import java.util.function.Predicate;
|
---|
25 | import java.util.stream.Collectors;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.data.Bounds;
|
---|
29 | import org.openstreetmap.josm.data.Data;
|
---|
30 | import org.openstreetmap.josm.data.DataSource;
|
---|
31 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
32 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
33 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
34 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
35 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
36 | import org.openstreetmap.josm.data.osm.event.ChangesetIdChangedEvent;
|
---|
37 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
38 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
39 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
40 | import org.openstreetmap.josm.data.osm.event.PrimitiveFlagsChangedEvent;
|
---|
41 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
42 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
43 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
44 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
45 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
46 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
47 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
48 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
49 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
50 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
51 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
52 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
53 | import org.openstreetmap.josm.tools.Utils;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * DataSet is the data behind the application. It can consists of only a few points up to the whole
|
---|
57 | * osm database. DataSet's can be merged together, saved, (up/down/disk)loaded etc.
|
---|
58 | *
|
---|
59 | * Note that DataSet is not an osm-primitive and so has no key association but a few members to
|
---|
60 | * store some information.
|
---|
61 | *
|
---|
62 | * Dataset is threadsafe - accessing Dataset simultaneously from different threads should never
|
---|
63 | * lead to data corruption or ConcurrentModificationException. However when for example one thread
|
---|
64 | * removes primitive and other thread try to add another primitive referring to the removed primitive,
|
---|
65 | * DataIntegrityException will occur.
|
---|
66 | *
|
---|
67 | * To prevent such situations, read/write lock is provided. While read lock is used, it's guaranteed that
|
---|
68 | * Dataset will not change. Sample usage:
|
---|
69 | * <code>
|
---|
70 | * ds.getReadLock().lock();
|
---|
71 | * try {
|
---|
72 | * // .. do something with dataset
|
---|
73 | * } finally {
|
---|
74 | * ds.getReadLock().unlock();
|
---|
75 | * }
|
---|
76 | * </code>
|
---|
77 | *
|
---|
78 | * Write lock should be used in case of bulk operations. In addition to ensuring that other threads can't
|
---|
79 | * use dataset in the middle of modifications it also stops sending of dataset events. That's good for performance
|
---|
80 | * reasons - GUI can be updated after all changes are done.
|
---|
81 | * Sample usage:
|
---|
82 | * <code>
|
---|
83 | * ds.beginUpdate()
|
---|
84 | * try {
|
---|
85 | * // .. do modifications
|
---|
86 | * } finally {
|
---|
87 | * ds.endUpdate();
|
---|
88 | * }
|
---|
89 | * </code>
|
---|
90 | *
|
---|
91 | * Note that it is not necessary to call beginUpdate/endUpdate for every dataset modification - dataset will get locked
|
---|
92 | * automatically.
|
---|
93 | *
|
---|
94 | * Note that locks cannot be upgraded - if one threads use read lock and and then write lock, dead lock will occur - see #5814 for
|
---|
95 | * sample ticket
|
---|
96 | *
|
---|
97 | * @author imi
|
---|
98 | */
|
---|
99 | public final class DataSet implements Data, ProjectionChangeListener {
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Maximum number of events that can be fired between beginUpdate/endUpdate to be send as single events (ie without DatasetChangedEvent)
|
---|
103 | */
|
---|
104 | private static final int MAX_SINGLE_EVENTS = 30;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Maximum number of events to kept between beginUpdate/endUpdate. When more events are created, that simple DatasetChangedEvent is sent)
|
---|
108 | */
|
---|
109 | private static final int MAX_EVENTS = 1000;
|
---|
110 |
|
---|
111 | private final Storage<OsmPrimitive> allPrimitives = new Storage<>(new Storage.PrimitiveIdHash(), true);
|
---|
112 | private final Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new Storage.PrimitiveIdHash());
|
---|
113 | private final CopyOnWriteArrayList<DataSetListener> listeners = new CopyOnWriteArrayList<>();
|
---|
114 |
|
---|
115 | // provide means to highlight map elements that are not osm primitives
|
---|
116 | private Collection<WaySegment> highlightedVirtualNodes = new LinkedList<>();
|
---|
117 | private Collection<WaySegment> highlightedWaySegments = new LinkedList<>();
|
---|
118 |
|
---|
119 | // Number of open calls to beginUpdate
|
---|
120 | private int updateCount;
|
---|
121 | // Events that occurred while dataset was locked but should be fired after write lock is released
|
---|
122 | private final List<AbstractDatasetChangedEvent> cachedEvents = new ArrayList<>();
|
---|
123 |
|
---|
124 | private int highlightUpdateCount;
|
---|
125 |
|
---|
126 | private boolean uploadDiscouraged;
|
---|
127 |
|
---|
128 | private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
---|
129 | private final Object selectionLock = new Object();
|
---|
130 |
|
---|
131 | private Area cachedDataSourceArea;
|
---|
132 | private List<Bounds> cachedDataSourceBounds;
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Constructs a new {@code DataSet}.
|
---|
136 | */
|
---|
137 | public DataSet() {
|
---|
138 | // Transparently register as projection change listener. No need to explicitly remove
|
---|
139 | // the listener, projection change listeners are managed as WeakReferences.
|
---|
140 | Main.addProjectionChangeListener(this);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Creates a new {@link DataSet}.
|
---|
145 | * @param copyFrom An other {@link DataSet} to copy the contents of this dataset from.
|
---|
146 | * @since 10346
|
---|
147 | */
|
---|
148 | public DataSet(DataSet copyFrom) {
|
---|
149 | this();
|
---|
150 | copyFrom.getReadLock().lock();
|
---|
151 | try {
|
---|
152 | Map<OsmPrimitive, OsmPrimitive> primMap = new HashMap<>();
|
---|
153 | for (Node n : copyFrom.nodes) {
|
---|
154 | Node newNode = new Node(n);
|
---|
155 | primMap.put(n, newNode);
|
---|
156 | addPrimitive(newNode);
|
---|
157 | }
|
---|
158 | for (Way w : copyFrom.ways) {
|
---|
159 | Way newWay = new Way(w);
|
---|
160 | primMap.put(w, newWay);
|
---|
161 | List<Node> newNodes = new ArrayList<>();
|
---|
162 | for (Node n: w.getNodes()) {
|
---|
163 | newNodes.add((Node) primMap.get(n));
|
---|
164 | }
|
---|
165 | newWay.setNodes(newNodes);
|
---|
166 | addPrimitive(newWay);
|
---|
167 | }
|
---|
168 | // Because relations can have other relations as members we first clone all relations
|
---|
169 | // and then get the cloned members
|
---|
170 | for (Relation r : copyFrom.relations) {
|
---|
171 | Relation newRelation = new Relation(r, r.isNew());
|
---|
172 | newRelation.setMembers(null);
|
---|
173 | primMap.put(r, newRelation);
|
---|
174 | addPrimitive(newRelation);
|
---|
175 | }
|
---|
176 | for (Relation r : copyFrom.relations) {
|
---|
177 | Relation newRelation = (Relation) primMap.get(r);
|
---|
178 | List<RelationMember> newMembers = new ArrayList<>();
|
---|
179 | for (RelationMember rm: r.getMembers()) {
|
---|
180 | newMembers.add(new RelationMember(rm.getRole(), primMap.get(rm.getMember())));
|
---|
181 | }
|
---|
182 | newRelation.setMembers(newMembers);
|
---|
183 | }
|
---|
184 | for (DataSource source : copyFrom.dataSources) {
|
---|
185 | dataSources.add(new DataSource(source));
|
---|
186 | }
|
---|
187 | version = copyFrom.version;
|
---|
188 | } finally {
|
---|
189 | copyFrom.getReadLock().unlock();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Adds a new data source.
|
---|
195 | * @param source data source to add
|
---|
196 | * @return {@code true} if the collection changed as a result of the call
|
---|
197 | * @since 11626
|
---|
198 | */
|
---|
199 | public synchronized boolean addDataSource(DataSource source) {
|
---|
200 | return addDataSources(Collections.singleton(source));
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Adds new data sources.
|
---|
205 | * @param sources data sources to add
|
---|
206 | * @return {@code true} if the collection changed as a result of the call
|
---|
207 | * @since 11626
|
---|
208 | */
|
---|
209 | public synchronized boolean addDataSources(Collection<DataSource> sources) {
|
---|
210 | boolean changed = dataSources.addAll(sources);
|
---|
211 | if (changed) {
|
---|
212 | cachedDataSourceArea = null;
|
---|
213 | cachedDataSourceBounds = null;
|
---|
214 | }
|
---|
215 | return changed;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Returns the lock used for reading.
|
---|
220 | * @return the lock used for reading
|
---|
221 | */
|
---|
222 | public Lock getReadLock() {
|
---|
223 | return lock.readLock();
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * This method can be used to detect changes in highlight state of primitives. If highlighting was changed
|
---|
228 | * then the method will return different number.
|
---|
229 | * @return the current highlight counter
|
---|
230 | */
|
---|
231 | public int getHighlightUpdateCount() {
|
---|
232 | return highlightUpdateCount;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * History of selections - shared by plugins and SelectionListDialog
|
---|
237 | */
|
---|
238 | private final LinkedList<Collection<? extends OsmPrimitive>> selectionHistory = new LinkedList<>();
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Replies the history of JOSM selections
|
---|
242 | *
|
---|
243 | * @return list of history entries
|
---|
244 | */
|
---|
245 | public LinkedList<Collection<? extends OsmPrimitive>> getSelectionHistory() {
|
---|
246 | return selectionHistory;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Clears selection history list
|
---|
251 | */
|
---|
252 | public void clearSelectionHistory() {
|
---|
253 | selectionHistory.clear();
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Maintains a list of used tags for autocompletion.
|
---|
258 | */
|
---|
259 | private AutoCompletionManager autocomplete;
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Returns the autocompletion manager, which maintains a list of used tags for autocompletion.
|
---|
263 | * @return the autocompletion manager
|
---|
264 | */
|
---|
265 | public AutoCompletionManager getAutoCompletionManager() {
|
---|
266 | if (autocomplete == null) {
|
---|
267 | autocomplete = new AutoCompletionManager(this);
|
---|
268 | addDataSetListener(autocomplete);
|
---|
269 | }
|
---|
270 | return autocomplete;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * The API version that created this data set, if any.
|
---|
275 | */
|
---|
276 | private String version;
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Replies the API version this dataset was created from. May be null.
|
---|
280 | *
|
---|
281 | * @return the API version this dataset was created from. May be null.
|
---|
282 | */
|
---|
283 | public String getVersion() {
|
---|
284 | return version;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Sets the API version this dataset was created from.
|
---|
289 | *
|
---|
290 | * @param version the API version, i.e. "0.6"
|
---|
291 | */
|
---|
292 | public void setVersion(String version) {
|
---|
293 | this.version = version;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Determines if upload is being discouraged (i.e. this dataset contains private data which should not be uploaded)
|
---|
298 | * @return {@code true} if upload is being discouraged, {@code false} otherwise
|
---|
299 | * @see #setUploadDiscouraged
|
---|
300 | */
|
---|
301 | public boolean isUploadDiscouraged() {
|
---|
302 | return uploadDiscouraged;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Sets the "upload discouraged" flag.
|
---|
307 | * @param uploadDiscouraged {@code true} if this dataset contains private data which should not be uploaded
|
---|
308 | * @see #isUploadDiscouraged
|
---|
309 | */
|
---|
310 | public void setUploadDiscouraged(boolean uploadDiscouraged) {
|
---|
311 | this.uploadDiscouraged = uploadDiscouraged;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Holding bin for changeset tag information, to be applied when or if this is ever uploaded.
|
---|
316 | */
|
---|
317 | private final Map<String, String> changeSetTags = new HashMap<>();
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Replies the set of changeset tags to be applied when or if this is ever uploaded.
|
---|
321 | * @return the set of changeset tags
|
---|
322 | * @see #addChangeSetTag
|
---|
323 | */
|
---|
324 | public Map<String, String> getChangeSetTags() {
|
---|
325 | return changeSetTags;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Adds a new changeset tag.
|
---|
330 | * @param k Key
|
---|
331 | * @param v Value
|
---|
332 | * @see #getChangeSetTags
|
---|
333 | */
|
---|
334 | public void addChangeSetTag(String k, String v) {
|
---|
335 | this.changeSetTags.put(k, v);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * All nodes goes here, even when included in other data (ways etc). This enables the instant
|
---|
340 | * conversion of the whole DataSet by iterating over this data structure.
|
---|
341 | */
|
---|
342 | private final QuadBuckets<Node> nodes = new QuadBuckets<>();
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Gets a filtered collection of primitives matching the given predicate.
|
---|
346 | * @param <T> The primitive type.
|
---|
347 | * @param predicate The predicate to match
|
---|
348 | * @return The list of primtives.
|
---|
349 | * @since 10590
|
---|
350 | */
|
---|
351 | public <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<? super OsmPrimitive> predicate) {
|
---|
352 | return new SubclassFilteredCollection<>(allPrimitives, predicate);
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Replies an unmodifiable collection of nodes in this dataset
|
---|
357 | *
|
---|
358 | * @return an unmodifiable collection of nodes in this dataset
|
---|
359 | */
|
---|
360 | public Collection<Node> getNodes() {
|
---|
361 | return getPrimitives(Node.class::isInstance);
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Searches for nodes in the given bounding box.
|
---|
366 | * @param bbox the bounding box
|
---|
367 | * @return List of nodes in the given bbox. Can be empty but not null
|
---|
368 | */
|
---|
369 | public List<Node> searchNodes(BBox bbox) {
|
---|
370 | lock.readLock().lock();
|
---|
371 | try {
|
---|
372 | return nodes.search(bbox);
|
---|
373 | } finally {
|
---|
374 | lock.readLock().unlock();
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Determines if the given node can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
380 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
381 | *
|
---|
382 | * @param n The node to search
|
---|
383 | * @return {@code true} if {@code n} ban be retrieved in this data set, {@code false} otherwise
|
---|
384 | * @since 7501
|
---|
385 | */
|
---|
386 | public boolean containsNode(Node n) {
|
---|
387 | return nodes.contains(n);
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * All ways (Streets etc.) in the DataSet.
|
---|
392 | *
|
---|
393 | * The way nodes are stored only in the way list.
|
---|
394 | */
|
---|
395 | private final QuadBuckets<Way> ways = new QuadBuckets<>();
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Replies an unmodifiable collection of ways in this dataset
|
---|
399 | *
|
---|
400 | * @return an unmodifiable collection of ways in this dataset
|
---|
401 | */
|
---|
402 | public Collection<Way> getWays() {
|
---|
403 | return getPrimitives(Way.class::isInstance);
|
---|
404 | }
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Searches for ways in the given bounding box.
|
---|
408 | * @param bbox the bounding box
|
---|
409 | * @return List of ways in the given bbox. Can be empty but not null
|
---|
410 | */
|
---|
411 | public List<Way> searchWays(BBox bbox) {
|
---|
412 | lock.readLock().lock();
|
---|
413 | try {
|
---|
414 | return ways.search(bbox);
|
---|
415 | } finally {
|
---|
416 | lock.readLock().unlock();
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Determines if the given way can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
422 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
423 | *
|
---|
424 | * @param w The way to search
|
---|
425 | * @return {@code true} if {@code w} ban be retrieved in this data set, {@code false} otherwise
|
---|
426 | * @since 7501
|
---|
427 | */
|
---|
428 | public boolean containsWay(Way w) {
|
---|
429 | return ways.contains(w);
|
---|
430 | }
|
---|
431 |
|
---|
432 | /**
|
---|
433 | * All relations/relationships
|
---|
434 | */
|
---|
435 | private final Collection<Relation> relations = new ArrayList<>();
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Replies an unmodifiable collection of relations in this dataset
|
---|
439 | *
|
---|
440 | * @return an unmodifiable collection of relations in this dataset
|
---|
441 | */
|
---|
442 | public Collection<Relation> getRelations() {
|
---|
443 | return getPrimitives(Relation.class::isInstance);
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Searches for relations in the given bounding box.
|
---|
448 | * @param bbox the bounding box
|
---|
449 | * @return List of relations in the given bbox. Can be empty but not null
|
---|
450 | */
|
---|
451 | public List<Relation> searchRelations(BBox bbox) {
|
---|
452 | lock.readLock().lock();
|
---|
453 | try {
|
---|
454 | // QuadBuckets might be useful here (don't forget to do reindexing after some of rm is changed)
|
---|
455 | return relations.stream()
|
---|
456 | .filter(r -> r.getBBox().intersects(bbox))
|
---|
457 | .collect(Collectors.toList());
|
---|
458 | } finally {
|
---|
459 | lock.readLock().unlock();
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Determines if the given relation can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
465 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
466 | *
|
---|
467 | * @param r The relation to search
|
---|
468 | * @return {@code true} if {@code r} ban be retrieved in this data set, {@code false} otherwise
|
---|
469 | * @since 7501
|
---|
470 | */
|
---|
471 | public boolean containsRelation(Relation r) {
|
---|
472 | return relations.contains(r);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * All data sources of this DataSet.
|
---|
477 | */
|
---|
478 | public final Collection<DataSource> dataSources = new LinkedList<>();
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Returns a collection containing all primitives of the dataset.
|
---|
482 | * @return A collection containing all primitives of the dataset. Data is not ordered
|
---|
483 | */
|
---|
484 | public Collection<OsmPrimitive> allPrimitives() {
|
---|
485 | return getPrimitives(o -> true);
|
---|
486 | }
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Returns a collection containing all not-deleted primitives.
|
---|
490 | * @return A collection containing all not-deleted primitives.
|
---|
491 | * @see OsmPrimitive#isDeleted
|
---|
492 | */
|
---|
493 | public Collection<OsmPrimitive> allNonDeletedPrimitives() {
|
---|
494 | return getPrimitives(p -> !p.isDeleted());
|
---|
495 | }
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Returns a collection containing all not-deleted complete primitives.
|
---|
499 | * @return A collection containing all not-deleted complete primitives.
|
---|
500 | * @see OsmPrimitive#isDeleted
|
---|
501 | * @see OsmPrimitive#isIncomplete
|
---|
502 | */
|
---|
503 | public Collection<OsmPrimitive> allNonDeletedCompletePrimitives() {
|
---|
504 | return getPrimitives(primitive -> !primitive.isDeleted() && !primitive.isIncomplete());
|
---|
505 | }
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Returns a collection containing all not-deleted complete physical primitives.
|
---|
509 | * @return A collection containing all not-deleted complete physical primitives (nodes and ways).
|
---|
510 | * @see OsmPrimitive#isDeleted
|
---|
511 | * @see OsmPrimitive#isIncomplete
|
---|
512 | */
|
---|
513 | public Collection<OsmPrimitive> allNonDeletedPhysicalPrimitives() {
|
---|
514 | return getPrimitives(primitive -> !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation));
|
---|
515 | }
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * Returns a collection containing all modified primitives.
|
---|
519 | * @return A collection containing all modified primitives.
|
---|
520 | * @see OsmPrimitive#isModified
|
---|
521 | */
|
---|
522 | public Collection<OsmPrimitive> allModifiedPrimitives() {
|
---|
523 | return getPrimitives(OsmPrimitive::isModified);
|
---|
524 | }
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Adds a primitive to the dataset.
|
---|
528 | *
|
---|
529 | * @param primitive the primitive.
|
---|
530 | */
|
---|
531 | public void addPrimitive(OsmPrimitive primitive) {
|
---|
532 | Objects.requireNonNull(primitive, "primitive");
|
---|
533 | beginUpdate();
|
---|
534 | try {
|
---|
535 | if (getPrimitiveById(primitive) != null)
|
---|
536 | throw new DataIntegrityProblemException(
|
---|
537 | tr("Unable to add primitive {0} to the dataset because it is already included", primitive.toString()));
|
---|
538 |
|
---|
539 | allPrimitives.add(primitive);
|
---|
540 | primitive.setDataset(this);
|
---|
541 | primitive.updatePosition(); // Set cached bbox for way and relation (required for reindexWay and reindexRelation to work properly)
|
---|
542 | boolean success = false;
|
---|
543 | if (primitive instanceof Node) {
|
---|
544 | success = nodes.add((Node) primitive);
|
---|
545 | } else if (primitive instanceof Way) {
|
---|
546 | success = ways.add((Way) primitive);
|
---|
547 | } else if (primitive instanceof Relation) {
|
---|
548 | success = relations.add((Relation) primitive);
|
---|
549 | }
|
---|
550 | if (!success)
|
---|
551 | throw new JosmRuntimeException("failed to add primitive: "+primitive);
|
---|
552 | firePrimitivesAdded(Collections.singletonList(primitive), false);
|
---|
553 | } finally {
|
---|
554 | endUpdate();
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * Removes a primitive from the dataset. This method only removes the
|
---|
560 | * primitive form the respective collection of primitives managed
|
---|
561 | * by this dataset, i.e. from {@link #nodes}, {@link #ways}, or
|
---|
562 | * {@link #relations}. References from other primitives to this
|
---|
563 | * primitive are left unchanged.
|
---|
564 | *
|
---|
565 | * @param primitiveId the id of the primitive
|
---|
566 | */
|
---|
567 | public void removePrimitive(PrimitiveId primitiveId) {
|
---|
568 | beginUpdate();
|
---|
569 | try {
|
---|
570 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
571 | if (primitive == null)
|
---|
572 | return;
|
---|
573 | boolean success = false;
|
---|
574 | if (primitive instanceof Node) {
|
---|
575 | success = nodes.remove(primitive);
|
---|
576 | } else if (primitive instanceof Way) {
|
---|
577 | success = ways.remove(primitive);
|
---|
578 | } else if (primitive instanceof Relation) {
|
---|
579 | success = relations.remove(primitive);
|
---|
580 | }
|
---|
581 | if (!success)
|
---|
582 | throw new JosmRuntimeException("failed to remove primitive: "+primitive);
|
---|
583 | synchronized (selectionLock) {
|
---|
584 | selectedPrimitives.remove(primitive);
|
---|
585 | selectionSnapshot = null;
|
---|
586 | }
|
---|
587 | allPrimitives.remove(primitive);
|
---|
588 | primitive.setDataset(null);
|
---|
589 | firePrimitivesRemoved(Collections.singletonList(primitive), false);
|
---|
590 | } finally {
|
---|
591 | endUpdate();
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | /*---------------------------------------------------
|
---|
596 | * SELECTION HANDLING
|
---|
597 | *---------------------------------------------------*/
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * A list of listeners to selection changed events. The list is static, as listeners register
|
---|
601 | * themselves for any dataset selection changes that occur, regardless of the current active
|
---|
602 | * dataset. (However, the selection does only change in the active layer)
|
---|
603 | */
|
---|
604 | private static final Collection<SelectionChangedListener> selListeners = new CopyOnWriteArrayList<>();
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Adds a new selection listener.
|
---|
608 | * @param listener The selection listener to add
|
---|
609 | */
|
---|
610 | public static void addSelectionListener(SelectionChangedListener listener) {
|
---|
611 | ((CopyOnWriteArrayList<SelectionChangedListener>) selListeners).addIfAbsent(listener);
|
---|
612 | }
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Removes a selection listener.
|
---|
616 | * @param listener The selection listener to remove
|
---|
617 | */
|
---|
618 | public static void removeSelectionListener(SelectionChangedListener listener) {
|
---|
619 | selListeners.remove(listener);
|
---|
620 | }
|
---|
621 |
|
---|
622 | /**
|
---|
623 | * Notifies all registered {@link SelectionChangedListener} about the current selection in
|
---|
624 | * this dataset.
|
---|
625 | *
|
---|
626 | */
|
---|
627 | public void fireSelectionChanged() {
|
---|
628 | Collection<? extends OsmPrimitive> currentSelection = getAllSelected();
|
---|
629 | for (SelectionChangedListener l : selListeners) {
|
---|
630 | l.selectionChanged(currentSelection);
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | private Set<OsmPrimitive> selectedPrimitives = new LinkedHashSet<>();
|
---|
635 | private Collection<OsmPrimitive> selectionSnapshot;
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Returns selected nodes and ways.
|
---|
639 | * @return selected nodes and ways
|
---|
640 | */
|
---|
641 | public Collection<OsmPrimitive> getSelectedNodesAndWays() {
|
---|
642 | return new SubclassFilteredCollection<>(getSelected(), primitive -> primitive instanceof Node || primitive instanceof Way);
|
---|
643 | }
|
---|
644 |
|
---|
645 | /**
|
---|
646 | * Returns an unmodifiable collection of *WaySegments* whose virtual
|
---|
647 | * nodes should be highlighted. WaySegments are used to avoid having
|
---|
648 | * to create a VirtualNode class that wouldn't have much purpose otherwise.
|
---|
649 | *
|
---|
650 | * @return unmodifiable collection of WaySegments
|
---|
651 | */
|
---|
652 | public Collection<WaySegment> getHighlightedVirtualNodes() {
|
---|
653 | return Collections.unmodifiableCollection(highlightedVirtualNodes);
|
---|
654 | }
|
---|
655 |
|
---|
656 | /**
|
---|
657 | * Returns an unmodifiable collection of WaySegments that should be highlighted.
|
---|
658 | *
|
---|
659 | * @return unmodifiable collection of WaySegments
|
---|
660 | */
|
---|
661 | public Collection<WaySegment> getHighlightedWaySegments() {
|
---|
662 | return Collections.unmodifiableCollection(highlightedWaySegments);
|
---|
663 | }
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * Replies an unmodifiable collection of primitives currently selected
|
---|
667 | * in this dataset, except deleted ones. May be empty, but not null.
|
---|
668 | *
|
---|
669 | * @return unmodifiable collection of primitives
|
---|
670 | */
|
---|
671 | public Collection<OsmPrimitive> getSelected() {
|
---|
672 | return new SubclassFilteredCollection<>(getAllSelected(), p -> !p.isDeleted());
|
---|
673 | }
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Replies an unmodifiable collection of primitives currently selected
|
---|
677 | * in this dataset, including deleted ones. May be empty, but not null.
|
---|
678 | *
|
---|
679 | * @return unmodifiable collection of primitives
|
---|
680 | */
|
---|
681 | public Collection<OsmPrimitive> getAllSelected() {
|
---|
682 | Collection<OsmPrimitive> currentList;
|
---|
683 | synchronized (selectionLock) {
|
---|
684 | if (selectionSnapshot == null) {
|
---|
685 | selectionSnapshot = Collections.unmodifiableList(new ArrayList<>(selectedPrimitives));
|
---|
686 | }
|
---|
687 | currentList = selectionSnapshot;
|
---|
688 | }
|
---|
689 | return currentList;
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Returns selected nodes.
|
---|
694 | * @return selected nodes
|
---|
695 | */
|
---|
696 | public Collection<Node> getSelectedNodes() {
|
---|
697 | return new SubclassFilteredCollection<>(getSelected(), Node.class::isInstance);
|
---|
698 | }
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * Returns selected ways.
|
---|
702 | * @return selected ways
|
---|
703 | */
|
---|
704 | public Collection<Way> getSelectedWays() {
|
---|
705 | return new SubclassFilteredCollection<>(getSelected(), Way.class::isInstance);
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Returns selected relations.
|
---|
710 | * @return selected relations
|
---|
711 | */
|
---|
712 | public Collection<Relation> getSelectedRelations() {
|
---|
713 | return new SubclassFilteredCollection<>(getSelected(), Relation.class::isInstance);
|
---|
714 | }
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * Determines whether the selection is empty or not
|
---|
718 | * @return whether the selection is empty or not
|
---|
719 | */
|
---|
720 | public boolean selectionEmpty() {
|
---|
721 | return selectedPrimitives.isEmpty();
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Determines whether the given primitive is selected or not
|
---|
726 | * @param osm the primitive
|
---|
727 | * @return whether {@code osm} is selected or not
|
---|
728 | */
|
---|
729 | public boolean isSelected(OsmPrimitive osm) {
|
---|
730 | return selectedPrimitives.contains(osm);
|
---|
731 | }
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * Toggles the selected state of the given collection of primitives.
|
---|
735 | * @param osm The primitives to toggle
|
---|
736 | */
|
---|
737 | public void toggleSelected(Collection<? extends PrimitiveId> osm) {
|
---|
738 | boolean changed = false;
|
---|
739 | synchronized (selectionLock) {
|
---|
740 | for (PrimitiveId o : osm) {
|
---|
741 | changed = changed | this.dotoggleSelected(o);
|
---|
742 | }
|
---|
743 | if (changed) {
|
---|
744 | selectionSnapshot = null;
|
---|
745 | }
|
---|
746 | }
|
---|
747 | if (changed) {
|
---|
748 | fireSelectionChanged();
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Toggles the selected state of the given collection of primitives.
|
---|
754 | * @param osm The primitives to toggle
|
---|
755 | */
|
---|
756 | public void toggleSelected(PrimitiveId... osm) {
|
---|
757 | toggleSelected(Arrays.asList(osm));
|
---|
758 | }
|
---|
759 |
|
---|
760 | private boolean dotoggleSelected(PrimitiveId primitiveId) {
|
---|
761 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
762 | if (primitive == null)
|
---|
763 | return false;
|
---|
764 | if (!selectedPrimitives.remove(primitive)) {
|
---|
765 | selectedPrimitives.add(primitive);
|
---|
766 | }
|
---|
767 | selectionSnapshot = null;
|
---|
768 | return true;
|
---|
769 | }
|
---|
770 |
|
---|
771 | /**
|
---|
772 | * set what virtual nodes should be highlighted. Requires a Collection of
|
---|
773 | * *WaySegments* to avoid a VirtualNode class that wouldn't have much use
|
---|
774 | * otherwise.
|
---|
775 | * @param waySegments Collection of way segments
|
---|
776 | */
|
---|
777 | public void setHighlightedVirtualNodes(Collection<WaySegment> waySegments) {
|
---|
778 | if (highlightedVirtualNodes.isEmpty() && waySegments.isEmpty())
|
---|
779 | return;
|
---|
780 |
|
---|
781 | highlightedVirtualNodes = waySegments;
|
---|
782 | fireHighlightingChanged();
|
---|
783 | }
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * set what virtual ways should be highlighted.
|
---|
787 | * @param waySegments Collection of way segments
|
---|
788 | */
|
---|
789 | public void setHighlightedWaySegments(Collection<WaySegment> waySegments) {
|
---|
790 | if (highlightedWaySegments.isEmpty() && waySegments.isEmpty())
|
---|
791 | return;
|
---|
792 |
|
---|
793 | highlightedWaySegments = waySegments;
|
---|
794 | fireHighlightingChanged();
|
---|
795 | }
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Sets the current selection to the primitives in <code>selection</code>.
|
---|
799 | * Notifies all {@link SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
|
---|
800 | *
|
---|
801 | * @param selection the selection
|
---|
802 | * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
|
---|
803 | */
|
---|
804 | public void setSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
|
---|
805 | boolean changed;
|
---|
806 | synchronized (selectionLock) {
|
---|
807 | Set<OsmPrimitive> oldSelection = new LinkedHashSet<>(selectedPrimitives);
|
---|
808 | selectedPrimitives = new LinkedHashSet<>();
|
---|
809 | addSelected(selection, false);
|
---|
810 | changed = !oldSelection.equals(selectedPrimitives);
|
---|
811 | if (changed) {
|
---|
812 | selectionSnapshot = null;
|
---|
813 | }
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (changed && fireSelectionChangeEvent) {
|
---|
817 | // If selection is not empty then event was already fired in addSelecteds
|
---|
818 | fireSelectionChanged();
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | /**
|
---|
823 | * Sets the current selection to the primitives in <code>selection</code>
|
---|
824 | * and notifies all {@link SelectionChangedListener}.
|
---|
825 | *
|
---|
826 | * @param selection the selection
|
---|
827 | */
|
---|
828 | public void setSelected(Collection<? extends PrimitiveId> selection) {
|
---|
829 | setSelected(selection, true /* fire selection change event */);
|
---|
830 | }
|
---|
831 |
|
---|
832 | /**
|
---|
833 | * Sets the current selection to the primitives in <code>osm</code>
|
---|
834 | * and notifies all {@link SelectionChangedListener}.
|
---|
835 | *
|
---|
836 | * @param osm the primitives to set
|
---|
837 | */
|
---|
838 | public void setSelected(PrimitiveId... osm) {
|
---|
839 | if (osm.length == 1 && osm[0] == null) {
|
---|
840 | setSelected();
|
---|
841 | return;
|
---|
842 | }
|
---|
843 | List<PrimitiveId> list = Arrays.asList(osm);
|
---|
844 | setSelected(list);
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Adds the primitives in <code>selection</code> to the current selection
|
---|
849 | * and notifies all {@link SelectionChangedListener}.
|
---|
850 | *
|
---|
851 | * @param selection the selection
|
---|
852 | */
|
---|
853 | public void addSelected(Collection<? extends PrimitiveId> selection) {
|
---|
854 | addSelected(selection, true /* fire selection change event */);
|
---|
855 | }
|
---|
856 |
|
---|
857 | /**
|
---|
858 | * Adds the primitives in <code>osm</code> to the current selection
|
---|
859 | * and notifies all {@link SelectionChangedListener}.
|
---|
860 | *
|
---|
861 | * @param osm the primitives to add
|
---|
862 | */
|
---|
863 | public void addSelected(PrimitiveId... osm) {
|
---|
864 | addSelected(Arrays.asList(osm));
|
---|
865 | }
|
---|
866 |
|
---|
867 | /**
|
---|
868 | * Adds the primitives in <code>selection</code> to the current selection.
|
---|
869 | * Notifies all {@link SelectionChangedListener} if <code>fireSelectionChangeEvent</code> is true.
|
---|
870 | *
|
---|
871 | * @param selection the selection
|
---|
872 | * @param fireSelectionChangeEvent true, if the selection change listeners are to be notified; false, otherwise
|
---|
873 | * @return if the selection was changed in the process
|
---|
874 | */
|
---|
875 | private boolean addSelected(Collection<? extends PrimitiveId> selection, boolean fireSelectionChangeEvent) {
|
---|
876 | boolean changed = false;
|
---|
877 | synchronized (selectionLock) {
|
---|
878 | for (PrimitiveId id: selection) {
|
---|
879 | OsmPrimitive primitive = getPrimitiveByIdChecked(id);
|
---|
880 | if (primitive != null) {
|
---|
881 | changed = changed | selectedPrimitives.add(primitive);
|
---|
882 | }
|
---|
883 | }
|
---|
884 | if (changed) {
|
---|
885 | selectionSnapshot = null;
|
---|
886 | }
|
---|
887 | }
|
---|
888 | if (fireSelectionChangeEvent && changed) {
|
---|
889 | fireSelectionChanged();
|
---|
890 | }
|
---|
891 | return changed;
|
---|
892 | }
|
---|
893 |
|
---|
894 | /**
|
---|
895 | * clear all highlights of virtual nodes
|
---|
896 | */
|
---|
897 | public void clearHighlightedVirtualNodes() {
|
---|
898 | setHighlightedVirtualNodes(new ArrayList<WaySegment>());
|
---|
899 | }
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * clear all highlights of way segments
|
---|
903 | */
|
---|
904 | public void clearHighlightedWaySegments() {
|
---|
905 | setHighlightedWaySegments(new ArrayList<WaySegment>());
|
---|
906 | }
|
---|
907 |
|
---|
908 | /**
|
---|
909 | * Removes the selection from every value in the collection.
|
---|
910 | * @param osm The collection of ids to remove the selection from.
|
---|
911 | */
|
---|
912 | public void clearSelection(PrimitiveId... osm) {
|
---|
913 | clearSelection(Arrays.asList(osm));
|
---|
914 | }
|
---|
915 |
|
---|
916 | /**
|
---|
917 | * Removes the selection from every value in the collection.
|
---|
918 | * @param list The collection of ids to remove the selection from.
|
---|
919 | */
|
---|
920 | public void clearSelection(Collection<? extends PrimitiveId> list) {
|
---|
921 | boolean changed = false;
|
---|
922 | synchronized (selectionLock) {
|
---|
923 | for (PrimitiveId id:list) {
|
---|
924 | OsmPrimitive primitive = getPrimitiveById(id);
|
---|
925 | if (primitive != null) {
|
---|
926 | changed = changed | selectedPrimitives.remove(primitive);
|
---|
927 | }
|
---|
928 | }
|
---|
929 | if (changed) {
|
---|
930 | selectionSnapshot = null;
|
---|
931 | }
|
---|
932 | }
|
---|
933 | if (changed) {
|
---|
934 | fireSelectionChanged();
|
---|
935 | }
|
---|
936 | }
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Clears the current selection.
|
---|
940 | */
|
---|
941 | public void clearSelection() {
|
---|
942 | if (!selectedPrimitives.isEmpty()) {
|
---|
943 | synchronized (selectionLock) {
|
---|
944 | selectedPrimitives.clear();
|
---|
945 | selectionSnapshot = null;
|
---|
946 | }
|
---|
947 | fireSelectionChanged();
|
---|
948 | }
|
---|
949 | }
|
---|
950 |
|
---|
951 | @Override
|
---|
952 | public synchronized Area getDataSourceArea() {
|
---|
953 | if (cachedDataSourceArea == null) {
|
---|
954 | cachedDataSourceArea = Data.super.getDataSourceArea();
|
---|
955 | }
|
---|
956 | return cachedDataSourceArea;
|
---|
957 | }
|
---|
958 |
|
---|
959 | @Override
|
---|
960 | public synchronized List<Bounds> getDataSourceBounds() {
|
---|
961 | if (cachedDataSourceBounds == null) {
|
---|
962 | cachedDataSourceBounds = Data.super.getDataSourceBounds();
|
---|
963 | }
|
---|
964 | return Collections.unmodifiableList(cachedDataSourceBounds);
|
---|
965 | }
|
---|
966 |
|
---|
967 | @Override
|
---|
968 | public synchronized Collection<DataSource> getDataSources() {
|
---|
969 | return Collections.unmodifiableCollection(dataSources);
|
---|
970 | }
|
---|
971 |
|
---|
972 | /**
|
---|
973 | * Returns a primitive with a given id from the data set. null, if no such primitive exists
|
---|
974 | *
|
---|
975 | * @param id uniqueId of the primitive. Might be < 0 for newly created primitives
|
---|
976 | * @param type the type of the primitive. Must not be null.
|
---|
977 | * @return the primitive
|
---|
978 | * @throws NullPointerException if type is null
|
---|
979 | */
|
---|
980 | public OsmPrimitive getPrimitiveById(long id, OsmPrimitiveType type) {
|
---|
981 | return getPrimitiveById(new SimplePrimitiveId(id, type));
|
---|
982 | }
|
---|
983 |
|
---|
984 | /**
|
---|
985 | * Returns a primitive with a given id from the data set. null, if no such primitive exists
|
---|
986 | *
|
---|
987 | * @param primitiveId type and uniqueId of the primitive. Might be < 0 for newly created primitives
|
---|
988 | * @return the primitive
|
---|
989 | */
|
---|
990 | public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId) {
|
---|
991 | return primitiveId != null ? primitivesMap.get(primitiveId) : null;
|
---|
992 | }
|
---|
993 |
|
---|
994 | /**
|
---|
995 | * Show message and stack trace in log in case primitive is not found
|
---|
996 | * @param primitiveId primitive id to look for
|
---|
997 | * @return Primitive by id.
|
---|
998 | */
|
---|
999 | private OsmPrimitive getPrimitiveByIdChecked(PrimitiveId primitiveId) {
|
---|
1000 | OsmPrimitive result = getPrimitiveById(primitiveId);
|
---|
1001 | if (result == null && primitiveId != null) {
|
---|
1002 | Main.warn(tr("JOSM expected to find primitive [{0} {1}] in dataset but it is not there. Please report this "
|
---|
1003 | + "at {2}. This is not a critical error, it should be safe to continue in your work.",
|
---|
1004 | primitiveId.getType(), Long.toString(primitiveId.getUniqueId()), Main.getJOSMWebsite()));
|
---|
1005 | Main.error(new Exception());
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | return result;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | private static void deleteWay(Way way) {
|
---|
1012 | way.setNodes(null);
|
---|
1013 | way.setDeleted(true);
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | /**
|
---|
1017 | * Removes all references from ways in this dataset to a particular node.
|
---|
1018 | *
|
---|
1019 | * @param node the node
|
---|
1020 | * @return The set of ways that have been modified
|
---|
1021 | */
|
---|
1022 | public Set<Way> unlinkNodeFromWays(Node node) {
|
---|
1023 | Set<Way> result = new HashSet<>();
|
---|
1024 | beginUpdate();
|
---|
1025 | try {
|
---|
1026 | for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(), Way.class)) {
|
---|
1027 | List<Node> wayNodes = way.getNodes();
|
---|
1028 | if (wayNodes.remove(node)) {
|
---|
1029 | if (wayNodes.size() < 2) {
|
---|
1030 | deleteWay(way);
|
---|
1031 | } else {
|
---|
1032 | way.setNodes(wayNodes);
|
---|
1033 | }
|
---|
1034 | result.add(way);
|
---|
1035 | }
|
---|
1036 | }
|
---|
1037 | } finally {
|
---|
1038 | endUpdate();
|
---|
1039 | }
|
---|
1040 | return result;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /**
|
---|
1044 | * removes all references from relations in this dataset to this primitive
|
---|
1045 | *
|
---|
1046 | * @param primitive the primitive
|
---|
1047 | * @return The set of relations that have been modified
|
---|
1048 | */
|
---|
1049 | public Set<Relation> unlinkPrimitiveFromRelations(OsmPrimitive primitive) {
|
---|
1050 | Set<Relation> result = new HashSet<>();
|
---|
1051 | beginUpdate();
|
---|
1052 | try {
|
---|
1053 | for (Relation relation : relations) {
|
---|
1054 | List<RelationMember> members = relation.getMembers();
|
---|
1055 |
|
---|
1056 | Iterator<RelationMember> it = members.iterator();
|
---|
1057 | boolean removed = false;
|
---|
1058 | while (it.hasNext()) {
|
---|
1059 | RelationMember member = it.next();
|
---|
1060 | if (member.getMember().equals(primitive)) {
|
---|
1061 | it.remove();
|
---|
1062 | removed = true;
|
---|
1063 | }
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | if (removed) {
|
---|
1067 | relation.setMembers(members);
|
---|
1068 | result.add(relation);
|
---|
1069 | }
|
---|
1070 | }
|
---|
1071 | } finally {
|
---|
1072 | endUpdate();
|
---|
1073 | }
|
---|
1074 | return result;
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /**
|
---|
1078 | * Removes all references from other primitives to the referenced primitive.
|
---|
1079 | *
|
---|
1080 | * @param referencedPrimitive the referenced primitive
|
---|
1081 | * @return The set of primitives that have been modified
|
---|
1082 | */
|
---|
1083 | public Set<OsmPrimitive> unlinkReferencesToPrimitive(OsmPrimitive referencedPrimitive) {
|
---|
1084 | Set<OsmPrimitive> result = new HashSet<>();
|
---|
1085 | beginUpdate();
|
---|
1086 | try {
|
---|
1087 | if (referencedPrimitive instanceof Node) {
|
---|
1088 | result.addAll(unlinkNodeFromWays((Node) referencedPrimitive));
|
---|
1089 | }
|
---|
1090 | result.addAll(unlinkPrimitiveFromRelations(referencedPrimitive));
|
---|
1091 | } finally {
|
---|
1092 | endUpdate();
|
---|
1093 | }
|
---|
1094 | return result;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | /**
|
---|
1098 | * Replies true if there is at least one primitive in this dataset with
|
---|
1099 | * {@link OsmPrimitive#isModified()} == <code>true</code>.
|
---|
1100 | *
|
---|
1101 | * @return true if there is at least one primitive in this dataset with
|
---|
1102 | * {@link OsmPrimitive#isModified()} == <code>true</code>.
|
---|
1103 | */
|
---|
1104 | public boolean isModified() {
|
---|
1105 | for (OsmPrimitive p: allPrimitives) {
|
---|
1106 | if (p.isModified())
|
---|
1107 | return true;
|
---|
1108 | }
|
---|
1109 | return false;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | private void reindexNode(Node node, LatLon newCoor, EastNorth eastNorth) {
|
---|
1113 | if (!nodes.remove(node))
|
---|
1114 | throw new JosmRuntimeException("Reindexing node failed to remove");
|
---|
1115 | node.setCoorInternal(newCoor, eastNorth);
|
---|
1116 | if (!nodes.add(node))
|
---|
1117 | throw new JosmRuntimeException("Reindexing node failed to add");
|
---|
1118 | for (OsmPrimitive primitive: node.getReferrers()) {
|
---|
1119 | if (primitive instanceof Way) {
|
---|
1120 | reindexWay((Way) primitive);
|
---|
1121 | } else {
|
---|
1122 | reindexRelation((Relation) primitive);
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | private void reindexWay(Way way) {
|
---|
1128 | BBox before = way.getBBox();
|
---|
1129 | if (!ways.remove(way))
|
---|
1130 | throw new JosmRuntimeException("Reindexing way failed to remove");
|
---|
1131 | way.updatePosition();
|
---|
1132 | if (!ways.add(way))
|
---|
1133 | throw new JosmRuntimeException("Reindexing way failed to add");
|
---|
1134 | if (!way.getBBox().equals(before)) {
|
---|
1135 | for (OsmPrimitive primitive: way.getReferrers()) {
|
---|
1136 | reindexRelation((Relation) primitive);
|
---|
1137 | }
|
---|
1138 | }
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | private static void reindexRelation(Relation relation) {
|
---|
1142 | BBox before = relation.getBBox();
|
---|
1143 | relation.updatePosition();
|
---|
1144 | if (!before.equals(relation.getBBox())) {
|
---|
1145 | for (OsmPrimitive primitive: relation.getReferrers()) {
|
---|
1146 | reindexRelation((Relation) primitive);
|
---|
1147 | }
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | /**
|
---|
1152 | * Adds a new data set listener.
|
---|
1153 | * @param dsl The data set listener to add
|
---|
1154 | */
|
---|
1155 | public void addDataSetListener(DataSetListener dsl) {
|
---|
1156 | listeners.addIfAbsent(dsl);
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | /**
|
---|
1160 | * Removes a data set listener.
|
---|
1161 | * @param dsl The data set listener to remove
|
---|
1162 | */
|
---|
1163 | public void removeDataSetListener(DataSetListener dsl) {
|
---|
1164 | listeners.remove(dsl);
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | /**
|
---|
1168 | * Can be called before bigger changes on dataset. Events are disabled until {@link #endUpdate()}.
|
---|
1169 | * {@link DataSetListener#dataChanged(DataChangedEvent event)} event is triggered after end of changes
|
---|
1170 | * <br>
|
---|
1171 | * Typical usecase should look like this:
|
---|
1172 | * <pre>
|
---|
1173 | * ds.beginUpdate();
|
---|
1174 | * try {
|
---|
1175 | * ...
|
---|
1176 | * } finally {
|
---|
1177 | * ds.endUpdate();
|
---|
1178 | * }
|
---|
1179 | * </pre>
|
---|
1180 | */
|
---|
1181 | public void beginUpdate() {
|
---|
1182 | lock.writeLock().lock();
|
---|
1183 | updateCount++;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | /**
|
---|
1187 | * @see DataSet#beginUpdate()
|
---|
1188 | */
|
---|
1189 | public void endUpdate() {
|
---|
1190 | if (updateCount > 0) {
|
---|
1191 | updateCount--;
|
---|
1192 | List<AbstractDatasetChangedEvent> eventsToFire = Collections.emptyList();
|
---|
1193 | if (updateCount == 0) {
|
---|
1194 | eventsToFire = new ArrayList<>(cachedEvents);
|
---|
1195 | cachedEvents.clear();
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | if (!eventsToFire.isEmpty()) {
|
---|
1199 | lock.readLock().lock();
|
---|
1200 | lock.writeLock().unlock();
|
---|
1201 | try {
|
---|
1202 | if (eventsToFire.size() < MAX_SINGLE_EVENTS) {
|
---|
1203 | for (AbstractDatasetChangedEvent event: eventsToFire) {
|
---|
1204 | fireEventToListeners(event);
|
---|
1205 | }
|
---|
1206 | } else if (eventsToFire.size() == MAX_EVENTS) {
|
---|
1207 | fireEventToListeners(new DataChangedEvent(this));
|
---|
1208 | } else {
|
---|
1209 | fireEventToListeners(new DataChangedEvent(this, eventsToFire));
|
---|
1210 | }
|
---|
1211 | } finally {
|
---|
1212 | lock.readLock().unlock();
|
---|
1213 | }
|
---|
1214 | } else {
|
---|
1215 | lock.writeLock().unlock();
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | } else
|
---|
1219 | throw new AssertionError("endUpdate called without beginUpdate");
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | private void fireEventToListeners(AbstractDatasetChangedEvent event) {
|
---|
1223 | for (DataSetListener listener: listeners) {
|
---|
1224 | event.fire(listener);
|
---|
1225 | }
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | private void fireEvent(AbstractDatasetChangedEvent event) {
|
---|
1229 | if (updateCount == 0)
|
---|
1230 | throw new AssertionError("dataset events can be fired only when dataset is locked");
|
---|
1231 | if (cachedEvents.size() < MAX_EVENTS) {
|
---|
1232 | cachedEvents.add(event);
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | void firePrimitivesAdded(Collection<? extends OsmPrimitive> added, boolean wasIncomplete) {
|
---|
1237 | fireEvent(new PrimitivesAddedEvent(this, added, wasIncomplete));
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | void firePrimitivesRemoved(Collection<? extends OsmPrimitive> removed, boolean wasComplete) {
|
---|
1241 | fireEvent(new PrimitivesRemovedEvent(this, removed, wasComplete));
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | void fireTagsChanged(OsmPrimitive prim, Map<String, String> originalKeys) {
|
---|
1245 | fireEvent(new TagsChangedEvent(this, prim, originalKeys));
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | void fireRelationMembersChanged(Relation r) {
|
---|
1249 | reindexRelation(r);
|
---|
1250 | fireEvent(new RelationMembersChangedEvent(this, r));
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | void fireNodeMoved(Node node, LatLon newCoor, EastNorth eastNorth) {
|
---|
1254 | reindexNode(node, newCoor, eastNorth);
|
---|
1255 | fireEvent(new NodeMovedEvent(this, node));
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | void fireWayNodesChanged(Way way) {
|
---|
1259 | reindexWay(way);
|
---|
1260 | fireEvent(new WayNodesChangedEvent(this, way));
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | void fireChangesetIdChanged(OsmPrimitive primitive, int oldChangesetId, int newChangesetId) {
|
---|
1264 | fireEvent(new ChangesetIdChangedEvent(this, Collections.singletonList(primitive), oldChangesetId, newChangesetId));
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | void firePrimitiveFlagsChanged(OsmPrimitive primitive) {
|
---|
1268 | fireEvent(new PrimitiveFlagsChangedEvent(this, primitive));
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | void fireHighlightingChanged() {
|
---|
1272 | highlightUpdateCount++;
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | /**
|
---|
1276 | * Invalidates the internal cache of projected east/north coordinates.
|
---|
1277 | *
|
---|
1278 | * This method can be invoked after the globally configured projection method
|
---|
1279 | * changed.
|
---|
1280 | */
|
---|
1281 | public void invalidateEastNorthCache() {
|
---|
1282 | if (Main.getProjection() == null) return; // sanity check
|
---|
1283 | try {
|
---|
1284 | beginUpdate();
|
---|
1285 | for (Node n: Utils.filteredCollection(allPrimitives, Node.class)) {
|
---|
1286 | n.invalidateEastNorthCache();
|
---|
1287 | }
|
---|
1288 | } finally {
|
---|
1289 | endUpdate();
|
---|
1290 | }
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /**
|
---|
1294 | * Cleanups all deleted primitives (really delete them from the dataset).
|
---|
1295 | */
|
---|
1296 | public void cleanupDeletedPrimitives() {
|
---|
1297 | beginUpdate();
|
---|
1298 | try {
|
---|
1299 | boolean changed = cleanupDeleted(nodes.iterator());
|
---|
1300 | if (cleanupDeleted(ways.iterator())) {
|
---|
1301 | changed = true;
|
---|
1302 | }
|
---|
1303 | if (cleanupDeleted(relations.iterator())) {
|
---|
1304 | changed = true;
|
---|
1305 | }
|
---|
1306 | if (changed) {
|
---|
1307 | fireSelectionChanged();
|
---|
1308 | }
|
---|
1309 | } finally {
|
---|
1310 | endUpdate();
|
---|
1311 | }
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | private boolean cleanupDeleted(Iterator<? extends OsmPrimitive> it) {
|
---|
1315 | boolean changed = false;
|
---|
1316 | synchronized (selectionLock) {
|
---|
1317 | while (it.hasNext()) {
|
---|
1318 | OsmPrimitive primitive = it.next();
|
---|
1319 | if (primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew())) {
|
---|
1320 | selectedPrimitives.remove(primitive);
|
---|
1321 | selectionSnapshot = null;
|
---|
1322 | allPrimitives.remove(primitive);
|
---|
1323 | primitive.setDataset(null);
|
---|
1324 | changed = true;
|
---|
1325 | it.remove();
|
---|
1326 | }
|
---|
1327 | }
|
---|
1328 | if (changed) {
|
---|
1329 | selectionSnapshot = null;
|
---|
1330 | }
|
---|
1331 | }
|
---|
1332 | return changed;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * Removes all primitives from the dataset and resets the currently selected primitives
|
---|
1337 | * to the empty collection. Also notifies selection change listeners if necessary.
|
---|
1338 | *
|
---|
1339 | */
|
---|
1340 | public void clear() {
|
---|
1341 | beginUpdate();
|
---|
1342 | try {
|
---|
1343 | clearSelection();
|
---|
1344 | for (OsmPrimitive primitive:allPrimitives) {
|
---|
1345 | primitive.setDataset(null);
|
---|
1346 | }
|
---|
1347 | nodes.clear();
|
---|
1348 | ways.clear();
|
---|
1349 | relations.clear();
|
---|
1350 | allPrimitives.clear();
|
---|
1351 | } finally {
|
---|
1352 | endUpdate();
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | /**
|
---|
1357 | * Marks all "invisible" objects as deleted. These objects should be always marked as
|
---|
1358 | * deleted when downloaded from the server. They can be undeleted later if necessary.
|
---|
1359 | *
|
---|
1360 | */
|
---|
1361 | public void deleteInvisible() {
|
---|
1362 | for (OsmPrimitive primitive:allPrimitives) {
|
---|
1363 | if (!primitive.isVisible()) {
|
---|
1364 | primitive.setDeleted(true);
|
---|
1365 | }
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | /**
|
---|
1370 | * Moves all primitives and datasources from DataSet "from" to this DataSet.
|
---|
1371 | * @param from The source DataSet
|
---|
1372 | */
|
---|
1373 | public void mergeFrom(DataSet from) {
|
---|
1374 | mergeFrom(from, null);
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | /**
|
---|
1378 | * Moves all primitives and datasources from DataSet "from" to this DataSet.
|
---|
1379 | * @param from The source DataSet
|
---|
1380 | * @param progressMonitor The progress monitor
|
---|
1381 | */
|
---|
1382 | public synchronized void mergeFrom(DataSet from, ProgressMonitor progressMonitor) {
|
---|
1383 | if (from != null) {
|
---|
1384 | new DataSetMerger(this, from).merge(progressMonitor);
|
---|
1385 | synchronized (from) {
|
---|
1386 | if (!from.dataSources.isEmpty()) {
|
---|
1387 | if (dataSources.addAll(from.dataSources)) {
|
---|
1388 | cachedDataSourceArea = null;
|
---|
1389 | cachedDataSourceBounds = null;
|
---|
1390 | }
|
---|
1391 | from.dataSources.clear();
|
---|
1392 | from.cachedDataSourceArea = null;
|
---|
1393 | from.cachedDataSourceBounds = null;
|
---|
1394 | }
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | /* --------------------------------------------------------------------------------- */
|
---|
1400 | /* interface ProjectionChangeListner */
|
---|
1401 | /* --------------------------------------------------------------------------------- */
|
---|
1402 | @Override
|
---|
1403 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
1404 | invalidateEastNorthCache();
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Returns the data sources bounding box.
|
---|
1409 | * @return the data sources bounding box
|
---|
1410 | */
|
---|
1411 | public synchronized ProjectionBounds getDataSourceBoundingBox() {
|
---|
1412 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
1413 | for (DataSource source : dataSources) {
|
---|
1414 | bbox.visit(source.bounds);
|
---|
1415 | }
|
---|
1416 | if (bbox.hasExtend()) {
|
---|
1417 | return bbox.getBounds();
|
---|
1418 | }
|
---|
1419 | return null;
|
---|
1420 | }
|
---|
1421 | }
|
---|