Skip to content

Commit 168a094

Browse files
committed
added split
1 parent 59896f4 commit 168a094

File tree

5 files changed

+119
-4
lines changed

5 files changed

+119
-4
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Bounding boxes, or rectangular extents, are represented as an array of 4 numbers
2424
- [polygon](#polygon)
2525
- [reproject](#reproject)
2626
- [scale](#scale)
27+
- [split](#split)
2728
- [union](#union)
2829
- [validate](#validate)
2930

@@ -268,6 +269,28 @@ scale([0, 9, 50, 200], [2, 10]);
268269
[0, 90, 100, 2000]
269270
```
270271

272+
### split
273+
Split bounding box that crosses given x or y boundaries. For example, split a bbox that crosses the antimeridian into two bounding boxes.
274+
```js
275+
import split from "bbox-fns/split.js";
276+
277+
// split across the antimeridian
278+
split([-200, -90, 160, 90], { x: [-180, 180] })
279+
[
280+
[-200, -90, -180, 90], // overflow, left of antimeridian
281+
[-180, -90, 160, 90] // right of antimeridian
282+
]
283+
284+
// split across the antimeridian and the poles
285+
split([-200, -90, 160, 100], { x: [-180, 180], y: [-90, 90] })
286+
[
287+
[-200,-90,-180,90],
288+
[-200,90,-180,100],
289+
[-180,-90,160,90],
290+
[-180,90,160,100]
291+
]
292+
```
293+
271294
### reproject
272295
Reproject a bounding box using the given reprojection function
273296
```js

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const preciseDivide = require("./precise/divide.js");
2020
const preciseReproject = require("./precise/reproject.js");
2121
const reproject = require("./reproject.js");
2222
const scale = require("./scale.js");
23+
const split = require("./split.js");
2324
const validate = require("./validate.js");
2425
const preciseValidate = require("./precise/validate.js");
2526
const union = require("./union.js");
@@ -45,6 +46,7 @@ const bboxfns = {
4546
preciseReproject,
4647
reproject,
4748
scale,
49+
split,
4850
validate,
4951
preciseValidate,
5052
union

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"precise/divide.js",
2929
"precise/reproject.js",
3030
"precise/validate.js",
31+
"split.js",
3132
"union.js",
3233
"validate.js"
3334
],

split.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
/**
4+
* @name split
5+
* @description break the given bounding box into pieces at given breakpoints
6+
* @example split(extent, { x: [-180, 180], y: [-90, 90] })
7+
* @param {Array} bbox - a bounding box in form [xmin, ymin, xmax, ymax]
8+
* @param {Object} breakpoints
9+
* @param {Array<Number>} breakpoints.x - array of x values to break on
10+
* @param {Array<Number>} breakpoints.y - array of y values to break on
11+
* @return {bbox} an array of bboxes in form [xmin, ymin, xmax, ymax]
12+
*/
13+
function split(bbox, breakpoints) {
14+
const [xmin, ymin, xmax, ymax] = bbox;
15+
const xbrks = breakpoints.x || [];
16+
const ybrks = breakpoints.y || [];
17+
18+
const xedges = [xmin]
19+
.concat(xbrks.filter(x => x > xmin && x < xmax))
20+
.concat([xmax]);
21+
const yedges = [ymin]
22+
.concat(ybrks.filter(y => y > ymin && y < ymax))
23+
.concat([ymax]);
24+
25+
const bboxes = [];
26+
27+
for (let i = 1; i < xedges.length; i++) {
28+
const xmin = xedges[i - 1];
29+
const xmax = xedges[i];
30+
for (let ii = 1; ii < yedges.length; ii++) {
31+
const ymin = yedges[ii - 1];
32+
const ymax = yedges[ii];
33+
bboxes.push([xmin, ymin, xmax, ymax]);
34+
}
35+
}
36+
37+
return bboxes;
38+
}
39+
40+
module.exports = split;
41+
module.exports.default = split;

test.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
reproject,
4545
preciseReproject,
4646
scale,
47+
split,
4748
preciseDivide,
4849
validate,
4950
preciseValidate,
@@ -79,10 +80,10 @@ test("bboxArray", ({ eq }) => {
7980
});
8081

8182
test("bboxSize", ({ eq }) => {
82-
eq(bboxSize([-180, 84.48577680525165, -179, 86.06126914660831]), [
83-
1,
84-
1.5754923413566644
85-
]);
83+
eq(
84+
bboxSize([-180, 84.48577680525165, -179, 86.06126914660831]),
85+
[1, 1.5754923413566644]
86+
);
8687
eq(bboxSize([-540, -90, -180, 90]), [360, 180]);
8788
});
8889

@@ -628,6 +629,53 @@ test("scale", async ({ eq }) => {
628629
eq(scale([0, 9, 50, 200], [2, 10]), [0, 90, 100, 2000]);
629630
});
630631

632+
test("split", async ({ eq }) => {
633+
eq(split([-180, -90, 180, 90], { x: [-180, 180], y: [-90, 90] }), [
634+
[-180, -90, 180, 90]
635+
]);
636+
eq(split([-200, -90, 160, 90], { x: [-180, 180] }), [
637+
[-200, -90, -180, 90],
638+
[-180, -90, 160, 90]
639+
]);
640+
eq(split([-200, -90, 160, 90], { x: [-180, 180], y: [-90, 90] }), [
641+
[-200, -90, -180, 90],
642+
[-180, -90, 160, 90]
643+
]);
644+
eq(split([-200, -90, 220, 90], { x: [-180, 180], y: [-90, 90] }), [
645+
[-200, -90, -180, 90],
646+
[-180, -90, 180, 90],
647+
[180, -90, 220, 90]
648+
]);
649+
eq(split([-185, 40, -70, 90], { x: [-180, 180], y: [-90, 90] }), [
650+
[-185, 40, -180, 90],
651+
[-180, 40, -70, 90]
652+
]);
653+
eq(split([-200, -90, 160, 100], { x: [-180, 180], y: [-90, 90] }), [
654+
[-200, -90, -180, 90],
655+
[-200, 90, -180, 100],
656+
[-180, -90, 160, 90],
657+
[-180, 90, 160, 100]
658+
]);
659+
eq(split([-185, 40, -70, 95], { x: [-180, 180], y: [-90, 90] }), [
660+
[-185, 40, -180, 90],
661+
[-185, 90, -180, 95],
662+
[-180, 40, -70, 90],
663+
[-180, 90, -70, 95]
664+
]);
665+
eq(
666+
split([-185, 40, -70, 95], {
667+
x: [-360, -180, 180, 360],
668+
y: [-180, -90, 90, 180]
669+
}),
670+
[
671+
[-185, 40, -180, 90],
672+
[-185, 90, -180, 95],
673+
[-180, 40, -70, 90],
674+
[-180, 90, -70, 95]
675+
]
676+
);
677+
});
678+
631679
test("preciseDivide", ({ eq }) => {
632680
eq(preciseDivide([0, 9, 50, 200], 2), ["0", "4.5", "25", "100"]);
633681
eq(preciseDivide([0, 9, 50, 200], [2]), ["0", "4.5", "25", "100"]);

0 commit comments

Comments
 (0)