UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

38 lines (37 loc) 1.5 kB
// SPDX-License-Identifier: Apache-2.0 import { fromUrl } from 'geotiff'; export async function extractGeoTransform(image) { const tiePoint = (await image.getTiePoints())[0]; const pixelScale = image.getFileDirectory().getValue('ModelPixelScale'); return { tiePoint, pixelScale }; } export function mapToPixel(transform, mapX, mapY) { const pixelX = Math.round((mapX - transform.tiePoint.x) / transform.pixelScale[0]); const pixelY = Math.round((transform.tiePoint.y - mapY) / transform.pixelScale[1]); return [pixelX, pixelY]; } export function pixelToMap(transform, pixelX, pixelY) { const mapX = transform.tiePoint.x + pixelX * transform.pixelScale[0]; const mapY = transform.tiePoint.y - pixelY * transform.pixelScale[1]; return [mapX, mapY]; } export async function getImage(url) { const tiff = await fromUrl(url); const image = await tiff.getImage(); return image; } export async function getPixelValue(image, transform, coordinates) { const [pixelX, pixelY] = mapToPixel(transform, coordinates[0], coordinates[1]); const window = [pixelX, pixelY, pixelX + 1, pixelY + 1]; const width = image.getWidth(); const height = image.getHeight(); let pixelValue; if (pixelX >= 0 && pixelX < width && pixelY >= 0 && pixelY < height) { const pixelData = (await image.readRasters({ samples: [0], window: window })); pixelValue = pixelData[0][0]; } else { pixelValue = null; } return pixelValue; }