Skip to content

Instantly share code, notes, and snippets.

@Akiyamka
Last active June 30, 2020 08:29
Show Gist options
  • Save Akiyamka/f76e5f3f2a9033d3c21008b97291240a to your computer and use it in GitHub Desktop.
Save Akiyamka/f76e5f3f2a9033d3c21008b97291240a to your computer and use it in GitHub Desktop.
bbox utils
/**
* bbox: [llX, llY, urX, urY]
* ┌──╗ - ur (2)
* ╚──┘
* └ ll (1)
*/
function bboxToPolygon(bbox) {
const [llX, llY, urX, urY] = bbox;
return {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[ llX, llY ],
[ llX, urY ],
[ urX, urY ],
[ urX, llY ],
[ llX, llY ]
]
]
}
}
}
function polygonToBbox(feature) {
if (feature.type === "Polygon") {
const coordinates = feature.geometry.coordinates[0];
return [coordinates[0], coordinates[2]];
} else {
throw new Error('Only polygon feature supported');
}
}
const isOdd = num => num % 2 === 1;
export function sum(boxes) {
const lat = [];
const lng = [];
for (const box of boxes) {
box.forEach((b, i) => (isOdd(i) ? lng : lat).push(b);
}
return [
Math.min(...lat),
Math.min(...lng),
Math.max(...lat),
Math.max(...lng),
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment