Fixed issue #44, now accepting all valid locators

pull/49/head
Arty Bishop 2021-02-27 10:58:07 +00:00
rodzic 68fbd4fc1a
commit f6ca069896
5 zmienionych plików z 58 dodań i 114 usunięć

Wyświetl plik

@ -30,7 +30,7 @@ import com.github.amsacode.predict4java.SatPos
import com.rtbishop.look4sat.data.SatPass
import com.rtbishop.look4sat.data.SelectedSat
import com.rtbishop.look4sat.utility.PrefsManager
import com.rtbishop.look4sat.utility.Utilities
import com.rtbishop.look4sat.utility.QthConverter
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
@ -48,7 +48,10 @@ import kotlin.math.pow
import kotlin.math.sqrt
@HiltViewModel
class MapViewModel @Inject constructor(val prefsManager: PrefsManager) : ViewModel() {
class MapViewModel @Inject constructor(
private val prefsManager: PrefsManager,
private val qthConverter: QthConverter
) : ViewModel() {
private val dateNow = Date()
private val trackPaint = Paint().apply {
@ -109,7 +112,7 @@ class MapViewModel @Inject constructor(val prefsManager: PrefsManager) : ViewMod
withContext(Dispatchers.Default) {
val satPos = pass.predictor.getSatPos(dateNow)
val osmPos = getOsmPosition(satPos.latitude, satPos.longitude, true)
val qthLoc = Utilities.locToQTH(osmPos.lat, osmPos.lon)
val qthLoc = qthConverter.locationToQTH(osmPos.lat, osmPos.lon) ?: "-- --"
val velocity = getSatVelocity(satPos.altitude)
val coverage = satPos.rangeCircleRadiusKm * 2
val footprint = getSatFootprint(satPos)

Wyświetl plik

@ -30,7 +30,7 @@ import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.snackbar.Snackbar
import com.rtbishop.look4sat.R
import com.rtbishop.look4sat.utility.PrefsManager
import com.rtbishop.look4sat.utility.Utilities
import com.rtbishop.look4sat.utility.QthConverter
import com.rtbishop.look4sat.utility.round
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
@ -47,6 +47,9 @@ class PrefsFragment : PreferenceFragmentCompat() {
@Inject
lateinit var prefsManager: PrefsManager
@Inject
lateinit var qthConverter: QthConverter
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference, rootKey)
}
@ -68,18 +71,15 @@ class PrefsFragment : PreferenceFragmentCompat() {
}
}
private fun setPositionFromQth(qthLocator: String): Boolean {
val qthPattern = "[A-X][A-X][0-9][0-9][a-x][a-x]".toRegex()
return if (qthLocator.matches(qthPattern)) {
val location = Utilities.qthToGSP(qthLocator)
val latitude = location.latitude.round(4)
val longitude = location.longitude.round(4)
prefsManager.setStationPosition(latitude, longitude, location.heightAMSL)
showSnack(getString(R.string.pref_pos_success))
true
} else {
private fun setPositionFromQth(qthString: String): Boolean {
val loc = qthConverter.qthToLocation(qthString)
return if (loc == null) {
showSnack(getString(R.string.pref_pos_qth_error))
false
} else {
prefsManager.setStationPosition(loc.latitude, loc.longitude, loc.heightAMSL)
showSnack(getString(R.string.pref_pos_success))
true
}
}

Wyświetl plik

@ -25,20 +25,21 @@ import javax.inject.Inject
class QthConverter @Inject constructor() {
fun qthToLocation(qthString: String): GroundStationPosition? {
if (!isValidQTH(qthString)) return null
val lonFirst = (qthString[0].toUpperCase().toInt() - 65) * 20
val latFirst = (qthString[1].toUpperCase().toInt() - 65) * 10
val lonSecond = qthString[2].toString().toInt() * 2
val latSecond = qthString[3].toString().toInt()
val lonThird = (((qthString[4].toLowerCase().toInt() - 97) / 12.0) + (1.0 / 24.0)) - 180
val latThird = (((qthString[5].toLowerCase().toInt() - 97) / 24.0) + (1.0 / 48.0)) - 90
val longitude = lonFirst + lonSecond + lonThird
val latitude = latFirst + latSecond + latThird
val trimmedQth = qthString.take(6)
if (!isValidQTH(trimmedQth)) return null
val lonFirst = (trimmedQth[0].toUpperCase().toInt() - 65) * 20
val latFirst = (trimmedQth[1].toUpperCase().toInt() - 65) * 10
val lonSecond = trimmedQth[2].toString().toInt() * 2
val latSecond = trimmedQth[3].toString().toInt()
val lonThird = (((trimmedQth[4].toLowerCase().toInt() - 97) / 12.0) + (1.0 / 24.0)) - 180
val latThird = (((trimmedQth[5].toLowerCase().toInt() - 97) / 24.0) + (1.0 / 48.0)) - 90
val longitude = (lonFirst + lonSecond + lonThird).round(4)
val latitude = (latFirst + latSecond + latThird).round(4)
return GroundStationPosition(latitude, longitude, 0.0)
}
fun locationToQTH(lat: Double, lon: Double): String? {
if (!isValidLoc(lat, lon)) return null
if (!isValidLocation(lat, lon)) return null
val tempLon = if (lon > 180.0) lon - 360 else lon
val upper = "ABCDEFGHIJKLMNOPQRSTUVWX"
val lower = "abcdefghijklmnopqrstuvwx"
@ -58,7 +59,7 @@ class QthConverter @Inject constructor() {
return qthString.matches(qthPattern)
}
private fun isValidLoc(lat: Double, lon: Double): Boolean {
private fun isValidLocation(lat: Double, lon: Double): Boolean {
return (lat > -90.0 && lat < 90.0) && (lon > -180.0 && lon < 360.0)
}
}

Wyświetl plik

@ -1,59 +0,0 @@
/*******************************************************************************
Look4Sat. Amateur radio satellite tracker and pass predictor.
Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
******************************************************************************/
package com.rtbishop.look4sat.utility
import com.github.amsacode.predict4java.GroundStationPosition
import java.util.concurrent.TimeUnit
import kotlin.math.round
object Utilities {
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"
}
}

Wyświetl plik

@ -20,13 +20,32 @@ with this program; if not, write to the Free Software Foundation, Inc.,
package com.rtbishop.look4sat
import com.rtbishop.look4sat.utility.QthConverter
import com.rtbishop.look4sat.utility.round
import org.junit.Test
class QthConverterTest {
private val converter = QthConverter()
@Test
fun `Given valid QTH returns correct location`() {
var result = converter.qthToLocation("io91VL39FX")
assert(result?.latitude == 51.4792 && result.longitude == -0.2083)
result = converter.qthToLocation("JN58TD")
assert(result?.latitude == 48.1458 && result.longitude == 11.6250)
result = converter.qthToLocation("gf15vc")
assert(result?.latitude == -34.8958 && result.longitude == -56.2083)
result = converter.qthToLocation("fm18LW")
assert(result?.latitude == 38.9375 && result.longitude == -77.0417)
}
@Test
fun `Given invalid QTH returns null`() {
var result = converter.qthToLocation("ZZ00zz")
assert(result == null)
result = converter.qthToLocation("JN58tz")
assert(result == null)
}
@Test
fun `Given valid location returns correct QTH`() {
assert(converter.locationToQTH(51.4878, -0.2146) == "IO91vl")
@ -40,24 +59,4 @@ class QthConverterTest {
assert(converter.locationToQTH(91.0542, -170.1142) == null)
assert(converter.locationToQTH(89.0542, -240.1142) == null)
}
@Test
fun `Given valid QTH returns correct location`() {
var result = converter.qthToLocation("IO91vl")
assert(result?.latitude?.round(4) == 51.4792 && result.longitude.round(4) == -0.2083)
result = converter.qthToLocation("JN58TD")
assert(result?.latitude?.round(4) == 48.1458 && result.longitude.round(4) == 11.625)
result = converter.qthToLocation("gf15vc")
assert(result?.latitude?.round(4) == -34.8958 && result.longitude.round(4) == -56.2083)
result = converter.qthToLocation("fm18LW")
assert(result?.latitude?.round(4) == 38.9375 && result.longitude.round(4) == -77.0417)
}
@Test
fun `Given invalid QTH returns correct location`() {
var result = converter.qthToLocation("ZZ00zz")
assert(result == null)
result = converter.qthToLocation("em75KBzz")
assert(result == null)
}
}