WIP: clustering in DB

This commit is contained in:
johan12345
2023-12-02 23:23:26 +01:00
parent 4cc54bd376
commit 26de099baa
2 changed files with 21 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package net.vonforst.evmap.model
import android.content.Context
import android.os.Parcelable
import androidx.core.text.HtmlCompat
import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
@@ -356,8 +357,8 @@ abstract class ChargerPhoto(open val id: String) : Parcelable {
}
data class ChargeLocationCluster(
val clusterCount: Int,
val coordinates: Coordinate,
@ColumnInfo("clusterCount") val clusterCount: Int,
@ColumnInfo("coordinates") val coordinates: Coordinate,
val items: List<ChargeLocation>? = null
) : ChargepointListItem()

View File

@@ -100,6 +100,18 @@ abstract class ChargeLocationsDao {
@RawQuery(observedEntities = [ChargeLocation::class])
abstract fun getChargeLocationsCustom(query: SupportSQLiteQuery): LiveData<List<ChargeLocation>>
@SkipQueryVerification
@Query("SELECT SUM(1) AS clusterCount, MakePoint(AVG(X(coordinates)), AVG(Y(coordinates)), 4326) as center, SnapToGrid(coordinates, :precision) AS snapped FROM chargelocation WHERE dataSource == :dataSource AND Within(coordinates, BuildMbr(:lng1, :lat1, :lng2, :lat2)) AND timeRetrieved > :after GROUP BY snapped")
abstract fun getChargeLocationClusters(
lat1: Double,
lat2: Double,
lng1: Double,
lng2: Double,
dataSource: String,
after: Long,
precision: Double
): LiveData<List<ChargeLocationClusterSimple>>
@Query("SELECT COUNT(*) FROM chargelocation")
abstract fun getCount(): LiveData<Long>
@@ -111,6 +123,12 @@ abstract class ChargeLocationsDao {
abstract suspend fun getSize(): Long
}
data class ChargeLocationClusterSimple(
@ColumnInfo("clusterCount") val clusterCount: Int,
@ColumnInfo("center") val center: Coordinate,
@ColumnInfo("snapped") val snapped: Coordinate,
)
/**
* The ChargeLocationsRepository wraps the ChargepointApi and the DB to provide caching
* and clustering functionality.