Skip to content

Commit

Permalink
Add support of GeoPackage content size determination.
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksymenko committed Jan 5, 2024
1 parent fa30b2e commit 0ae5d95
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ actual open class GeoPackage actual constructor(pathName: String, isReadOnly: Bo
} }
}

override suspend fun readTilesDataSize(tableName: String) = connection.openDatabase().use { database ->
database.rawQuery("SELECT SUM(LENGTH(tile_data)) FROM '$tableName'", emptyArray()).use { it.run {
if (moveToNext()) getInt(0) else 0
} }
}

override suspend fun deleteContent(content: GpkgContent) {
connection.openDatabase().use { database ->
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package earth.worldwind.globe.elevation

interface CacheSourceFactory : ElevationSourceFactory {
/**
* Unique key of this coverage in the cache
*/
val contentKey: String

/**
* Deletes all tiles from current cache content.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ package earth.worldwind.util
* Tile factory with cache support.
*/
interface CacheTileFactory : TileFactory {
/**
* Unique key of this layer in the cache
*/
val contentKey: String

/**
* Deletes all tiles from current cache content.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface CacheableElevationCoverage : ElevationCoverage {
/**
* Unique key of this coverage in the cache
*/
var contentKey: String?
val contentKey get() = cacheSourceFactory?.contentKey
/**
* Checks if cache is successfully configured
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ actual open class TiledElevationCoverage actual constructor(

protected val elevationDecoder = ElevationDecoder()
override var cacheSourceFactory: CacheSourceFactory? = null
override var contentKey: String? = null
override var isCacheOnly = false
/**
* Number of reties of bulk tile retrieval before long timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface CacheableImageLayer : Layer {
/**
* Unique key of this layer in the cache
*/
var contentKey: String?
val contentKey get() = tiledSurfaceImage?.cacheTileFactory?.contentKey
/**
* Checks if cache is successfully configured
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GpkgContentManager(pathName: String, readOnly: Boolean = false): ContentMa
} else {
TiledSurfaceImage(GpkgTileFactory(content), LevelSet(config))
})
}.also { if (it is CacheableImageLayer) it.contentKey = content.tableName }
}
}.onFailure {
Logger.logMessage(Logger.WARN, "GpkgContentManager", "getTiledImageLayers", it.message!!)
}.getOrNull()
Expand Down Expand Up @@ -89,7 +89,6 @@ class GpkgContentManager(pathName: String, readOnly: Boolean = false): ContentMa
if (!geoPackage.isReadOnly && config.numLevels < levelSet.numLevels) geoPackage.setupTileMatrices(contentKey, levelSet)
} ?: geoPackage.setupTilesContent(layer, contentKey, levelSet, boundingSector, setupWebLayer)

layer.contentKey = contentKey
layer.tiledSurfaceImage?.cacheTileFactory = GpkgTileFactory(content, imageFormat)
}

Expand Down Expand Up @@ -125,7 +124,7 @@ class GpkgContentManager(pathName: String, readOnly: Boolean = false): ContentMa
).apply { cacheSourceFactory = factory }

else -> TiledElevationCoverage(matrixSet, factory)
}.apply { contentKey = content.tableName }
}
}.onFailure {
Logger.logMessage(Logger.WARN, "GpkgContentManager", "getTiledElevationCoverages", it.message!!)
}.getOrNull()
Expand Down Expand Up @@ -153,10 +152,13 @@ class GpkgContentManager(pathName: String, readOnly: Boolean = false): ContentMa
}
} ?: geoPackage.setupGriddedCoverageContent(coverage, contentKey, boundingSector, setupWebCoverage, isFloat)

coverage.contentKey = contentKey
coverage.cacheSourceFactory = GpkgElevationSourceFactory(content, isFloat)
}

override suspend fun getTilesDataSize(contentKey: String) = withContext(Dispatchers.IO) {
geoPackage.readTilesDataSize(contentKey)
}

override fun getLastUpdateDate(contentKey: String) = geoPackage.content[contentKey]?.lastChange

override fun getBoundingSector(contentKey: String) = geoPackage.getBoundingSector(contentKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import earth.worldwind.globe.elevation.ElevationSource
import earth.worldwind.ogc.gpkg.GpkgContent

open class GpkgElevationSourceFactory(protected val tiles: GpkgContent, protected val isFloat: Boolean) : CacheSourceFactory {
override val contentKey get() = tiles.tableName

@Throws(IllegalStateException::class)
override suspend fun clearContent(deleteMetadata: Boolean) = if (deleteMetadata) {
tiles.container.deleteContent(tiles.tableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import earth.worldwind.util.CacheTileFactory
import earth.worldwind.util.Level

open class GpkgTileFactory(protected val tiles: GpkgContent, protected val imageFormat: String? = null): CacheTileFactory {
override val contentKey get() = tiles.tableName

@Throws(IllegalStateException::class)
override suspend fun clearContent(deleteMetadata: Boolean) = if (deleteMetadata) {
tiles.container.deleteContent(tiles.tableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ abstract class AbstractGeoPackage(val pathName: String, val isReadOnly: Boolean)
webServices.remove(tableName)?.let { deleteWebService(it) }
}

fun getBoundingSector(contentKey: String) = content[contentKey]?.let { content ->
fun getBoundingSector(tableName: String) = content[tableName]?.let { content ->
val minX = content.minX ?: return@let null
val minY = content.minY ?: return@let null
val maxX = content.maxX ?: return@let null
Expand Down Expand Up @@ -425,6 +425,8 @@ abstract class AbstractGeoPackage(val pathName: String, val isReadOnly: Boolean)

protected open fun latFromEPSG3857(y: Double) = (atan(exp(y / Ellipsoid.WGS84.semiMajorAxis)) * 2.0 - PI / 2.0).radians

protected open fun lonFromEPSG3857(x: Double) = (x / Ellipsoid.WGS84.semiMajorAxis).radians

protected open fun buildSector(
minX: Double, minY: Double, maxX: Double, maxY: Double, srsId: Int
) = if (srsId == EPSG_3857) MercatorSector.fromSector(Sector(
Expand All @@ -439,8 +441,6 @@ abstract class AbstractGeoPackage(val pathName: String, val isReadOnly: Boolean)
sector.maxLongitude.inDegrees, sector.maxLatitude.inDegrees
)

protected open fun lonFromEPSG3857(x: Double) = (x / Ellipsoid.WGS84.semiMajorAxis).radians

protected abstract suspend fun initConnection(pathName: String, isReadOnly: Boolean)

protected abstract suspend fun createRequiredTables()
Expand Down Expand Up @@ -470,6 +470,8 @@ abstract class AbstractGeoPackage(val pathName: String, val isReadOnly: Boolean)
protected abstract suspend fun readGriddedTile(tableName: String, tileId: Int): GpkgGriddedTile?
protected abstract suspend fun readTileUserData(tableName: String, zoom: Int, column: Int, row: Int): GpkgTileUserData?

abstract suspend fun readTilesDataSize(tableName: String): Int

protected abstract suspend fun deleteContent(content: GpkgContent)
protected abstract suspend fun deleteMatrixSet(matrixSet: GpkgTileMatrixSet)
protected abstract suspend fun deleteMatrix(matrix: GpkgTileMatrix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ interface ContentManager {
setupWebCoverage: Boolean = true, isFloat: Boolean = false
)

/**
* Gets estimated tiles data size
*
* @return Gets estimated tiles data size of specified content or null, if content does not exist
*/
suspend fun getTilesDataSize(contentKey: String): Int

/**
* Returns last update date of specified content
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import kotlinx.coroutines.Job
actual open class TiledImageLayer actual constructor(
name: String, tiledSurfaceImage: TiledSurfaceImage?
): AbstractTiledImageLayer(name, tiledSurfaceImage), CacheableImageLayer {
override var contentKey: String? = null

/**
* Start a new coroutine Job that downloads all imagery for a given sector and resolution,
* without downloading imagery that is already in the cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ actual open class GeoPackage actual constructor(pathName: String, isReadOnly: Bo
TODO("Not yet implemented")
}

override suspend fun readTilesDataSize(tableName: String): Int {
TODO("Not yet implemented")
}

override suspend fun deleteContent(content: GpkgContent) {
TODO("Not yet implemented")
}
Expand Down

0 comments on commit 0ae5d95

Please sign in to comment.