Add ToGeohash and FromGeohash operations

This commit is contained in:
GCHQ 77703 2018-08-26 14:27:15 +01:00
parent 3905c01a0d
commit 3d505b4248
8 changed files with 2215 additions and 2039 deletions

4081
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -106,6 +106,7 @@
"loglevel-message-prefix": "^3.0.0",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
"ngeohash": "^0.6.0",
"node-forge": "^0.7.5",
"node-md6": "^0.1.0",
"notepack.io": "^2.1.3",

View File

@ -53,7 +53,9 @@
"To MessagePack",
"From MessagePack",
"To Braille",
"From Braille"
"From Braille",
"To Geohash",
"From Geohash"
]
},
{

View File

@ -0,0 +1,45 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import geohash from "ngeohash";
/**
* From Geohash operation
*/
class FromGeohash extends Operation {
/**
* FromGeohash constructor
*/
constructor() {
super();
this.name = "From Geohash";
this.module = "Default";
this.description = "Converts Geohash strings into Lat / Long coordinates. For example, <code>ww8p1r4t8</code> becomes <code>37.8324,112.5584</code>.";
this.infoURL = "https://en.wikipedia.org/wiki/Geohash";
this.inputType = "string";
this.outputType = "string";
this.args = [
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
return input.split("\n").map(line => {
const coords = geohash.decode(line);
return [coords.latitude, coords.longitude].join(",");
}).join("\n");
}
}
export default FromGeohash;

View File

@ -0,0 +1,53 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import geohash from "ngeohash";
/**
* To Geohash operation
*/
class ToGeohash extends Operation {
/**
* ToGeohash constructor
*/
constructor() {
super();
this.name = "To Geohash";
this.module = "Default";
this.description = "Converts Lat / Long coordinates into a Geohash string. For example, <code>37.8324,112.5584</code> becomes <code>ww8p1r4t8</code>.";
this.infoURL = "https://en.wikipedia.org/wiki/Geohash";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
name: "Precision",
type: "number",
value: 9
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [precision] = args;
return input.split("\n").map(line => {
line = line.replace(/ /g, "");
if (line === "") return "";
return geohash.encode(...line.split(",").map(num => parseFloat(num)), precision);
}).join("\n");
}
}
export default ToGeohash;

View File

@ -64,6 +64,8 @@ import "./tests/operations/SetUnion";
import "./tests/operations/SymmetricDifference";
import "./tests/operations/TranslateDateTimeFormat";
import "./tests/operations/Magic";
import "./tests/operations/ToGeohash.mjs";
import "./tests/operations/FromGeohash.mjs";
let allTestsPassing = true;
const testStatusCounts = {

View File

@ -0,0 +1,34 @@
/**
* To Geohash tests
*
* @author gchq77703
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import TestRegister from "../../TestRegister";
TestRegister.addTests([
{
name: "From Geohash",
input: "ww8p1r4t8\nww8p1r\nww8\nw",
expectedOutput: "37.83238649368286,112.55838632583618\n37.83416748046875,112.5604248046875\n37.265625,113.203125\n22.5,112.5",
recipeConfig: [
{
op: "From Geohash",
args: [],
},
],
},
{
name: "From Geohash",
input: "w\nw\n\n\nw",
expectedOutput: "22.5,112.5\n0,0\n22.5,112.5\n0,0\n0,0\n22.5,112.5",
recipeConfig: [
{
op: "From Geohash",
args: [],
},
],
},
]);

View File

@ -0,0 +1,34 @@
/**
* To Geohash tests
*
* @author gchq77703
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import TestRegister from "../../TestRegister";
TestRegister.addTests([
{
name: "To Geohash",
input: "37.8324,112.5584\n37.9324,-112.2584",
expectedOutput: "ww8p1r4t8\n9w8pv3ruj",
recipeConfig: [
{
op: "To Geohash",
args: [9],
},
],
},
{
name: "To Geohash",
input: "37.8324,112.5584\n\n\n",
expectedOutput: "ww8p1r4t8\n\n\n",
recipeConfig: [
{
op: "To Geohash",
args: [9],
},
],
},
]);