UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

93 lines (92 loc) 3.24 kB
import ConfigManager from '../../tools/configuration/configmanager'; import { getPixelValue, getImage, extractGeoTransform } from '../../tools/raster/rasterutils'; import proj4 from 'proj4'; export class RasterManager { constructor(MapContextMenuState) { Object.defineProperty(this, "MapContextMenuState", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "sources", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "rasters", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.MapContextMenuState = MapContextMenuState; this.sources = ConfigManager.getInstance().Config.contextmenu.sources; this.rasters = []; this.init(); } init() { this.sources.forEach(async (src) => { const raster = { id: src.id, url: src.url, type: 'COG', crs: src.crs, label: src.translation, image: null, transform: null, pixelvalue: null, pixelstring: null, prefix: src.prefix, suffix: src.suffix, precision: src.precision }; this.rasters.push(raster); this.MapContextMenuState.sources.push({ id: src.id, url: src.url, crs: src.crs, translation: src.translation, content: null }); try { const image = await getImage(src.url); const transform = extractGeoTransform(image); raster.image = image; raster.transform = transform; } catch (err) { console.error(`Failed to load raster ${src.id}:`, err); } }); } reset() { this.rasters.forEach((el) => { el.pixelvalue = null; el.pixelstring = null; }); this.MapContextMenuState.sources.forEach((src) => { src.content = null; }); } refresh(projection, position) { this.reset(); this.rasters.forEach(async (raster) => { if (!raster.image || !raster.transform) return; try { const [mx, my] = proj4(projection, raster.crs, position); const val = await getPixelValue(raster.image, raster.transform, [mx, my]); raster.pixelvalue = val; raster.pixelstring = val != null ? `${raster.prefix}${val.toFixed(raster.precision)}${raster.suffix}` : null; const entry = this.MapContextMenuState.sources.find((s) => s.id === raster.id); if (entry) entry.content = raster.pixelstring; } catch (err) { console.warn(`Failed to query raster source: ${raster.url}:`, err); } }); } }