-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from jillesvangurp/geojsonio-support-and-impro…
…ve-geo-transformations geojsonio support and improve geo transformations
- Loading branch information
Showing
14 changed files
with
890 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/commonMain/kotlin/com/jillesvangurp/geojson/json-helpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.jillesvangurp.geojson | ||
|
||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
val DEFAULT_JSON: Json by lazy { | ||
Json { | ||
// don't rely on external systems being written in kotlin or even having a language with default values | ||
// the default of false is FFing insane and dangerous | ||
encodeDefaults = true | ||
// save space | ||
prettyPrint = false | ||
// people adding shit to the json is OK, we're forward compatible and will just ignore it | ||
isLenient = true | ||
// encoding nulls is meaningless and a waste of space. | ||
explicitNulls = false | ||
// adding enum values is OK even if older clients won't understand it | ||
ignoreUnknownKeys = true | ||
} | ||
} | ||
|
||
val DEFAULT_JSON_PRETTY: Json by lazy { | ||
Json { | ||
// don't rely on external systems being written in kotlin or even having a language with default values | ||
// the default of false is FFing insane and dangerous | ||
encodeDefaults = true | ||
// save space | ||
prettyPrint = false | ||
// people adding shit to the json is OK, we're forward compatible and will just ignore it | ||
isLenient = true | ||
// encoding nulls is meaningless and a waste of space. | ||
explicitNulls = false | ||
// adding enum values is OK even if older clients won't understand it | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
} | ||
} | ||
|
||
|
||
val FeatureCollection.geoJsonIOUrl get() = DEFAULT_JSON.encodeToString(this).let { json-> | ||
"https://geojson.io/#data=${"data:application/json,$json".urlEncode()}" | ||
} | ||
|
||
val Geometry.geoJsonIOUrl get() = this.asFeatureCollection.geoJsonIOUrl | ||
|
||
fun String.urlEncode(): String { | ||
val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9') + listOf('-', '.', '_', '~') | ||
return buildString { | ||
this@urlEncode.forEach { char -> | ||
if (char in allowedChars) { | ||
append(char) | ||
} else { | ||
append(char.toByte().toInt().let { | ||
"%${it.shr(4).and(0xF).toString(16)}${it.and(0xF).toString(16)}" | ||
}.toUpperCase()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.