📦 Extent class for creating bounding boxes
npm install @id-sdk/extent
This library is available in both ES5/CommonJS and ES6 module formats.
const Extent = require('@id-sdk/extent').Extent; // CommonJS
// or
import { Extent } from '@id-sdk/extent'; // ES6This project is just getting started! 🌱
We're not able to support external contributors at this time, but check back in a bit when things have matured.
- new Extent(otherOrMin?: Extent | Vec2, max?: Vec2) constructor
- equals(other: any): boolean
- area(): number
- center(): Vec2
- rectangle(): number[4]
- toParam(): string
- bbox(): BBox
- polygon(): Vec2[5]
- contains(other: any): boolean
- intersects(other: any): boolean
- intersection(other: any): Extent
- percentContainedIn(other: any): number
- extend(other: any): Extent
- padByMeters(meters: number): Extent
# new Extent(otherOrMin?: Extent | Vec2, max?: Vec2) <>
Constructs a new Extent.
const e1 = new Extent(); // construct an initially empty extent
const e2 = new Extent([0, 0]); // construct as a point (min and max both [0, 0])
const e3 = new Extent([0, 0], [5, 5]); // construct as a point with given min and max
const e4 = new Extent(e3); // copy an Extent to a new Extent# equals(other: any): boolean <>
Test whether extent equals another extent. Returns true if they are equal, false if not.
const a = new Extent([0, 0], [10, 10]);
const b = new Extent([0, 0], [10, 10]);
const c = new Extent([0, 0], [12, 12]);
a.equals(b); // returns true
a.equals(c); // returns falseReturns the area of an extent.
new Extent([0, 0], [5, 10]).area(); // returns 50Returns the center point of an extent.
new Extent([0, 0], [5, 10]).center(); // returns [2.5, 5]Returns an array rectangle as [minX, minY, maxX, maxY].
new Extent([0, 0], [5, 10]).rectangle(); // returns [0, 0, 5, 10]Returns a string representation of this extent's rectangle formatted as "minX,minY,maxX,maxY". This is often used to request a bounding box from a REST API.
new Extent([0, 0], [5, 10]).toParam(); // returns '0,0,5,10'Returns a BBox Object with minX, minY, maxX, maxY properties.
new Extent([0, 0], [5, 10]).bbox(); // returns { minX: 0, minY: 0, maxX: 5, maxY: 10 };Returns an array of coordinates as a polygon representing the extent wound clockwise.
new Extent([0, 0], [5, 10]).polygon(); // returns [[0, 0], [0, 10], [5, 10], [5, 0], [0, 0]]# contains(other: any): boolean <>
Test whether this extent fully contains another extent. Returns true if it does, false if not.
const a = new Extent([0, 0], [5, 5]);
const b = new Extent([1, 1], [2, 2]);
a.contains(b); // returns true
b.contains(a); // returns false# intersects(other: any): boolean <>
Test whether this extent intersects another extent. Returns true if it does, false if not.
const a = new Extent([0, 0], [5, 5]);
const b = new Extent([1, 1], [6, 6]);
a.intersects(b); // returns true
b.intersects(a); // returns true# intersection(other: any): Extent <>
Returns a new Extent representing the intersection of this and other extent.
const a = new Extent([0, 0], [5, 5]);
const b = new Extent([1, 1], [6, 6]);
a.intersection(b); // returns new Extent { min: [ 1, 1 ], max: [ 5, 5 ] }
b.intersection(a); // returns new Extent { min: [ 1, 1 ], max: [ 5, 5 ] }# percentContainedIn(other: any): number <>
Returns the percent of other extent contained within this extent, by area.
const a = new Extent([0, 0], [4, 1]);
const b = new Extent([3, 0], [4, 2]);
a.percentContainedIn(b); // returns 0.25
b.percentContainedIn(a); // returns 0.5# extend(other: any): Extent <>
Extend the bounds of an extent, returning a new Extent. This method does not modify the original or other extents.
const a = new Extent([0, 0], [5, 10]);
const b = new Extent([4, -1], [5, 10]);
const c = a.extend(b); // returns new Extent { min: [ 0, -1 ], max: [ 5, 10 ] }# padByMeters(meters: number): Extent <>
Returns a new Extent representing the current extent (assumed to be defined in WGS84 geographic coordinates) padded by given meters. This method does not modify the original extent.
All of the Extent methods are designed to be used in an immutable programming style, and return new Extents instead of modifying the original object.
const e1 = new Extent(); // construct an initially empty extent
const e2 = e1.extend([[0, 0], [5, 5]]); // `extend` returns a new Extent, does not modify e1However we make the min and max properties publicly mutable, in case you need to modify an extent bounds directly for performance reasons.
const e1 = new Extent(); // construct an initially empty extent
e1.min = [0, 0]; // adjust its min/max later
e1.max = [5, 5];# Vec2
An array of two numbers.
[number, number]
# BBox
An Object containing minX, minY, maxX, maxY numbers.
{ minX: number, minY: number, maxX: number, maxY: number }