Chore: add some docs, tests and clearer variable names

This commit is contained in:
Pieter Vander Vennet 2024-06-11 15:05:53 +02:00
parent 3f799398d7
commit aea2a35609

View file

@ -16,6 +16,18 @@ export class BBox {
/*** /***
* Coordinates should be [[lon, lat],[lon, lat]] * Coordinates should be [[lon, lat],[lon, lat]]
* @param coordinates * @param coordinates
*
* const bb = new BBox([[5,6]])
* bb.minLat // => 6
* bb.minLon // => 5
* bb.maxLat // => 6
* bb.maxLon // => 5
*
* const bb = new BBox([[-100,-100]])
* bb.minLat // => -90
* bb.minLon // => -100
* bb.maxLat // => -90
* bb.maxLon // => -100
*/ */
constructor(coordinates: [number, number][]) { constructor(coordinates: [number, number][]) {
this.maxLat = -90 this.maxLat = -90
@ -45,13 +57,24 @@ export class BBox {
]) ])
} }
/**
* Gets the bbox from a feature and caches it (by monkeypatching) the relevant feature
*
*
* const f = {type:"Feature",properties: {}, geometry: {type: "Point", coordinates: [-100,45]}}
* const bb = BBox.get(f)
* bb.minLat // => 45
* bb.minLon // => -100
*
*/
static get(feature: Feature): BBox { static get(feature: Feature): BBox {
const f = <any>feature const f = <any>feature
if (f.bbox?.overlapsWith === undefined) { if (f.bbox?.overlapsWith === undefined) {
const turfBbox: number[] = turf.bbox(feature) const [minX, minY, maxX, maxY]: number[] = turf.bbox(feature)
// Note: x is longitude
f["bbox"] = new BBox([ f["bbox"] = new BBox([
[turfBbox[0], turfBbox[1]], [minX, minY],
[turfBbox[2], turfBbox[3]], [maxX, maxY],
]) ])
} }
return f["bbox"] return f["bbox"]