-
Notifications
You must be signed in to change notification settings - Fork 49
/
LiveMapOptionsParser.kt
69 lines (58 loc) · 3.03 KB
/
LiveMapOptionsParser.kt
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.plot.config
import jetbrains.datalore.plot.base.livemap.LiveMapOptions
import jetbrains.datalore.plot.base.livemap.LivemapConstants.DisplayMode
import jetbrains.datalore.plot.base.livemap.LivemapConstants.Projection
import jetbrains.datalore.plot.config.Option.Geom.LiveMap
import jetbrains.datalore.plot.config.Option.Plot.LAYERS
class LiveMapOptionsParser {
companion object {
fun parseFromPlotSpec(plotSpec: Map<String, Any>): LiveMapOptions? {
fun Map<*, *>.isLiveMap(): Boolean = this[Option.Layer.GEOM] == Option.GeomName.LIVE_MAP
val layers = plotSpec.getMaps(LAYERS)!!
if (layers.any { it.isLiveMap() }) {
require(layers.count { it.isLiveMap() } == 1) { "Only one geom_livemap is allowed per plot" }
require(layers.first().isLiveMap()) { "geom_livemap should be a first geom" }
return parseFromLayerOptions(OptionsAccessor(layers.first()))
}
return null
}
fun parseFromLayerOptions(opts: OptionsAccessor): LiveMapOptions {
return LiveMapOptions(
zoom = opts.getInteger(LiveMap.ZOOM),
location = opts.get(LiveMap.LOCATION),
stroke = opts.getDouble(LiveMap.STROKE),
interactive = opts.getBoolean(LiveMap.INTERACTIVE, true),
displayMode = opts.getString(LiveMap.DISPLAY_MODE)?.let(::parseDisplayMode) ?: DisplayMode.POINT,
scaled = opts.getBoolean(LiveMap.SCALED, false),
clustering = opts.getBoolean(LiveMap.CLUSTERING, false),
labels = opts.getBoolean(LiveMap.LABELS, true),
projection = opts.getString(LiveMap.PROJECTION)?.let(::parseProjection) ?: Projection.EPSG3857,
geodesic = opts.getBoolean(LiveMap.GEODESIC, true),
geocodingService = opts.getMap(LiveMap.GEOCODING),
tileProvider = opts.getMap(LiveMap.TILES),
devParams = opts.getMap(LiveMap.DEV_PARAMS)
)
}
private fun <ValueT : Enum<ValueT>> formatValues(values: Array<ValueT>): String {
return values.joinToString(prefix = "=[", separator = "|", postfix = "]") { "'${it.name.toLowerCase()}'" }
}
private fun parseDisplayMode(displayMode: String): DisplayMode {
try {
return DisplayMode.valueOf(displayMode.toUpperCase())
} catch (ignored: Exception) {
throw IllegalArgumentException("geom" + formatValues(DisplayMode.values()))
}
}
private fun parseProjection(projection: String): Projection {
try {
return Projection.valueOf(projection.toUpperCase())
} catch (ignored: Exception) {
throw IllegalArgumentException(LiveMap.PROJECTION + formatValues(Projection.values()))
}
}
}
}