-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathareas_optimizer.main.kts
executable file
·55 lines (43 loc) · 1.62 KB
/
areas_optimizer.main.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env -S kotlin -Xplugin=/opt/homebrew/opt/kotlin/libexec/lib/kotlinx-serialization-compiler-plugin.jar
// brew install kotlin
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
import Areas_optimizer_main.MultiPolygon
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
val json = Json {
prettyPrint = true
ignoreUnknownKeys = true
}
val filePath = "kmp/compose/resources/src/commonMain/composeResources/files/areas.geojson"
val jsonData = File(filePath).readText()
val featureCollection = json.decodeFromString<AreasDTO>(jsonData)
val areas = featureCollection.features.map {
AreaDTO(
name = it.properties.name,
coordinates = it.geometry.coordinates.first().first().map { coordinates ->
LatLng(
latitude = coordinates[1],
longitude = coordinates[0]
)
}
)
}
val updatedAreas = json.encodeToString(areas)
File(filePath).writeText(updatedAreas)
println("Updated JSON data: $updatedAreas")
// Old models
typealias MultiPolygon = List<List<List<List<Double>>>>
@Serializable
data class AreasDTO(val type: String, val features: List<Feature>)
@Serializable
data class Feature(val geometry: MultiPolygonGeometry, val properties: Properties)
@Serializable
data class MultiPolygonGeometry(val coordinates: MultiPolygon)
@Serializable
data class Properties(val name: String)
@Serializable
data class AreaDTO(val name: String, val coordinates: List<LatLng>)
@Serializable
data class LatLng(val latitude: Double, val longitude: Double)