App now finally represents feature for which it was named
This commit is contained in:
parent
996f9b83ca
commit
49cd9c6154
3 changed files with 53 additions and 14 deletions
|
|
@ -1,27 +1,61 @@
|
||||||
package com.mirenkov.ktheightmap
|
package com.mirenkov.ktheightmap
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import java.io.DataInputStream
|
import java.io.DataInputStream
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
|
|
||||||
|
private data class HeightInfo(
|
||||||
|
val minLon: Float,
|
||||||
|
val minLat: Float,
|
||||||
|
val lonPerValue: Float,
|
||||||
|
val latPerValue: Float,
|
||||||
|
val width: Int,
|
||||||
|
val height: Int
|
||||||
|
)
|
||||||
|
|
||||||
class KhmParser {
|
class KhmParser {
|
||||||
companion object {
|
companion object {
|
||||||
fun parse(filePath: String) {
|
const val HEIGHT_FILE: String = "height.khm"
|
||||||
Log.i(TAG, "start")
|
fun load(filePath: String, ctx: Context) {
|
||||||
val dis = DataInputStream(FileInputStream(filePath))
|
if (ctx.getFileStreamPath(HEIGHT_FILE).exists()) {
|
||||||
with(dis) {
|
Log.i(TAG, "height.khm already exists")
|
||||||
Log.i(TAG, "start2")
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val inp = FileInputStream(filePath)
|
||||||
|
ctx.openFileOutput(HEIGHT_FILE, Context.MODE_PRIVATE).use {
|
||||||
|
it.write(inp.readBytes())
|
||||||
|
Log.i(TAG, "Copied %s to height.khm".format(filePath))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getOffset(header: HeightInfo, x: Int, y: Int): Int {
|
||||||
|
Log.i(TAG, "Offset for (%d, %d) = %d".format(x, y, y * header.height + x))
|
||||||
|
return (x * header.width + y) * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun readHeader(dis: DataInputStream): HeightInfo {
|
||||||
|
return with(dis) {
|
||||||
val minLon = readFloat()
|
val minLon = readFloat()
|
||||||
val minLat = readFloat()
|
val minLat = readFloat()
|
||||||
val lonPerPixel = readFloat()
|
val lonPerValue = readFloat()
|
||||||
val latPerPixel = readFloat()
|
val latPerValue = readFloat()
|
||||||
val rasterWidth = readInt()
|
val width = readInt()
|
||||||
val rasterHeight = readInt()
|
val height = readInt()
|
||||||
Log.i(TAG, "mLon: %.6f, mLat: %.6f, pLon: %.6f, pLat: %.6f, w: %d, h: %d".format(
|
Log.i(TAG, "Header: %.6f, %.6f, %.6f, %.6f, %d, %d".format(minLon, minLat, lonPerValue, latPerValue, width, height))
|
||||||
minLon, minLat, lonPerPixel, latPerPixel, rasterWidth, rasterHeight
|
HeightInfo( minLon, minLat, lonPerValue, latPerValue, width, height )
|
||||||
))
|
|
||||||
}
|
}
|
||||||
Log.i(TAG, "end")
|
}
|
||||||
|
|
||||||
|
fun getHeight(lon: Float, lat: Float, ctx: Context): UShort {
|
||||||
|
val dis = DataInputStream(ctx.openFileInput(HEIGHT_FILE))
|
||||||
|
val header = readHeader(dis)
|
||||||
|
val x = ((lon - header.minLon) / header.lonPerValue).toInt()
|
||||||
|
val y = ((lat - header.minLat) / header.latPerValue).toInt()
|
||||||
|
val offset = getOffset(header, x, y)
|
||||||
|
dis.skipBytes(offset)
|
||||||
|
return dis.readUnsignedShort().toUShort()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -175,6 +175,7 @@ fun ToolButton(viewModel: TileViewModel) {
|
||||||
@Suppress("VariableNeverRead", "AssignedValueIsNeverRead")
|
@Suppress("VariableNeverRead", "AssignedValueIsNeverRead")
|
||||||
@Composable
|
@Composable
|
||||||
fun SetLocationDialog(vm: TileViewModel, dialogShown: MutableState<Boolean>) {
|
fun SetLocationDialog(vm: TileViewModel, dialogShown: MutableState<Boolean>) {
|
||||||
|
val ctx = LocalContext.current
|
||||||
var showLocationDialog by dialogShown
|
var showLocationDialog by dialogShown
|
||||||
if (showLocationDialog) {
|
if (showLocationDialog) {
|
||||||
var latitudeText by remember { vm.latitudeText }
|
var latitudeText by remember { vm.latitudeText }
|
||||||
|
|
@ -214,6 +215,7 @@ fun SetLocationDialog(vm: TileViewModel, dialogShown: MutableState<Boolean>) {
|
||||||
Log.i(TAG, "Y = %.6f".format(SphereMercator.mercateLat(latitude, vm.scale.floatValue.toInt(), -TILE_SIZE.toDouble())))
|
Log.i(TAG, "Y = %.6f".format(SphereMercator.mercateLat(latitude, vm.scale.floatValue.toInt(), -TILE_SIZE.toDouble())))
|
||||||
offsetX = SphereMercator.mercateLon(longitude, vm.scale.floatValue.toInt(), -vm.halvedOffsetX!!.toDouble() - TILE_SIZE).toFloat()
|
offsetX = SphereMercator.mercateLon(longitude, vm.scale.floatValue.toInt(), -vm.halvedOffsetX!!.toDouble() - TILE_SIZE).toFloat()
|
||||||
offsetY = SphereMercator.mercateLat(latitude, vm.scale.floatValue.toInt(), -vm.halvedOffsetY!!.toDouble() - TILE_SIZE).toFloat()
|
offsetY = SphereMercator.mercateLat(latitude, vm.scale.floatValue.toInt(), -vm.halvedOffsetY!!.toDouble() - TILE_SIZE).toFloat()
|
||||||
|
Log.i(TAG, "%d".format(KhmParser.getHeight(longitude.toFloat(), latitude.toFloat(), ctx).toInt()))
|
||||||
}) {
|
}) {
|
||||||
Text("Confirm".uppercase())
|
Text("Confirm".uppercase())
|
||||||
} },
|
} },
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@ class SettingsActivity : ComponentActivity() {
|
||||||
fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFilePickerConfig?>) {
|
fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFilePickerConfig?>) {
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val filePath by remember { SettingsActivity.filePath }
|
val filePath by remember { SettingsActivity.filePath }
|
||||||
|
val ctx = LocalContext.current
|
||||||
KtHeightMapTheme {
|
KtHeightMapTheme {
|
||||||
Column(Modifier.fillMaxSize()
|
Column(Modifier.fillMaxSize()
|
||||||
.safeDrawingPadding()) {
|
.safeDrawingPadding()) {
|
||||||
|
|
@ -100,7 +101,9 @@ fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher<DocumentFil
|
||||||
onClick = { coroutineScope.launch { vm.repository.clearTiles() } },
|
onClick = { coroutineScope.launch { vm.repository.clearTiles() } },
|
||||||
) { Text(text = "Clear database") }
|
) { Text(text = "Clear database") }
|
||||||
Button(
|
Button(
|
||||||
onClick = { filePath?.let { KhmParser.parse(it) } },
|
onClick = { filePath?.let {
|
||||||
|
KhmParser.load(it, ctx)
|
||||||
|
} },
|
||||||
) { Text(text = "Load .khm") }
|
) { Text(text = "Load .khm") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue