Skip to content

Commit 6d867b2

Browse files
committed
improved NaN support
1 parent a2df37c commit 6d867b2

File tree

5 files changed

+72
-30
lines changed

5 files changed

+72
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"flug": "^2.7.1"
5151
},
5252
"dependencies": {
53-
"bbox-fns": "^0.19.0",
53+
"bbox-fns": "^0.20.2",
5454
"proj4-fully-loaded": "^0.2.0",
5555
"proj4-merge": "^0.1.1"
5656
}

reproject-bbox.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function reprojectBoundingBox({
88
bbox: [number, number, number, number] | Readonly<[number, number, number, number]>,
99
density?: number | undefined,
1010
from: number | string,
11+
nan_strategy?: "skip" | "throw" | string | undefined,
1112
proj4?: any,
1213
split?: boolean | undefined,
1314
to: number | string

reproject-bbox.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (typeof merge !== "function") {
1111
const CUSTOM_PROJECTION_ERROR =
1212
"[reproject-bbox] You passed in a value of 32767 for {{%s}}, which means a custom non-standard projection. Please pass in a Well-Known Text or PROJ4JS String instead.";
1313

14-
function reprojectBoundingBox({ bbox, density, from, proj4: _proj4, split = true, to }) {
14+
function reprojectBoundingBox({ bbox, debug_level = 0, density, from, nan_strategy = "throw", proj4: _proj4, split = true, to }) {
1515
if (from === 32767) throw new Error(CUSTOM_PROJECTION_ERROR.replace("{{%s}}", "from"));
1616
if (to === 32767) throw new Error(CUSTOM_PROJECTION_ERROR.replace("{{%s}}", "to"));
1717

@@ -27,10 +27,12 @@ function reprojectBoundingBox({ bbox, density, from, proj4: _proj4, split = true
2727
const fwd = proj(from, to).forward;
2828

2929
const bboxes = split ? bboxSplit(bbox, { x: [0], y: [0] }) : [bbox];
30+
if (debug_level >= 2) console.log("[reproject-bbox] bboxes:", bboxes);
3031

3132
const bboxes_reprojected = bboxes.map((bbox) => {
32-
return reproject(bbox, fwd, { density });
33+
return reproject(bbox, fwd, { density, nan_strategy });
3334
});
35+
if (debug_level >= 2) console.log("[reproject-bbox] bboxes_reprojected:", bboxes_reprojected);
3436

3537
const merged = bboxMerge(bboxes_reprojected);
3638

reproject-bbox.min.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,37 @@
66
* @param {Array} points - an array (aka ring) of points
77
* @return {bbox} bbox in form [xmin, ymin, xmax, ymax]
88
*/
9-
function bboxArray(points) {
9+
function bboxArray(points, { nan_strategy = "throw" } = { nan_strategy: "throw" }) {
1010
const count = points.length;
11-
const [x, y] = points[0];
12-
let xmin = x;
13-
let xmax = x;
14-
let ymin = y;
15-
let ymax = y;
16-
for (let i = 1; i < count; i++) {
11+
let xmin = null;
12+
let xmax = null;
13+
14+
let ymin = null;
15+
let ymax = null;
16+
for (let i = 0; i < count; i++) {
1717
const [x, y] = points[i];
18-
if (x < xmin) xmin = x;
19-
else if (x > xmax) xmax = x;
20-
if (y < ymin) ymin = y;
21-
else if (y > ymax) ymax = y;
18+
if (isNaN(x)) {
19+
if (nan_strategy === "throw") {
20+
throw new Error("[bbox-fns/bbox-array] encountered point with a NaN value: [" + x + ", " + y + "]");
21+
}
22+
} else if (xmin === null) {
23+
xmin = x;
24+
xmax = x;
25+
} else {
26+
if (x < xmin) xmin = x;
27+
else if (x > xmax) xmax = x;
28+
}
29+
if (isNaN(y)) {
30+
if (nan_strategy === "throw") {
31+
throw new Error("[bbox-fns/bbox-array] encountered point with a NaN value: [" + x + ", " + y + "]");
32+
}
33+
} else if (ymin === null) {
34+
ymin = y;
35+
ymax = y;
36+
} else {
37+
if (y < ymin) ymin = y;
38+
else if (y > ymax) ymax = y;
39+
}
2240
}
2341
return [xmin, ymin, xmax, ymax];
2442
}
@@ -29,10 +47,7 @@ module.exports.default = bboxArray;
2947
},{}],2:[function(require,module,exports){
3048
"use_strict";
3149

32-
function densePolygon(
33-
[xmin, ymin, xmax, ymax],
34-
{ density = 0 } = { density: 0 }
35-
) {
50+
function densePolygon([xmin, ymin, xmax, ymax], { density = 0 } = { density: 0 }) {
3651
if (typeof density === "number") density = [density, density];
3752

3853
const [x_density, y_density] = density;
@@ -113,14 +128,14 @@ module.exports.default = merge;
113128
const bboxArray = require("./bbox-array.js");
114129
const densePolygon = require("./dense-polygon.js");
115130

116-
function reproject(bbox, reproject, { async = false, density } = {}) {
131+
function reproject(bbox, fwd, { async = false, density, nan_strategy = "throw" } = {}) {
117132
const polygon = densePolygon(bbox, { density });
118133
const ring = polygon[0];
119-
const reprojected = ring.map(pt => reproject(pt));
134+
const reprojected = ring.map(pt => fwd(pt));
120135
if (async) {
121-
return Promise.all(reprojected).then(points => bboxArray(points));
136+
return Promise.all(reprojected).then(points => bboxArray(points, { nan_strategy }));
122137
} else {
123-
return bboxArray(reprojected);
138+
return bboxArray(reprojected, { nan_strategy });
124139
}
125140
}
126141

@@ -146,12 +161,8 @@ function split(bbox, breakpoints) {
146161
const xbrks = breakpoints.x || [];
147162
const ybrks = breakpoints.y || [];
148163

149-
const xedges = [xmin]
150-
.concat(xbrks.filter(x => x > xmin && x < xmax))
151-
.concat([xmax]);
152-
const yedges = [ymin]
153-
.concat(ybrks.filter(y => y > ymin && y < ymax))
154-
.concat([ymax]);
164+
const xedges = [xmin].concat(xbrks.filter(x => x > xmin && x < xmax)).concat([xmax]);
165+
const yedges = [ymin].concat(ybrks.filter(y => y > ymin && y < ymax)).concat([ymax]);
155166

156167
const bboxes = [];
157168

@@ -7634,7 +7645,7 @@ if (typeof merge !== "function") {
76347645
const CUSTOM_PROJECTION_ERROR =
76357646
"[reproject-bbox] You passed in a value of 32767 for {{%s}}, which means a custom non-standard projection. Please pass in a Well-Known Text or PROJ4JS String instead.";
76367647

7637-
function reprojectBoundingBox({ bbox, density, from, proj4: _proj4, split = true, to }) {
7648+
function reprojectBoundingBox({ bbox, debug_level = 0, density, from, nan_strategy = "throw", proj4: _proj4, split = true, to }) {
76387649
if (from === 32767) throw new Error(CUSTOM_PROJECTION_ERROR.replace("{{%s}}", "from"));
76397650
if (to === 32767) throw new Error(CUSTOM_PROJECTION_ERROR.replace("{{%s}}", "to"));
76407651

@@ -7650,10 +7661,13 @@ function reprojectBoundingBox({ bbox, density, from, proj4: _proj4, split = true
76507661
const fwd = proj(from, to).forward;
76517662

76527663
const bboxes = split ? bboxSplit(bbox, { x: [0], y: [0] }) : [bbox];
7664+
if (debug_level >= 2) console.log("bboxes:", bboxes);
76537665

76547666
const bboxes_reprojected = bboxes.map((bbox) => {
7655-
return reproject(bbox, fwd, { density });
7667+
console.log({ bbox, fwd, density, nan_strategy });
7668+
return reproject(bbox, fwd, { density, nan_strategy });
76567669
});
7670+
if (debug_level >= 2) console.log("bboxes_reprojected:", bboxes_reprojected);
76577671

76587672
const merged = bboxMerge(bboxes_reprojected);
76597673

test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,28 @@ test("throw error on 32767", ({ eq }) => {
7171
}
7272
eq(msg.startsWith(`[reproject-bbox] You passed in a value of 32767 for from`), true);
7373
});
74+
75+
test("4326 to web mercator", ({ eq }) => {
76+
const reprojected = reprojectBoundingBox({
77+
bbox: [-180, -90, 180, 90],
78+
density: 100,
79+
from: "EPSG:4326",
80+
nan_strategy: "skip",
81+
split: false,
82+
to: "EPSG:3857",
83+
});
84+
eq(reprojected, [-20037508.342789244, -26555100.29330386, 20037508.342789244, 26555100.293303788]);
85+
});
86+
87+
test("4326 to web mercator (again)", ({ eq }) => {
88+
const reprojected = reprojectBoundingBox({
89+
bbox: [-180, -89.99928, 179.99856, 90],
90+
debug_level: 0,
91+
density: 100,
92+
from: "EPSG:4326",
93+
nan_strategy: "skip",
94+
split: false,
95+
to: "EPSG:3857",
96+
});
97+
eq(reprojected, [-20037508.342789244, -76394987.3448674, 20037348.0427225, 26555125.810017247]);
98+
});

0 commit comments

Comments
 (0)