UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

113 lines (112 loc) 4.54 kB
import { BaseCustomizer, requestReport as requestReportMFP, getDownloadUrl as getDownloadUrlMFP, cancelPrint as cancelPrintMFP, getPrintExtent as getPrintExtentMFP } from '@geoblocks/mapfishprint'; import { MFPLegendEncoder } from './MFPLegendEncoder.js'; import MFPEncoder from './MFPEncoder.js'; import { removeUnwantedOlParams } from '../../../tools/utils/olutils.js'; import { intersects } from 'ol/extent.js'; import WfsManager from '../../../tools/wfs/wfsmanager.js'; /** * An interface class that manages printing functionality (including legend). */ export default class PrintManager { legendEncoder; encoder; customizer; constructor() { this.legendEncoder = new MFPLegendEncoder(); this.encoder = new MFPEncoder(); this.customizer = new BaseCustomizer(); } /** * Requests a print report. * @returns A promise that resolves with the response from the report request. */ async requestReport(printUrl, spec) { return requestReportMFP(printUrl, spec); } /** * Cancels a report. * @returns A promise that resolves with the cancellation response. */ async cancelReport(printUrl, ref) { return cancelPrintMFP(printUrl, ref); } /** * Retrieves the download URL for a given request report. * @returns A promise that resolves to the download URL. */ async getDownloadUrl(requestReport, response, interval = 1000, timeout = 30000) { return getDownloadUrlMFP(requestReport, response, interval, timeout); } /** * Introspect the map and convert each of its layers to MFP v3 format. * @returns a top level MFP spec */ encode(options) { const center = options.mapManager.getMap().getView().getCenter() || [0, 0]; this.customizer.setPrintExtent(this.getExtent(options.pageSize, options.scale, center) || [0, 0, 0, 0]); const mapSpec = this.encoder.encodeMap({ state: options.state, mapManager: options.mapManager, scale: options.scale, printResolution: options.mapManager.getMap().getView().getResolution() || 100, dpi: options.dpi, customizer: this.customizer }); const attributes = { map: mapSpec }; Object.assign(attributes, options.customAttributes); return { attributes, format: options.format, layout: options.layout, lang: options.state.language ?? undefined }; } /** * Encodes a print legend based on the given options. * @returns The encoded legend, or null if encoding is empty or fails. */ encodeLegend(options) { const center = options.mapManager.getMap().getView().getCenter() || [0, 0]; const extent = this.getExtent(options.pageSize, options.scale, center) || [0, 0, 0, 0]; options.extent = options.extent ?? extent; return this.legendEncoder.encodeLegend(options); } /** * Calculates the extent of the paper based on the given scale and center coordinates. * @returns The extent of the paper. */ getExtent(pageSize, scale, center) { return getPrintExtentMFP(pageSize, center, scale); } /** * Formats the data source for printing, based on selected features filtered by the given extent. * @static * */ static getPrintDatasourceFromSelectedFeatures(selectedFeatures, extent, i18nManager) { return (selectedFeatures.reduce((datasources, feature) => { const featureExtent = feature.getGeometry()?.getExtent() ?? []; if (!intersects(featureExtent, extent)) { return datasources; } const rawTitle = WfsManager.extractFeatureTypeFromId(feature); const title = i18nManager.getTranslation(rawTitle); const properties = removeUnwantedOlParams(feature); const datasource = datasources.find((datasource) => datasource.title === title); const values = Object.values(properties); if (datasource) { datasource.table.data.push(values); return datasources; } datasources.push({ title, table: { data: [values], columns: Object.keys(properties).map((column) => i18nManager.getTranslation(column)) } }); return datasources; }, []) || []); } }