-
Notifications
You must be signed in to change notification settings - Fork 47
/
Builder.kt
377 lines (309 loc) · 10.1 KB
/
Builder.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package jetbrains.livemap.api
import jetbrains.datalore.base.async.Async
import jetbrains.datalore.base.async.Asyncs.constant
import jetbrains.datalore.base.event.MouseEventSource
import jetbrains.datalore.base.geometry.DoubleRectangle
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.projectionGeometry.MultiPolygon
import jetbrains.datalore.base.projectionGeometry.Polygon
import jetbrains.datalore.base.projectionGeometry.Ring
import jetbrains.datalore.base.values.Color
import jetbrains.datalore.maps.cell.mapobjects.MapPath
import jetbrains.datalore.visualization.plot.base.livemap.LivemapConstants
import jetbrains.gis.geoprotocol.*
import jetbrains.gis.tileprotocol.TileLayer
import jetbrains.gis.tileprotocol.TileService
import jetbrains.gis.tileprotocol.socket.Socket
import jetbrains.gis.tileprotocol.socket.SocketBuilder
import jetbrains.gis.tileprotocol.socket.SocketHandler
import jetbrains.gis.tileprotocol.socket.TileWebSocketBuilder
import jetbrains.livemap.DevParams
import jetbrains.livemap.LiveMapSpec
import jetbrains.livemap.MapLocation
import jetbrains.livemap.entities.geometry.LonLatGeometry
import jetbrains.livemap.mapobjects.MapLayer
import jetbrains.livemap.mapobjects.MapLayerKind.*
import jetbrains.livemap.mapobjects.MapLine
import jetbrains.livemap.mapobjects.MapPoint
import jetbrains.livemap.mapobjects.MapPolygon
import jetbrains.livemap.projections.ProjectionType
import jetbrains.livemap.projections.createArcPath
@DslMarker
annotation class LiveMapDsl {}
@LiveMapDsl
class LiveMapBuilder {
lateinit var size: DoubleVector
lateinit var geocodingService: GeocodingService
lateinit var tileService: TileService
lateinit var mouseEventSource: MouseEventSource
var zoom: Int? = null
var interactive: Boolean = true
var mapLocation: MapLocation? = null
var level: FeatureLevel? = null
var parent: MapRegion? = null
var layers: MutableList<MapLayer> = ArrayList()
var theme: LivemapConstants.Theme = LivemapConstants.Theme.COLOR
var projectionType: ProjectionType = ProjectionType.MERCATOR
var isLoopX: Boolean = true
var isLoopY: Boolean = false
var mapLocationConsumer: (DoubleRectangle) -> Unit = { _ -> Unit }
var devParams: Map<String, Any> = HashMap()
fun params(vararg vals: Pair<String, Any>) {
this.devParams = mapOf(*vals)
}
fun build(): LiveMapSpec {
return LiveMapSpec(
size = size,
zoom = zoom,
isInteractive = interactive,
layers = layers,
level = level,
location = mapLocation,
parent = parent,
projectionType = projectionType,
isLoopX = isLoopX,
isLoopY = isLoopY,
tileService = tileService,
theme = theme,
geocodingService = geocodingService,
mapLocationConsumer = mapLocationConsumer,
eventSource = mouseEventSource,
devParams = DevParams(devParams),
// deprecated
isClustering = false,
isEnableMagnifier = false,
isLabels = true,
isScaled = false,
isTiles = true,
isUseFrame = true
)
}
}
@LiveMapDsl
class LayersBuilder {
val items = ArrayList<MapLayer>()
}
@LiveMapDsl
class Points {
val items = ArrayList<MapPoint>()
}
@LiveMapDsl
class Paths {
val items = ArrayList<MapPath>()
}
@LiveMapDsl
class Polygons {
val items = ArrayList<MapPolygon>()
}
@LiveMapDsl
class Lines {
val items = ArrayList<MapLine>()
}
@LiveMapDsl
class PointBuilder {
var animation: Int? = null
var label: String? = null
var shape: Int? = null
var lat: Double? = null
var lon: Double? = null
var radius: Double? = null
var fillColor: Color? = null
var strokeColor: Color? = null
var strokeWidth: Double? = null
var index: Int? = null
var mapId: String? = null
var regionId: String? = null
fun build(): MapPoint {
return MapPoint(
index!!,
mapId,
regionId,
DoubleVector(lon!!, lat!!),
label!!,
animation!!,
shape!!,
radius!!,
fillColor!!,
strokeColor!!,
strokeWidth!!
)
}
}
@LiveMapDsl
class PathBuilder {
var index: Int? = null
var mapId: String? = null
var regionId: String? = null
var lineDash: List<Double>? = null
var strokeColor: Color? = null
var strokeWidth: Double? = null
var coordinates: List<DoubleVector>? = null
var animation: Int? = null
var speed: Double? = null
var flow: Double? = null
var geodesic: Boolean? = null
fun build(): MapPath {
val coord = coordinates.takeIf { !geodesic!! } ?: createArcPath(coordinates!!)
return MapPath(
index!!, mapId, regionId,
animation!!, speed!!, flow!!,
lineDash!!, strokeColor!!, strokeWidth!!,
coord
.run(::Ring)
.run(::Polygon)
.run(::MultiPolygon)
.run(LonLatGeometry.Companion::create)
)
}
}
@LiveMapDsl
class PolygonsBuilder {
var index: Int? = null
var mapId: String? = null
var regionId: String? = null
var lineDash: List<Double>? = null
var strokeColor: Color? = null
var strokeWidth: Double? = null
var fillColor: Color? = null
var coordinates: List<DoubleVector>? = null
fun build(): MapPolygon {
return MapPolygon(
index!!, mapId, regionId,
lineDash!!, strokeColor!!, strokeWidth!!,
fillColor!!,
coordinates!!
.run(::Ring)
.run(::Polygon)
.run(::MultiPolygon)
.run(LonLatGeometry.Companion::create)
)
}
}
@LiveMapDsl
class LineBuilder {
var index: Int? = null
var mapId: String? = null
var regionId: String? = null
var lon: Double? = null
var lat: Double? = null
var lineDash: List<Double>? = null
var strokeColor: Color? = null
var strokeWidth: Double? = null
fun build(): MapLine {
return MapLine(
index!!, mapId, regionId,
lineDash!!, strokeColor!!, strokeWidth!!,
DoubleVector(lon!!, lat!!)
)
}
}
@LiveMapDsl
class Location {
var name: String? = null
set(v) {
field = v; mapRegion = v?.let { MapRegion.withName(it) }
}
var osmId: String? = null
set(v) {
field = v; mapRegion = v?.let { MapRegion.withId(it) }
}
var mapRegion: MapRegion? = null
var hint: GeocodingHint? = null
}
@LiveMapDsl
class GeocodingHint {
var level: FeatureLevel? = null
var parent: MapRegion? = null
}
@LiveMapDsl
class Projection {
var kind = ProjectionType.MERCATOR
var loopX = true
var loopY = false
}
@LiveMapDsl
class LiveMapTileServiceBuilder {
var theme = LivemapConstants.Theme.COLOR
var host = "localhost"
var port = 3012
fun build(): TileService {
return TileService(TileWebSocketBuilder(host, port), theme.name.toLowerCase())
}
}
fun liveMapConfig(block: LiveMapBuilder.() -> Unit) = LiveMapBuilder().apply(block)
fun LiveMapBuilder.layers(block: LayersBuilder.() -> Unit) {
layers.addAll(LayersBuilder().apply(block).items)
}
fun LayersBuilder.points(block: Points.() -> Unit) {
items.add(MapLayer(POINT, Points().apply(block).items))
}
fun LayersBuilder.paths(block: Paths.() -> Unit) {
items.add(MapLayer(PATH, Paths().apply(block).items))
}
fun LayersBuilder.polygons(block: Polygons.() -> Unit) {
items.add(MapLayer(POLYGON, Polygons().apply(block).items))
}
fun LayersBuilder.hLines(block: Lines.() -> Unit) {
items.add(MapLayer(H_LINE, Lines().apply(block).items))
}
fun LayersBuilder.vLines(block: Lines.() -> Unit) {
items.add(MapLayer(V_LINE, Lines().apply(block).items))
}
fun point(block: PointBuilder.() -> Unit) = PointBuilder().apply(block)
fun path(block: PathBuilder.() -> Unit) = PathBuilder().apply(block)
fun LiveMapBuilder.location(block: Location.() -> Unit) {
Location().apply(block).let { location ->
level = location.hint?.level
parent = location.hint?.parent
mapLocation = location.mapRegion?.run(MapLocation.Companion::create)
}
}
fun Location.geocodingHint(block: GeocodingHint.() -> Unit) {
GeocodingHint().apply(block).let {
hint = it
}
}
fun LiveMapBuilder.projection(block: Projection.() -> Unit) {
Projection().apply(block).let {
projectionType = it.kind
isLoopX = it.loopX
isLoopY = it.loopY
}
}
fun internalTiles(block: LiveMapTileServiceBuilder.() -> Unit): TileService {
return LiveMapTileServiceBuilder()
.apply {
theme = LivemapConstants.Theme.COLOR
host = "10.0.0.127"
port = 3933
}
.apply(block).build()
}
fun liveMapTiles(block: LiveMapTileServiceBuilder.() -> Unit) = LiveMapTileServiceBuilder().apply(block).build()
val dummyGeocodingService: GeocodingService = GeocodingService(
object : GeoTransport {
override fun send(request: GeoRequest): Async<GeoResponse> {
TODO("not implemented")
}
}
)
val dummyTileService: TileService = object : TileService(DummySocketBuilder(), LivemapConstants.Theme.COLOR.name) {
override fun getTileData(bbox: DoubleRectangle, zoom: Int): Async<List<TileLayer>> {
return constant(emptyList<TileLayer>())
}
}
internal class DummySocketBuilder : SocketBuilder {
override fun build(handler: SocketHandler): Socket {
return object : Socket {
override fun connect() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun close() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun send(msg: String) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
}
}