1 | /**
|
---|
2 | * This program is free software: you can redistribute it and/or modify it under
|
---|
3 | * the terms of the GNU General Public License as published by the
|
---|
4 | * Free Software Foundation, either version 3 of the License, or
|
---|
5 | * (at your option) any later version.
|
---|
6 | *
|
---|
7 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
---|
8 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
9 | * See the GNU General Public License for more details.
|
---|
10 | *
|
---|
11 | * You should have received a copy of the GNU General Public License along with this program.
|
---|
12 | * If not, see <http://www.gnu.org/licenses/>.
|
---|
13 | */
|
---|
14 |
|
---|
15 | package org.openstreetmap.josm.plugins.elevation.gpx;
|
---|
16 |
|
---|
17 | import java.util.Collection;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
20 | import org.openstreetmap.josm.data.gpx.GpxRoute;
|
---|
21 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
22 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
23 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Utility class to apply a visitor on GPX containers (track, route, data).
|
---|
27 | * @author Oliver Wieland <oliver.wieland@online.de>
|
---|
28 | */
|
---|
29 | public class GpxIterator {
|
---|
30 | /**
|
---|
31 | * Static class, no need to instantiate me.
|
---|
32 | */
|
---|
33 | private GpxIterator() {}
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Runs the given visitor on a GPX data instance. If one or both
|
---|
37 | * arguments are null, this method will return immediately.
|
---|
38 | *
|
---|
39 | * @param data
|
---|
40 | * The GPX data instance.
|
---|
41 | * @param visitor
|
---|
42 | * The visitor which inspects all GPX entities.
|
---|
43 | */
|
---|
44 | public static void visit(GpxData data, IGpxVisitor visitor) {
|
---|
45 | if (data == null) return;
|
---|
46 | if (visitor == null) return;
|
---|
47 |
|
---|
48 | visitor.start();
|
---|
49 | visitSingleWaypoints(data, visitor);
|
---|
50 | visitor.end();
|
---|
51 |
|
---|
52 | // routes
|
---|
53 | if (data.hasRoutePoints()) {
|
---|
54 | for (GpxRoute rte : data.routes) {
|
---|
55 | visitRoute(visitor, rte);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | // tracks
|
---|
60 | for (GpxTrack trk : data.tracks) {
|
---|
61 | visitTrack(visitor, trk);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Visits a single GPX track.
|
---|
67 | * @param track The track to visit.
|
---|
68 | * @param visitor
|
---|
69 | * The visitor which inspects all GPX entities.
|
---|
70 | */
|
---|
71 | public static void visit(GpxTrack track, IGpxVisitor visitor) {
|
---|
72 | visitTrack(visitor, track);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Visits a single GPX route.
|
---|
77 | * @param route The route to visit.
|
---|
78 | * @param visitor
|
---|
79 | * The visitor which inspects all GPX entities.
|
---|
80 | */
|
---|
81 | public static void visit(GpxRoute route, IGpxVisitor visitor) {
|
---|
82 | visitRoute(visitor, route);
|
---|
83 | }
|
---|
84 |
|
---|
85 | // ---------------------- Helper methods ----------------
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * @param visitor
|
---|
89 | * @param trk
|
---|
90 | */
|
---|
91 | private static void visitTrack(IGpxVisitor visitor, GpxTrack trk) {
|
---|
92 | if (trk == null) return;
|
---|
93 | if (visitor == null) return;
|
---|
94 |
|
---|
95 | Collection<GpxTrackSegment> segments = trk.getSegments();
|
---|
96 | visitor.start();
|
---|
97 |
|
---|
98 | if (segments != null) {
|
---|
99 | for (GpxTrackSegment segment : segments) {
|
---|
100 | Collection<WayPoint> waypts = segment.getWayPoints();
|
---|
101 | // no visitor here...
|
---|
102 | if (waypts == null)
|
---|
103 | continue;
|
---|
104 |
|
---|
105 | for (WayPoint wayPoint : waypts) {
|
---|
106 | visitor.visit(wayPoint);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | visitor.end();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * @param visitor
|
---|
115 | * @param route
|
---|
116 | */
|
---|
117 | private static void visitRoute(IGpxVisitor visitor, GpxRoute route) {
|
---|
118 | if (route == null) return;
|
---|
119 | if (visitor == null) return;
|
---|
120 |
|
---|
121 | visitor.start();
|
---|
122 | for (WayPoint wpt : route.routePoints) {
|
---|
123 | visitor.visit(wpt);
|
---|
124 | }
|
---|
125 | visitor.end();
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * @param data
|
---|
130 | * @param visitor
|
---|
131 | */
|
---|
132 | private static void visitSingleWaypoints(GpxData data, IGpxVisitor visitor) {
|
---|
133 | // isolated way points
|
---|
134 | if (data.waypoints != null) { // better with an hasWaypoints method!?
|
---|
135 | for (WayPoint wpt : data.waypoints) {
|
---|
136 | visitor.visit(wpt);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|