Last active
December 13, 2015 21:39
-
-
Save chomy/4979247 to your computer and use it in GitHub Desktop.
Grid Locator calculation code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
gl.js | |
Grid Locator Calcuration Library | |
version 1.0 | |
Copyright (C) 2013 Keisuke Nakao (chome@argv.org) | |
This library is free software: you can redistribute is and/or modify | |
it under the terms of the GNU General Public Licenses as published by | |
the Free Software Foundation, version 2. | |
*/ | |
/* | |
指定した緯度経度から、グリッドロケーターを返す関数 | |
lat: 緯度 | |
lng: 経度 | |
*/ | |
function calcGL(lat, lng){ | |
var char = function(n){return String.fromCharCode(0x41 + n)} | |
var modf = function(n){return new Array(Math.floor(n), n - Math.floor(n))} | |
var lng = modf(lng) | |
var lat = modf(lat) | |
return new Array( | |
char(Math.floor((lng[0] + 180)/20.0)), | |
char(Math.floor((lat[0] + 90.0)/10.0)), | |
Math.floor((lng[0]%20)/2).toString(), | |
Math.floor(lat[0]%10).toString(), | |
char(Math.floor(((lng[0]%2) + lng[1])*60/5)), | |
char(Math.floor(lat[1]*60/2.5)) | |
).join("") | |
} | |
/* | |
指定したグリッドロケーターの北西の緯度経度を返す関数。 | |
*/ | |
function calcCorner(gl){ | |
var f = function(offset, unit, base) | |
{ | |
return function(c) | |
{ | |
return (c.charCodeAt(0) - base.charCodeAt(0)) * unit - offset | |
} | |
} | |
var fmap = new Array(f(180, 20, "A"), f(90, 10, "A"), | |
f(0, 2, "0"), f(0, 1, "0"), | |
f(0, 5.0/60.0, "A"), f(0, 2.5/60.0, "A") | |
) | |
// map | |
var result = new Array() | |
for(i=0; i<gl.length; i++){ | |
result.push(fmap[i](gl.charAt(i))) | |
} | |
// reduce | |
return result.reduce(function(prev, curr, idx){ | |
if(idx%2 === 0){ | |
return new Array(prev[0], prev[1] + curr) | |
}else{ | |
return new Array(prev[0] + curr, prev[1]) | |
} | |
}, new Array(0,0)) | |
} | |
/* | |
指定したグリッドロケーターの南西と北東の緯度経度を返す関数 | |
*/ | |
function calcRect(gl) | |
{ | |
var sw = calcCorner(gl) // North-West | |
var offset | |
if(gl.length === 6){ | |
offset = new Array(2.5/60, 5.0/60) | |
}else if(gl.length === 4){ | |
offset = new Array(1, 2) | |
}else if(gl.length === 2){ | |
offset = new Array(10, 20) | |
}else{ | |
return new Array(NaN, NaN) | |
} | |
return new Array(sw, new Array(sw[0] + offset[0], sw[1] + offset[1])) | |
} | |
// Test | |
// JARL HQ, 35.729, 139.7436, PM95UR | |
//print(calcGL(35+43.0/60+45.0/3600, 139+44.0/60.0+37.0/3600)) | |
//print(calcRect('PM95UR')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment