spatial-k

Project Url: dellisd/spatial-k
Introduction: Spatial K - A set of Kotlin Multiplatform Libraries for working with geospatial data
More: Author   ReportBugs   OfficialWebsite   
Tags:

Maven Central Version Snapshot Version License Kotlin Version Documentation API Reference Slack

Introduction

Spatial K is a set of libraries for working with geospatial data in Kotlin.

It includes:

  • GeoJSON implementation and DSL for building GeoJSON objects
  • Port of Turf.js geospatial analysis functions in pure Kotlin
  • Library for working with units of measure
  • GPX implementation
  • PMTiles v3 archive reader
  • Google Encoded Polyline Algorithm support

Spatial K supports Kotlin Multiplatform and Java projects.

Getting Started

Add Spatial K to your project:

dependencies {
    implementation("org.maplibre.spatialk:geojson:VERSION")
    implementation("org.maplibre.spatialk:turf:VERSION")
    implementation("org.maplibre.spatialk:units:VERSION")
    implementation("org.maplibre.spatialk:gpx:VERSION")
    implementation("org.maplibre.spatialk:pmtiles:VERSION")
    implementation("org.maplibre.spatialk:polyline-encoding:VERSION")
}

GeoJSON

val sf = buildFeature {
    geometry = Point(longitude = -122.4, latitude = 37.8)
    properties = buildJsonObject {
        put("name", "San Francisco")
    }
}

val json: String = sf.toJson()

Turf

val from = Position(longitude = -122.4, latitude = 37.8)
val to = Position(longitude = -74.0, latitude = 40.7)
val distance = distance(from, to)
val bearing = from.bearingTo(to)

Units

val distance = 5.kilometers
val inMeters = distance.inMeters     // 5000.0
val formatted = distance.toString()  // "5000 m"

GPX

val document = Document(
    metadata = Metadata(name = "My GPX File"),
    waypoints = listOf(
        Waypoint(latitude = 1.0, longitude = 2.0),
        Waypoint(latitude = 3.0, longitude = 4.0),
    ),
)
val gpxString = Gpx.encodeToString(document)

PMTiles

// Inside a suspend function or coroutine scope:
PmTiles.open(source).use { archive ->
    val header = archive.header
    val tile = archive.readDecompressedTile(z = 0, x = 0, y = 0)
}

Polyline Encoding

val positions = listOf(
    Position(longitude = -120.2, latitude = 38.5),
    Position(longitude = -120.95, latitude = 40.7),
)
val encoded = PolylineEncoding.encode(positions)
val decoded = PolylineEncoding.decode(encoded)

See the project site for more info.

Getting Involved

Join the #maplibre Slack channel at OSMUS (get an invite at https://slack.openstreetmap.us/).

Read the CONTRIBUTING.md guide to get familiar with how we do things around here.

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools
AI Daily Digest