Skip to content

Commit

Permalink
Create path serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Sep 4, 2023
1 parent 1947ef7 commit d9ccb08
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.kylecorry.trail_sense.navigation.paths.domain

import androidx.annotation.ColorInt
import com.kylecorry.trail_sense.shared.colors.AppColor

data class PathStyle(
val line: LineStyle,
val point: PathPointColoringStyle,
@ColorInt val color: Int,
val visible: Boolean
)
){
companion object {
fun default(): PathStyle {
return PathStyle(
LineStyle.Dotted,
PathPointColoringStyle.None,
AppColor.Gray.color,
true
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.kylecorry.trail_sense.navigation.paths.infrastructure.serialization

import com.kylecorry.andromeda.core.io.ISerializer
import com.kylecorry.andromeda.gpx.GPXData
import com.kylecorry.andromeda.gpx.GPXRoute
import com.kylecorry.andromeda.gpx.GPXSerializer
import com.kylecorry.andromeda.gpx.GPXTrack
import com.kylecorry.andromeda.gpx.GPXTrackSegment
import com.kylecorry.andromeda.gpx.GPXWaypoint
import com.kylecorry.trail_sense.navigation.paths.domain.FullPath
import com.kylecorry.trail_sense.navigation.paths.domain.Path
import com.kylecorry.trail_sense.navigation.paths.domain.PathMetadata
import com.kylecorry.trail_sense.navigation.paths.domain.PathPoint
import com.kylecorry.trail_sense.navigation.paths.domain.PathStyle
import java.io.InputStream
import java.io.OutputStream

class PathSerializer(private val defaultImportStyle: PathStyle = PathStyle.default()) :
ISerializer<List<FullPath>> {

private val gpxSerializer = GPXSerializer("Trail Sense")

override fun deserialize(stream: InputStream): List<FullPath> {
val gpx = gpxSerializer.deserialize(stream)
val paths = mutableListOf<FullPath>()

gpx.tracks.forEach {
paths.add(trackToPath(it))
}

gpx.routes.forEach {
paths.add(routeToPath(it))
}

return paths
}

override fun serialize(paths: List<FullPath>, stream: OutputStream) {
val tracks = paths.map { (path, points, parent) ->
val waypoints = points.map {
GPXWaypoint(it.coordinate, elevation = it.elevation, time = it.time)
}
val pathId = points.firstOrNull()?.pathId ?: 0

val trackSegment = GPXTrackSegment(waypoints)
GPXTrack(
path.name, id = pathId, segments = listOf(trackSegment), group = parent?.name
)
}

val gpx = GPXData(emptyList(), tracks, emptyList())

gpxSerializer.serialize(gpx, stream)
}

private fun routeToPath(route: GPXRoute): FullPath {
return FullPath(Path(0, route.name, defaultImportStyle, PathMetadata.empty),
route.points.map {
PathPoint(0, 0, it.coordinate, it.elevation, it.time)
})
}

private fun trackToPath(track: GPXTrack): FullPath {
val points = mutableListOf<PathPoint>()
for (segment in track.segments) {
points.addAll(segment.points.map {
PathPoint(0, 0, it.coordinate, it.elevation, it.time)
})
}
return FullPath(
Path(0, track.name, defaultImportStyle, PathMetadata.empty), points
)
}
}

0 comments on commit d9ccb08

Please sign in to comment.