Ignore:
Timestamp:
2013-08-28T12:15:41+02:00 (6 years ago)
Author:
bastiK
Message:

fixed #8323 - Add support for GPX routes (<rte> element)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java

    r6069 r6206  
    55import java.io.File;
    66import java.util.Collection;
     7import java.util.Iterator;
    78import java.util.LinkedList;
    89import java.util.Map;
     
    238239        return best;
    239240    }
     241
     242    /**
     243     * Iterate over all track segments and over all routes.
     244     *
     245     * @param trackVisibility An array indicating which tracks should be
     246     * included in the iteration. Can be null, then all tracks are included.
     247     * @return an Iterable object, which iterates over all track segments and
     248     * over all routes
     249     */
     250    public Iterable<Collection<WayPoint>> getLinesIterable(final boolean[] trackVisibility) {
     251        return new Iterable<Collection<WayPoint>>() {
     252            @Override
     253            public Iterator<Collection<WayPoint>> iterator() {
     254                return new LinesIterator(GpxData.this, trackVisibility);
     255            }
     256        };
     257    }
     258   
     259    /**
     260     * Iterates over all track segments and then over all routes.
     261     */
     262    public static class LinesIterator implements Iterator<Collection<WayPoint>> {
     263
     264        private Iterator<GpxTrack> itTracks;
     265        private int idxTracks;
     266        private Iterator<GpxTrackSegment> itTrackSegments;
     267        private Iterator<GpxRoute> itRoutes;
     268
     269        private Collection<WayPoint> next;
     270        private boolean[] trackVisibility;
     271
     272        public LinesIterator(GpxData data, boolean[] trackVisibility) {
     273            itTracks = data.tracks.iterator();
     274            idxTracks = -1;
     275            itRoutes = data.routes.iterator();
     276            this.trackVisibility = trackVisibility;
     277            next = getNext();
     278        }
     279
     280        @Override
     281        public boolean hasNext() {
     282            return next != null;
     283        }
     284
     285        @Override
     286        public Collection<WayPoint> next() {
     287            Collection<WayPoint> current = next;
     288            next = getNext();
     289            return current;
     290        }
     291
     292        private Collection<WayPoint> getNext() {
     293            if (itTracks != null) {
     294                if (itTrackSegments != null && itTrackSegments.hasNext()) {
     295                    return itTrackSegments.next().getWayPoints();
     296                } else {
     297                    while (itTracks.hasNext()) {
     298                        GpxTrack nxtTrack = itTracks.next();
     299                        idxTracks++;
     300                        if (trackVisibility != null && !trackVisibility[idxTracks])
     301                            continue;
     302                        itTrackSegments = nxtTrack.getSegments().iterator();
     303                        if (itTrackSegments.hasNext()) {
     304                            return itTrackSegments.next().getWayPoints();
     305                        }
     306                    }
     307                    // if we get here, all the Tracks are finished; Continue with
     308                    // Routes
     309                    itTracks = null;
     310                }
     311            }
     312            if (itRoutes.hasNext()) {
     313                return itRoutes.next().routePoints;
     314            }
     315            return null;
     316        }
     317
     318        @Override
     319        public void remove() {
     320            throw new UnsupportedOperationException();
     321        }
     322    }
     323   
    240324}
Note: See TracChangeset for help on using the changeset viewer.