UNPKG

@thi.ng/pixel-io-geotiff

Version:

GeoTIFF reader support for @thi.ng/pixel

43 lines (42 loc) 1.28 kB
import { typedArrayType } from "@thi.ng/api"; import { FLOAT_GRAY_RANGE, floatBuffer } from "@thi.ng/pixel"; import { Pool, fromArrayBuffer } from "geotiff"; const readGeoTiff = async (src, opts = {}) => { const tiff = await fromArrayBuffer(src.buffer); const tiffImg = await tiff.getImage(); const width = tiffImg.getWidth(); const height = tiffImg.getHeight(); const pool = opts.pool instanceof Pool ? opts.pool : opts.pool ? new Pool() : void 0; const data = (await tiffImg.readRasters({ pool, samples: [opts.channel || 0] }))[0]; const type = typedArrayType(data); const [min, max] = opts.range ? opts.range : data.reduce( (acc, x) => { acc[0] = Math.min(acc[0], x); acc[1] = Math.max(acc[1], x); return acc; }, [Infinity, -Infinity] ); const fmt = FLOAT_GRAY_RANGE(min, max); let img; switch (type) { case "i8": case "u8": case "i16": case "u16": case "i32": case "u32": img = floatBuffer(width, height, fmt, new Float32Array(data)); break; case "f32": img = floatBuffer(width, height, fmt, data); break; case "f64": img = floatBuffer(width, height, fmt, new Float32Array(data)); break; } return { img, tiff: tiffImg }; }; export { readGeoTiff };