All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.org.jetbrains.letsPlot.livemap.geocoding.LocationCalculateSystem.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2023. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package org.jetbrains.letsPlot.livemap.geocoding

import org.jetbrains.letsPlot.commons.intern.typedGeometry.GeometryType.*
import org.jetbrains.letsPlot.commons.intern.typedGeometry.LineString
import org.jetbrains.letsPlot.commons.intern.typedGeometry.Polygon
import org.jetbrains.letsPlot.commons.intern.typedGeometry.Rect
import org.jetbrains.letsPlot.livemap.World
import org.jetbrains.letsPlot.livemap.chart.ChartElementLocationComponent
import org.jetbrains.letsPlot.livemap.chart.fragment.RegionBBoxComponent
import org.jetbrains.letsPlot.livemap.core.MapRuler
import org.jetbrains.letsPlot.livemap.core.ecs.AbstractSystem
import org.jetbrains.letsPlot.livemap.core.ecs.EcsComponentManager
import org.jetbrains.letsPlot.livemap.geocoding.MapLocationGeocoder.Companion.convertToWorldRects
import org.jetbrains.letsPlot.livemap.geometry.WorldGeometryComponent
import org.jetbrains.letsPlot.livemap.mapengine.LiveMapContext
import org.jetbrains.letsPlot.livemap.mapengine.MapProjection
import org.jetbrains.letsPlot.livemap.mapengine.placement.WorldDimensionComponent
import org.jetbrains.letsPlot.livemap.mapengine.placement.WorldOriginComponent

class LocationCalculateSystem(
    private val mapRuler: MapRuler,
    private val mapProjection: MapProjection,
    componentManager: EcsComponentManager
) : AbstractSystem(componentManager) {
    private lateinit var myLocation: LocationComponent

    override fun initImpl(context: LiveMapContext) {
        myLocation = getSingleton()
    }

    override fun updateImpl(context: LiveMapContext, dt: Double) {
        getMutableEntities(READY_CALCULATE)
            .forEach { entity ->
                when {
                    entity.contains() -> {
                        with(entity.get().geometry) {
                            when (type) {
                                MULTI_POLYGON -> mapRuler.calculateBoundingBox(multiPolygon.mapNotNull(Polygon::bbox))
                                MULTI_LINESTRING -> mapRuler.calculateBoundingBox(multiLineString.mapNotNull(LineString::bbox))
                                MULTI_POINT -> mapRuler.calculateBoundingBox(listOfNotNull(multiPoint.bbox))
                                else -> error("Unsupported geometry: $type")
                            }
                        }
                    }
                    entity.contains() -> {
                        with(entity.get().geometry) {
                            when (type) {
                                MULTI_POLYGON -> mapRuler.calculateBoundingBox(multiPolygon.mapNotNull(Polygon::bbox))
                                MULTI_LINESTRING -> mapRuler.calculateBoundingBox(multiLineString.mapNotNull(LineString::bbox))
                                MULTI_POINT -> mapRuler.calculateBoundingBox(listOfNotNull(multiPoint.bbox))
                                else -> error("Unsupported geometry: $type")
                            }
                        }
                    }
                    entity.contains() -> {
                        Rect.XYWH(
                            entity.get().origin,
                            entity.tryGet()?.dimension ?: World.ZERO_VEC
                        )
                    }
                    entity.contains() -> {
                        val worldRects = entity.get().bbox.convertToWorldRects(mapProjection)
                        mapRuler.calculateBoundingBox(worldRects)
                    }
                    else -> null
                }?.let { entityLocation ->
                    myLocation.add(entityLocation)
                    entity.remove()
                }
            }
    }

    companion object {
        val READY_CALCULATE = listOf(
            NeedCalculateLocationComponent::class
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy