Ignore:
Timestamp:
2010-01-30T20:04:10+01:00 (14 years ago)
Author:
jttt
Message:

Gpx refactoring - GpxTrack and GpxTrackSegment is now interface, implementations for specific use can be provided (currently JOSM supports immutable gpx track, livegps plugin supports append only track).

  • track length and bounds are precalculated
  • GpxLayer paints only currently visible segments
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r2867 r2907  
    5353import org.openstreetmap.josm.data.gpx.GpxData;
    5454import org.openstreetmap.josm.data.gpx.GpxTrack;
     55import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
    5556import org.openstreetmap.josm.data.gpx.WayPoint;
    5657import org.openstreetmap.josm.data.osm.DataSet;
     
    197198                GpxData namedTrackPoints = new GpxData();
    198199                for (GpxTrack track : data.tracks) {
    199                     for (Collection<WayPoint> seg : track.trackSegs) {
    200                         for (WayPoint point : seg)
     200                    for (GpxTrackSegment seg : track.getSegments()) {
     201                        for (WayPoint point : seg.getWayPoints())
    201202                            if (point.attr.containsKey("name") || point.attr.containsKey("desc")) {
    202203                                namedTrackPoints.waypoints.add(point);
     
    385386
    386387                info.append("<tr><td>");
    387                 if (trk.attr.containsKey("name")) {
    388                     info.append(trk.attr.get("name"));
     388                if (trk.getAttributes().containsKey("name")) {
     389                    info.append(trk.getAttributes().get("name"));
    389390                }
    390391                info.append("</td><td>");
    391                 if (trk.attr.containsKey("desc")) {
    392                     info.append(" ").append(trk.attr.get("desc"));
     392                if (trk.getAttributes().containsKey("desc")) {
     393                    info.append(" ").append(trk.getAttributes().get("desc"));
    393394                }
    394395                info.append("</td><td>");
    395396
    396                 for (Collection<WayPoint> seg : trk.trackSegs) {
    397                     for (WayPoint pnt : seg) {
     397                for (GpxTrackSegment seg : trk.getSegments()) {
     398                    for (WayPoint pnt : seg.getWayPoints()) {
    398399                        if (latest == null) {
    399400                            latest = earliest = pnt;
     
    429430                info.append(new DecimalFormat("#0.00").format(trk.length() / 1000) + "km");
    430431                info.append("</td><td>");
    431                 if (trk.attr.containsKey("url")) {
    432                     info.append(trk.attr.get("url"));
     432                if (trk.getAttributes().containsKey("url")) {
     433                    info.append(trk.getAttributes().get("url"));
    433434                }
    434435                info.append("</td></tr>");
     
    552553            WayPoint oldWp = null;
    553554            for (GpxTrack trk : data.tracks) {
    554                 for (Collection<WayPoint> segment : trk.trackSegs) {
     555                for (GpxTrackSegment segment : trk.getSegments()) {
    555556                    if (!forceLines) { // don't draw lines between segments, unless forced to
    556557                        oldWp = null;
    557558                    }
    558                     for (WayPoint trkPnt : segment) {
     559                    for (WayPoint trkPnt : segment.getWayPoints()) {
    559560                        LatLon c = trkPnt.getCoor();
    560561                        if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     
    609610        }
    610611
     612        List<Collection<WayPoint>> visibleSegments = new ArrayList<Collection<WayPoint>>();
     613        for (GpxTrack trk: data.tracks) {
     614            for (GpxTrackSegment trkSeg: trk.getSegments()) {
     615                if (trkSeg.getBounds().asRect().intersects(box.asRect())) {
     616                    visibleSegments.add(trkSeg.getWayPoints());
     617                }
     618            }
     619        }
     620
    611621        /****************************************************************
    612622         ********** STEP 3a - DRAW LINES ********************************
     
    614624        if (lines) {
    615625            Point old = null;
    616             for (GpxTrack trk : data.tracks) {
    617                 for (Collection<WayPoint> segment : trk.trackSegs) {
    618                     for (WayPoint trkPnt : segment) {
    619                         LatLon c = trkPnt.getCoor();
    620                         if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    621                             continue;
    622                         }
    623                         Point screen = mv.getPoint(trkPnt.getEastNorth());
    624                         if (trkPnt.drawLine) {
    625                             // skip points that are on the same screenposition
    626                             if (old != null && ((old.x != screen.x) || (old.y != screen.y))) {
    627                                 g.setColor(trkPnt.customColoring);
    628                                 g.drawLine(old.x, old.y, screen.x, screen.y);
    629                             }
    630                         }
    631                         old = screen;
    632                     } // end for trkpnt
    633                 } // end for segment
    634             } // end for trk
     626            for (Collection<WayPoint> segment : visibleSegments) {
     627                for (WayPoint trkPnt : segment) {
     628                    LatLon c = trkPnt.getCoor();
     629                    if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     630                        continue;
     631                    }
     632                    Point screen = mv.getPoint(trkPnt.getEastNorth());
     633                    if (trkPnt.drawLine) {
     634                        // skip points that are on the same screenposition
     635                        if (old != null && ((old.x != screen.x) || (old.y != screen.y))) {
     636                            g.setColor(trkPnt.customColoring);
     637                            g.drawLine(old.x, old.y, screen.x, screen.y);
     638                        }
     639                    }
     640                    old = screen;
     641                } // end for trkpnt
     642            } // end for segment
    635643        } // end if lines
    636644
     
    641649            Point old = null;
    642650            Point oldA = null; // last arrow painted
    643             for (GpxTrack trk : data.tracks) {
    644                 for (Collection<WayPoint> segment : trk.trackSegs) {
    645                     for (WayPoint trkPnt : segment) {
    646                         LatLon c = trkPnt.getCoor();
    647                         if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    648                             continue;
    649                         }
    650                         if (trkPnt.drawLine) {
    651                             Point screen = mv.getPoint(trkPnt.getEastNorth());
    652                             // skip points that are on the same screenposition
    653                             if (old != null
    654                                     && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
    655                                             || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
    656                                 g.setColor(trkPnt.customColoring);
    657                                 double t = Math.atan2(screen.y - old.y, screen.x - old.x) + Math.PI;
    658                                 g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
    659                                         (int) (screen.y + 10 * Math.sin(t - PHI)));
    660                                 g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
    661                                         (int) (screen.y + 10 * Math.sin(t + PHI)));
    662                                 oldA = screen;
    663                             }
    664                             old = screen;
    665                         }
    666                     } // end for trkpnt
    667                 } // end for segment
    668             } // end for trk
     651            for (Collection<WayPoint> segment : visibleSegments) {
     652                for (WayPoint trkPnt : segment) {
     653                    LatLon c = trkPnt.getCoor();
     654                    if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     655                        continue;
     656                    }
     657                    if (trkPnt.drawLine) {
     658                        Point screen = mv.getPoint(trkPnt.getEastNorth());
     659                        // skip points that are on the same screenposition
     660                        if (old != null
     661                                && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
     662                                        || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
     663                            g.setColor(trkPnt.customColoring);
     664                            double t = Math.atan2(screen.y - old.y, screen.x - old.x) + Math.PI;
     665                            g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
     666                                    (int) (screen.y + 10 * Math.sin(t - PHI)));
     667                            g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
     668                                    (int) (screen.y + 10 * Math.sin(t + PHI)));
     669                            oldA = screen;
     670                        }
     671                        old = screen;
     672                    }
     673                } // end for trkpnt
     674            } // end for segment
    669675        } // end if lines
    670676
     
    675681            Point old = null;
    676682            Point oldA = null; // last arrow painted
    677             for (GpxTrack trk : data.tracks) {
    678                 for (Collection<WayPoint> segment : trk.trackSegs) {
    679                     for (WayPoint trkPnt : segment) {
    680                         LatLon c = trkPnt.getCoor();
    681                         if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    682                             continue;
    683                         }
    684                         if (trkPnt.drawLine) {
    685                             Point screen = mv.getPoint(trkPnt.getEastNorth());
    686                             // skip points that are on the same screenposition
    687                             if (old != null
    688                                     && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
    689                                             || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
    690                                 g.setColor(trkPnt.customColoring);
    691                                 g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
    692                                         + dir[trkPnt.dir][1]);
    693                                 g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
    694                                         + dir[trkPnt.dir][3]);
    695                                 oldA = screen;
    696                             }
    697                             old = screen;
    698                         }
    699                     } // end for trkpnt
    700                 } // end for segment
    701             } // end for trk
     683            for (Collection<WayPoint> segment : visibleSegments) {
     684                for (WayPoint trkPnt : segment) {
     685                    LatLon c = trkPnt.getCoor();
     686                    if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     687                        continue;
     688                    }
     689                    if (trkPnt.drawLine) {
     690                        Point screen = mv.getPoint(trkPnt.getEastNorth());
     691                        // skip points that are on the same screenposition
     692                        if (old != null
     693                                && (oldA == null || screen.x < oldA.x - delta || screen.x > oldA.x + delta
     694                                        || screen.y < oldA.y - delta || screen.y > oldA.y + delta)) {
     695                            g.setColor(trkPnt.customColoring);
     696                            g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
     697                                    + dir[trkPnt.dir][1]);
     698                            g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
     699                                    + dir[trkPnt.dir][3]);
     700                            oldA = screen;
     701                        }
     702                        old = screen;
     703                    }
     704                } // end for trkpnt
     705            } // end for segment
    702706        } // end if lines
    703707
     
    707711        if (large || hdopcircle) {
    708712            g.setColor(neutralColor);
    709             for (GpxTrack trk : data.tracks) {
    710                 for (Collection<WayPoint> segment : trk.trackSegs) {
    711                     for (WayPoint trkPnt : segment) {
    712                         LatLon c = trkPnt.getCoor();
    713                         if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    714                             continue;
    715                         }
    716                         Point screen = mv.getPoint(trkPnt.getEastNorth());
    717                         g.setColor(trkPnt.customColoring);
    718                         if (hdopcircle && trkPnt.attr.get("hdop") != null) {
    719                             // hdop value
    720                             float hdop = ((Float)trkPnt.attr.get("hdop")).floatValue();
    721                             if (hdop < 0) {
    722                                 hdop = 0;
    723                             }
    724                             // hdop pixels
    725                             int hdopp = mv.getPoint(new LatLon(trkPnt.getCoor().lat(), trkPnt.getCoor().lon() + 2*6*hdop*360/40000000)).x - screen.x;
    726                             g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
    727                         }
    728                         if (large) {
    729                             g.fillRect(screen.x-1, screen.y-1, 3, 3);
    730                         }
    731                     } // end for trkpnt
    732                 } // end for segment
    733             } // end for trk
     713            for (Collection<WayPoint> segment : visibleSegments) {
     714                for (WayPoint trkPnt : segment) {
     715                    LatLon c = trkPnt.getCoor();
     716                    if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     717                        continue;
     718                    }
     719                    Point screen = mv.getPoint(trkPnt.getEastNorth());
     720                    g.setColor(trkPnt.customColoring);
     721                    if (hdopcircle && trkPnt.attr.get("hdop") != null) {
     722                        // hdop value
     723                        float hdop = ((Float)trkPnt.attr.get("hdop")).floatValue();
     724                        if (hdop < 0) {
     725                            hdop = 0;
     726                        }
     727                        // hdop pixels
     728                        int hdopp = mv.getPoint(new LatLon(trkPnt.getCoor().lat(), trkPnt.getCoor().lon() + 2*6*hdop*360/40000000)).x - screen.x;
     729                        g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
     730                    }
     731                    if (large) {
     732                        g.fillRect(screen.x-1, screen.y-1, 3, 3);
     733                    }
     734                } // end for trkpnt
     735            } // end for segment
    734736        } // end if large || hdopcircle
    735737
     
    739741        if (!large && lines) {
    740742            g.setColor(neutralColor);
    741             for (GpxTrack trk : data.tracks) {
    742                 for (Collection<WayPoint> segment : trk.trackSegs) {
    743                     for (WayPoint trkPnt : segment) {
    744                         LatLon c = trkPnt.getCoor();
    745                         if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    746                             continue;
    747                         }
    748                         if (!trkPnt.drawLine) {
    749                             Point screen = mv.getPoint(trkPnt.getEastNorth());
    750                             g.drawRect(screen.x, screen.y, 0, 0);
    751                         }
    752                     } // end for trkpnt
    753                 } // end for segment
    754             } // end for trk
     743            for (Collection<WayPoint> segment : visibleSegments) {
     744                for (WayPoint trkPnt : segment) {
     745                    LatLon c = trkPnt.getCoor();
     746                    if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     747                        continue;
     748                    }
     749                    if (!trkPnt.drawLine) {
     750                        Point screen = mv.getPoint(trkPnt.getEastNorth());
     751                        g.drawRect(screen.x, screen.y, 0, 0);
     752                    }
     753                } // end for trkpnt
     754            } // end for segment
    755755        } // end if large
    756756
     
    760760        if (!large && !lines) {
    761761            g.setColor(neutralColor);
    762             for (GpxTrack trk : data.tracks) {
    763                 for (Collection<WayPoint> segment : trk.trackSegs) {
    764                     for (WayPoint trkPnt : segment) {
    765                         LatLon c = trkPnt.getCoor();
    766                         if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
    767                             continue;
    768                         }
    769                         Point screen = mv.getPoint(trkPnt.getEastNorth());
    770                         g.setColor(trkPnt.customColoring);
    771                         g.drawRect(screen.x, screen.y, 0, 0);
    772                     } // end for trkpnt
    773                 } // end for segment
    774             } // end for trk
     762            for (Collection<WayPoint> segment : visibleSegments) {
     763                for (WayPoint trkPnt : segment) {
     764                    LatLon c = trkPnt.getCoor();
     765                    if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
     766                        continue;
     767                    }
     768                    Point screen = mv.getPoint(trkPnt.getEastNorth());
     769                    g.setColor(trkPnt.customColoring);
     770                    g.drawRect(screen.x, screen.y, 0, 0);
     771                } // end for trkpnt
     772            } // end for segment
    775773        } // end if large
    776774
     
    802800            DataSet ds = new DataSet();
    803801            for (GpxTrack trk : data.tracks) {
    804                 for (Collection<WayPoint> segment : trk.trackSegs) {
     802                for (GpxTrackSegment segment : trk.getSegments()) {
    805803                    List<Node> nodes = new ArrayList<Node>();
    806                     for (WayPoint p : segment) {
     804                    for (WayPoint p : segment.getWayPoints()) {
    807805                        Node n = new Node(p.getCoor());
    808806                        String timestr = p.getString("time");
     
    887885
    888886            for (GpxTrack trk : data.tracks) {
    889                 for (Collection<WayPoint> segment : trk.trackSegs) {
    890                     for (WayPoint p : segment) {
     887                for (GpxTrackSegment segment : trk.getSegments()) {
     888                    for (WayPoint p : segment.getWayPoints()) {
    891889                        latsum += p.getCoor().lat();
    892890                        latcnt++;
     
    920918            LatLon previous = null;
    921919            for (GpxTrack trk : data.tracks) {
    922                 for (Collection<WayPoint> segment : trk.trackSegs) {
    923                     for (WayPoint p : segment) {
     920                for (GpxTrackSegment segment : trk.getSegments()) {
     921                    for (WayPoint p : segment.getWayPoints()) {
    924922                        LatLon c = p.getCoor();
    925923                        if (previous == null || c.greatCircleDistance(previous) > buffer_dist) {
     
    10471045        if (data.tracks != null && !data.tracks.isEmpty()) {
    10481046            for (GpxTrack track : data.tracks) {
    1049                 if (track.trackSegs == null) {
    1050                     continue;
    1051                 }
    1052                 for (Collection<WayPoint> seg : track.trackSegs) {
    1053                     for (WayPoint w : seg) {
     1047                for (GpxTrackSegment seg : track.getSegments()) {
     1048                    for (WayPoint w : seg.getWayPoints()) {
    10541049                        firstTime = w.time;
    10551050                        break;
     
    11111106                && !data.tracks.isEmpty()) {
    11121107            for (GpxTrack track : data.tracks) {
    1113                 if (track.trackSegs == null) {
    1114                     continue;
    1115                 }
    1116                 for (Collection<WayPoint> seg : track.trackSegs) {
    1117                     for (WayPoint w : seg) {
     1108                for (GpxTrackSegment seg : track.getSegments()) {
     1109                    for (WayPoint w : seg.getWayPoints()) {
    11181110                        if (w.attr.containsKey("name") || w.attr.containsKey("desc")) {
    11191111                            waypoints.add(w);
     
    11371129
    11381130            for (GpxTrack track : data.tracks) {
    1139                 if (track.trackSegs == null) {
    1140                     continue;
    1141                 }
    1142                 for (Collection<WayPoint> seg : track.trackSegs) {
    1143                     for (WayPoint w : seg) {
     1131                for (GpxTrackSegment seg : track.getSegments()) {
     1132                    for (WayPoint w : seg.getWayPoints()) {
    11441133                        if (startTime < w.time) {
    11451134                            w2 = w;
     
    11771166            boolean gotOne = false;
    11781167            for (GpxTrack track : data.tracks) {
    1179                 if (track.trackSegs == null) {
    1180                     continue;
    1181                 }
    1182                 for (Collection<WayPoint> seg : track.trackSegs) {
    1183                     for (WayPoint w : seg) {
     1168                for (GpxTrackSegment seg : track.getSegments()) {
     1169                    for (WayPoint w : seg.getWayPoints()) {
    11841170                        WayPoint wStart = new WayPoint(w.getCoor());
    11851171                        wStart.attr.put("name", "start");
     
    12901276            return null;
    12911277        for (GpxTrack track : data.tracks) {
    1292             if (track.trackSegs == null) {
    1293                 continue;
    1294             }
    1295             for (Collection<WayPoint> seg : track.trackSegs) {
     1278            for (GpxTrackSegment seg : track.getSegments()) {
    12961279                WayPoint R = null;
    1297                 for (WayPoint S : seg) {
     1280                for (WayPoint S : seg.getWayPoints()) {
    12981281                    EastNorth c = S.getEastNorth();
    12991282                    if (R == null) {
Note: See TracChangeset for help on using the changeset viewer.