From 107ec10ccb6359eec8e4840fab50c680f804df1f Mon Sep 17 00:00:00 2001 From: johan12345 Date: Sat, 2 Dec 2023 23:23:26 +0100 Subject: [PATCH] WIP: clustering in DB --- .../net/vonforst/evmap/model/ChargersModel.kt | 5 +++-- .../evmap/storage/ChargeLocationsDao.kt | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/vonforst/evmap/model/ChargersModel.kt b/app/src/main/java/net/vonforst/evmap/model/ChargersModel.kt index ff03c4e19..de533a127 100644 --- a/app/src/main/java/net/vonforst/evmap/model/ChargersModel.kt +++ b/app/src/main/java/net/vonforst/evmap/model/ChargersModel.kt @@ -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 @@ -352,8 +353,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? = null ) : ChargepointListItem() diff --git a/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt b/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt index 9328b4414..53b82026b 100644 --- a/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt +++ b/app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt @@ -98,6 +98,18 @@ abstract class ChargeLocationsDao { @RawQuery(observedEntities = [ChargeLocation::class]) abstract fun getChargeLocationsCustom(query: SupportSQLiteQuery): LiveData> + @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> + @Query("SELECT COUNT(*) FROM chargelocation") abstract fun getCount(): LiveData @@ -109,6 +121,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.