From 996f9b83cad3e31334e5715c7a48600bc5f26b08 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Thu, 18 Sep 2025 14:56:04 +0300 Subject: [PATCH] KhmParser start --- .../com/mirenkov/ktheightmap/KhmParser.kt | 27 ++++++++++ .../com/mirenkov/ktheightmap/MapCanvas.kt | 3 -- .../mirenkov/ktheightmap/SettingsActivity.kt | 53 +++++++++++-------- 3 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/mirenkov/ktheightmap/KhmParser.kt diff --git a/app/src/main/java/com/mirenkov/ktheightmap/KhmParser.kt b/app/src/main/java/com/mirenkov/ktheightmap/KhmParser.kt new file mode 100644 index 0000000..2de27b4 --- /dev/null +++ b/app/src/main/java/com/mirenkov/ktheightmap/KhmParser.kt @@ -0,0 +1,27 @@ +package com.mirenkov.ktheightmap + +import android.util.Log +import java.io.DataInputStream +import java.io.FileInputStream + +class KhmParser { + companion object { + fun parse(filePath: String) { + Log.i(TAG, "start") + val dis = DataInputStream(FileInputStream(filePath)) + with(dis) { + Log.i(TAG, "start2") + val minLon = readFloat() + val minLat = readFloat() + val lonPerPixel = readFloat() + val latPerPixel = readFloat() + val rasterWidth = readInt() + val rasterHeight = readInt() + Log.i(TAG, "mLon: %.6f, mLat: %.6f, pLon: %.6f, pLat: %.6f, w: %d, h: %d".format( + minLon, minLat, lonPerPixel, latPerPixel, rasterWidth, rasterHeight + )) + } + Log.i(TAG, "end") + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt b/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt index 8aa8619..51b5453 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt @@ -2,13 +2,10 @@ package com.mirenkov.ktheightmap import android.util.Log import androidx.compose.foundation.Canvas -import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableFloatState import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier diff --git a/app/src/main/java/com/mirenkov/ktheightmap/SettingsActivity.kt b/app/src/main/java/com/mirenkov/ktheightmap/SettingsActivity.kt index 7ba8f2c..f8e0c89 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/SettingsActivity.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/SettingsActivity.kt @@ -13,6 +13,10 @@ import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -31,7 +35,7 @@ import java.util.zip.ZipInputStream class SettingsActivity : ComponentActivity() { companion object { - var filePath: String? = null + var filePath: MutableState = mutableStateOf(null) } fun filePickerResult(result: FilePickerResult): String? { @@ -41,7 +45,7 @@ class SettingsActivity : ComponentActivity() { } else { val filePath = result.selectedFilePath filePath?.let { - if (it.endsWith(".zip")) + if (it.endsWith(".zip") || it.endsWith(".khm")) return filePath } return null @@ -50,7 +54,7 @@ class SettingsActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val launcher = registerForActivityResult(FilePickerResultContracts.PickDocumentFile()) - { result -> filePath = filePickerResult(result) } + { result -> filePath.value = filePickerResult(result) } enableEdgeToEdge() setContent { @@ -70,31 +74,34 @@ class SettingsActivity : ComponentActivity() { @Composable fun SettingsMain(vm: TileViewModel, launcher: ActivityResultLauncher) { val coroutineScope = rememberCoroutineScope() + val filePath by remember { SettingsActivity.filePath } KtHeightMapTheme { Column(Modifier.fillMaxSize() .safeDrawingPadding()) { - Button(onClick = { - launcher.launch(null) - }) { Text(text = "Select database") } - Button(onClick = { coroutineScope.launch { - processZip(SettingsActivity.filePath).forEach { - with(vm.repository) { - val t = getTile(it.x, it.y, it.level) - if (t == null) { - pushTile(it) - Log.i(TAG, "pushed to db: %d, %d, %d".format(it.level, it.x, it.y)) - } - else - Log.i(TAG, "found in db: %d, %d, %d".format(t.level, t.x, t.y)) + Button( + onClick = { launcher.launch(null) } + ) { Text(text = "Select database") } + Button( + onClick = { coroutineScope.launch { + processZip(filePath).forEach { + with(vm.repository) { + val t = getTile(it.x, it.y, it.level) + if (t == null) { + pushTile(it) + Log.i(TAG, "pushed to db: %d, %d, %d".format(it.level, it.x, it.y)) + } + else + Log.i(TAG, "found in db: %d, %d, %d".format(t.level, t.x, t.y)) } } - } - }) { Text(text = "Load database") } - Button(onClick = { - coroutineScope.launch { - vm.repository.clearTiles() - } - }) { Text(text = "Clear database") } + } }, + ) { Text(text = "Load .zip") } + Button( + onClick = { coroutineScope.launch { vm.repository.clearTiles() } }, + ) { Text(text = "Clear database") } + Button( + onClick = { filePath?.let { KhmParser.parse(it) } }, + ) { Text(text = "Load .khm") } } } }