-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDsl.kt
207 lines (194 loc) · 6.56 KB
/
Dsl.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
val isPoint = """["==", ["geometry-type"], "Point"]"""
val isLines = """["==", ["geometry-type"], "LineString"]"""
val isPolygon = """["==", ["geometry-type"], "Polygon"]"""
fun byZoom(vararg n: Pair<Double, Double>): String = byZoom(n.toList())
fun byZoom(n: Iterable<Pair<Double, Double>>): String {
val values = n.flatMap { (z, v) -> listOf(z, v) }.joinToString()
return """["interpolate", ["exponential", 2], ["zoom"], $values]"""
}
fun tagIs(key: String, value: Any) = """["==", ["get", "$key"], ${ if (value is String) "\"$value\"" else value }]"""
fun tagIsNot(key: String, value: Any) = """["!=", ["get", "$key"], ${ if (value is String) "\"$value\"" else value }]"""
fun tagIn(key: String, vararg values: String) =
"""["in", ["get", "$key"], ["literal", [${ values.joinToString { "\"$it\"" } }]]]"""
fun tagNotIn(key: String, vararg values: String) =
"""["!", ${tagIn(key, *values)}]"""
data class Layer(
val id: String,
val src: String,
val filter: List<String> = emptyList(),
val minZoom: Double? = null,
val maxZoom: Double? = null,
val paint: Paint
) {
fun toJson() = "{ " +
listOfNotNull(
"\"id\": \"$id\"",
"\"source\": \"jawg-streets\"",
"\"source-layer\": \"$src\"",
minZoom?.let { "\"minzoom\": $it" },
maxZoom?.let { "\"maxzoom\": $it" },
when (filter.size) {
0 -> null
1 -> "\"filter\": " + filter.single()
else -> "\"filter\": [\"all\", " + filter.joinToString() + "]"
},
paint.toJson(),
).joinToString() +
" }"
}
interface Paint {
fun toJson(): String
}
data class Fill(
val color: String,
val opacity: String? = null,
val antialias: Boolean? = null
): Paint {
override fun toJson() = listOf(
"\"type\": \"fill\"",
"\"paint\": { " +
listOfNotNull(
"\"fill-color\": \"$color\"",
opacity?.let { "\"fill-opacity\": $it" },
antialias?.let { "\"fill-antialias\": $it" },
).joinToString() +
"}"
).joinToString()
}
data class FillExtrusion(
val color: String,
val base: String,
val height: String,
val opacity: String? = null,
) : Paint {
override fun toJson() = listOf(
"\"type\": \"fill-extrusion\"",
"\"paint\": { " +
listOfNotNull(
"\"fill-extrusion-color\": \"$color\"",
"\"fill-extrusion-height\": $height",
"\"fill-extrusion-base\": $base",
opacity?.let { "\"fill-extrusion-opacity\": $it" },
).joinToString() +
"}"
).joinToString()
}
data class Line(
val color: String,
val width: String,
val gapWidth: String? = null,
val offset: String? = null,
val blur: String? = null,
val opacity: String? = null,
val miterLimit: Number? = null,
val dashes: String? = null,
val cap: String? = null,
val join: String? = null
): Paint {
override fun toJson() = listOfNotNull(
"\"type\": \"line\"",
"\"paint\": {" +
listOfNotNull(
"\"line-color\": \"$color\"",
"\"line-width\": $width",
gapWidth?.let { "\"line-gap-width\": $it" },
offset?.let { "\"line-offset\": $it" },
blur?.let { "\"line-blur\": $it" },
dashes?.let { "\"line-dasharray\": $it" },
opacity?.let { "\"line-opacity\": $it" },
).joinToString() +
"}",
if (cap != null || join != null || miterLimit != null) {
"\"layout\": {" +
listOfNotNull(
cap?.let { "\"line-cap\": \"$it\"" },
join?.let { "\"line-join\": \"$it\"" },
miterLimit?.let { "\"line-miter-limit\": $it" },
).joinToString() +
"}"
} else null
).joinToString(",")
}
data class Circle(
val color: String,
val radius: String,
val opacity: String? = null,
): Paint {
override fun toJson() = listOfNotNull(
"\"type\": \"circle\"",
"\"paint\": {" +
listOfNotNull(
"\"circle-color\": \"$color\"",
"\"circle-radius\": $radius",
opacity?.let { "\"circle-opacity\": $it" },
).joinToString() + "}",
).joinToString()
}
data class Symbol(
val image: String,
val color: String? = null,
val padding: Number? = null,
val placement: String? = null,
val spacing: String? = null,
val opacity: String? = null,
val size: String? = null,
val rotate: Number? = null,
val rotationAlignment: String? = null,
) : Paint {
override fun toJson() = listOfNotNull(
"\"type\": \"symbol\"",
"\"paint\": {" +
listOfNotNull(
color?.let { "\"icon-color\": \"$it\"" },
opacity?.let { "\"icon-opacity\": $it" },
).joinToString() +
"}",
"\"layout\": {" +
listOfNotNull(
"\"icon-image\": \"$image\"",
size?.let { "\"icon-size\": $it" },
spacing?.let { "\"symbol-spacing\": $it" },
placement?.let { "\"symbol-placement\": \"$it\"" },
padding?.let { "\"icon-padding\": $it" },
rotate?.let { "\"icon-rotate\": $it" },
rotationAlignment?.let { "\"icon-rotation-alignment\": \"$it\"" },
).joinToString() +
"}",
).joinToString()
}
data class Text(
val text: String,
val size: String,
val color: String,
val fonts: List<String>,
val wrap: Number? = null,
val padding: Number? = null,
val outlineColor: String? = null,
val outlineWidth: Number? = null,
val placement: String? = null,
val opacity: String? = null,
val sortKey: String? = null
) : Paint {
override fun toJson() = listOfNotNull(
"\"type\": \"symbol\"",
"\"paint\": {" +
listOfNotNull(
"\"text-color\": \"$color\"",
outlineColor?.let { "\"text-halo-color\": \"$it\"" },
outlineWidth?.let { "\"text-halo-width\": $it" },
opacity?.let { "\"text-opacity\": $it" },
).joinToString() +
"}",
"\"layout\": {" +
listOfNotNull(
"\"text-field\": $text",
"\"text-size\": $size",
"\"text-font\": [${fonts.joinToString { "\"$it\""}}]",
placement?.let { "\"symbol-placement\": \"$it\"" },
padding?.let { "\"text-padding\": $it" },
wrap?.let { "\"text-max-width\": $it" },
sortKey?.let { "\"symbol-sort-key\": $it" },
).joinToString() +
"}",
).joinToString()
}