-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverse.js
41 lines (35 loc) · 1.19 KB
/
inverse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const add = require("preciso/add");
const divide = require("preciso/divide");
const subtract = require("preciso/subtract");
// bbox to wld
module.exports = function inverse({ bbox, size, precise = true }) {
const [xmin, ymin, xmax, ymax] = bbox;
const { height, width } = size;
if (precise) {
const cell_width = divide(subtract(xmax.toString(), xmin.toString()), width.toString());
const half_cell_width = divide(cell_width, "2");
const cell_height = divide(subtract(ymax.toString(), ymin.toString()), height.toString());
const half_cell_height = divide(cell_height, "2");
return {
xScale: cell_width,
yScale: "-" + cell_height,
ySkew: "0",
xSkew: "0",
xOrigin: add(xmin.toString(), half_cell_width),
yOrigin: subtract(ymax.toString(), half_cell_height)
};
} else {
const cell_width = (xmax - xmin) / width;
const half_cell_width = cell_width / 2;
const cell_height = (ymax - ymin) / height;
const half_cell_height = cell_height / 2;
return {
xScale: cell_width,
yScale: -1 * cell_height,
ySkew: 0,
xSkew: 0,
xOrigin: xmin + half_cell_width,
yOrigin: ymax - half_cell_height
};
}
};