Skip to content

Commit

Permalink
Add overpass turbo GPX support
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Oct 20, 2024
1 parent d402280 commit 5cb1bb0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
extra.apply {
set("groupId", "com.kylecorry.andromeda")
set("versionName", "10.4.0")
set("versionName", "10.4.1")
set("targetVersion", 35)
set("compileVersion", 35)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class GPXParserTest {
3.4f,
description = "A test comment",
time = Instant.parse("2016-06-17T23:41:03Z"),
group ="Test Group"
group = "Test Group"
),
GPXWaypoint(Coordinate(31.0, 100.0), "Beacon 2", null, null, null, null),
GPXWaypoint(
Coordinate(32.0, 101.0), "Beacon 3", 1000.1f, description = "ele=1000.1\n" +
"gnis:feature_id=871441\n" +
"name=Beacon 3\n" +
"natural=peak", time = null, group = null
),
),
data.waypoints
)
Expand Down Expand Up @@ -64,7 +70,7 @@ class GPXParserTest {
null,
-3.14f,
null,
time = Instant.parse("2016-06-17T23:41:03Z")
time = Instant.parse("2016-06-17T23:41:03Z")
),
GPXWaypoint(
Coordinate(32.0, 10.0),
Expand All @@ -82,7 +88,7 @@ class GPXParserTest {
null,
1f,
null,
time = Instant.parse("2017-06-17T23:41:03Z")
time = Instant.parse("2017-06-17T23:41:03Z")
),
GPXWaypoint(
Coordinate(31.0, 11.0),
Expand Down Expand Up @@ -176,7 +182,10 @@ class GPXParserTest {
),
GPXWaypoint(Coordinate(31.0, 100.0), "Beacon 2", null, null, null, null),
)
assertEquals(xml, GPXParser.toGPX(GPXData(waypoints, emptyList(), emptyList()), "Trail Sense"))
assertEquals(
xml,
GPXParser.toGPX(GPXData(waypoints, emptyList(), emptyList()), "Trail Sense")
)
}

@Test
Expand Down Expand Up @@ -310,6 +319,13 @@ class GPXParserTest {
<wpt lat="31" lon="100">
<name>Beacon 2</name>
</wpt>
<wpt lat="32" lon="101">
<name>Beacon 3</name>
<desc>ele=1000.1
gnis:feature_id=871441
name=Beacon 3
natural=peak</desc>
</wpt>
</gpx>"""

private val gpx = """<?xml version="1.0"?>
Expand Down
34 changes: 28 additions & 6 deletions gpx/src/main/java/com/kylecorry/andromeda/gpx/GPXParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.kylecorry.andromeda.core.toFloatCompat
import com.kylecorry.andromeda.core.toLongCompat
import com.kylecorry.andromeda.core.tryOrDefault
import com.kylecorry.andromeda.core.ui.Colors
import com.kylecorry.sol.units.Coordinate
import com.kylecorry.andromeda.xml.XMLConvert
import com.kylecorry.andromeda.xml.XMLNode
import com.kylecorry.sol.units.Coordinate
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.OutputStream
Expand Down Expand Up @@ -105,9 +105,11 @@ object GPXParser {
val type = node.children.firstOrNull { it.tag.lowercase() == "type" }?.text
val extensions = node.children.firstOrNull { it.tag.lowercase() == "extensions" }
val colorHex = extensions?.children?.firstOrNull { it.tag.lowercase() == "color" }?.text
val colorInt = tryOrDefault(null){ colorHex?.let { Color.parseColor(it) } }
val lineStyle = extensions?.children?.firstOrNull { it.tag.lowercase() == "trailsense:linestyle" }?.text
val group = extensions?.children?.firstOrNull { it.tag.lowercase() == "trailsense:group" }?.text
val colorInt = tryOrDefault(null) { colorHex?.let { Color.parseColor(it) } }
val lineStyle =
extensions?.children?.firstOrNull { it.tag.lowercase() == "trailsense:linestyle" }?.text
val group =
extensions?.children?.firstOrNull { it.tag.lowercase() == "trailsense:group" }?.text

return GPXTrack(name, type, id, description, comment, colorInt, lineStyle, group, segments)
}
Expand Down Expand Up @@ -153,14 +155,23 @@ object GPXParser {
val comment = node.children.firstOrNull { it.tag.lowercase() == "cmt" }?.text
val type = node.children.firstOrNull { it.tag.lowercase() == "type" }?.text
val time = node.children.firstOrNull { it.tag.lowercase() == "time" }?.text
val ele = node.children.firstOrNull { it.tag.lowercase() == "ele" }?.text?.toFloatCompat()
var ele = node.children.firstOrNull { it.tag.lowercase() == "ele" }?.text?.toFloatCompat()
val symbol = node.children.firstOrNull { it.tag.lowercase() == "sym" }?.text
val extensions = node.children.firstOrNull { it.tag.lowercase() == "extensions" }
val group =
extensions?.children?.firstOrNull { it.tag.lowercase() == "trailsense:group" }?.text
val colorHex = extensions?.children?.firstOrNull { it.tag.lowercase() == "color" }?.text
val colorInt = tryOrDefault(null) { colorHex?.let { Color.parseColor(it) } }

if (ele == null && desc != null) {
// Try to parse the elevation from the description
val elevationRegex = Regex("ele=([0-9.]+)")
val elevationMatch = elevationRegex.find(desc)
if (elevationMatch != null) {
ele = elevationMatch.groupValues[1].toFloatCompat()
}
}

if (lat == null || lon == null) {
return null
}
Expand All @@ -175,7 +186,18 @@ object GPXParser {
null
}

return GPXWaypoint(Coordinate(lat, lon), name, ele, type, desc, comment, instant, group, symbol, colorInt)
return GPXWaypoint(
Coordinate(lat, lon),
name,
ele,
type,
desc,
comment,
instant,
group,
symbol,
colorInt
)
}

private fun toXML(waypoint: GPXWaypoint, tag: String): XMLNode {
Expand Down

0 comments on commit 5cb1bb0

Please sign in to comment.