-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsedata.js
49 lines (42 loc) · 1.13 KB
/
parsedata.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
38
39
40
41
42
43
44
45
46
47
48
49
const fs = require("fs");
const GeoTIFF = require("geotiff");
const getPixel = (values, x, y, w, h) => {
return parseInt(values[y * w + x]);
};
const load = async () => {
const tiff = await GeoTIFF.fromFile("./data/reproject-0.01.tif");
const image = await tiff.getImage();
//console.log(image.fileDirectory);
const w = image.fileDirectory.ImageWidth;
const h = image.fileDirectory.ImageLength;
console.log(w, h);
const pxSize = image.fileDirectory.ModelPixelScale[0];
const coordinatesOrigin = [
image.fileDirectory.ModelTiepoint[3],
image.fileDirectory.ModelTiepoint[4],
];
const [values] = await image.readRasters({
width: w,
height: h,
resampleMethod: "bilinear",
});
const imageArr = [];
for (let x = 0; x !== w; x++) {
if (!imageArr[x]) {
imageArr[x] = [];
}
for (let y = 0; y !== h; y++) {
const px = getPixel(values, x, y, w, h);
imageArr[x][y] = px < 0 ? false : px;
}
}
//console.log(imageArr);
fs.writeFileSync(
"data/population.json",
JSON.stringify({
data: imageArr,
meta: { pxSize, coordinatesOrigin, w, h },
})
);
};
load();