Fix: fix some fallout from updating turf to 7.2.0, make 'bbox'-attribute in line with the actual spec

This commit is contained in:
Pieter Vander Vennet 2025-02-07 01:09:00 +01:00
parent c67baa1c65
commit 12869321fd
2 changed files with 29 additions and 36 deletions

View file

@ -1,13 +1,12 @@
import { bbox } from "@turf/bbox"
import { AllGeoJSON } from "@turf/turf"
import { TileRange, Tiles } from "../Models/TileRange"
import { GeoOperations } from "./GeoOperations"
import { Feature, Polygon } from "geojson"
import { bbox } from "@turf/bbox"
export class BBox {
static global: BBox = new BBox([
[-180, -90],
[180, 90],
[180, 90]
])
readonly maxLat: number
readonly maxLon: number
@ -30,7 +29,16 @@ export class BBox {
* bb.maxLat // => -90
* bb.maxLon // => -100
*/
constructor(coordinates: [number, number][]) {
constructor(coordinates: [number, number, number, number] | [number, number][]) {
if (!Array.isArray(coordinates[0])) {
// already the bbox coordinates
const coors = <[number, number, number, number]>coordinates
this.maxLon = Math.max(coors[0], coors[2])
this.minLon = Math.min(coors[0], coors[2])
this.maxLat = Math.max(coors[1], coors[3])
this.minLat = Math.min(coors[1], coors[3])
return
}
this.maxLat = -90
this.maxLon = -180
this.minLat = 90
@ -54,7 +62,7 @@ export class BBox {
static fromLeafletBounds(bounds) {
return new BBox([
[bounds.getWest(), bounds.getNorth()],
[bounds.getEast(), bounds.getSouth()],
[bounds.getEast(), bounds.getSouth()]
])
}
@ -69,26 +77,11 @@ export class BBox {
*
*/
static get(feature: Feature): BBox {
const f = <any>feature
if (f.bbox?.overlapsWith === undefined) {
const bb = bbox(<AllGeoJSON>feature)
console.log(">>> ", bb)
if (Array.isArray(bb)) {
const [minX, minY, maxX, maxY]: number[] = bb
f["bbox"] = new BBox([
[minX, minY],
[maxX, maxY]
])
} else {
const { minLon, minLat, maxLon, maxLat } = bb
// Note: x is longitude
f["bbox"] = new BBox([
[minLon, minLat],
[maxLon, maxLat]
])
}
const f = feature
if (!f.bbox) {
f.bbox = <[number, number, number, number]>bbox(f)
}
return f["bbox"]
return new BBox(<[number, number, number, number]>f.bbox)
}
static bboxAroundAll(bboxes: BBox[]): BBox {
@ -105,7 +98,7 @@ export class BBox {
}
return new BBox([
[maxLon, maxLat],
[minLon, minLat],
[minLon, minLat]
])
}
@ -132,7 +125,7 @@ export class BBox {
public unionWith(other: BBox) {
return new BBox([
[Math.max(this.maxLon, other.maxLon), Math.max(this.maxLat, other.maxLat)],
[Math.min(this.minLon, other.minLon), Math.min(this.minLat, other.minLat)],
[Math.min(this.minLon, other.minLon), Math.min(this.minLat, other.minLat)]
])
}
@ -185,7 +178,7 @@ export class BBox {
return new BBox([
[lon - s / 2, lat - s / 2],
[lon + s / 2, lat + s / 2],
[lon + s / 2, lat + s / 2]
])
}
@ -242,21 +235,21 @@ export class BBox {
const lonDiff = Math.min(maxIncrease / 2, Math.abs(this.maxLon - this.minLon) * factor)
return new BBox([
[this.minLon - lonDiff, this.minLat - latDiff],
[this.maxLon + lonDiff, this.maxLat + latDiff],
[this.maxLon + lonDiff, this.maxLat + latDiff]
])
}
padAbsolute(degrees: number): BBox {
return new BBox([
[this.minLon - degrees, this.minLat - degrees],
[this.maxLon + degrees, this.maxLat + degrees],
[this.maxLon + degrees, this.maxLat + degrees]
])
}
toLngLat(): [[number, number], [number, number]] {
return [
[this.minLon, this.minLat],
[this.maxLon, this.maxLat],
[this.maxLon, this.maxLat]
]
}
@ -275,7 +268,7 @@ export class BBox {
return {
type: "Feature",
properties: properties,
geometry: this.asGeometry(),
geometry: this.asGeometry()
}
}
@ -288,9 +281,9 @@ export class BBox {
[this.maxLon, this.minLat],
[this.maxLon, this.maxLat],
[this.minLon, this.maxLat],
[this.minLon, this.minLat],
],
],
[this.minLon, this.minLat]
]
]
}
}
@ -317,7 +310,7 @@ export class BBox {
minLon,
maxLon,
minLat,
maxLat,
maxLat
}
}

View file

@ -99,7 +99,7 @@ export class GeoOperations {
if (feature === undefined || feature === null) {
return undefined
}
return <[number, number]>turf.center(<turf.Feature>feature).geometry.coordinates
return <[number, number]>turf.center(feature).geometry.coordinates
}
/**