Last active
June 30, 2020 08:29
-
-
Save Akiyamka/f76e5f3f2a9033d3c21008b97291240a to your computer and use it in GitHub Desktop.
bbox utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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