/** * Copyright Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import {connect} from 'react-redux'; import FileSaver from 'file-saver'; import isEmpty from 'lodash.isempty'; import PropTypes from 'prop-types'; import {addThemeSublayer} from '../../actions/layers'; import {logAction} from '../../actions/logging'; import {zoomToPoint} from '../../actions/map'; import {setCurrentTask} from '../../actions/task'; import Icon from '../../components/Icon'; import MapSelection from '../../components/MapSelection'; import ResizeableWindow from '../../components/ResizeableWindow'; import Spinner from '../../components/widgets/Spinner'; import ConfigUtils from '../../utils/ConfigUtils'; import CoordinatesUtils from '../../utils/CoordinatesUtils'; import LocaleUtils from '../../utils/LocaleUtils'; import MapUtils from '../../utils/MapUtils'; import {UrlParams} from '../../utils/PermaLinkUtils'; import Request from '../../utils/Request'; import VectorLayerUtils from '../../utils/VectorLayerUtils'; import './style/PlotInfoTool.css'; /** * Plugin for requesting plot information, including Swiss Public-law Restrictions on landownership (PLR) cadastre. * * **`config.json` sample configuration:** * * ``` * { * "name": "PlotInfoTool", * "cfg": { * "toolLayers": ["", ...], * "infoQueries": [ * { * "key": "plotdescr", * "titleMsgId": "plotdescr.title", * "query": "/plot/$egrid$", * "pdfQuery": null, * "urlKey": "cadastre_egrid" * }, * { * "key": "oereb", * "titleMsgId": "oereb.title", * "query": "/oereb/json/$egrid$", * "pdfQuery": "/oereb/pdf/$egrid$", * "pdfTooltip": "oereb.requestPdf", * "urlKey": "oereb_egrid", * "cfg": { * "subthemes": { * "LandUsePlans": ["Grundnutzung", "Überlagerungen", "Linienbezogene Festlegungen", "Objektbezogene Festlegungen"] * } * } * } * ] * } * } * ``` * Where: * * * `toolLayers`: List of layers to load when activating tool. * * `infoQueries`: List of additional info queries to offer in the dialog (PLR cadastre query is built-in). By default, these render some HTML data in an iframe. If a custom component is needed for rendering the result, see configuration in `appConfig.js` below. * - `key`: A unique key name. * - `title`: The human visible title. * - `titleMsgId`: Instead of `title`, a message id for the title which will be looked up in the translations. * - `query`: The query to perform to retreive the info. Must return HTML, which is then rendered in an iframe. `$egrid$` is replaced with the EGRID of the current plot. If the specified URL is relative, it is resolved with respect to `plotInfoService` as defined in `config.json`. * - `pdfQuery`: Optional query to retreive a PDF report, which is then presented as a download the the user. Again, `$egrid$` is replaced with the EGRID of the current plot. * - `pdfTooltip`: Message id for the pdf button tooltip. * - `urlKey`: Optional query parameter key name. If QWC2 is started with `=` in the URL, the plot info tool is automatically enabled and the respective query performed. * - `cfg`: Arbitrary custom config to pass to a custom component, see `appConfig.js` configuration below. * * **`appConfig.js` configuration:** * * Sample `PlotInfoToolPlugin` configuration, as can be defined in the `cfg` section of `pluginsDef` in `appConfig.js`: * * ``` * PlotInfoToolPlugin: { * themeLayerRestorer: require('./themeLayerRestorer'), * customInfoComponents: { * oereb: require('qwc2-extra/components/OerebDocument') * } * } * ``` * Where: * * * `themeLayerRestorer`: Function which restores theme layers, used for loading the `toolLayers` specified in the configuration in `config.json`. See `themeLayerRestorer` in the [sample `appConfig.js`](https://github.com/qgis/qwc2-demo-app/blob/master/js/appConfig.js). * * `customInfoComponents`: Customized components for rendering plot info query results. The `key` specifies a the info query for which this component should be used, as specified in `infoQueries` in config.json (see above). An example of a minimal custom component: * * class CustomPlotInfoComponent extends React.Component { * static propTypes = { * data: PropTypes.object, // PropType according to format of data returned by the specified query URL * config: PropTypes.object // Custom configuration * } * render() { * return (
{this.props.data.field}
); * } * }; * * module.exports = CustomPlotInfoComponent; */ class PlotInfoTool extends React.Component { static propTypes = { addThemeSublayer: PropTypes.func, currentTask: PropTypes.string, customInfoComponents: PropTypes.object, infoQueries: PropTypes.array, logAction: PropTypes.func, map: PropTypes.object, setCurrentTask: PropTypes.func, theme: PropTypes.object, themeLayerRestorer: PropTypes.func, toolLayers: PropTypes.array, windowSize: PropTypes.object, zoomToPoint: PropTypes.func }; static defaultProps = { toolLayers: [], infoQueries: [], customInfoComponents: {}, windowSize: {width: 500, height: 800} }; state = { plotInfo: null, currentPlot: null, expandedInfo: null, expandedInfoData: null, pendingPdfs: [] }; constructor(props) { super(props); this.selection = null; } componentDidUpdate(prevProps, prevState) { if (this.props.theme && !prevProps.theme) { if (UrlParams.getParam('realty') !== undefined) { this.props.setCurrentTask('PlotInfoTool'); } else { for (const entry of this.props.infoQueries) { if (entry.urlKey && UrlParams.getParam(entry.urlKey)) { this.props.setCurrentTask('PlotInfoTool'); this.queryInfoByEgrid(entry, UrlParams.getParam(entry.urlKey)); UrlParams.updateParams({[entry.urlKey]: undefined}); break; } } } } else if (this.props.currentTask === 'PlotInfoTool' && prevProps.currentTask !== 'PlotInfoTool') { this.activated(); } else if (this.props.currentTask !== 'PlotInfoTool' && prevProps.currentTask === 'PlotInfoTool') { this.deactivated(); } if (this.state.plotInfo) { if ( this.state.plotInfo !== prevState.plotInfo || this.state.currentPlot !== prevState.currentPlot ) { const wkt = this.state.plotInfo[this.state.currentPlot].geom; const feature = VectorLayerUtils.wktToGeoJSON(wkt, "EPSG:2056", this.props.map.projection); feature.styleName = 'default'; feature.styleOptions = { fillColor: [0, 0, 0, 0], strokeColor: [242, 151, 84, 0.75], strokeWidth: 8, strokeDash: [] }; this.selection = MapUtils.addUnmanagedLayer({features: [feature]}, this.selection); } } else if (prevState.plotInfo && !this.state.plotInfo) { MapUtils.removeUnmanagedLayer(this.selection); } } render() { if (this.props.currentTask !== 'PlotInfoTool') { return null; } let resultDialog = null; if (!isEmpty(this.state.plotInfo)) { let scrollable = false; if (this.state.expandedInfo) { const entry = this.props.infoQueries.find(e => e.key === this.state.expandedInfo); if (entry) { scrollable = entry.scrollmode === "parent"; } } resultDialog = ( this.props.setCurrentTask(null)} scrollable={scrollable} title={LocaleUtils.tr("appmenu.items.PlotInfoTool")} > {this.renderBody()} ); } const assetsPath = ConfigUtils.getAssetsPath(); const selectionStyleOptions = { fillColor: [0, 0, 0, 0], strokeColor: [0, 0, 0, 0] }; return [resultDialog, ( this.queryBasicInfoAtPoint(geom.coordinates)} key="MapSelection" styleOptions={selectionStyleOptions} /> )]; } renderBody = () => { const plotServiceUrl = ConfigUtils.getConfigProp("plotInfoService").replace(/\/$/, ''); const plot = this.state.plotInfo[this.state.currentPlot]; return (
{this.state.plotInfo.map((entry, idx) => ([(
this.toggleCurrentPlot(idx)}> {entry.label}
), this.state.currentPlot !== idx ? null : (
{plot.fields.map(e => ( ))}
)]))}
{this.props.infoQueries.map((entry) => { let query = entry.query.replace('$egrid$', plot.egrid); if (!query.startsWith('http')) { query = plotServiceUrl + query; } let pdfQuery = entry.pdfQuery ? entry.pdfQuery.replace('$egrid$', plot.egrid) : null; if (pdfQuery && !pdfQuery.startsWith('http')) { pdfQuery = plotServiceUrl + pdfQuery; } const pdfTooltip = entry.pdfTooltip ? LocaleUtils.tr(entry.pdfTooltip) : ""; const expanded = this.state.expandedInfo === entry.key; return [ (
this.toggleEgridInfo(entry, query)}> {entry.titleMsgId ? LocaleUtils.tr(entry.titleMsgId) : entry.title} {entry.pdfQuery ? this.state.pendingPdfs.includes(pdfQuery) ? () : ( this.queryPdf(ev, entry, pdfQuery)} title={pdfTooltip} />) : null}
), expanded ? (
{!this.state.expandedInfoData ? this.renderWait() : this.state.expandedInfoData.failed ? this.renderError() : this.renderInfoData()}
) : null ]; })}
); }; toggleCurrentPlot = (idx) => { if (this.state.currentPlot !== idx) { this.setState({currentPlot: idx, expandedInfo: null, expandedInfoData: null, pendingPdfs: []}); } }; renderWait = () => { return (
{LocaleUtils.tr("plotinfotool.loading")}
); }; renderError = () => { return (
{this.state.expandedInfoData.failed === true ? LocaleUtils.tr("common.dataloadfailed") : LocaleUtils.tr(this.state.expandedInfoData.failed)}
); }; renderInfoData = () => { if (this.props.customInfoComponents[this.state.expandedInfo]) { const Component = this.props.customInfoComponents[this.state.expandedInfo]; const config = (this.props.infoQueries.find(entry => entry.key === this.state.expandedInfo) || {}).cfg || {}; return (); } else { const assetsPath = ConfigUtils.getAssetsPath(); const src = assetsPath + "/templates/blank.html"; return (