Simplify createGpxString with appendLine and string interpolation.
This commit is contained in:
parent
c3922240e0
commit
74cfbaedea
1 changed files with 31 additions and 101 deletions
|
@ -109,118 +109,48 @@ object TrackHelper {
|
||||||
|
|
||||||
/* Creates GPX string for given track */
|
/* Creates GPX string for given track */
|
||||||
fun createGpxString(track: Track): String {
|
fun createGpxString(track: Track): String {
|
||||||
var gpxString: String
|
val gpxString = StringBuilder("")
|
||||||
|
|
||||||
// add header
|
// Header
|
||||||
gpxString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n" +
|
gpxString.appendLine("""
|
||||||
"<gpx version=\"1.1\" creator=\"Trackbook App (Android)\"\n" +
|
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
|
||||||
" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" +
|
<gpx
|
||||||
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
|
version="1.1" creator="Trackbook App (Android)"
|
||||||
" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n"
|
xmlns="http://www.topografix.com/GPX/1/1"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
|
||||||
|
>
|
||||||
|
""".trimIndent())
|
||||||
|
gpxString.appendLine("\t<metadata>")
|
||||||
|
gpxString.appendLine("\t\t<name>Trackbook Recording: ${track.name}</name>")
|
||||||
|
gpxString.appendLine("\t</metadata>")
|
||||||
|
|
||||||
// add name
|
// POIs
|
||||||
gpxString += createGpxName(track)
|
|
||||||
|
|
||||||
// add POIs
|
|
||||||
gpxString += createGpxPois(track)
|
|
||||||
|
|
||||||
// add track
|
|
||||||
gpxString += createGpxTrk(track)
|
|
||||||
|
|
||||||
// add closing tag
|
|
||||||
gpxString += "</gpx>\n"
|
|
||||||
|
|
||||||
return gpxString
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Creates name for GPX file */
|
|
||||||
private fun createGpxName(track: Track): String {
|
|
||||||
val gpxName = StringBuilder("")
|
|
||||||
gpxName.append("\t<metadata>\n")
|
|
||||||
gpxName.append("\t\t<name>")
|
|
||||||
gpxName.append("Trackbook Recording: ${track.name}")
|
|
||||||
gpxName.append("</name>\n")
|
|
||||||
gpxName.append("\t</metadata>\n")
|
|
||||||
return gpxName.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Creates GPX formatted points of interest */
|
|
||||||
private fun createGpxPois(track: Track): String {
|
|
||||||
val gpxPois = StringBuilder("")
|
|
||||||
val poiList: List<WayPoint> = track.wayPoints.filter { it.starred }
|
val poiList: List<WayPoint> = track.wayPoints.filter { it.starred }
|
||||||
poiList.forEach { poi ->
|
poiList.forEach { poi ->
|
||||||
gpxPois.append("\t<wpt lat=\"")
|
gpxString.appendLine("\t<wpt lat=\"${poi.latitude}\" lon=\"${poi.longitude}\">")
|
||||||
gpxPois.append(poi.latitude)
|
gpxString.appendLine("\t\t<name>Point of interest</name>")
|
||||||
gpxPois.append("\" lon=\"")
|
gpxString.appendLine("\t\t<ele>${poi.altitude}</ele>")
|
||||||
gpxPois.append(poi.longitude)
|
gpxString.appendLine("\t</wpt>")
|
||||||
gpxPois.append("\">\n")
|
|
||||||
|
|
||||||
// add name to waypoint
|
|
||||||
gpxPois.append("\t\t<name>")
|
|
||||||
gpxPois.append("Point of interest")
|
|
||||||
gpxPois.append("</name>\n")
|
|
||||||
|
|
||||||
// add altitude
|
|
||||||
gpxPois.append("\t\t<ele>")
|
|
||||||
gpxPois.append(poi.altitude)
|
|
||||||
gpxPois.append("</ele>\n")
|
|
||||||
|
|
||||||
// add closing tag
|
|
||||||
gpxPois.append("\t</wpt>\n")
|
|
||||||
}
|
}
|
||||||
return gpxPois.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// TRK
|
||||||
/* Creates GPX formatted track */
|
|
||||||
private fun createGpxTrk(track: Track): String {
|
|
||||||
val gpxTrack = StringBuilder("")
|
|
||||||
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
|
||||||
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
|
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
|
||||||
|
gpxString.appendLine("\t<trk>")
|
||||||
// add opening track tag
|
gpxString.appendLine("\t\t<name>${track.name}</name>")
|
||||||
gpxTrack.append("\t<trk>\n")
|
gpxString.appendLine("\t\t<trkseg>")
|
||||||
|
|
||||||
// add name to track
|
|
||||||
gpxTrack.append("\t\t<name>")
|
|
||||||
gpxTrack.append("Track")
|
|
||||||
gpxTrack.append("</name>\n")
|
|
||||||
|
|
||||||
// add opening track segment tag
|
|
||||||
gpxTrack.append("\t\t<trkseg>\n")
|
|
||||||
|
|
||||||
// add route point
|
|
||||||
track.wayPoints.forEach { wayPoint ->
|
track.wayPoints.forEach { wayPoint ->
|
||||||
// add longitude and latitude
|
gpxString.appendLine("\t\t\t<trkpt lat=\"${wayPoint.latitude}\" lon=\"${wayPoint.longitude}\">")
|
||||||
gpxTrack.append("\t\t\t<trkpt lat=\"")
|
gpxString.appendLine("\t\t\t\t<ele>${wayPoint.altitude}</ele>")
|
||||||
gpxTrack.append(wayPoint.latitude)
|
gpxString.appendLine("\t\t\t\t<time>${dateFormat.format(Date(wayPoint.time))}</time>")
|
||||||
gpxTrack.append("\" lon=\"")
|
gpxString.appendLine("\t\t\t</trkpt>")
|
||||||
gpxTrack.append(wayPoint.longitude)
|
|
||||||
gpxTrack.append("\">\n")
|
|
||||||
|
|
||||||
// add altitude
|
|
||||||
gpxTrack.append("\t\t\t\t<ele>")
|
|
||||||
gpxTrack.append(wayPoint.altitude)
|
|
||||||
gpxTrack.append("</ele>\n")
|
|
||||||
|
|
||||||
// add time
|
|
||||||
gpxTrack.append("\t\t\t\t<time>")
|
|
||||||
gpxTrack.append(dateFormat.format(Date(wayPoint.time)))
|
|
||||||
gpxTrack.append("</time>\n")
|
|
||||||
|
|
||||||
// add closing tag
|
|
||||||
gpxTrack.append("\t\t\t</trkpt>\n")
|
|
||||||
}
|
}
|
||||||
|
gpxString.appendLine("\t\t</trkseg>")
|
||||||
|
gpxString.appendLine("\t</trk>")
|
||||||
|
gpxString.appendLine("</gpx>")
|
||||||
|
|
||||||
// add closing track segment tag
|
return gpxString.toString()
|
||||||
gpxTrack.append("\t\t</trkseg>\n")
|
|
||||||
|
|
||||||
// add closing track tag
|
|
||||||
gpxTrack.append("\t</trk>\n")
|
|
||||||
|
|
||||||
return gpxTrack.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Toggles starred flag for given position */
|
/* Toggles starred flag for given position */
|
||||||
|
|
Loading…
Reference in a new issue