Skip to content

Commit

Permalink
Fix encode Polyline function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabi755 committed Dec 19, 2024
1 parent b41c01e commit 38fe8cd
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.maplibre.navigation.geo.util
import org.maplibre.navigation.geo.Point
import kotlin.math.pow
import kotlin.math.round
import kotlin.math.roundToLong


object PolylineUtils {
Expand All @@ -22,11 +23,11 @@ object PolylineUtils {
val result = StringBuilder()

// OSRM uses precision=6, the default Polyline spec divides by 1E5, capping at precision=5
val factor: Double = 10.0.pow(precision.toDouble())
val factor: Double = (10.0).pow(precision)

for (point in path) {
val lat: Long = round(point.latitude * factor).toLong()
val lng: Long = round(point.longitude * factor).toLong()
val lat: Long = (point.latitude * factor).roundToLong()
val lng: Long = (point.longitude * factor).roundToLong()

val varLat = lat - lastLat
val varLng = lng - lastLng
Expand All @@ -41,15 +42,16 @@ object PolylineUtils {
}

private fun encode(variable: Long, result: StringBuilder) {
var value = variable
value = if (value < 0) (value shl 1).inv() else value shl 1
while (value >= 0x20) {
result.append(Char(((0x20 or ((variable and 0x1f).toInt())) + 63)))
value = value shr 5
var encoded = variable
encoded = if (encoded < 0) (encoded shl 1).inv() else encoded shl 1
while (encoded >= 0x20) {
result.append(((0x20L or (encoded and 0x1fL)) + 63).toInt().toChar())
encoded = encoded shr 5
}
result.append(Char((value + 63).toInt()))
result.append((encoded + 63).toInt().toChar())
}


/**
* Decodes an encoded path string into a sequence of [Point].
*
Expand Down

0 comments on commit 38fe8cd

Please sign in to comment.