Esempio n. 1
0
  /** Loads track segments of a GPX file, and returns them as a single line marker. */
  public static List<Feature> loadData(PApplet p, String gpxFilename) {
    List<Feature> trackFeatures = new ArrayList<Feature>();

    // Load GPX file
    XMLElement gpx = new XMLElement(p, gpxFilename);

    // Create track with all track points
    ShapeFeature trackFeature = new ShapeFeature(FeatureType.LINES);

    XMLElement[] itemXMLElements = gpx.getChildren("trk/trkseg/trkpt");
    for (int i = 0; i < itemXMLElements.length; i++) {
      // Adds location for track point
      float lat = itemXMLElements[i].getFloat("lat");
      float lon = itemXMLElements[i].getFloat("lon");
      Location location = new Location(lat, lon);
      trackFeature.addLocation(location);
    }

    // Add time as property
    XMLElement timeXMLElement = gpx.getChild("trk/time");
    trackFeature.addProperty("time", timeXMLElement.getContent());

    trackFeatures.add(trackFeature);
    return trackFeatures;
  }
Esempio n. 2
0
  /**
   * Loads track segments of a GPX file, and returns them as a lines marker. Additionally, the speed
   * is calculated between two points, and stored as property.
   *
   * <p>This varies from {@link GPXReader#loadData(PApplet, String)}.
   */
  public static List<Feature> loadData(PApplet p, String gpxFilename) {
    List<Feature> trackFeatures = new ArrayList<Feature>();

    // Load GPX file
    XMLElement gpx = new XMLElement(p, gpxFilename);

    Calendar prevTime = null;
    Location prevLocation = null;

    // Create track with all track points
    ShapeFeature trackFeature = new ShapeFeature(FeatureType.LINES);
    List<Double> speedList = new ArrayList<Double>();

    XMLElement[] itemXMLElements = gpx.getChildren("trk/trkseg/trkpt");
    for (int i = 0; i < itemXMLElements.length; i++) {

      // Adds location for track point
      float lat = itemXMLElements[i].getFloat("lat");
      float lon = itemXMLElements[i].getFloat("lon");
      Location location = new Location(lat, lon);

      // Calculates speed for track point
      // Uses time span (h) and distance (km) to previous point to get km/h
      double speed = 0;
      try {
        String timeStr = itemXMLElements[i].getChild("time").getContent();
        // Replace "Z" for Zulu/GMT time with parseable hour offset
        timeStr = timeStr.replaceAll("Z", "+0000");
        Calendar time = StringUtils.parseIsoDateTime(timeStr);

        if (prevTime != null) {
          long timeMS = time.getTimeInMillis();
          long timeDiff = timeMS - prevTime.getTimeInMillis();
          double dist = GeoUtils.getDistance(location, prevLocation);
          speed = dist / ((float) timeDiff / 1000 / 60 / 60);
        }

        prevTime = time;
        prevLocation = location;

      } catch (ParseException e) {
        // println("Error:" + e);
      }

      speedList.add(speed);
      trackFeature.addLocation(location);
    }
    trackFeature.putProperty("speedList", speedList);
    trackFeatures.add(trackFeature);

    return trackFeatures;
  }