mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-04 11:37:22 +00:00
Added QthConverter.kt class and unit tests
This commit is contained in:
1 parent
68f124ca98
commit
02dd0a5a78
2 files changed
+127
No files matched your search
@@ -0,0 +1,64 @@
|
||||
/*******************************************************************************
|
||||
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 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
|
||||
return GroundStationPosition(latitude, longitude, 0.0)
|
||||
}
|
||||
|
||||
fun locationToQTH(lat: Double, lon: Double): String? {
|
||||
if (!isValidLoc(lat, lon)) return null
|
||||
val tempLon = if (lon > 180.0) lon - 360 else lon
|
||||
val upper = "ABCDEFGHIJKLMNOPQRSTUVWX"
|
||||
val lower = "abcdefghijklmnopqrstuvwx"
|
||||
val longitude = tempLon + 180
|
||||
val latitude = lat + 90
|
||||
val lonFirst = upper[(longitude / 20).toInt()]
|
||||
val latFirst = upper[(latitude / 10).toInt()]
|
||||
val lonSecond = ((longitude / 2) % 10).toInt().toString()
|
||||
val latSecond = (latitude % 10).toInt().toString()
|
||||
val lonThird = lower[((longitude % 2) * 12).toInt()]
|
||||
val latThird = lower[((latitude % 1) * 24).toInt()]
|
||||
return "$lonFirst$latFirst$lonSecond$latSecond$lonThird$latThird"
|
||||
}
|
||||
|
||||
private fun isValidQTH(qthString: String): Boolean {
|
||||
val qthPattern = "[a-xA-X][a-xA-X][0-9][0-9][a-xA-X][a-xA-X]".toRegex()
|
||||
return qthString.matches(qthPattern)
|
||||
}
|
||||
|
||||
private fun isValidLoc(lat: Double, lon: Double): Boolean {
|
||||
return (lat > -90.0 && lat < 90.0) && (lon > -180.0 && lon < 360.0)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user