gzd-utils
Version:
Utilities for working with MGRS GZD and GeoJSON
158 lines • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGZD = exports.getAllGZD = void 0;
var latitudeBands = 'CDEFGHJKLMNPQRSTUVWX';
var getAllGZD = function () {
var features = [];
for (var longitudeBand = 1; longitudeBand <= 60; longitudeBand++) {
for (var i = 0; i < latitudeBands.length; i++) {
var latitudeBand = latitudeBands[i];
/*
* Special case around Svalbard:
* - 32X, 34X, 36X removed
*/
if (longitudeBand === 32 && latitudeBand === 'X') {
continue;
}
else if (longitudeBand === 34 && latitudeBand === 'X') {
continue;
}
else if (longitudeBand === 36 && latitudeBand === 'X') {
continue;
}
features.push(getFeature(longitudeBand, latitudeBand));
}
}
/*
* Polar regions
*/
features.push(getPolarRegionFeature('A'));
features.push(getPolarRegionFeature('B'));
features.push(getPolarRegionFeature('X'));
features.push(getPolarRegionFeature('Y'));
return {
type: 'FeatureCollection',
features: features,
};
};
exports.getAllGZD = getAllGZD;
function getGZD(paramOne, paramTwo) {
// Handle case when called only with string, parse it into latitudeBand/longitudeBand
if (typeof paramOne === 'string') {
var name_1 = paramOne;
var longitudeBand = parseInt(name_1, 10);
var latitudeBand = name_1.replace(longitudeBand.toString(), '');
return getFeature(longitudeBand, latitudeBand);
}
else {
return getFeature(paramOne, paramTwo);
}
}
exports.getGZD = getGZD;
function getFeature(longitudeBand, latitudeBand) {
// Handle special case around polar regions
if (isNaN(longitudeBand)) {
return getPolarRegionFeature(latitudeBand);
}
// Validate input
validateGZD(longitudeBand, latitudeBand);
var name = "" + longitudeBand + latitudeBand;
/*
* Longitude bands 1..60 6° each, covering -180W to 180E
*/
var longitudeMin = -180 + (longitudeBand - 1) * 6;
var longitudeMax = longitudeMin + 6;
/*
* Latitude bands C..X 8° each, covering 80°S to 84°N
* Except band X covering 12°
*/
var i = latitudeBands.indexOf(latitudeBand);
var latitudeMin = -80 + i * 8;
var latitudeMax;
if (latitudeBand !== 'X') {
latitudeMax = latitudeMin + 8;
}
else {
latitudeMax = latitudeMin + 12;
}
/*
* Special case around Norway:
* Zone 32V is extended 3° west and 31V is shrunk 3°
*/
if (longitudeBand === 31 && latitudeBand === 'V') {
longitudeMax -= 3;
}
else if (longitudeBand === 32 && latitudeBand === 'V') {
longitudeMin -= 3;
}
/*
* Special case around Svalbard:
* - 31X and 37X extended 3°
* - 33X and 35X extended 6°
*/
if (longitudeBand === 31 && latitudeBand === 'X') {
longitudeMax += 3;
}
else if (longitudeBand === 33 && latitudeBand === 'X') {
longitudeMin -= 3;
longitudeMax += 3;
}
else if (longitudeBand === 35 && latitudeBand === 'X') {
longitudeMin -= 3;
longitudeMax += 3;
}
else if (longitudeBand === 37 && latitudeBand === 'X') {
longitudeMin -= 3;
}
return toPolygonFeature(name, longitudeMin, longitudeMax, latitudeMin, latitudeMax);
}
function validateGZD(longitudeBand, latitudeBand) {
if (longitudeBand < 1 || longitudeBand > 60) {
throw new RangeError('longitudeBand must be between 1 and 60');
}
if (latitudeBand.length !== 1) {
throw new RangeError('Invalid latitudeBand provided, should be one letter');
}
if (!latitudeBands.includes(latitudeBand)) {
throw new RangeError("Invalid latitudeBand provided, valid bands: " + latitudeBands);
}
// Handle invalid zones 32X, 34X, 36X around Svalbard
if (latitudeBand === 'X' && (longitudeBand === 32 || longitudeBand === 34 || longitudeBand === 36)) {
throw new RangeError('Invalid band');
}
}
function getPolarRegionFeature(band) {
switch (band) {
case 'A':
return toPolygonFeature('A', -180, 0, -90, -80);
case 'B':
return toPolygonFeature('B', 0, 180, -90, -80);
case 'X':
return toPolygonFeature('X', -180, 0, 84, 90);
case 'Y':
return toPolygonFeature('Y', 0, 180, 84, 90);
default:
throw new RangeError('Invalid band');
}
}
var toPolygonFeature = function (name, longitudeMin, longitudeMax, latitudeMin, latitudeMax) {
return {
type: 'Feature',
properties: {
name: name,
},
geometry: {
type: 'Polygon',
coordinates: [
[
[longitudeMin, latitudeMin],
[longitudeMin, latitudeMax],
[longitudeMax, latitudeMax],
[longitudeMax, latitudeMin],
[longitudeMin, latitudeMin],
],
],
},
};
};
//# sourceMappingURL=gzd-utils.js.map