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