Skip to content

Commit

Permalink
Retroactively update gpx file's statistics.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lr committed Nov 3, 2018
1 parent 8d17100 commit 87a92f1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.peterlaurence.trekadvisor.core.map.gson.MarkerGson
import com.peterlaurence.trekadvisor.core.map.gson.RouteGson
import com.peterlaurence.trekadvisor.util.FileUtils
import com.peterlaurence.trekadvisor.util.gpx.GPXParser
import com.peterlaurence.trekadvisor.util.gpx.GPXWriter
import com.peterlaurence.trekadvisor.util.gpx.model.Gpx
import com.peterlaurence.trekadvisor.util.gpx.model.Track
import com.peterlaurence.trekadvisor.util.gpx.model.TrackSegment
Expand Down Expand Up @@ -67,18 +68,27 @@ object TrackImporter {
* In this call, we consider that each gpx file has already been parsed, and that the
* [recordingsToGpx] Map is up to date. So typically, [getRecordingsToGpxMap] should be called
* first.
* First, we only keep the entries for which the [Gpx] value has no statistics for the first
* track.
* Then, we calculate the statistics for the first track.
* First, we calculate the statistics for the first track.
* If the [GPXParser] read statistics for this track, we check is there is any difference
* (because the statistics calculation is subjected to be adjusted frequently), we update the
* gpx file.
*/
fun computeMissingStatistics(): kotlin.collections.Map<File, Gpx> {
recordingsToGpx.filter { it.value.tracks.firstOrNull()?.statistics == null }.forEach {
fun computeStatistics(): kotlin.collections.Map<File, Gpx> {
recordingsToGpx.forEach {
val statCalculator = TrackStatCalculator()
it.value.tracks.firstOrNull()?.let { track ->
track.trackSegments.forEach { trackSegment ->
statCalculator.addTrackPointList(trackSegment.trackPoints)
}
track.statistics = statCalculator.getStatistics()

val updatedStatistics = statCalculator.getStatistics()
if (track.statistics != null && track.statistics != updatedStatistics) {
/* Track statistics have changed, update the file */
track.statistics = updatedStatistics
val fos = FileOutputStream(it.key)
GPXWriter.write(it.value, fos)
}
track.statistics = updatedStatistics
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,15 @@ class RecordFragment : Fragment(), CoroutineScope {
recordListView.setRecordings(it)
}

/* Recording to Gpx conversion.
* First, read all tracks which already have statistics */
/* Recording to Gpx conversion */
var recordingsToGpx = launch(Dispatchers.Default) {
TrackImporter.getRecordingsToGpxMap()
}
recordingsToGpx.join()

/* Then, ask for the computation of the statistics for the tracks that don't have any */
/* Then, ask for the computation of the statistics for all tracks */
recordingsToGpx = async(Dispatchers.Default) {
TrackImporter.computeMissingStatistics()
TrackImporter.computeStatistics()
}

recordListView.setGpxForRecording(recordingsToGpx.await())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ object GPXWriter {
longNode.nodeValue = trkPt.longitude.toString()
attrs.setNamedItem(longNode)
}
if (trkPt.elevation != 0.0) {
if (trkPt.elevation != null) {
val node = doc.createElement(TAG_ELEVATION)
node.appendChild(doc.createTextNode(trkPt.elevation.toString()))
wptNode.appendChild(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.peterlaurence.trekadvisor.util.gpx.model
*
* @author peterLaurence on 12/02/17.
*/
class TrackPoint(
data class TrackPoint(
var latitude: Double = 0.0,
var longitude: Double = 0.0,
var elevation: Double? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ package com.peterlaurence.trekadvisor.util.gpx.model
*
* @author peterLaurence on 12/02/17.
*/
class TrackSegment(val trackPoints: List<TrackPoint>)
data class TrackSegment(val trackPoints: List<TrackPoint>)

0 comments on commit 87a92f1

Please sign in to comment.