-
Notifications
You must be signed in to change notification settings - Fork 1
/
iter-tiles.js
37 lines (35 loc) · 1.2 KB
/
iter-tiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { wrapNextFunction } = require("iter-fun");
module.exports = function iterTiles(image, samples) {
const height = image.getHeight();
const width = image.getWidth();
const tileWidth = image.getTileWidth();
const tileHeight = image.getTileHeight();
const numTilesPerRow = Math.ceil(width / tileWidth);
const numTilesPerCol = Math.ceil(height / tileHeight);
const numTiles = numTilesPerRow * numTilesPerCol;
let tileIndex = -1;
const iterTiles = wrapNextFunction(function next() {
try {
tileIndex++;
if (tileIndex >= numTiles) {
return { value: false, done: true };
} else {
const row = Math.floor(tileIndex / numTilesPerRow);
const column = tileIndex % numTilesPerRow;
const xmin = column * tileWidth;
const ymin = row * tileHeight;
const xmax = xmin + tileWidth;
const ymax = ymin + tileHeight;
const imageWindow = [xmin, ymin, Math.min(width, xmax), Math.min(height, ymax)];
const rasters = image.readRasters({
samples,
window: imageWindow
});
return { value: rasters, done: false };
}
} catch (error) {
console.error(error);
}
});
return iterTiles;
};