Skip to content

Commit

Permalink
WIP: clustering in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Dec 2, 2023
1 parent b85ce9a commit 107ec10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/net/vonforst/evmap/model/ChargersModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ChargeLocation>? = null
) : ChargepointListItem()

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/net/vonforst/evmap/storage/ChargeLocationsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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>

Expand All @@ -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.
Expand Down

0 comments on commit 107ec10

Please sign in to comment.