45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
function getBBox(geometry) {
|
|
let minLng = Infinity, minLat = Infinity;
|
|
let maxLng = -Infinity, maxLat = -Infinity;
|
|
|
|
function process(coords) {
|
|
if (typeof coords[0] === "number") {
|
|
const [lng, lat] = coords;
|
|
|
|
minLng = Math.min(minLng, lng);
|
|
minLat = Math.min(minLat, lat);
|
|
maxLng = Math.max(maxLng, lng);
|
|
maxLat = Math.max(maxLat, lat);
|
|
} else {
|
|
coords.forEach(process);
|
|
}
|
|
}
|
|
|
|
// 🔥 handle theo type cho rõ ràng
|
|
switch (geometry.type) {
|
|
case "Point":
|
|
process(geometry.coordinates);
|
|
break;
|
|
|
|
case "MultiPoint":
|
|
case "LineString":
|
|
process(geometry.coordinates);
|
|
break;
|
|
|
|
case "MultiLineString":
|
|
case "Polygon":
|
|
process(geometry.coordinates);
|
|
break;
|
|
|
|
case "MultiPolygon":
|
|
process(geometry.coordinates);
|
|
break;
|
|
|
|
default:
|
|
throw new Error("Unsupported geometry type: " + geometry.type);
|
|
}
|
|
|
|
return { minLng, minLat, maxLng, maxLat };
|
|
}
|
|
|
|
module.exports = { getBBox }; |