Search in sources :

Example 1 with TrackPoint

use of com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint 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 TrackPoint

use of com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint 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 TrackPoint

use of com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint in project TrekAdvisor by peterLaurence.

the class LocationService method onCreate.

@Override
public void onCreate() {
    EventBus.getDefault().register(this);
    /* Start up the thread running the service.  Note that we create a separate thread because
         * the service normally runs in the process's main thread, which we don't want to block.
         * We also make it background priority so CPU-intensive work will not disrupt our UI.
         */
    HandlerThread thread = new HandlerThread("LocationServiceThread", Thread.MIN_PRIORITY);
    thread.start();
    /* Get the HandlerThread's Looper and use it for our Handler */
    mServiceLooper = thread.getLooper();
    mServiceHandler = new Handler(mServiceLooper);
    mServiceHandler.handleMessage(new Message());
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this.getApplicationContext());
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    /* Create the Gpx instance */
    mTrackPoints = new ArrayList<>();
    mLocationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                mServiceHandler.post(() -> {
                    TrackPoint.Builder pointBuilder = new TrackPoint.Builder();
                    pointBuilder.setLatitude(location.getLatitude());
                    pointBuilder.setLongitude(location.getLongitude());
                    pointBuilder.setElevation(location.getAltitude());
                    TrackPoint trackPoint = pointBuilder.build();
                    mTrackPoints.add(trackPoint);
                });
            }
        }
    };
    startLocationUpdates();
}
Also used : HandlerThread(android.os.HandlerThread) Message(android.os.Message) LocationRequest(com.google.android.gms.location.LocationRequest) LocationCallback(com.google.android.gms.location.LocationCallback) Handler(android.os.Handler) TrackPoint(com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint) LocationResult(com.google.android.gms.location.LocationResult) Location(android.location.Location)

Example 4 with TrackPoint

use of com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint in project TrekAdvisor by peterLaurence.

the class GPXWriter method addTrackSegmentToNode.

private static void addTrackSegmentToNode(TrackSegment ts, Node n, Document doc) {
    Node tsNode = doc.createElement(TAG_SEGMENT);
    for (TrackPoint trkpt : ts.getTrackPoints()) {
        addWaypointToNode(TAG_POINT, trkpt, tsNode, doc);
    }
    n.appendChild(tsNode);
}
Also used : Node(org.w3c.dom.Node) TrackPoint(com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint)

Aggregations

TrackPoint (com.peterlaurence.trekadvisor.util.gpx.model.TrackPoint)4 Gpx (com.peterlaurence.trekadvisor.util.gpx.model.Gpx)2 Track (com.peterlaurence.trekadvisor.util.gpx.model.Track)2 TrackSegment (com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 Test (org.junit.Test)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 Location (android.location.Location)1 Handler (android.os.Handler)1 HandlerThread (android.os.HandlerThread)1 Message (android.os.Message)1 LocationCallback (com.google.android.gms.location.LocationCallback)1 LocationRequest (com.google.android.gms.location.LocationRequest)1 LocationResult (com.google.android.gms.location.LocationResult)1 File (java.io.File)1