up
This commit is contained in:
@@ -49,6 +49,31 @@ export function gridToLatLon(grid: string): { lat: number; lon: number } | null
|
||||
return { lat, lon };
|
||||
}
|
||||
|
||||
// latLonToGrid encodes a lat/lon to a Maidenhead locator (default 6 chars).
|
||||
// Inverse of gridToLatLon. Used to derive a default grid from a cty.dat entity
|
||||
// centroid when no provider grid is available (e.g. a portable/rare DX call) —
|
||||
// a centre-of-entity locator is more useful than an empty field.
|
||||
export function latLonToGrid(lat: number, lon: number, precision = 6): string {
|
||||
let adjLon = Math.min(359.9999, Math.max(0, lon + 180));
|
||||
let adjLat = Math.min(179.9999, Math.max(0, lat + 90));
|
||||
const A = 'A'.charCodeAt(0);
|
||||
const a = 'a'.charCodeAt(0);
|
||||
const fLon = Math.floor(adjLon / 20);
|
||||
const fLat = Math.floor(adjLat / 10);
|
||||
adjLon -= fLon * 20;
|
||||
adjLat -= fLat * 10;
|
||||
const sLon = Math.floor(adjLon / 2);
|
||||
const sLat = Math.floor(adjLat);
|
||||
let grid = String.fromCharCode(A + fLon) + String.fromCharCode(A + fLat) + sLon + sLat;
|
||||
if (precision >= 6) {
|
||||
adjLon -= sLon * 2;
|
||||
adjLat -= sLat;
|
||||
grid += String.fromCharCode(a + Math.min(23, Math.floor(adjLon * 12)))
|
||||
+ String.fromCharCode(a + Math.min(23, Math.floor(adjLat * 24)));
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
// gridSquareBounds returns the SW/NE corners of a Maidenhead square so a map
|
||||
// can draw its outline. Half-extents shrink with locator precision.
|
||||
export function gridSquareBounds(grid: string):
|
||||
|
||||
Reference in New Issue
Block a user