@flumens/bigu
Version:
British Isles GridRef Utils
209 lines (179 loc) • 5.59 kB
JavaScript
import GridRefParser from './GridRefParser';
import OSIRef from '../OSIRef';
/**
* @constructor
*/
let GridRefParserIE = function() {};
GridRefParserIE.prototype = new GridRefParser();
GridRefParserIE.prototype.constructor = GridRefParserIE;
GridRefParserIE.prototype.country = 'IE';
GridRefParserIE.prototype.NationalRef = OSIRef;
GridRefParserIE.gridLetter = {
A: [0,4],
B: [1,4],
C: [2,4],
D: [3,4],
F: [0,3],
G: [1,3],
H: [2,3],
J: [3,3],
L: [0,2],
M: [1,2],
N: [2,2],
O: [3,2],
Q: [0,1],
R: [1,1],
S: [2,1],
T: [3,1],
V: [0,0],
W: [1,0],
X: [2,0],
Y: [3,0]};
/**
*
* @param {string} rawGridRef
* @throws Error
*/
GridRefParserIE.prototype.parse = function(rawGridRef) {
var trimmedLocality = rawGridRef.replace(/[\[\]\s\t\.-]+/g, '').toUpperCase();
if (/[ABCDEFGHIJKLMNPQRSTUVWXYZ]$/.test(trimmedLocality)) {
// tetrad or quadrant
if (GridRefParser.quadrantOffsets.hasOwnProperty(trimmedLocality.substr(trimmedLocality.length - 2))) {
this.quadrantCode = trimmedLocality.substr(trimmedLocality.length - 2);
trimmedLocality = trimmedLocality.substr(0, trimmedLocality.length - 2);
} else {
this.tetradLetter = trimmedLocality.substr(trimmedLocality.length - 1);
trimmedLocality = trimmedLocality.substr(0, trimmedLocality.length - 1);
}
}
this.parse_gr_string_without_tetrads(trimmedLocality);
if (this.length > 0) {
if (this.tetradLetter || this.quadrantCode) {
// tetrad or quadrant suffix
if (this.tetradLetter) {
this.preciseGridRef = this.hectad + this.tetradLetter;
this.tetrad = this.preciseGridRef;
this.length = 2000; // 2km square
this.osRef.x += GridRefParser.tetradOffsets[this.tetradLetter][0];
this.osRef.y += GridRefParser.tetradOffsets[this.tetradLetter][1];
} else {
// quadrant
this.preciseGridRef = this.hectad + this.quadrantCode;
this.quadrant = this.preciseGridRef;
this.length = 5000; // 5km square
this.osRef.x += GridRefParser.quadrantOffsets[this.quadrantCode][0];
this.osRef.y += GridRefParser.quadrantOffsets[this.quadrantCode][1];
}
} else {
this.preciseGridRef = trimmedLocality;
if (this.length <= 1000) {
// calculate tetrad for precise gridref
this.set_tetrad();
}
}
} else {
this.error = true;
this.errorMessage = "Irish grid reference format not understood. ('" + rawGridRef + "')";
}
};
GridRefParserIE.prototype.parse_well_formed = GridRefParserIE.prototype.parse;
GridRefParserIE._IE_GRID_LETTERS = 'VQLFAWRMGBXSNHCYTOJD';
/**
*
* @param {string} gridRef nn/nnnn or [A-Z]nnnn or [A-Z]/nnnn (no other punctuation by this point), all upper-case
* @return boolean
*/
GridRefParserIE.prototype.parse_gr_string_without_tetrads = function(gridRef) {
var x, y, ref, char;
if (/^\d{2}\/(?:\d\d){1,5}$/.test(gridRef)) {
// nn/nnnn etc.
// regex used to avoid matching oddly malformed refs, such as "32/SO763520"
x = parseInt(gridRef.charAt(0), 10);
y = parseInt(gridRef.charAt(1), 10);
if (x > 3 || y > 4) {
Logger("bad grid square, ref='" + gridRef + "' (Ireland)");
this.length = 0;
return false;
}
ref = gridRef.substr(3);
char = GridRefParserIE._IE_GRID_LETTERS.charAt((x * 5) + y);
x *= 100000;
y *= 100000;
} else {
// [A-Z]nnnn or [A-Z]/nnnn etc.
gridRef = gridRef.replace('/', '');
if (!/^[ABCDFGHJLMNOQRSTVWXY](?:\d\d){1,5}$/.test(gridRef)) {
this.length = 0;// mark error state
this.osRef = null;
return false;
}
if (gridRef) {
char = gridRef.charAt(0);
var p = GridRefParserIE._IE_GRID_LETTERS.indexOf(char);
if (p !== -1) {
x = Math.floor(p / 5) * 100000;
y = (p % 5) * 100000;
} else {
Logger("Bad grid ref grid-letter, ref='" + gridRef + "' (Ireland)");
this.length = 0; // mark error
this.osRef = null;
return false;
}
} else {
Logger('Bad (empty) Irish grid ref');
this.length = 0; // mark error
this.osRef = null;
return false;
}
ref = gridRef.substr(1);
}
switch (ref.length) {
case 2:
this.osRef = new OSIRef(
x + (ref.charAt(0) * 10000),
y + (ref.charAt(1) * 10000)
);
this.length = 10000; //10 km square
this.hectad = char + ref;
break;
case 4:
this.osRef = new OSIRef(
x + Math.floor(ref / 100) * 1000,
y + (ref % 100) * 1000
);
this.length = 1000; //1 km square
this.hectad = char + ref.charAt(0) + ref.charAt(2);
break;
case 6:
this.osRef = new OSIRef(
x + Math.floor(ref / 1000) * 100,
y + (ref % 1000) * 100
);
this.length = 100; // 100m square
this.hectad = char + ref.charAt(0) + ref.charAt(3);
break;
case 8:
this.osRef = new OSIRef(
x + Math.floor(ref / 10000) * 10,
y + (ref % 10000) * 10
);
this.length = 10; //10m square
this.hectad = char + ref.charAt(0) + ref.charAt(4);
break;
case 10:
this.osRef = new OSIRef(
x + Math.floor(ref / 100000),
y + (ref % 100000)
);
this.length = 1; //1m square
this.hectad = char + ref.charAt(0) + ref.charAt(5);
break;
default:
Logger("Bad grid ref length, ref='" + gridRef + "' (Ireland)");
this.length = 0;
this.osRef = null;
return false;
}
return true;
};
export default GridRefParserIE;