kopia lustrzana https://git.sr.ht/~michalr/hamlocator
69 wiersze
2.5 KiB
Vala
69 wiersze
2.5 KiB
Vala
|
|
// Ported from:
|
|
// https://metacpan.org/release/MEH/Ham-Locator-0.1000/source/lib/Ham/Locator.pm
|
|
|
|
namespace Hamlocator {
|
|
public class MaidenheadConverter : Object {
|
|
private const char[] L2M = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
|
|
private const double[] M2L_MULTIPLIERS = {10.0, 1.0, 1.0/24.0, 1.0/240.0, 1.0/5760.0};
|
|
|
|
public MaidenheadConverter() {}
|
|
|
|
public string location_to_maidenhead(GClue.Location pos) {
|
|
double lat = pos.latitude + 90;
|
|
double lon = pos.longitude + 180;
|
|
|
|
// Field
|
|
double lat1 = (lat / 10);
|
|
double lon1 = (lon / 20);
|
|
|
|
string loc1 = @"$(L2M[Math.lround(Math.floor(lon1))])$(L2M[Math.lround(Math.floor(lat1))])";
|
|
|
|
// Square
|
|
double lat2 = 10 * (lat1 - Math.floor(lat1));
|
|
double lon2 = 10 * (lon1 - Math.floor(lon1));
|
|
|
|
string loc2 = @"$(Math.lround(Math.floor(lon2)))$(Math.lround(Math.floor(lat2)))";
|
|
|
|
// Subsquare
|
|
double lat3 = 24 * (lat2 - Math.floor(lat2));
|
|
double lon3 = 24 * (lon2 - Math.floor(lon2));
|
|
|
|
string loc3 = @"$(L2M[Math.lround(Math.floor(lon3))])$(L2M[Math.lround(Math.floor(lat3))])".down();
|
|
|
|
return @"$loc1$loc2$loc3";
|
|
}
|
|
|
|
public GClue.Location? maidenhead_to_location(string locator) {
|
|
double lat = 0.0;
|
|
double lon = 0.0;
|
|
|
|
if (locator.length % 2 != 0) {
|
|
warning("Locator needs to be of even length");
|
|
return null;
|
|
}
|
|
long i = 0; // where we are in the locator
|
|
long mult_i = 0; // which multiplier are we currently using
|
|
var upLocator = locator.up();
|
|
while (i < upLocator.length) {
|
|
// letters
|
|
lon += (upLocator[i++] - 'A') * 2.0 * M2L_MULTIPLIERS[mult_i];
|
|
lat += (upLocator[i++] - 'A') * M2L_MULTIPLIERS[mult_i];
|
|
mult_i++;
|
|
if (i >= upLocator.length) { break; }
|
|
// numbers
|
|
lon += (upLocator[i++] - '0') * 2.0 * M2L_MULTIPLIERS[mult_i];
|
|
lat += (upLocator[i++] - '0') * M2L_MULTIPLIERS[mult_i];
|
|
mult_i++;
|
|
}
|
|
|
|
// move to the center of the box
|
|
mult_i--;
|
|
lon += 0.5 * 2.0 * M2L_MULTIPLIERS[mult_i];
|
|
lat += 0.5 * M2L_MULTIPLIERS[mult_i];
|
|
|
|
return new SimpleLocation((lat - 90.0), (lon - 180.0));
|
|
}
|
|
}
|
|
}
|