bbox-fns: work in progress
Super Light-weight JavaScript Bounding Box Utility Functions
the bounding box
Bounding boxes, or rectangular extents, are represented as an array of 4 numbers:
[xmin, ymin, xmax, ymax]
functions
bboxArea
Calculate the area of a bounding box
import bboxArea from "bbox-fns/bbox-area.js";
bboxArea([2, 3, 8, 9])
36
bboxArray
Calculate the bounding box of an array of points (aka "a polygon ring")
import bboxArray from "bbox-fns/bbox-array.js";
bboxArray([
[ -180, 86.06126914660831 ],
[ -180, 85.66739606126914 ],
[ -179, 84.87964989059081 ],
[ -179, 84.48577680525165 ]
]);
[-180, 84.48577680525165, -179, 86.06126914660831]
bboxPoint
Convert a single [x, y] point into a bounding box of zero width and height;
import bboxPoint from "bbox-fns/bbox-point.js";
bboxPoint([-180, 86.06126914660831]);
[-180, 86.06126914660831, -180, 86.06126914660831]
bboxSize
Calculate the width and height of a bounding box
import bboxSize from "bbox-fns/bbox-size.js";
bboxSize([-180, 84.48577680525165, -179, 86.06126914660831]);
[1, 1.5754923413566644]
booleanContainsPoint
Check if a bounding box contains a point
import booleanContainsPoint from "bbox-fns/boolean-contains-point.js";
const western_hemisphere = [-180, -90, 0, 90];
const eastern_hemisphere = [0, -90, 180, 90];
const hawaii = [-155.844437, 19.741755];
booleanContainsPoint(western_hemisphere, hawaii);
true
booleanContainsPoint(western_hemisphere, [0, 0]);
true
booleanContainsPoint(western_hemisphere, [0, 0], { exclusive: true });
false
booleanIntersects
Checks if two bounding boxes have any intersection at all.
import booleanIntersects from "bbox-fns/boolean-intersects.js";
const western_hemisphere = [-180, -90, 0, 90];
const eastern_hemisphere = [0, -90, 180, 90];
booleanIntersects(western_hemisphere, eastern_hemisphere);
true
intersect
import intersect from "bbox-fns/intersect.js";
const western_hemisphere = [-180, -90, 0, 90];
const eastern_hemisphere = [0, -90, 180, 90];
intersect(western_hemisphere, eastern_hemisphere);
[0, -90, 0, 90]
merge
import merge from "bbox-fns/merge.js";
const western_hemisphere = [-180, -90, 0, 90];
const eastern_hemisphere = [0, -90, 180, 90];
const bboxes = [western_hemisphere, eastern_hemisphere];
merge(bboxes);
[-180, -90, 180, 90]
polygon
Create GeoJSON-Like Polygon from a Bounding Box
import polygon from "bbox-fns/polygon.js";
polygon([-180, -90, 180, 90]);
[
[
[-180, 90],
[-180, -90],
[180, -90],
[180, 90],
[-180, 90]
]
]
densePolygon
A more advanced version of polygon. Create a polygon
while adding points to each side of the rectangle.
import polygon from "bbox-fns/dense-polygon.js";
densePolygon(bbox, { density: 100 });
densePolygon(bbox, { density: [100, 400] });
scale
Multiply x and y values by the given scale values
import scale from "bbox-fns/scale.js";
scale([0, 9, 50, 200], 0.5);
[0, 4.5, 25, 100];
scale([0, 9, 50, 200], [2, 10]);
[0, 90, 100, 2000]
reproject
Reproject a bounding box using the given reprojection function
import reproject from "bbox-fns/reproject.js";
import proj4 from "proj4-fully-loaded";
const { forward } = proj4("EPSG:4326", "EPSG:32610");
reproject(bbox, forward);
reproject(bbox, forwardAsync, { async: true })
reproject(bbox, forward, { density: 99 })
projection support
If you are looking for a library with greater projection support and a class-based approach, try geo-extent!
nomenclature
This library borrows the names of some similar Turf.js functions, but it does not borrow the internal code.
advanced usage
precise functions
In order to avoid floating point arithmetic errors, you can use the precise version of these functions where numbers are represented as strings.
import preciseBboxArray from "bbox-fns/precise/bbox-array.js";
import preciseDensePolygon from "bbox-fns/precise/dense-polygon.js";
import preciseDivide from "bbox-fns/precise/divide.js";
import preciseReproject from "bbox-fns/precise/reproject.js";
preciseDensePolygon(bbox, { density: [359, 179] });
[
[ '-180', '80' ], [ '-180', '79' ], [ '-180', '78' ], [ '-180', '77' ], , [ '-180', '80' ]
]
preciseDivide([0, 9, 50, 200], [3, 4], { ellipsis: true })
["0", "2.25", "16.666...", "50"]