use of com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment in project TrekAdvisor by peterLaurence.
the class GPXTest method writeTest.
/**
* Tests the gpx writer against the gpx parser : parse an existing gpx file, the use the gpx
* writer to write a gpx file somewhere in a temp folder, then use the gpx parser again to parse
* the resulting file. <br>
* The resulting file should have identical values (at least for tags that the writer supports).
*/
@Test
public void writeTest() {
try {
/* First read an existing gpx file */
Gpx gpxInput = null;
try {
gpxInput = GPXParser.parse(new FileInputStream(referenceGpxFile));
} catch (Exception e) {
e.printStackTrace();
fail();
}
/* Write it in a temporary folder */
File testFile = mTestFolder.newFile();
FileOutputStream fos = new FileOutputStream(testFile);
GPXWriter.write(gpxInput, fos);
/* Now read it back */
Gpx gpx = GPXParser.parse(new FileInputStream(testFile));
List<Track> trackList = gpx.getTracks();
assertEquals(1, trackList.size());
Track track = trackList.get(0);
List<TrackSegment> trackSegmentList = track.getTrackSegments();
assertEquals("Example track", track.getName());
assertEquals(1, trackSegmentList.size());
TrackSegment trackSegment = trackSegmentList.get(0);
List<TrackPoint> trackPointList = trackSegment.getTrackPoints();
assertEquals(7, trackPointList.size());
TrackPoint firstTrackPoint = trackPointList.get(0);
Double lat = firstTrackPoint.getLatitude();
Double lon = firstTrackPoint.getLongitude();
Double elevation = firstTrackPoint.getElevation();
assertEquals(46.57608333, lat);
assertEquals(8.89241667, lon);
assertEquals(2376.0, elevation);
assertEquals(firstTrackPoint.getTime(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH).parse("2007-10-14T10:09:57Z"));
} catch (IOException | ParserConfigurationException | TransformerException | ParseException | XmlPullParserException e) {
e.printStackTrace();
fail();
}
}
use of com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment in project TrekAdvisor by peterLaurence.
the class GPXTest method simpleFileTest.
@Test
public void simpleFileTest() {
if (mGpxFilesDirectory != null) {
if (referenceGpxFile.exists()) {
try {
Gpx gpx = GPXParser.parse(new FileInputStream(referenceGpxFile));
List<Track> trackList = gpx.getTracks();
assertEquals(1, trackList.size());
Track track = trackList.get(0);
List<TrackSegment> trackSegmentList = track.getTrackSegments();
assertEquals("Example track", track.getName());
assertEquals(1, trackSegmentList.size());
TrackSegment trackSegment = trackSegmentList.get(0);
List<TrackPoint> trackPointList = trackSegment.getTrackPoints();
assertEquals(7, trackPointList.size());
TrackPoint firstTrackPoint = trackPointList.get(0);
Double lat = firstTrackPoint.getLatitude();
Double lon = firstTrackPoint.getLongitude();
Double elevation = firstTrackPoint.getElevation();
assertEquals(46.57608333, lat);
assertEquals(8.89241667, lon);
assertEquals(2376.0, elevation);
assertEquals(firstTrackPoint.getTime(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH).parse("2007-10-14T10:09:57Z"));
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}
}
use of com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment in project TrekAdvisor by peterLaurence.
the class LocationService method createGpx.
/**
* When we stop recording the location events, create a {@link Gpx} object for further
* serialization. <br>
* Whatever the outcome of this process, a {@link GpxFileWriteEvent} is emitted in the
* LocationServiceThread.
*/
private void createGpx() {
mServiceHandler.post(() -> {
TrackSegment.Builder trkSegBuilder = new TrackSegment.Builder();
trkSegBuilder.setTrackPoints(mTrackPoints);
ArrayList<TrackSegment> trkSegList = new ArrayList<>();
trkSegList.add(trkSegBuilder.build());
Track.Builder trackBuilder = new Track.Builder().setName(TrekAdvisorContext.APP_FOLDER_NAME + " track");
trackBuilder.setTrackSegments(trkSegList);
ArrayList<Track> trkList = new ArrayList<>();
trkList.add(trackBuilder.build());
Gpx.Builder gpxBuilder = new Gpx.Builder();
gpxBuilder.setCreator(TrekAdvisorContext.APP_FOLDER_NAME);
gpxBuilder.setVersion(GPX_VERSION);
gpxBuilder.setTracks(trkList);
Gpx gpx = gpxBuilder.build();
try {
if (!TrekAdvisorContext.DEFAULT_RECORDINGS_DIR.exists()) {
TrekAdvisorContext.DEFAULT_RECORDINGS_DIR.mkdir();
}
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd\\MM\\yyyy-HH:mm:ss", Locale.ENGLISH);
String gpxFileName = "track-" + dateFormat.format(date) + ".gpx";
File gpxFile = new File(TrekAdvisorContext.DEFAULT_RECORDINGS_DIR, gpxFileName);
FileOutputStream fos = new FileOutputStream(gpxFile);
GPXWriter.write(gpx, fos);
} catch (Exception e) {
// for instance, don't care : we want to stop the service anyway
// TODO : warn the user that the gpx file could not be saved
} finally {
EventBus.getDefault().post(new GpxFileWriteEvent());
}
});
}
use of com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment in project TrekAdvisor by peterLaurence.
the class GPXWriter method addTrackToNode.
private static void addTrackToNode(Track trk, Node n, Document doc) {
Node trkNode = doc.createElement(TAG_TRACK);
if (trk.getName() != null) {
Node node = doc.createElement(TAG_NAME);
node.appendChild(doc.createTextNode(trk.getName()));
trkNode.appendChild(node);
}
if (trk.getTrackSegments() != null) {
for (TrackSegment ts : trk.getTrackSegments()) {
addTrackSegmentToNode(ts, trkNode, doc);
}
}
n.appendChild(trkNode);
}
Aggregations