snap-bbox
Snap a Bounding Box to a Grid
the bounding box partially cuts off some of the pixel grid cells | the bounding box expands to the pixel grid cell boundaries |
 |  |
why
I often try to pull pixel values from GeoTIFFs to display on a web map.
However, the pixels are often displayed in a different projection (like web mercator)
than the projection of the data (often 4326).
Because our pixels are in a structured array and you can't request only part of an array item (try running [1, 2, 3][0.5]
), we have to snap our bounding box to the grid structure of our data.
install
npm install snap-bbox
basic usage
const snap = require("snap-bbox");
const result = snap({
bbox: [ -85.341796875, 35.02999636902566, -85.2978515625, 35.06597313798418 ],
origin: [-180, 90],
scale: [0.083333333333333, -0.083333333333333],
padding: [3, 0],
container: [ -180, 0, 180, 90 ]
});
result will be something like:
{
bbox_in_coordinate_system: [
-85.66666666666706,
35.00000000000022,
-85.00000000000038,
35.083333333333556
],
bbox_in_grid_cells: [
1132,
660,
1140,
659
]
}
advanced precise mode
If you require the highest level of precision, you can use snap-bbox in "precise" mode.
Precise mode avoids floating point arithmetic issues. When using precise mode,
all numbers must be passed in as numerical strings and all output numbers will be
represented as strings. snap-bbox uses preciso
for precise numerical computations.
const snap = require("snap-bbox");
const result = snap({
bbox: [ "-85.341796875", "35.02999636902566", "-85.2978515625", "35.06597313798418" ],
origin: ["-180", "90"],
scale: ["0.083333333333333", "-0.083333333333333"],
padding: ["3", "0"],
container: ["-180", "0", "180", "90"],
precise: true
});
result will use precise numerical strings:
{
bbox_in_coordinate_system: [
"-85.666666666667044",
"35.00000000000022",
"-85.00000000000038",
"35.083333333333553"
],
bbox_in_grid_cells: [
"1132",
"660",
"1140",
"659"
]
}
limiting to edge of finite grid
If the grid has a fixed height and width, you can limit the result to the boundary of the grid
snap({
...options,
size: [width, height],
overflow: false
});