Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VLine, HLine and Polygon layers #26

Merged
merged 2 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Added polygon layer
  • Loading branch information
ISeleznev-HORIS committed Sep 13, 2019
commit 148c3734363be458d8713faf9463967188b0f4c9
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class FeaturesDemoModel(canvasControl: CanvasControl): DemoModelBase(canvasContr
}
}

polygons {
polygon {
coordinates = listOf(BOSTON, SPB, MOSCOW).map(GeoObject::geoCoord)
fillColor = Color.LIGHT_CYAN
}
}

hLines {
line {
lon = MOSCOW.lon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ fun Paths.path(block: PathBuilder.() -> Unit) {
)
}

fun Polygons.polygon(block: PolygonsBuilder.() -> Unit) {
items.add(
PolygonsBuilder().apply {
index = 0
mapId = ""
regionId = ""

lineDash = emptyList()
strokeColor = Color.BLACK
strokeWidth = 0.0
fillColor = Color.GREEN
coordinates = emptyList()
}
.apply(block)
.build()
)
}

fun Lines.line(block: LineBuilder.() -> Unit) {
items.add(
LineBuilder().apply {
Expand Down
6 changes: 3 additions & 3 deletions livemap/src/commonMain/kotlin/jetbrains/livemap/LiveMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ class LiveMap(
when(kind) {
MapLayerKind.POINT -> mapObject2Entity.processPoint(mapObjects)
MapLayerKind.PATH -> mapObject2Entity.processPath(mapObjects)
//MapLayerKind.POLYGON -> mapObject2Entity.processPolygon(mapObjects)
MapLayerKind.POLYGON -> mapObject2Entity.processPolygon(mapObjects)
//MapLayerKind.BAR -> mapObject2Entity.processBar(mapObjects)
//MapLayerKind.PIE -> mapObject2Entity.processPie(mapObjects)
MapLayerKind.H_LINE -> mapObject2Entity.processLine(mapObjects, true, myMapProjection)
MapLayerKind.V_LINE -> mapObject2Entity.processLine(mapObjects, false, myMapProjection)
MapLayerKind.H_LINE -> mapObject2Entity.processLine(mapObjects, true)
MapLayerKind.V_LINE -> mapObject2Entity.processLine(mapObjects, false)
//MapLayerKind.TEXT -> mapObject2Entity.processText(
//mapObjects,
//TextSpec.createMeasurer(context.mapRenderContext.canvasProvider.createCanvas(Vector.ZERO).context2d)
Expand Down
40 changes: 40 additions & 0 deletions livemap/src/commonMain/kotlin/jetbrains/livemap/api/Builder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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

Expand Down Expand Up @@ -109,6 +110,12 @@ class Points {
class Paths {
val items = ArrayList<MapPath>()
}

@LiveMapDsl
class Polygons {
val items = ArrayList<MapPolygon>()
}

@LiveMapDsl
class Lines {
val items = ArrayList<MapLine>()
Expand Down Expand Up @@ -178,6 +185,35 @@ class PathBuilder {
}
}



@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
Expand Down Expand Up @@ -255,6 +291,10 @@ 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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jetbrains.livemap.mapobjects

import jetbrains.datalore.base.values.Color
import jetbrains.livemap.entities.geometry.LonLatGeometry

class MapPolygon(
index: Int,
mapId: String?,
regionId: String?,

val lineDash: List<Double>,
val strokeColor: Color,
val strokeWidth: Double,
val fillColor: Color,
val geometry: LonLatGeometry?

) : MapObject(index, mapId, regionId)
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ class MapObject2Entity(
MapPathProcessor(myComponentManager, myLayerManager, myMapProjection).process(mapObjects)
}

fun processLine(mapObjects: List<MapObject>, horizontal: Boolean, myMapProjection: MapProjection) {
fun processLine(mapObjects: List<MapObject>, horizontal: Boolean) {
MapLineProcessor(myComponentManager, myLayerManager, myMapProjection).process(mapObjects, horizontal)
}

/*
fun processPolygon(mapObjects: List<MapObject>) {
MapPolygonProcessor(myComponentManager, myLayerManager).process(mapObjects)
MapPolygonProcessor(myComponentManager, myLayerManager, myMapProjection).process(mapObjects)
}


/*

fun processBar(mapObjects: List<MapObject>) {
MapBarProcessor(myComponentManager, myLayerManager).process(mapObjects)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package jetbrains.livemap.obj2entity

import jetbrains.datalore.maps.livemap.entities.geometry.Renderers
import jetbrains.datalore.maps.livemap.entities.geometry.WorldGeometryComponent
import jetbrains.gis.geoprotocol.GeometryUtil
import jetbrains.livemap.core.ecs.EcsComponentManager
import jetbrains.livemap.core.rendering.layers.LayerManager
import jetbrains.livemap.entities.Entities
import jetbrains.livemap.entities.geometry.LonLatGeometry
import jetbrains.livemap.entities.geometry.WorldGeometry
import jetbrains.livemap.entities.placement.Components
import jetbrains.livemap.entities.rendering.*
import jetbrains.livemap.entities.scaling.ScaleComponent
import jetbrains.livemap.mapobjects.MapObject
import jetbrains.livemap.mapobjects.MapPolygon
import jetbrains.livemap.projections.MapProjection
import jetbrains.livemap.projections.ProjectionUtil
import jetbrains.livemap.projections.toWorldPoint

internal class MapPolygonProcessor(
componentManager: EcsComponentManager,
layerManager: LayerManager,
private val myMapProjection: MapProjection
) {
private val myFactory: Entities.MapEntityFactory
private val myLayerEntitiesComponent = LayerEntitiesComponent()
private val toMapProjection: (LonLatGeometry) -> WorldGeometry

init {
myFactory = Entities.MapEntityFactory(
componentManager
.createEntity("map_layer_polygon")
.addComponent(layerManager.createRenderLayerComponent("geom_polygon"))
.addComponent(myLayerEntitiesComponent)
)

toMapProjection = { geometry ->
geometry.asMultipolygon()
.run { ProjectionUtil.transformMultipolygon(this, myMapProjection::project) }
.run { WorldGeometry.create(this) }
}
}

fun process(mapObjects: List<MapObject>) {
createEntities(mapObjects)
}

private fun createEntities(mapObjects: List<MapObject>) {
for (`object` in mapObjects) {
val mapPolygon = `object` as MapPolygon

if (mapPolygon.geometry != null) {
createStaticEntity(mapPolygon)
} else if (mapPolygon.regionId != null) {
// createDynamicEntity(mapPolygon)
} else {
// do not create entities for empty geometries
}
}
}

private fun createStaticEntity(mapPolygon: MapPolygon) {
val geometry = toMapProjection(mapPolygon.geometry!!)
val bbox = GeometryUtil.bbox(geometry.asMultipolygon()) ?: error("")

val geometryEntity = myFactory
.createMapEntity(bbox.origin.toWorldPoint(), SIMPLE_RENDERER, "map_ent_spolygon")
.addComponent(WorldGeometryComponent().apply { this.geometry = geometry } )
.addComponent(Components.WorldDimensionComponent(bbox.dimension.toWorldPoint()))
.addComponent(ScaleComponent())
.addComponent(
StyleComponent().apply {
setFillColor(mapPolygon.fillColor)
setStrokeColor(mapPolygon.strokeColor)
setStrokeWidth(mapPolygon.strokeWidth)
}
)

myLayerEntitiesComponent.add(geometryEntity.id)
}

// private fun createDynamicEntity(mapPolygon: MapPolygon) {
// val regionEntity = myFactory
// .createDynamicMapEntity("map_ent_dpolygon_" + mapPolygon.regionId, FRAGMENTS_RENDERER)
// .addComponent(RegionComponent().apply { id = mapPolygon.regionId })
// .addComponent(
// StyleComponent().apply {
// setFillColor(mapPolygon.fillColor)
// setStrokeColor(mapPolygon.strokeColor)
// setStrokeWidth(mapPolygon.strokeWidth)
// }
// )
//
// regionEntity.get<ScreenLoopComponent>().origins = listOf(ZERO_CLIENT_POINT)
// myLayerEntitiesComponent.add(regionEntity.id)
// }

companion object {
private val SIMPLE_RENDERER = Renderers.PolygonRenderer()
// private val FRAGMENTS_RENDERER = RegionRenderer()
}
}