1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.gpx;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.text.MessageFormat;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Date;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.Iterator;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.LongSummaryStatistics;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.NoSuchElementException;
|
---|
18 | import java.util.Objects;
|
---|
19 | import java.util.Optional;
|
---|
20 | import java.util.Set;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 | import java.util.stream.Stream;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.data.Bounds;
|
---|
25 | import org.openstreetmap.josm.data.Data;
|
---|
26 | import org.openstreetmap.josm.data.DataSource;
|
---|
27 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
28 | import org.openstreetmap.josm.data.gpx.IGpxTrack.GpxTrackChangeListener;
|
---|
29 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
30 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
31 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
32 | import org.openstreetmap.josm.tools.ListenerList;
|
---|
33 | import org.openstreetmap.josm.tools.ListeningCollection;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Objects of this class represent a gpx file with tracks, waypoints and routes.
|
---|
37 | * It uses GPX v1.1, see <a href="http://www.topografix.com/GPX/1/1/">the spec</a>
|
---|
38 | * for details.
|
---|
39 | *
|
---|
40 | * @author Raphael Mack <ramack@raphael-mack.de>
|
---|
41 | */
|
---|
42 | public class GpxData extends WithAttributes implements Data {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructs a new GpxData.
|
---|
46 | */
|
---|
47 | public GpxData() {}
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Constructs a new GpxData that is currently being initialized, so no listeners will be fired until {@link #endUpdate()} is called.
|
---|
51 | * @param initializing true
|
---|
52 | * @since 15496
|
---|
53 | */
|
---|
54 | public GpxData(boolean initializing) {
|
---|
55 | this.initializing = initializing;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * The disk file this layer is stored in, if it is a local layer. May be <code>null</code>.
|
---|
60 | */
|
---|
61 | public File storageFile;
|
---|
62 | /**
|
---|
63 | * A boolean flag indicating if the data was read from the OSM server.
|
---|
64 | */
|
---|
65 | public boolean fromServer;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Creator metadata for this file (usually software)
|
---|
69 | */
|
---|
70 | public String creator;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * A list of tracks this file consists of
|
---|
74 | */
|
---|
75 | private final ArrayList<IGpxTrack> privateTracks = new ArrayList<>();
|
---|
76 | /**
|
---|
77 | * GPX routes in this file
|
---|
78 | */
|
---|
79 | private final ArrayList<GpxRoute> privateRoutes = new ArrayList<>();
|
---|
80 | /**
|
---|
81 | * Additional waypoints for this file.
|
---|
82 | */
|
---|
83 | private final ArrayList<WayPoint> privateWaypoints = new ArrayList<>();
|
---|
84 | /**
|
---|
85 | * All namespaces read from the original file
|
---|
86 | */
|
---|
87 | private final List<XMLNamespace> namespaces = new ArrayList<>();
|
---|
88 | /**
|
---|
89 | * The layer specific prefs formerly saved in the preferences, e.g. drawing options.
|
---|
90 | * NOT the track specific settings (e.g. color, width)
|
---|
91 | */
|
---|
92 | private final Map<String, String> layerPrefs = new HashMap<>();
|
---|
93 |
|
---|
94 | private final GpxTrackChangeListener proxy = e -> invalidate();
|
---|
95 | private boolean modified, updating, initializing;
|
---|
96 | private boolean suppressedInvalidate;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Tracks. Access is discouraged, use {@link #getTracks()} to read.
|
---|
100 | * @see #getTracks()
|
---|
101 | */
|
---|
102 | public final Collection<IGpxTrack> tracks = new ListeningCollection<IGpxTrack>(privateTracks, this::invalidate) {
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | protected void removed(IGpxTrack cursor) {
|
---|
106 | cursor.removeListener(proxy);
|
---|
107 | super.removed(cursor);
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | protected void added(IGpxTrack cursor) {
|
---|
112 | super.added(cursor);
|
---|
113 | cursor.addListener(proxy);
|
---|
114 | }
|
---|
115 | };
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Routes. Access is discouraged, use {@link #getTracks()} to read.
|
---|
119 | * @see #getRoutes()
|
---|
120 | */
|
---|
121 | public final Collection<GpxRoute> routes = new ListeningCollection<>(privateRoutes, this::invalidate);
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Waypoints. Access is discouraged, use {@link #getTracks()} to read.
|
---|
125 | * @see #getWaypoints()
|
---|
126 | */
|
---|
127 | public final Collection<WayPoint> waypoints = new ListeningCollection<>(privateWaypoints, this::invalidate);
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * All data sources (bounds of downloaded bounds) of this GpxData.<br>
|
---|
131 | * Not part of GPX standard but rather a JOSM extension, needed by the fact that
|
---|
132 | * OSM API does not provide {@code <bounds>} element in its GPX reply.
|
---|
133 | * @since 7575
|
---|
134 | */
|
---|
135 | public final Set<DataSource> dataSources = new HashSet<>();
|
---|
136 |
|
---|
137 | private final ListenerList<GpxDataChangeListener> listeners = ListenerList.create();
|
---|
138 |
|
---|
139 | private List<GpxTrackSegmentSpan> segSpans;
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Merges data from another object.
|
---|
143 | * @param other existing GPX data
|
---|
144 | */
|
---|
145 | public synchronized void mergeFrom(GpxData other) {
|
---|
146 | mergeFrom(other, false, false);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Merges data from another object.
|
---|
151 | * @param other existing GPX data
|
---|
152 | * @param cutOverlapping whether overlapping parts of the given track should be removed
|
---|
153 | * @param connect whether the tracks should be connected on cuts
|
---|
154 | * @since 14338
|
---|
155 | */
|
---|
156 | public synchronized void mergeFrom(GpxData other, boolean cutOverlapping, boolean connect) {
|
---|
157 | if (storageFile == null && other.storageFile != null) {
|
---|
158 | storageFile = other.storageFile;
|
---|
159 | }
|
---|
160 | fromServer = fromServer && other.fromServer;
|
---|
161 |
|
---|
162 | for (Map.Entry<String, Object> ent : other.attr.entrySet()) {
|
---|
163 | // TODO: Detect conflicts.
|
---|
164 | String k = ent.getKey();
|
---|
165 | if (META_LINKS.equals(k) && attr.containsKey(META_LINKS)) {
|
---|
166 | Collection<GpxLink> my = super.<GpxLink>getCollection(META_LINKS);
|
---|
167 | @SuppressWarnings("unchecked")
|
---|
168 | Collection<GpxLink> their = (Collection<GpxLink>) ent.getValue();
|
---|
169 | my.addAll(their);
|
---|
170 | } else {
|
---|
171 | put(k, ent.getValue());
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (cutOverlapping) {
|
---|
176 | for (IGpxTrack trk : other.privateTracks) {
|
---|
177 | cutOverlapping(trk, connect);
|
---|
178 | }
|
---|
179 | } else {
|
---|
180 | other.privateTracks.forEach(this::addTrack);
|
---|
181 | }
|
---|
182 | other.privateRoutes.forEach(this::addRoute);
|
---|
183 | other.privateWaypoints.forEach(this::addWaypoint);
|
---|
184 | dataSources.addAll(other.dataSources);
|
---|
185 | invalidate();
|
---|
186 | }
|
---|
187 |
|
---|
188 | private void cutOverlapping(IGpxTrack trk, boolean connect) {
|
---|
189 | List<IGpxTrackSegment> segsOld = new ArrayList<>(trk.getSegments());
|
---|
190 | List<IGpxTrackSegment> segsNew = new ArrayList<>();
|
---|
191 | for (IGpxTrackSegment seg : segsOld) {
|
---|
192 | GpxTrackSegmentSpan s = GpxTrackSegmentSpan.tryGetFromSegment(seg);
|
---|
193 | if (s != null && anySegmentOverlapsWith(s)) {
|
---|
194 | List<WayPoint> wpsNew = new ArrayList<>();
|
---|
195 | List<WayPoint> wpsOld = new ArrayList<>(seg.getWayPoints());
|
---|
196 | if (s.isInverted()) {
|
---|
197 | Collections.reverse(wpsOld);
|
---|
198 | }
|
---|
199 | boolean split = false;
|
---|
200 | WayPoint prevLastOwnWp = null;
|
---|
201 | Date prevWpTime = null;
|
---|
202 | for (WayPoint wp : wpsOld) {
|
---|
203 | Date wpTime = wp.getDate();
|
---|
204 | boolean overlap = false;
|
---|
205 | if (wpTime != null) {
|
---|
206 | for (GpxTrackSegmentSpan ownspan : getSegmentSpans()) {
|
---|
207 | if (wpTime.after(ownspan.firstTime) && wpTime.before(ownspan.lastTime)) {
|
---|
208 | overlap = true;
|
---|
209 | if (connect) {
|
---|
210 | if (!split) {
|
---|
211 | wpsNew.add(ownspan.getFirstWp());
|
---|
212 | } else {
|
---|
213 | connectTracks(prevLastOwnWp, ownspan, trk.getAttributes());
|
---|
214 | }
|
---|
215 | prevLastOwnWp = ownspan.getLastWp();
|
---|
216 | }
|
---|
217 | split = true;
|
---|
218 | break;
|
---|
219 | } else if (connect && prevWpTime != null
|
---|
220 | && prevWpTime.before(ownspan.firstTime)
|
---|
221 | && wpTime.after(ownspan.lastTime)) {
|
---|
222 | // the overlapping high priority track is shorter than the distance
|
---|
223 | // between two waypoints of the low priority track
|
---|
224 | if (split) {
|
---|
225 | connectTracks(prevLastOwnWp, ownspan, trk.getAttributes());
|
---|
226 | prevLastOwnWp = ownspan.getLastWp();
|
---|
227 | } else {
|
---|
228 | wpsNew.add(ownspan.getFirstWp());
|
---|
229 | // splitting needs to be handled here,
|
---|
230 | // because other high priority tracks between the same waypoints could follow
|
---|
231 | if (!wpsNew.isEmpty()) {
|
---|
232 | segsNew.add(new GpxTrackSegment(wpsNew));
|
---|
233 | }
|
---|
234 | if (!segsNew.isEmpty()) {
|
---|
235 | privateTracks.add(new GpxTrack(segsNew, trk.getAttributes()));
|
---|
236 | }
|
---|
237 | segsNew = new ArrayList<>();
|
---|
238 | wpsNew = new ArrayList<>();
|
---|
239 | wpsNew.add(ownspan.getLastWp());
|
---|
240 | // therefore no break, because another segment could overlap, see above
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | prevWpTime = wpTime;
|
---|
245 | }
|
---|
246 | if (!overlap) {
|
---|
247 | if (split) {
|
---|
248 | //track has to be split, because we have an overlapping short track in the middle
|
---|
249 | if (!wpsNew.isEmpty()) {
|
---|
250 | segsNew.add(new GpxTrackSegment(wpsNew));
|
---|
251 | }
|
---|
252 | if (!segsNew.isEmpty()) {
|
---|
253 | privateTracks.add(new GpxTrack(segsNew, trk.getAttributes()));
|
---|
254 | }
|
---|
255 | segsNew = new ArrayList<>();
|
---|
256 | wpsNew = new ArrayList<>();
|
---|
257 | if (connect && prevLastOwnWp != null) {
|
---|
258 | wpsNew.add(new WayPoint(prevLastOwnWp));
|
---|
259 | }
|
---|
260 | prevLastOwnWp = null;
|
---|
261 | split = false;
|
---|
262 | }
|
---|
263 | wpsNew.add(new WayPoint(wp));
|
---|
264 | }
|
---|
265 | }
|
---|
266 | if (!wpsNew.isEmpty()) {
|
---|
267 | segsNew.add(new GpxTrackSegment(wpsNew));
|
---|
268 | }
|
---|
269 | } else {
|
---|
270 | segsNew.add(seg);
|
---|
271 | }
|
---|
272 | }
|
---|
273 | if (segsNew.equals(segsOld)) {
|
---|
274 | privateTracks.add(trk);
|
---|
275 | } else if (!segsNew.isEmpty()) {
|
---|
276 | privateTracks.add(new GpxTrack(segsNew, trk.getAttributes()));
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | private void connectTracks(WayPoint prevWp, GpxTrackSegmentSpan span, Map<String, Object> attr) {
|
---|
281 | if (prevWp != null && !span.lastEquals(prevWp)) {
|
---|
282 | privateTracks.add(new GpxTrack(Arrays.asList(Arrays.asList(new WayPoint(prevWp), span.getFirstWp())), attr));
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | static class GpxTrackSegmentSpan {
|
---|
287 |
|
---|
288 | final Date firstTime;
|
---|
289 | final Date lastTime;
|
---|
290 | private final boolean inv;
|
---|
291 | private final WayPoint firstWp;
|
---|
292 | private final WayPoint lastWp;
|
---|
293 |
|
---|
294 | GpxTrackSegmentSpan(WayPoint a, WayPoint b) {
|
---|
295 | Date at = a.getDate();
|
---|
296 | Date bt = b.getDate();
|
---|
297 | inv = bt.before(at);
|
---|
298 | if (inv) {
|
---|
299 | firstWp = b;
|
---|
300 | firstTime = bt;
|
---|
301 | lastWp = a;
|
---|
302 | lastTime = at;
|
---|
303 | } else {
|
---|
304 | firstWp = a;
|
---|
305 | firstTime = at;
|
---|
306 | lastWp = b;
|
---|
307 | lastTime = bt;
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | WayPoint getFirstWp() {
|
---|
312 | return new WayPoint(firstWp);
|
---|
313 | }
|
---|
314 |
|
---|
315 | WayPoint getLastWp() {
|
---|
316 | return new WayPoint(lastWp);
|
---|
317 | }
|
---|
318 |
|
---|
319 | // no new instances needed, therefore own methods for that
|
---|
320 |
|
---|
321 | boolean firstEquals(Object other) {
|
---|
322 | return firstWp.equals(other);
|
---|
323 | }
|
---|
324 |
|
---|
325 | boolean lastEquals(Object other) {
|
---|
326 | return lastWp.equals(other);
|
---|
327 | }
|
---|
328 |
|
---|
329 | public boolean isInverted() {
|
---|
330 | return inv;
|
---|
331 | }
|
---|
332 |
|
---|
333 | boolean overlapsWith(GpxTrackSegmentSpan other) {
|
---|
334 | return (firstTime.before(other.lastTime) && other.firstTime.before(lastTime))
|
---|
335 | || (other.firstTime.before(lastTime) && firstTime.before(other.lastTime));
|
---|
336 | }
|
---|
337 |
|
---|
338 | static GpxTrackSegmentSpan tryGetFromSegment(IGpxTrackSegment seg) {
|
---|
339 | WayPoint b = getNextWpWithTime(seg, true);
|
---|
340 | if (b != null) {
|
---|
341 | WayPoint e = getNextWpWithTime(seg, false);
|
---|
342 | if (e != null) {
|
---|
343 | return new GpxTrackSegmentSpan(b, e);
|
---|
344 | }
|
---|
345 | }
|
---|
346 | return null;
|
---|
347 | }
|
---|
348 |
|
---|
349 | private static WayPoint getNextWpWithTime(IGpxTrackSegment seg, boolean forward) {
|
---|
350 | List<WayPoint> wps = new ArrayList<>(seg.getWayPoints());
|
---|
351 | for (int i = forward ? 0 : wps.size() - 1; i >= 0 && i < wps.size(); i += forward ? 1 : -1) {
|
---|
352 | if (wps.get(i).hasDate()) {
|
---|
353 | return wps.get(i);
|
---|
354 | }
|
---|
355 | }
|
---|
356 | return null;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Get a list of SegmentSpans containing the beginning and end of each segment
|
---|
362 | * @return the list of SegmentSpans
|
---|
363 | * @since 14338
|
---|
364 | */
|
---|
365 | public synchronized List<GpxTrackSegmentSpan> getSegmentSpans() {
|
---|
366 | if (segSpans == null) {
|
---|
367 | segSpans = new ArrayList<>();
|
---|
368 | for (IGpxTrack trk : privateTracks) {
|
---|
369 | for (IGpxTrackSegment seg : trk.getSegments()) {
|
---|
370 | GpxTrackSegmentSpan s = GpxTrackSegmentSpan.tryGetFromSegment(seg);
|
---|
371 | if (s != null) {
|
---|
372 | segSpans.add(s);
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 | segSpans.sort((o1, o2) -> o1.firstTime.compareTo(o2.firstTime));
|
---|
377 | }
|
---|
378 | return segSpans;
|
---|
379 | }
|
---|
380 |
|
---|
381 | private boolean anySegmentOverlapsWith(GpxTrackSegmentSpan other) {
|
---|
382 | return getSegmentSpans().stream().anyMatch(s -> s.overlapsWith(other));
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Get all tracks contained in this data set.
|
---|
387 | * @return The tracks.
|
---|
388 | */
|
---|
389 | public synchronized Collection<IGpxTrack> getTracks() {
|
---|
390 | return Collections.unmodifiableCollection(privateTracks);
|
---|
391 | }
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Get stream of track segments.
|
---|
395 | * @return {@code Stream<GPXTrack>}
|
---|
396 | */
|
---|
397 | public synchronized Stream<IGpxTrackSegment> getTrackSegmentsStream() {
|
---|
398 | return getTracks().stream().flatMap(trk -> trk.getSegments().stream());
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Clear all tracks, empties the current privateTracks container,
|
---|
403 | * helper method for some gpx manipulations.
|
---|
404 | */
|
---|
405 | private synchronized void clearTracks() {
|
---|
406 | privateTracks.forEach(t -> t.removeListener(proxy));
|
---|
407 | privateTracks.clear();
|
---|
408 | }
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Add a new track
|
---|
412 | * @param track The new track
|
---|
413 | * @since 12156
|
---|
414 | */
|
---|
415 | public synchronized void addTrack(IGpxTrack track) {
|
---|
416 | if (privateTracks.stream().anyMatch(t -> t == track)) {
|
---|
417 | throw new IllegalArgumentException(MessageFormat.format("The track was already added to this data: {0}", track));
|
---|
418 | }
|
---|
419 | privateTracks.add(track);
|
---|
420 | track.addListener(proxy);
|
---|
421 | invalidate();
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Remove a track
|
---|
426 | * @param track The old track
|
---|
427 | * @since 12156
|
---|
428 | */
|
---|
429 | public synchronized void removeTrack(IGpxTrack track) {
|
---|
430 | if (!privateTracks.removeIf(t -> t == track)) {
|
---|
431 | throw new IllegalArgumentException(MessageFormat.format("The track was not in this data: {0}", track));
|
---|
432 | }
|
---|
433 | track.removeListener(proxy);
|
---|
434 | invalidate();
|
---|
435 | }
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Combine tracks into a single, segmented track.
|
---|
439 | * The attributes of the first track are used, the rest discarded.
|
---|
440 | *
|
---|
441 | * @since 13210
|
---|
442 | */
|
---|
443 | public synchronized void combineTracksToSegmentedTrack() {
|
---|
444 | List<IGpxTrackSegment> segs = getTrackSegmentsStream()
|
---|
445 | .collect(Collectors.toCollection(ArrayList<IGpxTrackSegment>::new));
|
---|
446 | Map<String, Object> attrs = new HashMap<>(privateTracks.get(0).getAttributes());
|
---|
447 |
|
---|
448 | // do not let the name grow if split / combine operations are called iteratively
|
---|
449 | Object name = attrs.get("name");
|
---|
450 | if (name != null) {
|
---|
451 | attrs.put("name", name.toString().replaceFirst(" #\\d+$", ""));
|
---|
452 | }
|
---|
453 |
|
---|
454 | clearTracks();
|
---|
455 | addTrack(new GpxTrack(segs, attrs));
|
---|
456 | }
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * Ensures a unique name among gpx layers
|
---|
460 | * @param attrs attributes of/for an gpx track, written to if the name appeared previously in {@code counts}.
|
---|
461 | * @param counts a {@code HashMap} of previously seen names, associated with their count.
|
---|
462 | * @param srcLayerName Source layer name
|
---|
463 | * @return the unique name for the gpx track.
|
---|
464 | *
|
---|
465 | * @since 15397
|
---|
466 | */
|
---|
467 | public static String ensureUniqueName(Map<String, Object> attrs, Map<String, Integer> counts, String srcLayerName) {
|
---|
468 | String name = attrs.getOrDefault("name", srcLayerName).toString().replaceFirst(" #\\d+$", "");
|
---|
469 | Integer count = counts.getOrDefault(name, 0) + 1;
|
---|
470 | counts.put(name, count);
|
---|
471 |
|
---|
472 | attrs.put("name", MessageFormat.format("{0}{1}", name, " #" + count));
|
---|
473 | return attrs.get("name").toString();
|
---|
474 | }
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * Split tracks so that only single-segment tracks remain.
|
---|
478 | * Each segment will make up one individual track after this operation.
|
---|
479 | *
|
---|
480 | * @param srcLayerName Source layer name
|
---|
481 | *
|
---|
482 | * @since 15397
|
---|
483 | */
|
---|
484 | public synchronized void splitTrackSegmentsToTracks(String srcLayerName) {
|
---|
485 | final HashMap<String, Integer> counts = new HashMap<>();
|
---|
486 |
|
---|
487 | List<GpxTrack> trks = getTracks().stream()
|
---|
488 | .flatMap(trk -> trk.getSegments().stream().map(seg -> {
|
---|
489 | HashMap<String, Object> attrs = new HashMap<>(trk.getAttributes());
|
---|
490 | ensureUniqueName(attrs, counts, srcLayerName);
|
---|
491 | return new GpxTrack(Arrays.asList(seg), attrs);
|
---|
492 | }))
|
---|
493 | .collect(Collectors.toCollection(ArrayList<GpxTrack>::new));
|
---|
494 |
|
---|
495 | clearTracks();
|
---|
496 | trks.stream().forEachOrdered(this::addTrack);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | * Split tracks into layers, the result is one layer for each track.
|
---|
501 | * If this layer currently has only one GpxTrack this is a no-operation.
|
---|
502 | *
|
---|
503 | * The new GpxLayers are added to the LayerManager, the original GpxLayer
|
---|
504 | * is untouched as to preserve potential route or wpt parts.
|
---|
505 | *
|
---|
506 | * @param srcLayerName Source layer name
|
---|
507 | *
|
---|
508 | * @since 15397
|
---|
509 | */
|
---|
510 | public synchronized void splitTracksToLayers(String srcLayerName) {
|
---|
511 | final HashMap<String, Integer> counts = new HashMap<>();
|
---|
512 |
|
---|
513 | getTracks().stream()
|
---|
514 | .filter(trk -> privateTracks.size() > 1)
|
---|
515 | .map(trk -> {
|
---|
516 | HashMap<String, Object> attrs = new HashMap<>(trk.getAttributes());
|
---|
517 | GpxData d = new GpxData();
|
---|
518 | d.addTrack(trk);
|
---|
519 | return new GpxLayer(d, ensureUniqueName(attrs, counts, srcLayerName));
|
---|
520 | })
|
---|
521 | .forEachOrdered(layer -> MainApplication.getLayerManager().addLayer(layer));
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Replies the current number of tracks in this GpxData
|
---|
526 | * @return track count
|
---|
527 | * @since 13210
|
---|
528 | */
|
---|
529 | public synchronized int getTrackCount() {
|
---|
530 | return privateTracks.size();
|
---|
531 | }
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * Replies the accumulated total of all track segments,
|
---|
535 | * the sum of segment counts for each track present.
|
---|
536 | * @return track segments count
|
---|
537 | * @since 13210
|
---|
538 | */
|
---|
539 | public synchronized int getTrackSegsCount() {
|
---|
540 | return privateTracks.stream().mapToInt(t -> t.getSegments().size()).sum();
|
---|
541 | }
|
---|
542 |
|
---|
543 | /**
|
---|
544 | * Gets the list of all routes defined in this data set.
|
---|
545 | * @return The routes
|
---|
546 | * @since 12156
|
---|
547 | */
|
---|
548 | public synchronized Collection<GpxRoute> getRoutes() {
|
---|
549 | return Collections.unmodifiableCollection(privateRoutes);
|
---|
550 | }
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * Add a new route
|
---|
554 | * @param route The new route
|
---|
555 | * @since 12156
|
---|
556 | */
|
---|
557 | public synchronized void addRoute(GpxRoute route) {
|
---|
558 | if (privateRoutes.stream().anyMatch(r -> r == route)) {
|
---|
559 | throw new IllegalArgumentException(MessageFormat.format("The route was already added to this data: {0}", route));
|
---|
560 | }
|
---|
561 | privateRoutes.add(route);
|
---|
562 | invalidate();
|
---|
563 | }
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Remove a route
|
---|
567 | * @param route The old route
|
---|
568 | * @since 12156
|
---|
569 | */
|
---|
570 | public synchronized void removeRoute(GpxRoute route) {
|
---|
571 | if (!privateRoutes.removeIf(r -> r == route)) {
|
---|
572 | throw new IllegalArgumentException(MessageFormat.format("The route was not in this data: {0}", route));
|
---|
573 | }
|
---|
574 | invalidate();
|
---|
575 | }
|
---|
576 |
|
---|
577 | /**
|
---|
578 | * Gets a list of all way points in this data set.
|
---|
579 | * @return The way points.
|
---|
580 | * @since 12156
|
---|
581 | */
|
---|
582 | public synchronized Collection<WayPoint> getWaypoints() {
|
---|
583 | return Collections.unmodifiableCollection(privateWaypoints);
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Add a new waypoint
|
---|
588 | * @param waypoint The new waypoint
|
---|
589 | * @since 12156
|
---|
590 | */
|
---|
591 | public synchronized void addWaypoint(WayPoint waypoint) {
|
---|
592 | if (privateWaypoints.stream().anyMatch(w -> w == waypoint)) {
|
---|
593 | throw new IllegalArgumentException(MessageFormat.format("The waypoint was already added to this data: {0}", waypoint));
|
---|
594 | }
|
---|
595 | privateWaypoints.add(waypoint);
|
---|
596 | invalidate();
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Remove a waypoint
|
---|
601 | * @param waypoint The old waypoint
|
---|
602 | * @since 12156
|
---|
603 | */
|
---|
604 | public synchronized void removeWaypoint(WayPoint waypoint) {
|
---|
605 | if (!privateWaypoints.removeIf(w -> w == waypoint)) {
|
---|
606 | throw new IllegalArgumentException(MessageFormat.format("The waypoint was not in this data: {0}", waypoint));
|
---|
607 | }
|
---|
608 | invalidate();
|
---|
609 | }
|
---|
610 |
|
---|
611 | /**
|
---|
612 | * Determines if this GPX data has one or more track points
|
---|
613 | * @return {@code true} if this GPX data has track points, {@code false} otherwise
|
---|
614 | */
|
---|
615 | public synchronized boolean hasTrackPoints() {
|
---|
616 | return getTrackPoints().findAny().isPresent();
|
---|
617 | }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Gets a stream of all track points in the segments of the tracks of this data.
|
---|
621 | * @return The stream
|
---|
622 | * @see #getTracks()
|
---|
623 | * @see IGpxTrack#getSegments()
|
---|
624 | * @see IGpxTrackSegment#getWayPoints()
|
---|
625 | * @since 12156
|
---|
626 | */
|
---|
627 | public synchronized Stream<WayPoint> getTrackPoints() {
|
---|
628 | return getTracks().stream().flatMap(trk -> trk.getSegments().stream()).flatMap(trkseg -> trkseg.getWayPoints().stream());
|
---|
629 | }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Determines if this GPX data has one or more route points
|
---|
633 | * @return {@code true} if this GPX data has route points, {@code false} otherwise
|
---|
634 | */
|
---|
635 | public synchronized boolean hasRoutePoints() {
|
---|
636 | return privateRoutes.stream().anyMatch(rte -> !rte.routePoints.isEmpty());
|
---|
637 | }
|
---|
638 |
|
---|
639 | /**
|
---|
640 | * Determines if this GPX data is empty (i.e. does not contain any point)
|
---|
641 | * @return {@code true} if this GPX data is empty, {@code false} otherwise
|
---|
642 | */
|
---|
643 | public synchronized boolean isEmpty() {
|
---|
644 | return !hasRoutePoints() && !hasTrackPoints() && waypoints.isEmpty();
|
---|
645 | }
|
---|
646 |
|
---|
647 | /**
|
---|
648 | * Returns the bounds defining the extend of this data, as read in metadata, if any.
|
---|
649 | * If no bounds is defined in metadata, {@code null} is returned. There is no guarantee
|
---|
650 | * that data entirely fit in this bounds, as it is not recalculated. To get recalculated bounds,
|
---|
651 | * see {@link #recalculateBounds()}. To get downloaded areas, see {@link #dataSources}.
|
---|
652 | * @return the bounds defining the extend of this data, or {@code null}.
|
---|
653 | * @see #recalculateBounds()
|
---|
654 | * @see #dataSources
|
---|
655 | * @since 7575
|
---|
656 | */
|
---|
657 | public Bounds getMetaBounds() {
|
---|
658 | Object value = get(META_BOUNDS);
|
---|
659 | if (value instanceof Bounds) {
|
---|
660 | return (Bounds) value;
|
---|
661 | }
|
---|
662 | return null;
|
---|
663 | }
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * Calculates the bounding box of available data and returns it.
|
---|
667 | * The bounds are not stored internally, but recalculated every time
|
---|
668 | * this function is called.<br>
|
---|
669 | * To get bounds as read from metadata, see {@link #getMetaBounds()}.<br>
|
---|
670 | * To get downloaded areas, see {@link #dataSources}.<br>
|
---|
671 | *
|
---|
672 | * FIXME might perhaps use visitor pattern?
|
---|
673 | * @return the bounds
|
---|
674 | * @see #getMetaBounds()
|
---|
675 | * @see #dataSources
|
---|
676 | */
|
---|
677 | public synchronized Bounds recalculateBounds() {
|
---|
678 | Bounds bounds = null;
|
---|
679 | for (WayPoint wpt : privateWaypoints) {
|
---|
680 | if (bounds == null) {
|
---|
681 | bounds = new Bounds(wpt.getCoor());
|
---|
682 | } else {
|
---|
683 | bounds.extend(wpt.getCoor());
|
---|
684 | }
|
---|
685 | }
|
---|
686 | for (GpxRoute rte : privateRoutes) {
|
---|
687 | for (WayPoint wpt : rte.routePoints) {
|
---|
688 | if (bounds == null) {
|
---|
689 | bounds = new Bounds(wpt.getCoor());
|
---|
690 | } else {
|
---|
691 | bounds.extend(wpt.getCoor());
|
---|
692 | }
|
---|
693 | }
|
---|
694 | }
|
---|
695 | for (IGpxTrack trk : privateTracks) {
|
---|
696 | Bounds trkBounds = trk.getBounds();
|
---|
697 | if (trkBounds != null) {
|
---|
698 | if (bounds == null) {
|
---|
699 | bounds = new Bounds(trkBounds);
|
---|
700 | } else {
|
---|
701 | bounds.extend(trkBounds);
|
---|
702 | }
|
---|
703 | }
|
---|
704 | }
|
---|
705 | return bounds;
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * calculates the sum of the lengths of all track segments
|
---|
710 | * @return the length in meters
|
---|
711 | */
|
---|
712 | public synchronized double length() {
|
---|
713 | return privateTracks.stream().mapToDouble(IGpxTrack::length).sum();
|
---|
714 | }
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * returns minimum and maximum timestamps in the track
|
---|
718 | * @param trk track to analyze
|
---|
719 | * @return minimum and maximum dates in array of 2 elements
|
---|
720 | */
|
---|
721 | public static Date[] getMinMaxTimeForTrack(IGpxTrack trk) {
|
---|
722 | final LongSummaryStatistics statistics = trk.getSegments().stream()
|
---|
723 | .flatMap(seg -> seg.getWayPoints().stream())
|
---|
724 | .mapToLong(WayPoint::getTimeInMillis)
|
---|
725 | .summaryStatistics();
|
---|
726 | return statistics.getCount() == 0 || (statistics.getMin() == 0 && statistics.getMax() == 0)
|
---|
727 | ? null
|
---|
728 | : new Date[]{new Date(statistics.getMin()), new Date(statistics.getMax())};
|
---|
729 | }
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * Returns minimum and maximum timestamps for all tracks
|
---|
733 | * Warning: there are lot of track with broken timestamps,
|
---|
734 | * so we just ignore points from future and from year before 1970 in this method
|
---|
735 | * @return minimum and maximum dates in array of 2 elements
|
---|
736 | * @since 7319
|
---|
737 | */
|
---|
738 | public synchronized Date[] getMinMaxTimeForAllTracks() {
|
---|
739 | long now = System.currentTimeMillis();
|
---|
740 | final LongSummaryStatistics statistics = tracks.stream()
|
---|
741 | .flatMap(trk -> trk.getSegments().stream())
|
---|
742 | .flatMap(seg -> seg.getWayPoints().stream())
|
---|
743 | .mapToLong(WayPoint::getTimeInMillis)
|
---|
744 | .filter(t -> t > 0 && t <= now)
|
---|
745 | .summaryStatistics();
|
---|
746 | return statistics.getCount() == 0
|
---|
747 | ? new Date[0]
|
---|
748 | : new Date[]{new Date(statistics.getMin()), new Date(statistics.getMax())};
|
---|
749 | }
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * Makes a WayPoint at the projection of point p onto the track providing p is less than
|
---|
753 | * tolerance away from the track
|
---|
754 | *
|
---|
755 | * @param p : the point to determine the projection for
|
---|
756 | * @param tolerance : must be no further than this from the track
|
---|
757 | * @return the closest point on the track to p, which may be the first or last point if off the
|
---|
758 | * end of a segment, or may be null if nothing close enough
|
---|
759 | */
|
---|
760 | public synchronized WayPoint nearestPointOnTrack(EastNorth p, double tolerance) {
|
---|
761 | /*
|
---|
762 | * assume the coordinates of P are xp,yp, and those of a section of track between two
|
---|
763 | * trackpoints are R=xr,yr and S=xs,ys. Let N be the projected point.
|
---|
764 | *
|
---|
765 | * The equation of RS is Ax + By + C = 0 where A = ys - yr B = xr - xs C = - Axr - Byr
|
---|
766 | *
|
---|
767 | * Also, note that the distance RS^2 is A^2 + B^2
|
---|
768 | *
|
---|
769 | * If RS^2 == 0.0 ignore the degenerate section of track
|
---|
770 | *
|
---|
771 | * PN^2 = (Axp + Byp + C)^2 / RS^2 that is the distance from P to the line
|
---|
772 | *
|
---|
773 | * so if PN^2 is less than PNmin^2 (initialized to tolerance) we can reject the line
|
---|
774 | * otherwise... determine if the projected poijnt lies within the bounds of the line: PR^2 -
|
---|
775 | * PN^2 <= RS^2 and PS^2 - PN^2 <= RS^2
|
---|
776 | *
|
---|
777 | * where PR^2 = (xp - xr)^2 + (yp-yr)^2 and PS^2 = (xp - xs)^2 + (yp-ys)^2
|
---|
778 | *
|
---|
779 | * If so, calculate N as xn = xr + (RN/RS) B yn = y1 + (RN/RS) A
|
---|
780 | *
|
---|
781 | * where RN = sqrt(PR^2 - PN^2)
|
---|
782 | */
|
---|
783 |
|
---|
784 | double pnminsq = tolerance * tolerance;
|
---|
785 | EastNorth bestEN = null;
|
---|
786 | double bestTime = Double.NaN;
|
---|
787 | double px = p.east();
|
---|
788 | double py = p.north();
|
---|
789 | double rx = 0.0, ry = 0.0, sx, sy, x, y;
|
---|
790 | for (IGpxTrack track : privateTracks) {
|
---|
791 | for (IGpxTrackSegment seg : track.getSegments()) {
|
---|
792 | WayPoint r = null;
|
---|
793 | for (WayPoint wpSeg : seg.getWayPoints()) {
|
---|
794 | EastNorth en = wpSeg.getEastNorth(ProjectionRegistry.getProjection());
|
---|
795 | if (r == null) {
|
---|
796 | r = wpSeg;
|
---|
797 | rx = en.east();
|
---|
798 | ry = en.north();
|
---|
799 | x = px - rx;
|
---|
800 | y = py - ry;
|
---|
801 | double pRsq = x * x + y * y;
|
---|
802 | if (pRsq < pnminsq) {
|
---|
803 | pnminsq = pRsq;
|
---|
804 | bestEN = en;
|
---|
805 | if (r.hasDate()) {
|
---|
806 | bestTime = r.getTime();
|
---|
807 | }
|
---|
808 | }
|
---|
809 | } else {
|
---|
810 | sx = en.east();
|
---|
811 | sy = en.north();
|
---|
812 | double a = sy - ry;
|
---|
813 | double b = rx - sx;
|
---|
814 | double c = -a * rx - b * ry;
|
---|
815 | double rssq = a * a + b * b;
|
---|
816 | if (rssq == 0) {
|
---|
817 | continue;
|
---|
818 | }
|
---|
819 | double pnsq = a * px + b * py + c;
|
---|
820 | pnsq = pnsq * pnsq / rssq;
|
---|
821 | if (pnsq < pnminsq) {
|
---|
822 | x = px - rx;
|
---|
823 | y = py - ry;
|
---|
824 | double prsq = x * x + y * y;
|
---|
825 | x = px - sx;
|
---|
826 | y = py - sy;
|
---|
827 | double pssq = x * x + y * y;
|
---|
828 | if (prsq - pnsq <= rssq && pssq - pnsq <= rssq) {
|
---|
829 | double rnoverRS = Math.sqrt((prsq - pnsq) / rssq);
|
---|
830 | double nx = rx - rnoverRS * b;
|
---|
831 | double ny = ry + rnoverRS * a;
|
---|
832 | bestEN = new EastNorth(nx, ny);
|
---|
833 | if (r.hasDate() && wpSeg.hasDate()) {
|
---|
834 | bestTime = r.getTime() + rnoverRS * (wpSeg.getTime() - r.getTime());
|
---|
835 | }
|
---|
836 | pnminsq = pnsq;
|
---|
837 | }
|
---|
838 | }
|
---|
839 | r = wpSeg;
|
---|
840 | rx = sx;
|
---|
841 | ry = sy;
|
---|
842 | }
|
---|
843 | }
|
---|
844 | if (r != null) {
|
---|
845 | EastNorth c = r.getEastNorth(ProjectionRegistry.getProjection());
|
---|
846 | /* if there is only one point in the seg, it will do this twice, but no matter */
|
---|
847 | rx = c.east();
|
---|
848 | ry = c.north();
|
---|
849 | x = px - rx;
|
---|
850 | y = py - ry;
|
---|
851 | double prsq = x * x + y * y;
|
---|
852 | if (prsq < pnminsq) {
|
---|
853 | pnminsq = prsq;
|
---|
854 | bestEN = c;
|
---|
855 | if (r.hasDate()) {
|
---|
856 | bestTime = r.getTime();
|
---|
857 | }
|
---|
858 | }
|
---|
859 | }
|
---|
860 | }
|
---|
861 | }
|
---|
862 | if (bestEN == null)
|
---|
863 | return null;
|
---|
864 | WayPoint best = new WayPoint(ProjectionRegistry.getProjection().eastNorth2latlon(bestEN));
|
---|
865 | if (!Double.isNaN(bestTime)) {
|
---|
866 | best.setTimeInMillis((long) (bestTime * 1000));
|
---|
867 | }
|
---|
868 | return best;
|
---|
869 | }
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Iterate over all track segments and over all routes.
|
---|
873 | *
|
---|
874 | * @param trackVisibility An array indicating which tracks should be
|
---|
875 | * included in the iteration. Can be null, then all tracks are included.
|
---|
876 | * @return an Iterable object, which iterates over all track segments and
|
---|
877 | * over all routes
|
---|
878 | */
|
---|
879 | public Iterable<Line> getLinesIterable(final boolean... trackVisibility) {
|
---|
880 | return () -> new LinesIterator(this, trackVisibility);
|
---|
881 | }
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * Resets the internal caches of east/north coordinates.
|
---|
885 | */
|
---|
886 | public synchronized void resetEastNorthCache() {
|
---|
887 | privateWaypoints.forEach(WayPoint::invalidateEastNorthCache);
|
---|
888 | getTrackPoints().forEach(WayPoint::invalidateEastNorthCache);
|
---|
889 | for (GpxRoute route: getRoutes()) {
|
---|
890 | if (route.routePoints == null) {
|
---|
891 | continue;
|
---|
892 | }
|
---|
893 | for (WayPoint wp: route.routePoints) {
|
---|
894 | wp.invalidateEastNorthCache();
|
---|
895 | }
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 | /**
|
---|
900 | * Iterates over all track segments and then over all routes.
|
---|
901 | */
|
---|
902 | public static class LinesIterator implements Iterator<Line> {
|
---|
903 |
|
---|
904 | private Iterator<IGpxTrack> itTracks;
|
---|
905 | private int idxTracks;
|
---|
906 | private Iterator<IGpxTrackSegment> itTrackSegments;
|
---|
907 |
|
---|
908 | private Line next;
|
---|
909 | private final boolean[] trackVisibility;
|
---|
910 | private Map<String, Object> trackAttributes;
|
---|
911 | private IGpxTrack curTrack;
|
---|
912 |
|
---|
913 | /**
|
---|
914 | * Constructs a new {@code LinesIterator}.
|
---|
915 | * @param data GPX data
|
---|
916 | * @param trackVisibility An array indicating which tracks should be
|
---|
917 | * included in the iteration. Can be null, then all tracks are included.
|
---|
918 | */
|
---|
919 | public LinesIterator(GpxData data, boolean... trackVisibility) {
|
---|
920 | itTracks = data.tracks.iterator();
|
---|
921 | idxTracks = -1;
|
---|
922 | this.trackVisibility = trackVisibility;
|
---|
923 | next = getNext();
|
---|
924 | }
|
---|
925 |
|
---|
926 | @Override
|
---|
927 | public boolean hasNext() {
|
---|
928 | return next != null;
|
---|
929 | }
|
---|
930 |
|
---|
931 | @Override
|
---|
932 | public Line next() {
|
---|
933 | if (!hasNext()) {
|
---|
934 | throw new NoSuchElementException();
|
---|
935 | }
|
---|
936 | Line current = next;
|
---|
937 | next = getNext();
|
---|
938 | return current;
|
---|
939 | }
|
---|
940 |
|
---|
941 | private Line getNext() {
|
---|
942 | if (itTracks != null) {
|
---|
943 | if (itTrackSegments != null && itTrackSegments.hasNext()) {
|
---|
944 | return new Line(itTrackSegments.next(), trackAttributes, curTrack.getColor());
|
---|
945 | } else {
|
---|
946 | while (itTracks.hasNext()) {
|
---|
947 | curTrack = itTracks.next();
|
---|
948 | trackAttributes = curTrack.getAttributes();
|
---|
949 | idxTracks++;
|
---|
950 | if (trackVisibility != null && !trackVisibility[idxTracks])
|
---|
951 | continue;
|
---|
952 | itTrackSegments = curTrack.getSegments().iterator();
|
---|
953 | if (itTrackSegments.hasNext()) {
|
---|
954 | return new Line(itTrackSegments.next(), trackAttributes, curTrack.getColor());
|
---|
955 | }
|
---|
956 | }
|
---|
957 | // if we get here, all the Tracks are finished; Continue with Routes
|
---|
958 | trackAttributes = null;
|
---|
959 | itTracks = null;
|
---|
960 | }
|
---|
961 | }
|
---|
962 | return null;
|
---|
963 | }
|
---|
964 |
|
---|
965 | @Override
|
---|
966 | public void remove() {
|
---|
967 | throw new UnsupportedOperationException();
|
---|
968 | }
|
---|
969 | }
|
---|
970 |
|
---|
971 | @Override
|
---|
972 | public Collection<DataSource> getDataSources() {
|
---|
973 | return Collections.unmodifiableCollection(dataSources);
|
---|
974 | }
|
---|
975 |
|
---|
976 | /**
|
---|
977 | * The layer specific prefs formerly saved in the preferences, e.g. drawing options.
|
---|
978 | * NOT the track specific settings (e.g. color, width)
|
---|
979 | * @return Modifiable map
|
---|
980 | * @since 15496
|
---|
981 | */
|
---|
982 | public Map<String, String> getLayerPrefs() {
|
---|
983 | return layerPrefs;
|
---|
984 | }
|
---|
985 |
|
---|
986 | /**
|
---|
987 | * All XML namespaces read from the original file
|
---|
988 | * @return Modifiable list
|
---|
989 | * @since 15496
|
---|
990 | */
|
---|
991 | public List<XMLNamespace> getNamespaces() {
|
---|
992 | return namespaces;
|
---|
993 | }
|
---|
994 |
|
---|
995 | @Override
|
---|
996 | public synchronized int hashCode() {
|
---|
997 | return Objects.hash(
|
---|
998 | super.hashCode(),
|
---|
999 | namespaces,
|
---|
1000 | layerPrefs,
|
---|
1001 | dataSources,
|
---|
1002 | privateRoutes,
|
---|
1003 | privateTracks,
|
---|
1004 | privateWaypoints
|
---|
1005 | );
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | @Override
|
---|
1009 | public synchronized boolean equals(Object obj) {
|
---|
1010 | if (this == obj)
|
---|
1011 | return true;
|
---|
1012 | if (obj == null)
|
---|
1013 | return false;
|
---|
1014 | if (!super.equals(obj))
|
---|
1015 | return false;
|
---|
1016 | if (getClass() != obj.getClass())
|
---|
1017 | return false;
|
---|
1018 | GpxData other = (GpxData) obj;
|
---|
1019 | if (dataSources == null) {
|
---|
1020 | if (other.dataSources != null)
|
---|
1021 | return false;
|
---|
1022 | } else if (!dataSources.equals(other.dataSources))
|
---|
1023 | return false;
|
---|
1024 | if (layerPrefs == null) {
|
---|
1025 | if (other.layerPrefs != null)
|
---|
1026 | return false;
|
---|
1027 | } else if (!layerPrefs.equals(other.layerPrefs))
|
---|
1028 | return false;
|
---|
1029 | if (privateRoutes == null) {
|
---|
1030 | if (other.privateRoutes != null)
|
---|
1031 | return false;
|
---|
1032 | } else if (!privateRoutes.equals(other.privateRoutes))
|
---|
1033 | return false;
|
---|
1034 | if (privateTracks == null) {
|
---|
1035 | if (other.privateTracks != null)
|
---|
1036 | return false;
|
---|
1037 | } else if (!privateTracks.equals(other.privateTracks))
|
---|
1038 | return false;
|
---|
1039 | if (privateWaypoints == null) {
|
---|
1040 | if (other.privateWaypoints != null)
|
---|
1041 | return false;
|
---|
1042 | } else if (!privateWaypoints.equals(other.privateWaypoints))
|
---|
1043 | return false;
|
---|
1044 | if (namespaces == null) {
|
---|
1045 | if (other.namespaces != null)
|
---|
1046 | return false;
|
---|
1047 | } else if (!namespaces.equals(other.namespaces))
|
---|
1048 | return false;
|
---|
1049 | return true;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | @Override
|
---|
1053 | public void put(String key, Object value) {
|
---|
1054 | super.put(key, value);
|
---|
1055 | invalidate();
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | /**
|
---|
1059 | * Adds a listener that gets called whenever the data changed.
|
---|
1060 | * @param listener The listener
|
---|
1061 | * @since 12156
|
---|
1062 | */
|
---|
1063 | public void addChangeListener(GpxDataChangeListener listener) {
|
---|
1064 | listeners.addListener(listener);
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | /**
|
---|
1068 | * Adds a listener that gets called whenever the data changed. It is added with a weak link
|
---|
1069 | * @param listener The listener
|
---|
1070 | */
|
---|
1071 | public void addWeakChangeListener(GpxDataChangeListener listener) {
|
---|
1072 | listeners.addWeakListener(listener);
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /**
|
---|
1076 | * Removes a listener that gets called whenever the data changed.
|
---|
1077 | * @param listener The listener
|
---|
1078 | * @since 12156
|
---|
1079 | */
|
---|
1080 | public void removeChangeListener(GpxDataChangeListener listener) {
|
---|
1081 | listeners.removeListener(listener);
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | /**
|
---|
1085 | * Fires event listeners and sets the modified flag to true.
|
---|
1086 | */
|
---|
1087 | public void invalidate() {
|
---|
1088 | fireInvalidate(true);
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | private void fireInvalidate(boolean setModified) {
|
---|
1092 | if (updating || initializing) {
|
---|
1093 | suppressedInvalidate = true;
|
---|
1094 | } else {
|
---|
1095 | if (setModified) {
|
---|
1096 | modified = true;
|
---|
1097 | }
|
---|
1098 | if (listeners.hasListeners()) {
|
---|
1099 | GpxDataChangeEvent e = new GpxDataChangeEvent(this);
|
---|
1100 | listeners.fireEvent(l -> l.gpxDataChanged(e));
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | /**
|
---|
1106 | * Begins updating this GpxData and prevents listeners from being fired.
|
---|
1107 | * @since 15496
|
---|
1108 | */
|
---|
1109 | public void beginUpdate() {
|
---|
1110 | updating = true;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | /**
|
---|
1114 | * Finishes updating this GpxData and fires listeners if required.
|
---|
1115 | * @since 15496
|
---|
1116 | */
|
---|
1117 | public void endUpdate() {
|
---|
1118 | boolean setModified = updating;
|
---|
1119 | updating = initializing = false;
|
---|
1120 | if (suppressedInvalidate) {
|
---|
1121 | fireInvalidate(setModified);
|
---|
1122 | suppressedInvalidate = false;
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | /**
|
---|
1127 | * A listener that listens to GPX data changes.
|
---|
1128 | * @author Michael Zangl
|
---|
1129 | * @since 12156
|
---|
1130 | */
|
---|
1131 | @FunctionalInterface
|
---|
1132 | public interface GpxDataChangeListener {
|
---|
1133 | /**
|
---|
1134 | * Called when the gpx data changed.
|
---|
1135 | * @param e The event
|
---|
1136 | */
|
---|
1137 | void gpxDataChanged(GpxDataChangeEvent e);
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /**
|
---|
1141 | * A data change event in any of the gpx data.
|
---|
1142 | * @author Michael Zangl
|
---|
1143 | * @since 12156
|
---|
1144 | */
|
---|
1145 | public static class GpxDataChangeEvent {
|
---|
1146 | private final GpxData source;
|
---|
1147 |
|
---|
1148 | GpxDataChangeEvent(GpxData source) {
|
---|
1149 | super();
|
---|
1150 | this.source = source;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /**
|
---|
1154 | * Get the data that was changed.
|
---|
1155 | * @return The data.
|
---|
1156 | */
|
---|
1157 | public GpxData getSource() {
|
---|
1158 | return source;
|
---|
1159 | }
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /**
|
---|
1163 | * Determines whether anything has been modified.
|
---|
1164 | * @return whether anything has been modified (e.g. colors)
|
---|
1165 | * @since 15496
|
---|
1166 | */
|
---|
1167 | public boolean isModified() {
|
---|
1168 | return modified;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | /**
|
---|
1172 | * Sets the modified flag to the value.
|
---|
1173 | * @param value modified flag
|
---|
1174 | * @since 15496
|
---|
1175 | */
|
---|
1176 | public void setModified(boolean value) {
|
---|
1177 | modified = value;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | /**
|
---|
1181 | * A class containing prefix, URI and location of a namespace
|
---|
1182 | * @since 15496
|
---|
1183 | */
|
---|
1184 | public static class XMLNamespace {
|
---|
1185 | private final String uri, prefix;
|
---|
1186 | private String location;
|
---|
1187 |
|
---|
1188 | /**
|
---|
1189 | * Creates a schema with prefix and URI, tries to determine prefix from URI
|
---|
1190 | * @param fallbackPrefix the namespace prefix, if not determined from URI
|
---|
1191 | * @param uri the namespace URI
|
---|
1192 | */
|
---|
1193 | public XMLNamespace(String fallbackPrefix, String uri) {
|
---|
1194 | this.prefix = Optional.ofNullable(GpxExtension.findPrefix(uri)).orElse(fallbackPrefix);
|
---|
1195 | this.uri = uri;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | /**
|
---|
1199 | * Creates a schema with prefix, URI and location.
|
---|
1200 | * Does NOT try to determine prefix from URI!
|
---|
1201 | * @param prefix XML namespace prefix
|
---|
1202 | * @param uri XML namespace URI
|
---|
1203 | * @param location XML namespace location
|
---|
1204 | */
|
---|
1205 | public XMLNamespace(String prefix, String uri, String location) {
|
---|
1206 | this.prefix = prefix;
|
---|
1207 | this.uri = uri;
|
---|
1208 | this.location = location;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | /**
|
---|
1212 | * Returns the URI of the namespace.
|
---|
1213 | * @return the URI of the namespace
|
---|
1214 | */
|
---|
1215 | public String getURI() {
|
---|
1216 | return uri;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /**
|
---|
1220 | * Returns the prefix of the namespace.
|
---|
1221 | * @return the prefix of the namespace, determined from URI if possible
|
---|
1222 | */
|
---|
1223 | public String getPrefix() {
|
---|
1224 | return prefix;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /**
|
---|
1228 | * Returns the location of the schema.
|
---|
1229 | * @return the location of the schema
|
---|
1230 | */
|
---|
1231 | public String getLocation() {
|
---|
1232 | return location;
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | /**
|
---|
1236 | * Sets the location of the schema
|
---|
1237 | * @param location the location of the schema
|
---|
1238 | */
|
---|
1239 | public void setLocation(String location) {
|
---|
1240 | this.location = location;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | @Override
|
---|
1244 | public int hashCode() {
|
---|
1245 | return Objects.hash(prefix, uri, location);
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | @Override
|
---|
1249 | public boolean equals(Object obj) {
|
---|
1250 | if (this == obj)
|
---|
1251 | return true;
|
---|
1252 | if (obj == null)
|
---|
1253 | return false;
|
---|
1254 | if (getClass() != obj.getClass())
|
---|
1255 | return false;
|
---|
1256 | XMLNamespace other = (XMLNamespace) obj;
|
---|
1257 | if (prefix == null) {
|
---|
1258 | if (other.prefix != null)
|
---|
1259 | return false;
|
---|
1260 | } else if (!prefix.equals(other.prefix))
|
---|
1261 | return false;
|
---|
1262 | if (uri == null) {
|
---|
1263 | if (other.uri != null)
|
---|
1264 | return false;
|
---|
1265 | } else if (!uri.equals(other.uri))
|
---|
1266 | return false;
|
---|
1267 | if (location == null) {
|
---|
1268 | if (other.location != null)
|
---|
1269 | return false;
|
---|
1270 | } else if (!location.equals(other.location))
|
---|
1271 | return false;
|
---|
1272 | return true;
|
---|
1273 | }
|
---|
1274 | }
|
---|
1275 | }
|
---|