From ca92f8581e7bd77fa1aedee83e64ccae5874491d Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Tue, 27 Oct 2020 00:29:08 +0000 Subject: [PATCH] Added Maidenhead locator conversion functions and tests #2 --- .../rtbishop/look4sat/utility/Utilities.kt | 33 ++++++++++++++++ .../com/rtbishop/look4sat/LocationTest.kt | 39 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 app/src/test/java/com/rtbishop/look4sat/LocationTest.kt diff --git a/app/src/main/java/com/rtbishop/look4sat/utility/Utilities.kt b/app/src/main/java/com/rtbishop/look4sat/utility/Utilities.kt index df41f491..764ddbed 100644 --- a/app/src/main/java/com/rtbishop/look4sat/utility/Utilities.kt +++ b/app/src/main/java/com/rtbishop/look4sat/utility/Utilities.kt @@ -2,6 +2,7 @@ package com.rtbishop.look4sat.utility import android.content.SharedPreferences import android.view.View +import com.github.amsacode.predict4java.GroundStationPosition import com.google.android.material.snackbar.Snackbar import java.util.concurrent.TimeUnit import kotlin.math.round @@ -33,4 +34,36 @@ object Utilities { fun SharedPreferences.getDouble(key: String, default: Double): Double { return Double.fromBits(getLong(key, default.toRawBits())) } + + fun qthToGSP(qthString: String): GroundStationPosition { + val latFirst = (qthString[1].toInt() - 65) * 10 + val latSecond = qthString[3].toString().toInt() + val latThird = (((qthString[5].toInt() - 97) / 24.0) + (1.0 / 48.0)) - 90 + val latitude = latFirst + latSecond + latThird + + val lonFirst = (qthString[0].toInt() - 65) * 20 + val lonSecond = qthString[2].toString().toInt() * 2 + val lonThird = (((qthString[4].toInt() - 97) / 12.0) + (1.0 / 24.0)) - 180 + val longitude = lonFirst + lonSecond + lonThird + + return GroundStationPosition(latitude, longitude, 0.0) + } + + fun locToQTH(lat: Double, lon: Double): String { + val tempLon = if (lon > 180.0) lon - 360 else lon + val upper = "ABCDEFGHIJKLMNOPQRSTUVWX" + val lower = "abcdefghijklmnopqrstuvwx" + + val latitude = lat + 90 + val latFirst = upper[(latitude / 10).toInt()] + val latSecond = (latitude % 10).toInt().toString() + val latThird = lower[((latitude % 1) * 24).toInt()] + + val longitude = tempLon + 180 + val lonFirst = upper[(longitude / 20).toInt()] + val lonSecond = ((longitude / 2) % 10).toInt().toString() + val lonThird = lower[((longitude % 2) * 12).toInt()] + + return "$lonFirst$latFirst$lonSecond$latSecond$lonThird$latThird" + } } \ No newline at end of file diff --git a/app/src/test/java/com/rtbishop/look4sat/LocationTest.kt b/app/src/test/java/com/rtbishop/look4sat/LocationTest.kt new file mode 100644 index 00000000..0b4858a8 --- /dev/null +++ b/app/src/test/java/com/rtbishop/look4sat/LocationTest.kt @@ -0,0 +1,39 @@ +package com.rtbishop.look4sat + +import com.rtbishop.look4sat.utility.Utilities +import com.rtbishop.look4sat.utility.Utilities.round +import org.junit.Test + +class LocationTest { + @Test + fun testLocToQth() { + assert(Utilities.locToQTH(51.4878, -0.2146) == "IO91vl") + assert(Utilities.locToQTH(48.1466, 11.6083) == "JN58td") + assert(Utilities.locToQTH(-34.91, -56.2116) == "GF15vc") + assert(Utilities.locToQTH(38.92, -77.065) == "FM18lw") + assert(Utilities.locToQTH(-41.2833, 174.745) == "RE78ir") + assert(Utilities.locToQTH(41.7147, -72.7272) == "FN31pr") + assert(Utilities.locToQTH(37.4137, -122.1073) == "CM87wj") + assert(Utilities.locToQTH(35.0542, -85.1142) == "EM75kb") + } + + @Test + fun testQthToLoc() { + var result = Utilities.qthToGSP("IO91vl") + assert(result.latitude.round(4) == 51.4792 && result.longitude.round(4) == -0.2083) + result = Utilities.qthToGSP("JN58td") + assert(result.latitude.round(4) == 48.1458 && result.longitude.round(4) == 11.625) + result = Utilities.qthToGSP("GF15vc") + assert(result.latitude.round(4) == -34.8958 && result.longitude.round(4) == -56.2083) + result = Utilities.qthToGSP("FM18lw") + assert(result.latitude.round(4) == 38.9375 && result.longitude.round(4) == -77.0417) + result = Utilities.qthToGSP("RE78ir") + assert(result.latitude.round(4) == -41.2708 && result.longitude.round(4) == 174.7083) + result = Utilities.qthToGSP("FN31pr") + assert(result.latitude.round(4) == 41.7292 && result.longitude.round(4) == -72.7083) + result = Utilities.qthToGSP("CM87wj") + assert(result.latitude.round(4) == 37.3958 && result.longitude.round(4) == -122.125) + result = Utilities.qthToGSP("EM75kb") + assert(result.latitude.round(4) == 35.0625 && result.longitude.round(4) == -85.125) + } +} \ No newline at end of file