Search in sources :

Example 1 with Gpx

use of com.peterlaurence.trekadvisor.util.gpx.model.Gpx 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();
    }
}
Also used : IOException(java.io.IOException) Gpx(com.peterlaurence.trekadvisor.util.gpx.model.Gpx) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) ParseException(java.text.ParseException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TrackSegment(com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment) FileOutputStream(java.io.FileOutputStream) TrackPoint(com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParseException(java.text.ParseException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Track(com.peterlaurence.trekadvisor.util.gpx.model.Track) TransformerException(javax.xml.transform.TransformerException) Test(org.junit.Test)

Example 2 with Gpx

use of com.peterlaurence.trekadvisor.util.gpx.model.Gpx 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();
            }
        }
    }
}
Also used : TrackPoint(com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint) SimpleDateFormat(java.text.SimpleDateFormat) Gpx(com.peterlaurence.trekadvisor.util.gpx.model.Gpx) FileInputStream(java.io.FileInputStream) Track(com.peterlaurence.trekadvisor.util.gpx.model.Track) TransformerException(javax.xml.transform.TransformerException) ParseException(java.text.ParseException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TrackSegment(com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment) Test(org.junit.Test)

Example 3 with Gpx

use of com.peterlaurence.trekadvisor.util.gpx.model.Gpx 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());
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Gpx(com.peterlaurence.trekadvisor.util.gpx.model.Gpx) Date(java.util.Date) TrackSegment(com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment) GpxFileWriteEvent(com.peterlaurence.trekadvisor.service.event.GpxFileWriteEvent) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Track(com.peterlaurence.trekadvisor.util.gpx.model.Track)

Aggregations

Gpx (com.peterlaurence.trekadvisor.util.gpx.model.Gpx)3 Track (com.peterlaurence.trekadvisor.util.gpx.model.Track)3 TrackSegment (com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment)3 SimpleDateFormat (java.text.SimpleDateFormat)3 TrackPoint (com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 Test (org.junit.Test)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 GpxFileWriteEvent (com.peterlaurence.trekadvisor.service.event.GpxFileWriteEvent)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1