Skip to content

Commit 8fdebc4

Browse files
committed
added shift
1 parent dfce144 commit 8fdebc4

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Bounding boxes, or rectangular extents, are represented as an array of 4 numbers
2525
- [polygon](#polygon)
2626
- [reproject](#reproject)
2727
- [scale](#scale)
28+
- [shift](#shift)
2829
- [split](#split)
2930
- [union](#union)
3031
- [validate](#validate)
@@ -270,6 +271,22 @@ scale([0, 9, 50, 200], [2, 10]);
270271
[0, 90, 100, 2000]
271272
```
272273

274+
### shift
275+
Shift bounding box horizontally and/or vertically
276+
```js
277+
// shift bounding box overlapping left "edge"
278+
// to the right, so it overlaps the right "edge"
279+
shift([-200, 40, -160, 90], { x: 360 })
280+
[160,40,200,90];
281+
282+
// shift horizontally and vertically
283+
shift([-185, 90, -180, 95], [360, -90])
284+
[175, 0, 180, 5]
285+
286+
// same as above
287+
shift([-185, 90, -180, 95], { x: 360, y: -90 });
288+
```
289+
273290
### split
274291
Split bounding box that crosses given x or y boundaries. For example, split a bbox that crosses the antimeridian into two bounding boxes.
275292
```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 shift = require("./shift.js");
2324
const split = require("./split.js");
2425
const validate = require("./validate.js");
2526
const preciseValidate = require("./precise/validate.js");
@@ -46,6 +47,7 @@ const bboxfns = {
4647
preciseReproject,
4748
reproject,
4849
scale,
50+
shift,
4951
split,
5052
validate,
5153
preciseValidate,

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+
"shift.js",
3132
"split.js",
3233
"union.js",
3334
"validate.js"

shift.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
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;
14+
15+
return [xmin + x, ymin + y, xmax + x, ymax + y];
16+
}
17+
18+
module.exports = shift;
19+
module.exports.default = shift;

test.js

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

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

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

633+
test("shift", async ({ eq }) => {
634+
eq(shift([-200, 40, -160, 90], { x: 360 }), [160, 40, 200, 90]);
635+
eq(shift([-185, 40, -180, 90], { x: 360 }), [175, 40, 180, 90]);
636+
eq(shift([-185, 40, -180, 90], [360, 0]), [175, 40, 180, 90]);
637+
eq(shift([-185, 90, -180, 95], [360, -90]), [175, 0, 180, 5]);
638+
eq(shift([-185, 90, -180, 95], { x: 360, y: -90 }), [175, 0, 180, 5]);
639+
eq(shift([-185, 90, -180, 95], {}), [-185, 90, -180, 95]);
640+
eq(shift([-185, 90, -180, 95], []), [-185, 90, -180, 95]);
641+
});
642+
632643
test("split", async ({ eq }) => {
633644
eq(split([-180, -90, 180, 90], { x: [-180, 180], y: [-90, 90] }), [
634645
[-180, -90, 180, 90]

0 commit comments

Comments
 (0)