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

commonMain.earth.worldwind.ogc.WmtsTileFactory.kt Maven / Gradle / Ivy

Go to download

The WorldWind Kotlin SDK (WWK) includes the library, examples and tutorials for building multiplatform 3D virtual globe applications for Android, Web and Java.

The newest version!
package earth.worldwind.ogc

import earth.worldwind.geom.Sector
import earth.worldwind.render.image.ImageSource.Companion.fromUrlString
import earth.worldwind.render.image.ImageTile
import earth.worldwind.util.Level
import earth.worldwind.util.Logger
import earth.worldwind.util.Logger.logMessage
import earth.worldwind.util.TileFactory

open class WmtsTileFactory(
    private val template: String,
    private val tileMatrixIdentifiers: List,
    private val firstLevelHeight: Int,
    val imageFormat: String
): TileFactory {
    override val contentType = "WMTS 1.0.0"

    override fun createTile(sector: Sector, level: Level, row: Int, column: Int) = ImageTile(sector, level, row, column).apply {
        urlForTile(level.levelNumber, row, column)?.let { urlString ->
            // Assign resource post-processor to transform received resource and save it in cache if necessary
            imageSource = fromUrlString(urlString).also { it.postprocessor = this }
        }
    }

    fun urlForTile(level: Int, row: Int, column: Int): String? {
        if (level >= tileMatrixIdentifiers.size) {
            logMessage(
                Logger.WARN, "WmtsTileFactory", "urlForTile",
                "invalid level for tileMatrixIdentifiers: $level"
            )
            return null
        }

        // flip the row index
        val rowHeight = firstLevelHeight shl level
        val flipRow = rowHeight - row - 1
        return template.replace(TILE_MATRIX_TEMPLATE, tileMatrixIdentifiers[level])
            .replace(TILE_ROW_TEMPLATE, flipRow.toString()).replace(TILE_COL_TEMPLATE, column.toString())
    }

    companion object {
        const val TILE_MATRIX_TEMPLATE = "{TileMatrix}"
        const val TILE_ROW_TEMPLATE = "{TileRow}"
        const val TILE_COL_TEMPLATE = "{TileCol}"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy