Skip to content

Commit 9fdb913

Browse files
committed
added nan_strategy to bboxArray and reproject
1 parent 8b327e6 commit 9fdb913

20 files changed

+69
-172
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ bboxArray([
5353
[ -179, 84.48577680525165 ]
5454
]);
5555
[-180, 84.48577680525165, -179, 86.06126914660831]
56+
57+
// throws because of the NaN value
58+
bboxArray([ -180, 86.06 ], [NaN, NaN], [ -179, 84.48 ]);
59+
60+
// skip NaN values (don't throw)
61+
bboxArray(points, { nan_strategy: "skip" })
62+
[-180, 84.48, -179, 86.06]
5663
```
5764

5865
### bboxPoint
@@ -326,6 +333,9 @@ reproject(bbox, forwardAsync, { async: true })
326333

327334
// you can also control the point density of the intermediate polygon
328335
reproject(bbox, forward, { density: 99 })
336+
337+
// skip (don't throw error) when forward returns a NaN value
338+
reproject(bbox, forward, { density: 99, nan_stategy: "skip" })
329339
```
330340

331341
### sort

bbox-array.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param {Array} points - an array (aka ring) of points
66
* @return {bbox} bbox in form [xmin, ymin, xmax, ymax]
77
*/
8-
function bboxArray(points) {
8+
function bboxArray(points, { nan_strategy = "throw" } = { nan_strategy: "throw" }) {
99
const count = points.length;
1010
const [x, y] = points[0];
1111
let xmin = x;
@@ -14,10 +14,22 @@ function bboxArray(points) {
1414
let ymax = y;
1515
for (let i = 1; i < count; i++) {
1616
const [x, y] = points[i];
17-
if (x < xmin) xmin = x;
18-
else if (x > xmax) xmax = x;
19-
if (y < ymin) ymin = y;
20-
else if (y > ymax) ymax = y;
17+
if (isNaN(x)) {
18+
if (nan_strategy === "throw") {
19+
throw new Error("[bbox-fns/bbox-array] encountered point with a NaN value: [" + x + ", " + y + "]");
20+
}
21+
} else {
22+
if (x < xmin) xmin = x;
23+
else if (x > xmax) xmax = x;
24+
}
25+
if (isNaN(y)) {
26+
if (nan_strategy === "throw") {
27+
throw new Error("[bbox-fns/bbox-array] encountered point with a NaN value: [" + x + ", " + y + "]");
28+
}
29+
} else {
30+
if (y < ymin) ymin = y;
31+
else if (y > ymax) ymax = y;
32+
}
2133
}
2234
return [xmin, ymin, xmax, ymax];
2335
}

boolean-contains-point.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"use_strict";
22

33
// check if a bounding box contains a point
4-
function booleanContainsPoint(
5-
[xmin, ymin, xmax, ymax],
6-
[x, y],
7-
{ exclusive = false } = { exclusive: false }
8-
) {
4+
function booleanContainsPoint([xmin, ymin, xmax, ymax], [x, y], { exclusive = false } = { exclusive: false }) {
95
if (exclusive) {
106
// exclude points on an exact edge of the bounding box
117
return y < ymax && y > ymin && x < xmax && x > xmin;

boolean-contains.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"use_strict";
22

33
// check if the first bbox completely contains the second
4-
function booleanContains(
5-
[axmin, aymin, axmax, aymax],
6-
[bxmin, bymin, bxmax, bymax],
7-
{ exclusive = false } = { exclusive: false }
8-
) {
4+
function booleanContains([axmin, aymin, axmax, aymax], [bxmin, bymin, bxmax, bymax], { exclusive = false } = { exclusive: false }) {
95
if (exclusive) {
106
const xContained = bxmin > axmin && bxmax < axmax;
117
const yContained = bymin > aymin && bymax < aymax;

boolean-intersects.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
"use_strict";
22

33
// check if two bounding boxes overlap at all
4-
function booleanIntersects(
5-
[axmin, aymin, axmax, aymax],
6-
[bxmin, bymin, bxmax, bymax]
7-
) {
4+
function booleanIntersects([axmin, aymin, axmax, aymax], [bxmin, bymin, bxmax, bymax]) {
85
const yOverlaps = bymin <= aymax && bymax >= aymin;
96
const xOverlaps = bxmin <= axmax && bxmax >= axmin;
107
return xOverlaps && yOverlaps;

boolean-rectangle.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,14 @@ function booleanRectangle(coords, { debug = 0 } = { debug: 0 }) {
2626
// if multi-polygon, may need to unwrap twice
2727
if (coords.length === 1) coords = coords[0];
2828

29-
if (
30-
!coords.every(
31-
pt => Array.isArray(pt) && pt.every(n => typeof n === "number")
32-
)
33-
) {
29+
if (!coords.every(pt => Array.isArray(pt) && pt.every(n => typeof n === "number"))) {
3430
if (debug) console.log("[bbox-fns/booleanRectangle] invalid points");
3531
return false;
3632
}
3733

3834
// first and last coordinate should be the same
3935
if (JSON.stringify(coords[0]) !== JSON.stringify(coords[coords.length - 1])) {
40-
if (debug)
41-
console.log(
42-
"[bbox-fns/booleanRectangle] first and last coordinates not equal"
43-
);
36+
if (debug) console.log("[bbox-fns/booleanRectangle] first and last coordinates not equal");
4437
return false;
4538
}
4639

calc-all.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,12 @@ function calcAll(geom) {
3131
return dedupe(flatten(geom.geometries.map(calcAll)));
3232
}
3333

34-
if (
35-
Array.isArray(geom) &&
36-
Array.isArray(geom[0]) &&
37-
Array.isArray(geom[0][0])
38-
) {
34+
if (Array.isArray(geom) && Array.isArray(geom[0]) && Array.isArray(geom[0][0])) {
3935
return dedupe(flatten(geom.map(calcAll)));
4036
}
4137

4238
// array of [x, y] coordinate pairs
43-
if (
44-
Array.isArray(geom) &&
45-
Array.isArray(geom[0]) &&
46-
typeof geom[0][0] === "number"
47-
) {
39+
if (Array.isArray(geom) && Array.isArray(geom[0]) && typeof geom[0][0] === "number") {
4840
const [x, y] = geom[0];
4941
let xmin = x;
5042
let ymin = y;
@@ -60,11 +52,7 @@ function calcAll(geom) {
6052
}
6153

6254
// point
63-
if (
64-
Array.isArray(geom) &&
65-
(geom.length === 2 || geom.length === 3) &&
66-
typeof geom[0] === "number"
67-
) {
55+
if (Array.isArray(geom) && (geom.length === 2 || geom.length === 3) && typeof geom[0] === "number") {
6856
const [x, y] = geom;
6957
return [[x, y, x, y]];
7058
}
@@ -75,9 +63,7 @@ function calcAll(geom) {
7563
return [[x, y, x, y]];
7664
}
7765

78-
if (
79-
["xmin", "xmax", "ymin", "ymax"].every(k => typeof geom[k] === "number")
80-
) {
66+
if (["xmin", "xmax", "ymin", "ymax"].every(k => typeof geom[k] === "number")) {
8167
return [[geom.xmin, geom.ymin, geom.xmax, geom.ymax]];
8268
}
8369
}

calc.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,12 @@ function calc(geom) {
2020
return merge(geom.geometries.map(calc));
2121
}
2222

23-
if (
24-
Array.isArray(geom) &&
25-
Array.isArray(geom[0]) &&
26-
Array.isArray(geom[0][0])
27-
) {
23+
if (Array.isArray(geom) && Array.isArray(geom[0]) && Array.isArray(geom[0][0])) {
2824
return merge(geom.map(calc));
2925
}
3026

3127
// array of [x, y] coordinate pairs
32-
if (
33-
Array.isArray(geom) &&
34-
Array.isArray(geom[0]) &&
35-
typeof geom[0][0] === "number"
36-
) {
28+
if (Array.isArray(geom) && Array.isArray(geom[0]) && typeof geom[0][0] === "number") {
3729
const [x, y] = geom[0];
3830
let xmin = x;
3931
let ymin = y;
@@ -49,11 +41,7 @@ function calc(geom) {
4941
}
5042

5143
// point
52-
if (
53-
Array.isArray(geom) &&
54-
(geom.length === 2 || geom.length === 3) &&
55-
typeof geom[0] === "number"
56-
) {
44+
if (Array.isArray(geom) && (geom.length === 2 || geom.length === 3) && typeof geom[0] === "number") {
5745
const [x, y] = geom;
5846
return [x, y, x, y];
5947
}
@@ -64,9 +52,7 @@ function calc(geom) {
6452
return [x, y, x, y];
6553
}
6654

67-
if (
68-
["xmin", "xmax", "ymin", "ymax"].every(k => typeof geom[k] === "number")
69-
) {
55+
if (["xmin", "xmax", "ymin", "ymax"].every(k => typeof geom[k] === "number")) {
7056
return [geom.xmin, geom.ymin, geom.xmax, geom.ymax];
7157
}
7258
}

dense-polygon.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"use_strict";
22

3-
function densePolygon(
4-
[xmin, ymin, xmax, ymax],
5-
{ density = 0 } = { density: 0 }
6-
) {
3+
function densePolygon([xmin, ymin, xmax, ymax], { density = 0 } = { density: 0 }) {
74
if (typeof density === "number") density = [density, density];
85

96
const [x_density, y_density] = density;

grid.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ function grid([xmin, ymin, xmax, ymax], div) {
2424
const cell_ymax = r === rows.length - 1 ? ymax : cell_ymin + cell_height;
2525
for (let c = 0; c < columns; c++) {
2626
const cell_xmin = xmin + c * cell_width;
27-
const cell_xmax =
28-
c === columns.length - 1 ? xmax : cell_xmin + cell_width;
27+
const cell_xmax = c === columns.length - 1 ? xmax : cell_xmin + cell_width;
2928
cells.push([cell_xmin, cell_ymin, cell_xmax, cell_ymax]);
3029
}
3130
}

intersect.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ function intersect(a, b) {
88

99
if (!booleanIntersects(a, b)) return null;
1010

11-
return [
12-
Math.max(axmin, bxmin),
13-
Math.max(aymin, bymin),
14-
Math.min(axmax, bxmax),
15-
Math.min(aymax, bymax)
16-
];
11+
return [Math.max(axmin, bxmin), Math.max(aymin, bymin), Math.min(axmax, bxmax), Math.min(aymax, bymax)];
1712
}
1813

1914
module.exports = intersect;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
],
3838
"scripts": {
3939
"f": "npm run format",
40-
"format": "npx prettier --arrow-parens=avoid --trailing-comma=none --write *.js */*.js",
40+
"format": "npx prettier --arrow-parens=avoid --print-width=200 --trailing-comma=none --write *.js */*.js",
4141
"test": "node test.js"
4242
},
4343
"repository": {
@@ -56,6 +56,6 @@
5656
},
5757
"homepage": "https://github.com/DanielJDufour/bbox-fns#readme",
5858
"dependencies": {
59-
"preciso": "^0.12.0"
59+
"preciso": "^0.12.2"
6060
}
6161
}

precise/dense-polygon.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ function preciseDensePolygon(bbox, { density = 0 } = { density: 0 }) {
1717

1818
let [xmin, ymin, xmax, ymax] = bbox;
1919

20-
if (typeof density === "number" || typeof density === "string")
21-
density = [density, density];
20+
if (typeof density === "number" || typeof density === "string") density = [density, density];
2221

2322
density = density.map(n => n.toString());
2423

precise/divide.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,12 @@ const divide = require("preciso/divide.js");
88
* @param {[number, number]} divisors - divisors in form [dx, dy] or [number] or just a number
99
* @return {bbox} bbox
1010
*/
11-
function preciseDivide(
12-
[xmin, ymin, xmax, ymax],
13-
div,
14-
{ ellipsis = false, max_decimal_digits = 100 } = {}
15-
) {
16-
const [dx, dy] =
17-
typeof div === "string" || typeof div === "number"
18-
? [div, div]
19-
: div.length === 1
20-
? [div[0].toString(), div[0].toString()]
21-
: div;
11+
function preciseDivide([xmin, ymin, xmax, ymax], div, { ellipsis = false, max_decimal_digits = 100 } = {}) {
12+
const [dx, dy] = typeof div === "string" || typeof div === "number" ? [div, div] : div.length === 1 ? [div[0].toString(), div[0].toString()] : div;
2213
const opts = { ellipsis, max_decimal_digits };
2314
const dxstr = dx.toString();
2415
const dystr = dy.toString();
25-
return [
26-
divide(xmin.toString(), dxstr, opts),
27-
divide(ymin.toString(), dystr, opts),
28-
divide(xmax.toString(), dxstr, opts),
29-
divide(ymax.toString(), dystr, opts)
30-
];
16+
return [divide(xmin.toString(), dxstr, opts), divide(ymin.toString(), dystr, opts), divide(xmax.toString(), dxstr, opts), divide(ymax.toString(), dystr, opts)];
3117
}
3218

3319
module.exports = preciseDivide;

reproject.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
const bboxArray = require("./bbox-array.js");
44
const densePolygon = require("./dense-polygon.js");
55

6-
function reproject(bbox, reproject, { async = false, density } = {}) {
6+
function reproject(bbox, reproject, { async = false, density, nan_strategy = "throw" } = {}) {
77
const polygon = densePolygon(bbox, { density });
88
const ring = polygon[0];
99
const reprojected = ring.map(pt => reproject(pt));
1010
if (async) {
11-
return Promise.all(reprojected).then(points => bboxArray(points));
11+
return Promise.all(reprojected).then(points => bboxArray(points, { nan_strategy }));
1212
} else {
13-
return bboxArray(reprojected);
13+
return bboxArray(reprojected, { nan_strategy });
1414
}
1515
}
1616

scale.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
* @return {bbox} bbox
88
*/
99
function scale([xmin, ymin, xmax, ymax], scl) {
10-
const [sx, sy] =
11-
typeof scl === "number"
12-
? [scl, scl]
13-
: scl.length === 1
14-
? [scl[0], scl[0]]
15-
: scl;
10+
const [sx, sy] = typeof scl === "number" ? [scl, scl] : scl.length === 1 ? [scl[0], scl[0]] : scl;
1611
return [xmin * sx, ymin * sy, xmax * sx, ymax * sy];
1712
}
1813

shift.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
function shift([xmin, ymin, xmax, ymax], dist) {
2-
const x =
3-
Array.isArray(dist) && dist.length >= 1
4-
? dist[0]
5-
: typeof dist.x === "number"
6-
? dist.x
7-
: 0;
8-
const y =
9-
Array.isArray(dist) && dist.length >= 2
10-
? dist[1]
11-
: typeof dist.y === "number"
12-
? dist.y
13-
: 0;
2+
const x = Array.isArray(dist) && dist.length >= 1 ? dist[0] : typeof dist.x === "number" ? dist.x : 0;
3+
const y = Array.isArray(dist) && dist.length >= 2 ? dist[1] : typeof dist.y === "number" ? dist.y : 0;
144

155
return [xmin + x, ymin + y, xmax + x, ymax + y];
166
}

split.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ function split(bbox, breakpoints) {
1616
const xbrks = breakpoints.x || [];
1717
const ybrks = breakpoints.y || [];
1818

19-
const xedges = [xmin]
20-
.concat(xbrks.filter(x => x > xmin && x < xmax))
21-
.concat([xmax]);
22-
const yedges = [ymin]
23-
.concat(ybrks.filter(y => y > ymin && y < ymax))
24-
.concat([ymax]);
19+
const xedges = [xmin].concat(xbrks.filter(x => x > xmin && x < xmax)).concat([xmax]);
20+
const yedges = [ymin].concat(ybrks.filter(y => y > ymin && y < ymax)).concat([ymax]);
2521

2622
const bboxes = [];
2723

0 commit comments

Comments
 (0)