-
Notifications
You must be signed in to change notification settings - Fork 49
/
Builder.kt
301 lines (246 loc) · 8.49 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
/*
* 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.livemap.api
import jetbrains.datalore.base.geometry.DoubleRectangle
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.spatial.*
import jetbrains.datalore.base.typedGeometry.*
import jetbrains.datalore.base.values.Color
import jetbrains.gis.geoprotocol.FeatureLevel
import jetbrains.gis.geoprotocol.GeoTransportImpl
import jetbrains.gis.geoprotocol.GeocodingService
import jetbrains.gis.geoprotocol.MapRegion
import jetbrains.gis.tileprotocol.TileService
import jetbrains.gis.tileprotocol.socket.TileWebSocketBuilder
import jetbrains.livemap.MapLocation
import jetbrains.livemap.camera.CameraListenerComponent
import jetbrains.livemap.camera.CenterChangedComponent
import jetbrains.livemap.camera.ZoomChangedComponent
import jetbrains.livemap.config.DevParams
import jetbrains.livemap.config.LiveMapSpec
import jetbrains.livemap.core.ecs.EcsComponentManager
import jetbrains.livemap.core.ecs.EcsEntity
import jetbrains.livemap.core.ecs.addComponents
import jetbrains.livemap.core.projections.MapRuler
import jetbrains.livemap.core.projections.ProjectionType
import jetbrains.livemap.core.projections.createArcPath
import jetbrains.livemap.core.rendering.TextMeasurer
import jetbrains.livemap.core.rendering.layers.LayerManager
import jetbrains.livemap.core.rendering.layers.ParentLayerComponent
import jetbrains.livemap.projection.MapProjection
import jetbrains.livemap.projection.World
import jetbrains.livemap.rendering.LayerEntitiesComponent
import jetbrains.livemap.tiles.TileSystemProvider
import kotlin.math.abs
@DslMarker
annotation class LiveMapDsl
@LiveMapDsl
class LiveMapBuilder {
lateinit var size: DoubleVector
lateinit var geocodingService: GeocodingService
lateinit var tileSystemProvider: TileSystemProvider
var layers: List<LayersBuilder.() -> Unit> = emptyList()
var zoom: Int? = null
var interactive: Boolean = true
var mapLocation: MapLocation? = null
var projectionType: ProjectionType = ProjectionType.MERCATOR
var isLoopX: Boolean = true
var isLoopY: Boolean = false
var mapLocationConsumer: (DoubleRectangle) -> Unit = { _ -> Unit }
var attribution: String? = null
var devParams: DevParams =
DevParams(HashMap<String, Any>())
fun build(): LiveMapSpec {
return LiveMapSpec(
size = size,
zoom = zoom,
isInteractive = interactive,
layers = layers,
location = mapLocation,
projectionType = projectionType,
isLoopX = isLoopX,
isLoopY = isLoopY,
geocodingService = geocodingService,
mapLocationConsumer = mapLocationConsumer,
tileSystemProvider = tileSystemProvider,
devParams = devParams,
attribution = attribution,
// deprecated
isClustering = false,
isLabels = true,
isScaled = false,
isTiles = true,
isUseFrame = true
)
}
}
@LiveMapDsl
class LayersBuilder(
val myComponentManager: EcsComponentManager,
val layerManager: LayerManager,
val mapProjection: MapProjection,
val mapRuler: MapRuler<World>,
val pointScaling: Boolean,
val textMeasurer: TextMeasurer
)
@LiveMapDsl
class ChartSource {
var layerIndex: Int? = null
var point: Vec<LonLat>? = null
var radius: Double = 0.0
var strokeColor: Color = Color.BLACK
var strokeWidth: Double = 0.0
var indices: List<Int> = emptyList()
var values: List<Double> = emptyList()
var colors: List<Color> = emptyList()
}
fun geometry(
points: List<LonLatPoint>,
isClosed: Boolean,
isGeodesic: Boolean
): MultiPolygon<LonLat> {
val coord = points.map(::limitCoord)
return if (isClosed) {
createMultiPolygon(coord)
} else {
coord
.run { if (isGeodesic) createArcPath(this) else this }
.run(::splitPathByAntiMeridian)
.map { path -> Polygon(listOf(Ring(path))) }
.run(::MultiPolygon)
}
}
fun limitCoord(point: Vec<LonLat>): Vec<LonLat> {
return explicitVec(
limitLon(point.x),
limitLat(point.y)
)
}
private const val FULL_ANGLE = 360.0
private const val STRAIGHT_ANGLE = 180.0
fun splitPathByAntiMeridian(path: List<Vec<LonLat>>): List<List<Vec<LonLat>>> {
val pathList = ArrayList<List<Vec<LonLat>>>()
var currentPath = ArrayList<Vec<LonLat>>()
if (path.isNotEmpty()) {
currentPath.add(path[0])
for (i in 1 until path.size) {
val prev = path[i - 1]
val next = path[i]
val lonDelta = abs(next.x - prev.x)
if (lonDelta > FULL_ANGLE - lonDelta) {
val sign = (if (prev.x < 0.0) -1 else +1).toDouble()
val x1 = prev.x - sign * STRAIGHT_ANGLE
val x2 = next.x + sign * STRAIGHT_ANGLE
val lat = (next.y - prev.y) * (if (x2 == x1) 1.0 / 2.0 else x1 / (x1 - x2)) + prev.y
currentPath.add(explicitVec(sign * STRAIGHT_ANGLE, lat))
pathList.add(currentPath)
currentPath = ArrayList()
currentPath.add(explicitVec(-sign * STRAIGHT_ANGLE, lat))
}
currentPath.add(next)
}
}
pathList.add(currentPath)
return pathList
}
@LiveMapDsl
class Location {
var name: String? = null
set(v) {
field = v; mapLocation = v?.let { MapLocation.create(MapRegion.withName(it)) }
}
var osmId: String? = null
set(v) {
field = v; mapLocation = v?.let { MapLocation.create(MapRegion.withId(it)) }
}
var coordinate: Vec<LonLat>? = null
set(v) {
field = v; mapLocation = v?.let { MapLocation.create(
GeoRectangle(
it.x,
it.y,
it.x,
it.y
)
) }
}
internal var mapLocation: MapLocation? = null
internal 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 {
lateinit var url: String
var theme = TileService.Theme.COLOR
fun build(): TileService {
return TileService(TileWebSocketBuilder(url), theme)
}
}
@LiveMapDsl
class LiveMapGeocodingServiceBuilder {
lateinit var url: String
fun build(): GeocodingService {
return GeocodingService(GeoTransportImpl(url))
}
}
fun mapEntity(
componentManager: EcsComponentManager,
parentLayerComponent: ParentLayerComponent,
name: String
): EcsEntity {
return componentManager
.createEntity(name)
.addComponents {
+ parentLayerComponent
+ CameraListenerComponent()
+ CenterChangedComponent()
+ ZoomChangedComponent()
}
}
class MapEntityFactory(layerEntity: EcsEntity) {
private val myComponentManager: EcsComponentManager = layerEntity.componentManager
private val myParentLayerComponent: ParentLayerComponent = ParentLayerComponent(layerEntity.id)
private val myLayerEntityComponent: LayerEntitiesComponent = layerEntity.get()
fun createMapEntity(name: String): EcsEntity {
return mapEntity(myComponentManager, myParentLayerComponent, name)
.also { myLayerEntityComponent.add(it.id) }
}
}
fun liveMapConfig(block: LiveMapBuilder.() -> Unit) = LiveMapBuilder().apply(block)
fun LiveMapBuilder.layers(block: LayersBuilder.() -> Unit) {
layers = listOf(block)
}
fun LiveMapBuilder.location(block: Location.() -> Unit) {
Location().apply(block).let { location ->
mapLocation = location.mapLocation
}
}
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 liveMapVectorTiles(block: LiveMapTileServiceBuilder.() -> Unit) =
LiveMapTileServiceBuilder().apply(block).build()
fun liveMapGeocoding(block: LiveMapGeocodingServiceBuilder.() -> Unit): GeocodingService {
return LiveMapGeocodingServiceBuilder().apply(block).build()
}