Skip to content

Kotlin Multiplatform implementation for Google Polyline encoding algorithm to convert Polyline to/from String. And sampling polyline using Ramer–Douglas–Peucker algorithm.

License

Notifications You must be signed in to change notification settings

brudaswen/kotlin-polyline

Repository files navigation

Kotlin Multiplatform Polyline Encoding

Maven Central Snapshot CI Status Codecov License

Kotlin Multiplatform implementation for Google Polyline encoding algorithm to convert Polyline to/from String.

Gradle Dependencies

[versions]
kotlin-polyline = "1.0.0"

[libraries]
kotlin-polyline = { group = "de.brudaswen.kotlin.polyline", name = "kotlin-polyline", version.ref = "kotlin-polyline" }
// From Gradle version catalog
implementation(libs.kotlin.polyline)

// Manually
implementation("de.brudaswen.kotlin.polyline:kotlin-polyline:1.0.0")

Usage

The library provides default implementations of Polyline and Coordinate used for encoding and decoding.

val polyline = Polyline(
    coordinates = listOf(
        Coordinate(0.0, 0.0),
        Coordinate(1.0, 1.0),
    ),
)

val encoded = PolylineEncoding.encode(polyline)
val decoded = PolylineEncoding.decode(encoded)

Polyline Sampling

The Polyline can be sampled to reduce its size using a simple Distance Threshold algorithm and the Ramer–Douglas–Peucker algorithm.

// Sample using default distance threshold of 1 meter and Ramer–Douglas–Peucker epsilon of `1.0`
val sampled = polyline.sample()

// Set custom distance threshold and Ramer–Douglas–Peucker epsilon
val sampled = polyline.sample(
    thresholdInMeters = 5.0,
    epsilon = 2.0,
)

// Use Samplers manually
val sampled = DistanceThresholdSampler(thresholdInMeters = 5.0).sample(polyline)
val sampled = RamerDouglasPeuckerSampler(epsilon = 2.0).sample(polyline)

Custom Polyline or Coordinate implementation

Existing classes could implement the Polyline and/or Coordinate interface to directly encode and decode from your existing classes.

data class CustomPolyline(
    val points: List<CustomCoordinate>,
) : Polyline {
    override val coordinates: List<CustomCoordinate>
        get() = points
}

data class CustomCoordinate(
    val latitude: Float,
    val longitude: Float,
) : Coordinate {
    override val lat: Double
        get() = latitude.toDouble()

    override val lon: Double
        get() = longitude.toDouble()
}


val polyline = CustomPolyline(
    points = listOf(
        CustomCoordinate(0.0f, 0.0f),
        CustomCoordinate(1.0f, 1.0f),
    ),
)

val encoded = PolylineEncoding.encode(polyline)
val decoded = PolylineEncoding.decode(
    polyline = encoded,
    polylineFactory = { coordinates ->
        CustomPolyline(
            points = coordinates.toList(),
        )
    },
    coordinateFactory = { lat, lon ->
        CustomCoordinate(
            latitude = lat.toFloat(),
            longitude = lon.toFloat(),
        )
    },
)

License

MIT License

Copyright (c) 2025

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Kotlin Multiplatform implementation for Google Polyline encoding algorithm to convert Polyline to/from String. And sampling polyline using Ramer–Douglas–Peucker algorithm.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages