mirror of
https://github.com/maplibre/spatial-k.git
synced 2026-10-14 23:34:47 +00:00
Spatial K - A set of Kotlin Multiplatform Libraries for working with geospatial data
https://maplibre.org/spatial-k/
- Kotlin 95.4%
- Swift 2.8%
- Java 1.4%
- Shell 0.3%
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> |
||
|---|---|---|
| .github | ||
| .vscode | ||
| .zed | ||
| benchmark | ||
| buildSrc | ||
| docs | ||
| geohash | ||
| geojson | ||
| gpx | ||
| gradle | ||
| kotlin-js-store | ||
| mise-tasks/publish | ||
| pmtiles | ||
| polyline-encoding | ||
| testutil | ||
| tools/units-generator | ||
| turf | ||
| units | ||
| .coderabbit.yml | ||
| .gitattributes | ||
| .gitignore | ||
| AGENTS.md | ||
| AI_POLICY.md | ||
| BENCHMARK.md | ||
| build.gradle.kts | ||
| CLAUDE.md | ||
| CODE-OF-CONDUCT.md | ||
| CONTRIBUTING.md | ||
| detekt.yml | ||
| dprint.json | ||
| gradle.properties | ||
| gradlew | ||
| gradlew.bat | ||
| hk.pkl | ||
| LICENSE | ||
| mise.lock | ||
| mise.toml | ||
| package.json | ||
| pnpm-lock.yaml | ||
| pnpm-workspace.yaml | ||
| README.md | ||
| SECURITY.md | ||
| settings.gradle.kts | ||
Spatial K
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
- Geohash cells and OpenStreetMap shortlinks
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")
implementation("org.maplibre.spatialk:geohash: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.