@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
170 lines (169 loc) • 5.96 kB
JavaScript
import ConfigManager from '../../../tools/configuration/configmanager';
import I18nManager from '../../../tools/i18n/i18nmanager';
import { getPixelValue, getImage, extractGeoTransform } from '../../../tools/raster/rasterutils';
import proj4 from 'proj4';
export class MapContextMenuManager {
constructor(MapContextMenuState) {
Object.defineProperty(this, "MapContextMenuState", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "i18nManager", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "_position", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "projection", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
Object.defineProperty(this, "rasters", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
const config = ConfigManager.getInstance().Config.contextmenu;
this.i18nManager = I18nManager.getInstance();
this.MapContextMenuState = MapContextMenuState;
this.init(config);
}
get position() {
return this._position;
}
set position(value) {
this._position = value;
this.updateCoordinates();
this.updateRasterSamples();
this.updateLinks();
}
init(config) {
// Initialize positions
config.crs.forEach((el) => {
const crs = {
code: el.code,
translation: el.translation,
format: el.format,
precision: el.precision,
coordinate: [0, 0]
};
this.MapContextMenuState.crs.push(crs);
});
// Initialize links
config.links.forEach((el) => {
const link = {
translation: el.translation,
crs: el.crs,
url: el.url,
content: ''
};
this.MapContextMenuState.links.push(link);
});
// Initialize raster sources
config.sources.forEach(async (el) => {
const raster = {
id: el.id,
url: el.url,
type: 'COG',
crs: el.crs,
label: el.translation,
image: null,
transform: null,
pixelvalue: null,
pixelstring: null,
prefix: el.prefix,
suffix: el.suffix,
precision: el.precision,
nodata: el.nodata
};
this.rasters.push(raster);
this.MapContextMenuState.sources.push({
id: el.id,
url: el.url,
crs: el.crs,
translation: el.translation,
content: null,
loading: true
});
try {
const image = await getImage(el.url);
const transform = extractGeoTransform(image);
raster.image = image;
raster.transform = transform;
}
catch (err) {
console.error(`Failed to load raster ${el.id}:`, err);
}
});
}
getCoordinate(code) {
const entry = this.MapContextMenuState.crs.find((c) => c.code === code);
return entry ? entry.coordinate : null;
}
updateCoordinates() {
this.MapContextMenuState.projection = this.projection;
this.MapContextMenuState.position = this.position;
this.MapContextMenuState.crs.forEach(async (src) => {
src.coordinate = proj4(this.projection, src.code, this._position);
});
}
updateLinks() {
for (const link of this.MapContextMenuState.links) {
const coord = this.getCoordinate(link.crs);
if (coord && typeof link.url === 'string' && link.url.includes('###MAPX###') && link.url.includes('###MAPY###')) {
const [x, y] = coord;
link.content = link.url.replace(/###MAPX###/g, x.toString()).replace(/###MAPY###/g, y.toString());
}
else {
link.content = null;
}
}
}
resetRasters() {
this.rasters.forEach((el) => {
el.pixelvalue = null;
el.pixelstring = null;
});
this.MapContextMenuState.sources.forEach((src) => {
src.content = this.i18nManager.getTranslation('Loading');
src.loading = true;
});
}
updateRasterSamples() {
this.resetRasters();
this.rasters.forEach(async (raster) => {
if (!raster.image || !raster.transform)
return;
try {
const [mx, my] = proj4(this.projection, raster.crs, this.position);
const val = await getPixelValue(raster.image, raster.transform, [mx, my]);
raster.pixelvalue = val;
if (val == null || val == raster.nodata) {
raster.pixelstring = null;
}
else {
raster.pixelstring = `${raster.prefix}${val.toFixed(raster.precision)}${raster.suffix}`;
}
const entry = this.MapContextMenuState.sources.find((s) => s.id === raster.id);
if (entry) {
entry.content = raster.pixelstring;
entry.loading = false;
}
}
catch (err) {
console.warn(`Failed to query raster source: ${raster.url}:`, err);
}
});
}
}