hamlocator/src/maidenhead.vala

39 wiersze
1.2 KiB
Vala

// Ported from:
// https://metacpan.org/release/MEH/Ham-Locator-0.1000/source/lib/Ham/Locator.pm
namespace Hamlocator {
public class LocationToMaidenhead : Object {
private string l2m;
public LocationToMaidenhead() {
this.l2m = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
public string get_locator(GClue.Location pos) {
double lat = pos.latitude + 90;
double lon = pos.longitude + 180;
// Field
double lat1 = (lat / 10);
double lon1 = (lon / 20);
string loc1 = @"$(this.l2m[Math.lround(Math.floor(lon1))])$(this.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 = @"$(this.l2m[Math.lround(Math.floor(lon3))])$(this.l2m[Math.lround(Math.floor(lat3))])".down();
return @"$loc1$loc2$loc3";
}
}
}