Skip to content

Commit

Permalink
add regex support and improve parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesvangurp committed Sep 15, 2023
1 parent 32b7c15 commit 84c81f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
31 changes: 23 additions & 8 deletions src/commonMain/kotlin/com/jillesvangurp/geo/utmcoordinates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,31 @@ data class UTM(
}
}

fun String.parseUTM(): UTM {
val parts = split("\\s+".toRegex())
val zone = parts[0].toInt()
val letter = parts[1].uppercase()[0]
val easting = parts[2].toDouble()
val northing = parts[3].toDouble()
return UTM(zone = zone, letter = letter, easting = easting, northing = northing)
val utmRegex = "(([0-9]{1,2})\\s*([a-zA-Z])\\s+(\\d*\\.?\\d+)\\s+(\\d*\\.?\\d+))".toRegex()

fun String.parseUTM(): UTM? {
return utmRegex.matchEntire(this)?.let {
UTM(
zone = it.groups[2]!!.value.toInt(),
letter = it.groups[3]!!.value.uppercase()[0],
easting = it.groups[4]!!.value.toDouble(),
northing = it.groups[5]!!.value.toDouble()
)
}
}

fun String.findUTMCoordinates(): List<UTM> {
return utmRegex.findAll(this).map {
UTM(
zone = it.groups[2]!!.value.toInt(),
letter = it.groups[3]!!.value.uppercase()[0],
easting = it.groups[4]!!.value.toDouble(),
northing = it.groups[5]!!.value.toDouble()
)
}.toList()
}

val String.utmAsWgs84 get() = parseUTM().toWgs84()
val String.utmAsWgs84 get() = parseUTM()?.toWgs84()

fun PointCoordinates.toUTM(): UTM {
val zone = floor(longitude / 6 + 31).toInt()
Expand Down
53 changes: 27 additions & 26 deletions src/commonTest/kotlin/com/jillesvangurp/geogeometry/UTMTest.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
package com.jillesvangurp.geogeometry

import com.jillesvangurp.geo.GeoGeometry
import com.jillesvangurp.geo.format
import com.jillesvangurp.geo.toUTM
import com.jillesvangurp.geo.utmAsWgs84
import com.jillesvangurp.geojson.PointCoordinates
import io.kotest.assertions.assertionCounter
import io.kotest.assertions.collectOrThrow
import io.kotest.assertions.eq.eq
import io.kotest.assertions.errorCollector
import io.kotest.matchers.Matcher
import io.kotest.matchers.doubles.shouldBeLessThan
import io.kotest.matchers.should
import com.jillesvangurp.geo.*
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlin.test.Test

class UTMTest {
Expand All @@ -21,21 +11,32 @@ class UTMTest {
fun shouldConvertCoordinates() {
brandenBurgerGate.toUTM().toString() shouldBe "33 U 389880.94 5819700.4"
brandenBurgerGate.format shouldBe "52.516279N 13.377157E"
"33 U 389880.94 5819700.4".utmAsWgs84 shouldBeNear brandenBurgerGate
"33 U 389880.94 5819700.4".utmAsWgs84!! shouldBeNear brandenBurgerGate
}
}

@Test
fun shouldDetectUtmCoordinatesInStrings() {
val utmCoordinateStrings = listOf(
"33 U 389880.94 5819700.4 bla bla",
"bla bla 33\tU\t\t389880.94\t\t5819700.4",
"33U 3898 5819"
)
utmCoordinateStrings.forEach { str ->
str.findUTMCoordinates().size shouldBe 1
}

utmCoordinateStrings.joinToString(" ").findUTMCoordinates().size shouldBe utmCoordinateStrings.size
}


//@Suppress("UNCHECKED_CAST")
//infix fun <T, U : T> T.shouldBe(expected: U?): T {
// when (expected) {
// is Matcher<*> -> should(expected as Matcher<T>)
// else -> {
// val actual = this
// assertionCounter.inc()
// eq(actual, expected)?.let(errorCollector::collectOrThrow)
// }
// }
// return this
//}
@Test
fun shouldParseUtmCoordinates() {
val utmCoordinates = listOf(
"33 U 3898.111111 5819",
"33U 3898 5819",
"33 U 389880.94 5819700.4"
).forEach { str ->
str.parseUTM() shouldNotBe null
}
}
}

0 comments on commit 84c81f3

Please sign in to comment.