public static void addOpenRoute(Track route) { if (OpenRoute == null) { route.setColor(new Color(0.85f, 0.1f, 0.2f, 1f)); Routes.add(0, route); OpenRoute = route; } else { // erst die alte route löschen Routes.remove(OpenRoute); route.setColor(OpenRoute.getColor()); Routes.add(0, route); OpenRoute = route; } RoutesChanged(); }
// Read track from gpx file // attention it is possible that a gpx file contains more than 1 <trk> segments public static Track MultiLoadRoute(String file, Color color) { float[] dist = new float[4]; double Distance = 0; double AltitudeDifference = 0; double DeltaAltitude = 0; CoordinateGPS FromPosition = new CoordinateGPS(0, 0); BufferedReader reader; try { InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF8"); reader = new BufferedReader(isr); Track route = new Track(null, color); String line = null; String tmpLine = null; String GPXName = null; boolean isSeg = false; boolean isTrk = false; boolean isRte = false; boolean IStrkptORrtept = false; boolean ReadName = false; int AnzTracks = 0; CoordinateGPS lastAcceptedCoordinate = null; double lastAcceptedDirection = -1; Date lastAcceptedTime = null; StringBuilder sb = new StringBuilder(); String rline = null; while ((rline = reader.readLine()) != null) { for (int i = 0; i < rline.length(); i++) { char nextChar = rline.charAt(i); sb.append(nextChar); if (nextChar == '>') { line = sb.toString().trim().toLowerCase(); tmpLine = sb.toString(); sb = new StringBuilder(); if (!isTrk) // Begin of the Track detected? { if (line.indexOf("<trk>") > -1) { isTrk = true; continue; } } if (!isSeg) // Begin of the Track Segment detected? { if (line.indexOf("<trkseg>") > -1) { isSeg = true; route = new Track(null, color); route.FileName = file; Distance = 0; AltitudeDifference = 0; AnzTracks++; if (GPXName == null) route.Name = FileIO.GetFileName(file); else { if (AnzTracks <= 1) route.Name = GPXName; else route.Name = GPXName + AnzTracks; } continue; } } if (!isRte) // Begin of the Route detected? { if (line.indexOf("<rte>") > -1) { isRte = true; route = new Track(null, color); route.FileName = file; Distance = 0; AltitudeDifference = 0; AnzTracks++; if (GPXName != null) route.Name = FileIO.GetFileName(file); else { if (AnzTracks <= 1) route.Name = GPXName; else route.Name = GPXName + AnzTracks; } continue; } } if ((line.indexOf("<name>") > -1) & !IStrkptORrtept) // found <name>? { ReadName = true; continue; } if (ReadName & !IStrkptORrtept) { int cdata_start = 0; int name_start = 0; int name_end; name_end = line.indexOf("</name>"); // Name contains cdata? cdata_start = line.indexOf("[cdata["); if (cdata_start > -1) { name_start = cdata_start + 7; name_end = line.indexOf("]"); } if (name_end > name_start) { // tmpLine, damit Groß-/Kleinschreibung beachtet wird if (isSeg | isRte) route.Name = tmpLine.substring(name_start, name_end); else GPXName = tmpLine.substring(name_start, name_end); } ReadName = false; continue; } if (line.indexOf("</trkseg>") > -1) // End of the Track Segment detected? { if (route.Points.size() < 2) route.Name = "no Route segment found"; route.ShowRoute = true; route.TrackLength = Distance; route.AltitudeDifference = AltitudeDifference; add(route); isSeg = false; break; } if (line.indexOf("</rte>") > -1) // End of the Route detected? { if (route.Points.size() < 2) route.Name = "no Route segment found"; route.ShowRoute = true; route.TrackLength = Distance; route.AltitudeDifference = AltitudeDifference; add(route); isRte = false; break; } if ((line.indexOf("<trkpt") > -1) | (line.indexOf("<rtept") > -1)) { IStrkptORrtept = true; // Trackpoint lesen int lonIdx = line.indexOf("lon=\"") + 5; int latIdx = line.indexOf("lat=\"") + 5; int lonEndIdx = line.indexOf("\"", lonIdx); int latEndIdx = line.indexOf("\"", latIdx); String latStr = line.substring(latIdx, latEndIdx); String lonStr = line.substring(lonIdx, lonEndIdx); double lat = Double.valueOf(latStr); double lon = Double.valueOf(lonStr); lastAcceptedCoordinate = new CoordinateGPS(lat, lon); } if (line.indexOf("</time>") > -1) { // Time lesen int timIdx = line.indexOf("<time>") + 6; if (timIdx == 5) timIdx = 0; int timEndIdx = line.indexOf("</time>", timIdx); String timStr = line.substring(timIdx, timEndIdx); lastAcceptedTime = parseDate(timStr); } if (line.indexOf("</course>") > -1) { // Course lesen int couIdx = line.indexOf("<course>") + 8; if (couIdx == 7) couIdx = 0; int couEndIdx = line.indexOf("</course>", couIdx); String couStr = line.substring(couIdx, couEndIdx); lastAcceptedDirection = Double.valueOf(couStr); } if ((line.indexOf("</ele>") > -1) & IStrkptORrtept) { // Elevation lesen int couIdx = line.indexOf("<ele>") + 5; if (couIdx == 4) couIdx = 0; int couEndIdx = line.indexOf("</ele>", couIdx); String couStr = line.substring(couIdx, couEndIdx); lastAcceptedCoordinate.setElevation(Double.valueOf(couStr)); } if (line.indexOf("</gpxx:colorrgb>") > -1) { // Color lesen int couIdx = line.indexOf("<gpxx:colorrgb>") + 15; if (couIdx == 14) couIdx = 0; int couEndIdx = line.indexOf("</gpxx:colorrgb>", couIdx); String couStr = line.substring(couIdx, couEndIdx); color = new HSV_Color(couStr); route.setColor(color); } if ((line.indexOf("</trkpt>") > -1) | (line.indexOf("</rtept>") > -1) | ((line.indexOf("/>") > -1) & IStrkptORrtept)) { // trkpt abgeschlossen, jetzt kann der Trackpunkt erzeugt werden IStrkptORrtept = false; route.Points.add( new TrackPoint( lastAcceptedCoordinate.getLongitude(), lastAcceptedCoordinate.getLatitude(), lastAcceptedCoordinate.getElevation(), lastAcceptedDirection, lastAcceptedTime)); // Calculate the length of a Track if (!FromPosition.isValid()) { FromPosition = new CoordinateGPS(lastAcceptedCoordinate); FromPosition.setElevation(lastAcceptedCoordinate.getElevation()); FromPosition.setValid(true); } else { MathUtils.computeDistanceAndBearing( CalculationType.ACCURATE, FromPosition.getLatitude(), FromPosition.getLongitude(), lastAcceptedCoordinate.getLatitude(), lastAcceptedCoordinate.getLongitude(), dist); Distance += dist[0]; DeltaAltitude = Math.abs(FromPosition.getElevation() - lastAcceptedCoordinate.getElevation()); FromPosition = new CoordinateGPS(lastAcceptedCoordinate); if (DeltaAltitude >= 25.0) // nur aufaddieren wenn Höhenunterschied größer 10 Meter { FromPosition.setElevation(lastAcceptedCoordinate.getElevation()); AltitudeDifference = AltitudeDifference + DeltaAltitude; } } } } } } reader.close(); return route; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } }