Changeset 29962 in osm


Ignore:
Timestamp:
2013-09-25T20:34:15+02:00 (11 years ago)
Author:
oliverw
Message:

[josm_elevationprofile] Revised GPX visitor, ignore single way points

Location:
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gpx
Files:
4 edited
1 moved

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gpx/ElevationModel.java

    r29958 r29962  
    2222import org.openstreetmap.josm.data.gpx.GpxTrack;
    2323import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
     24import org.openstreetmap.josm.data.gpx.IWithAttributes;
    2425import org.openstreetmap.josm.data.gpx.WayPoint;
    2526import org.openstreetmap.josm.plugins.elevation.IElevationModel;
     
    4445        private List<WayPoint> buffer = new ArrayList<WayPoint>();
    4546        private int currentProfileIndex = 0;
    46         private ElevationProfileBase curProfile = null;
     47        private ElevationProfile curProfile = null;
    4748
    4849        /**
     
    115116        }
    116117
    117         /*
    118          * (non-Javadoc)
    119          *
    120          * @see
    121          * org.openstreetmap.josm.plugins.elevation.IGpxVisitor#visit(org.openstreetmap
    122          * .josm.data.gpx.GpxRoute, org.openstreetmap.josm.data.gpx.WayPoint)
    123          */
    124         public void visit(GpxRoute route, WayPoint wp) {
    125                 processWayPoint(wp);
    126         }
    127 
    128         /*
    129          * (non-Javadoc)
    130          *
    131          * @see
    132          * org.openstreetmap.josm.plugins.elevation.IGpxVisitor#visit(org.openstreetmap
    133          * .josm.data.gpx.GpxTrack, org.openstreetmap.josm.data.gpx.GpxTrackSegment,
    134          * org.openstreetmap.josm.data.gpx.WayPoint)
    135          */
    136         public void visit(GpxTrack track, GpxTrackSegment segment, WayPoint wp) {
    137                 // we ignore the segment here
    138                 processWayPoint(wp);
    139                
     118        /* (non-Javadoc)
     119         * @see org.openstreetmap.josm.plugins.elevation.gpx.IElevationModel#getProfiles()
     120         */
     121        @Override
     122        public List<IElevationProfile> getProfiles() {
     123                return profiles;
     124        }
     125
     126        /* (non-Javadoc)
     127         * @see org.openstreetmap.josm.plugins.elevation.IElevationModel#getCurrentProfile()
     128         */
     129        @Override
     130        public IElevationProfile getCurrentProfile() {
     131            if (currentProfileIndex < 0 || currentProfileIndex >= profileCount()) return null;
     132           
     133            return profiles.get(currentProfileIndex);
     134        }
     135
     136        /* (non-Javadoc)
     137         * @see org.openstreetmap.josm.plugins.elevation.IElevationModel#setCurrentProfile(org.openstreetmap.josm.plugins.elevation.IElevationProfile)
     138         */
     139        @Override
     140        public void setCurrentProfile(IElevationProfile newProfile) {
     141            CheckParameterUtil.ensureParameterNotNull(newProfile);
     142           
     143            if (!profiles.contains(newProfile)) {
     144                profiles.add(newProfile);
     145            }
     146           
     147            setCurrentProfile(profiles.indexOf(newProfile));
     148        }
     149
     150        /* (non-Javadoc)
     151         * @see org.openstreetmap.josm.plugins.elevation.IElevationModel#setCurrentProfile(int)
     152         */
     153        @Override
     154        public void setCurrentProfile(int index) {
     155            if (index < 0 || index >= profileCount()) throw new RuntimeException("Invalid arg for setCurrentProfile: " + index + ", value must be 0.." + profileCount());
     156           
     157            currentProfileIndex = index;
     158            fireModelChanged();     
     159        }
     160
     161        /* (non-Javadoc)
     162         * @see org.openstreetmap.josm.plugins.elevation.IElevationModel#profileCount()
     163         */
     164        @Override
     165        public int profileCount() {
     166            return profiles != null ? profiles.size() : 0;
     167        }
     168
     169        // Visitor stuff starts here...
     170       
     171        /* (non-Javadoc)
     172         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#beginWayPoints()
     173         */
     174        public void beginWayPoints() {
     175            // we ignore single way points (elevation profile is quite meaningless...)
     176        }
     177
     178        /* (non-Javadoc)
     179         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#endWayPoints()
     180         */
     181        public void endWayPoints() {           
     182            // we ignore single way points (elevation profile is quite meaningless...)
    140183        }
    141184       
     
    144187         */
    145188        @Override
    146         public void visit(WayPoint wp) {
    147                 processWayPoint(wp);
    148         }
    149 
    150         public void start() {
    151                 curProfile = new ElevationProfileBase(name);
    152                 trackCounter++;
    153         }
    154 
    155         public void end() {             
    156                 commitTrack();
    157         }
    158        
    159 
    160         @Override
    161         public void start(GpxTrack track) {
     189        public void visitWayPoint(WayPoint wp) {
     190            // we ignore single way points (elevation profile is quite meaningless...)
     191        }
     192
     193
     194        /* (non-Javadoc)
     195         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#beginTrack(org.openstreetmap.josm.data.gpx.GpxTrack)
     196         */
     197        @Override
     198        public void beginTrack(GpxTrack track) {
     199            createProfile(track);
     200        }
     201
     202        /* (non-Javadoc)
     203         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#endTrack(org.openstreetmap.josm.data.gpx.GpxTrack)
     204         */
     205        @Override
     206        public void endTrack(GpxTrack track) {
     207            if (curProfile == null) throw new RuntimeException("Internal error: No elevation profile");
     208           
     209            curProfile.setDistance(track.length());
     210            commitProfile();
     211        }
     212       
     213        /* (non-Javadoc)
     214         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#beginTrackSegment(org.openstreetmap.josm.data.gpx.GpxTrack, org.openstreetmap.josm.data.gpx.GpxTrackSegment)
     215         */
     216        @Override
     217        public void beginTrackSegment(GpxTrack track, GpxTrackSegment segment) {
     218            // Nothing to do here for now
     219        }
     220
     221        /* (non-Javadoc)
     222         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#endTrackSegment(org.openstreetmap.josm.data.gpx.GpxTrack, org.openstreetmap.josm.data.gpx.GpxTrackSegment)
     223         */
     224        @Override
     225        public void endTrackSegment(GpxTrack track, GpxTrackSegment segment) {
     226            // Nothing to do here for now
     227        }
     228       
     229        /* (non-Javadoc)
     230         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#visitTrackPoint(org.openstreetmap.josm.data.gpx.WayPoint, org.openstreetmap.josm.data.gpx.GpxTrack, org.openstreetmap.josm.data.gpx.GpxTrackSegment)
     231         */
     232        @Override
     233        public void visitTrackPoint(WayPoint wp, GpxTrack track,
     234                GpxTrackSegment segment) {
     235           
     236            processWayPoint(wp);           
     237        }
     238       
     239
     240        /* (non-Javadoc)
     241         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#beginRoute(org.openstreetmap.josm.data.gpx.GpxRoute)
     242         */
     243        @Override
     244        public void beginRoute(GpxRoute route) {
     245            createProfile(route);
     246        }
     247
     248        /* (non-Javadoc)
     249         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#endRoute(org.openstreetmap.josm.data.gpx.GpxRoute)
     250         */
     251        @Override
     252        public void endRoute(GpxRoute route) {
     253            if (curProfile == null) throw new RuntimeException("Internal error: No elevation profile");
     254            // a GpxRoute has no 'length' property
     255            curProfile.setDistance(0);
     256            commitProfile();
     257        }
     258       
     259        /* (non-Javadoc)
     260         * @see org.openstreetmap.josm.plugins.elevation.gpx.IGpxVisitor#visitRoutePoint(org.openstreetmap.josm.data.gpx.WayPoint, org.openstreetmap.josm.data.gpx.GpxRoute)
     261         */
     262        @Override
     263        public void visitRoutePoint(WayPoint wp, GpxRoute route) {
     264            processWayPoint(wp);           
     265        }
     266       
     267        /**
     268         * Creates a new profile.
     269         *
     270         * @param trackOrRoute the track or route
     271         */
     272        private void createProfile(IWithAttributes trackOrRoute) {
    162273            // check GPX data
    163             String trackName = (String) track.get("name");
     274            String trackName = (String) trackOrRoute.get("name");
    164275           
    165276            // no name given, build artificial one
    166277            if (trackName == null) {
    167                 trackName = (String) track.get(GpxData.META_NAME);
     278                trackName = (String) trackOrRoute.get(GpxData.META_NAME);
    168279                if (trackName == null) {
    169280                    trackName = name + "." + trackCounter;
     
    171282            }
    172283           
    173             curProfile = new ElevationProfileBase(trackName);
    174         }
    175 
    176         @Override
    177         public void end(GpxTrack track) {
    178             if (curProfile == null) throw new RuntimeException("Internal error: No elevation profile");
    179            
    180             curProfile.setDistance(track.length());
    181             commitTrack();
    182         }
    183        
    184         @Override
    185         public void start(GpxTrack track, GpxTrackSegment segment) {
    186             // Nothing to do here for now
    187         }
    188 
    189         @Override
    190         public void end(GpxTrack track, GpxTrackSegment segment) {
    191             // Nothing to do here for now
    192         }
    193 
     284            curProfile = new ElevationProfile(trackName);
     285        }
    194286       
    195287        /**
     
    198290         * @param trackName the track name
    199291         */
    200         private void commitTrack() {
     292        private void commitProfile() {
    201293                if (buffer.size() > 0) {   
    202294                        // assign way points to profile...
     
    208300        }
    209301
     302        /**
     303         * Adds the given way point to the current buffer.
     304         *
     305         * @param wp the wp
     306         */
    210307        private void processWayPoint(WayPoint wp) {
    211308                if (wp == null) {
     
    215312                buffer.add(wp);
    216313        }
    217 
    218        
    219         /* (non-Javadoc)
    220          * @see org.openstreetmap.josm.plugins.elevation.gpx.IElevationModel#getProfiles()
    221          */
    222         @Override
    223         public List<IElevationProfile> getProfiles() {
    224                 return profiles;
    225         }
    226 
    227         @Override
    228         public IElevationProfile getCurrentProfile() {
    229             if (currentProfileIndex < 0 || currentProfileIndex >= profileCount()) return null;
    230            
    231             return profiles.get(currentProfileIndex);
    232         }
    233 
    234         @Override
    235         public void setCurrentProfile(IElevationProfile newProfile) {
    236             CheckParameterUtil.ensureParameterNotNull(newProfile);
    237            
    238             if (!profiles.contains(newProfile)) {
    239                 profiles.add(newProfile);
    240             }
    241            
    242             setCurrentProfile(profiles.indexOf(newProfile));
    243         }
    244 
    245         @Override
    246         public void setCurrentProfile(int index) {
    247             if (index < 0 || index >= profileCount()) throw new RuntimeException("Invalid arg for setCurrentProfile: " + index + ", value must be 0.." + profileCount());
    248            
    249             currentProfileIndex = index;
    250             fireModelChanged();     
    251         }
    252 
    253         @Override
    254         public int profileCount() {
    255             return profiles != null ? profiles.size() : 0;
    256         }
    257314}
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gpx/ElevationProfile.java

    r29961 r29962  
    3131 *
    3232 * The computation is done via implementing {@link IGpxWaypointVisitor},
    33  * subclasses may override the {@link ElevationProfileBase#visit(WayPoint)}
     33 * subclasses may override the {@link ElevationProfile#visitWayPoint(WayPoint)}
    3434 * method to compute own values or run specific actions. The computation is
    35  * triggered by calling {@link ElevationProfileBase#updateValues()}.
     35 * triggered by calling {@link ElevationProfile#updateValues()}.
    3636 *
    3737 * Elevation profiles can break down into further child profiles. This is
     
    4444 *
    4545 */
    46 public class ElevationProfileBase implements IElevationProfile,
     46public class ElevationProfile implements IElevationProfile,
    4747                IGpxWaypointVisitor {
    4848        public static final int WAYPOINT_START = 0;
     
    7474         * @param name
    7575         */
    76         public ElevationProfileBase(String name) {
     76        public ElevationProfile(String name) {
    7777                this(name, null, null, 0);
    7878        }
     
    9090         *            The requested target size of the profile.
    9191         */
    92         public ElevationProfileBase(String name, IElevationProfile parent,
     92        public ElevationProfile(String name, IElevationProfile parent,
    9393                        List<WayPoint> wayPoints, int sliceSize) {
    9494                super();
     
    114114         */
    115115        public static void setIgnoreZeroHeight(boolean ignoreZeroHeight) {
    116                 ElevationProfileBase.ignoreZeroHeight = ignoreZeroHeight;
     116                ElevationProfile.ignoreZeroHeight = ignoreZeroHeight;
    117117        }
    118118
     
    149149               
    150150                for (WayPoint wayPoint : this.wayPoints) {
    151                         visit(wayPoint);
     151                        visitWayPoint(wayPoint);
    152152                }
    153153               
     
    487487         * way point list.
    488488         */
    489         public void visit(WayPoint wp) {
     489        public void visitWayPoint(WayPoint wp) {
    490490                if (wp.getTime().after(end)) {
    491491                        setEnd(wp);
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gpx/GpxIterator.java

    r29955 r29962  
    4848                if (data.isEmpty()) return;
    4949               
    50                 visitor.start();
     50                visitor.beginWayPoints();
    5151                visitSingleWaypoints(data, visitor);
    52                 visitor.end();
     52                visitor.endWayPoints();
    5353
    5454                // routes
     
    9898               
    9999                if (segments != null) {
    100                         visitor.start(trk);
     100                        visitor.beginTrack(trk);
    101101                        // visit all segments
    102102                        for (GpxTrackSegment segment : segments) {
     
    106106                                        continue;
    107107                               
    108                                 visitor.start(trk, segment);
     108                                visitor.beginTrackSegment(trk, segment);
    109109                               
    110110                                for (WayPoint wayPoint : waypts) {
    111                                         visitor.visit(wayPoint);
     111                                        visitor.visitTrackPoint(wayPoint, trk, segment);
    112112                                }
    113113                               
    114                                 visitor.end(trk, segment);
     114                                visitor.endTrackSegment(trk, segment);
    115115                        }
    116                         visitor.end(trk);
     116                        visitor.endTrack(trk);
    117117                }               
    118118               
     
    127127                if (visitor == null) return;
    128128               
    129                 visitor.start();
     129                visitor.beginWayPoints();
    130130                for (WayPoint wpt : route.routePoints) {
    131                         visitor.visit(wpt);
     131                        visitor.visitRoutePoint(wpt, route);
    132132                }
    133                 visitor.end();
     133                visitor.endWayPoints();
    134134        }
    135135
     
    142142                if (data.waypoints != null) { // better with an hasWaypoints method!?
    143143                        for (WayPoint wpt : data.waypoints) {
    144                                 visitor.visit(wpt);
     144                                visitor.visitWayPoint(wpt);
    145145                        }
    146146                }
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gpx/IGpxVisitor.java

    r29955 r29962  
    1515package org.openstreetmap.josm.plugins.elevation.gpx;
    1616
     17import org.openstreetmap.josm.data.gpx.GpxRoute;
    1718import org.openstreetmap.josm.data.gpx.GpxTrack;
    1819import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
     
    2526public interface IGpxVisitor extends IGpxWaypointVisitor {
    2627        /**
    27          * Starts a GPX route or way point collection.
     28         * Starts a GPX way point collection.
    2829         */
    29         void start();
     30        void beginWayPoints();
    3031       
    3132        /**
    32          * Ends a GPX route or way point collection.
     33         * Ends a GPX way point collection.
    3334         */
    34         void end();
     35        void endWayPoints();
    3536       
    3637        /**
    3738         * Starts a GPX track.
    3839         */
    39         void start(GpxTrack track);
     40        void beginTrack(GpxTrack track);
    4041       
    4142        /**
    4243         * Ends a GPX track.
    4344         */
    44         void end(GpxTrack track);
     45        void endTrack(GpxTrack track);
     46       
     47        /**
     48         * Starts a GPX route.
     49         */
     50        void beginRoute(GpxRoute track);
     51       
     52        /**
     53         * Ends a GPX route.
     54         */
     55        void endRoute(GpxRoute track);
    4556
    4657       
     
    4859         * Starts a segment within a GPX track.
    4960         */
    50         void start(GpxTrack track, GpxTrackSegment segment);
     61        void beginTrackSegment(GpxTrack track, GpxTrackSegment segment);
    5162       
    5263        /**
    5364         * Ends a segment within a GPX track.
    5465         */
    55         void end(GpxTrack track, GpxTrackSegment segment);
     66        void endTrackSegment(GpxTrack track, GpxTrackSegment segment);
    5667       
    5768        /**
     
    6071         * @param wp The way point to visit.
    6172         */
    62         void visit(WayPoint wp);
     73        void visitWayPoint(WayPoint wp);
     74       
     75        /**
     76         * Visits a way point within a GPX track.
     77         *
     78         * @param wp The way point to visit.
     79         * @param track the track containing the way point.
     80         * @param segment the track segment
     81         */
     82        void visitTrackPoint(WayPoint wp, GpxTrack track, GpxTrackSegment segment);
     83       
     84        /**
     85         * Visits a way point within a GPX route.
     86         * @param route the route containing the way point.
     87         * @param wp the way point to visit.
     88         */
     89        void visitRoutePoint(WayPoint wp, GpxRoute route);
    6390}
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/gpx/IGpxWaypointVisitor.java

    r29907 r29962  
    2727         * @param wp The way point to visit.
    2828         */
    29         void visit(WayPoint wp);
     29        void visitWayPoint(WayPoint wp);
    3030}
Note: See TracChangeset for help on using the changeset viewer.