UNPKG

qwc2

Version:
344 lines (343 loc) 19.1 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Copyright 2024 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 isEmpty from 'lodash.isempty'; import ol from 'openlayers'; import pointInPolygon from 'point-in-polygon'; import polygonIntersectTest from 'polygon-intersect-test'; import PropTypes from 'prop-types'; import { v4 as uuidv4 } from 'uuid'; import { LayerRole, addLayerFeatures, clearLayer } from '../actions/layers'; import IdentifyUtils from '../utils/IdentifyUtils'; import LocaleUtils from '../utils/LocaleUtils'; import MapUtils from '../utils/MapUtils'; import VectorLayerUtils from '../utils/VectorLayerUtils'; import MapSelection from './MapSelection'; import PopupMenu from './widgets/PopupMenu'; import Spinner from './widgets/Spinner'; import './style/PickFeature.css'; var PickFeature = /*#__PURE__*/function (_React$Component) { function PickFeature(props) { var _this; _classCallCheck(this, PickFeature); _this = _callSuper(this, PickFeature, [props]); _defineProperty(_this, "handleIdentifyResponse", function (response, reqId, layer, infoFormat) { if (_this.state.reqId !== reqId) { return; } var result = IdentifyUtils.parseResponse(response, layer, infoFormat, _this.state.clickPos, _this.props.map.projection, false); if (_this.props.featureFilter) { Object.entries(result).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), layername = _ref2[0], features = _ref2[1]; result[layername] = features.filter(_this.props.featureFilter); }); } else { Object.entries(result).forEach(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), layername = _ref4[0], features = _ref4[1]; result[layername] = features.filter(function (feature) { return !!feature.geometry; }); }); } _this.setState(function (state) { var newState = { pickResults: [].concat(_toConsumableArray(state.pickResults), _toConsumableArray(Object.entries(result).map(function (_ref5) { var _ref6 = _slicedToArray(_ref5, 2), layername = _ref6[0], features = _ref6[1]; return features.map(function (feature) { return { layer: layername, feature: feature, mapName: layer.wms_name }; }); }).flat())), pendingQueries: state.pendingQueries - 1 }; if (newState.pendingQueries === 0) { if (newState.pickResults.length === 1) { _this.props.featurePicked(newState.pickResults[0].layer, newState.pickResults[0].feature, newState.pickResults[0].mapName); newState.pickResults = null; newState.pickGeom = null; } else if (newState.pickResults.length === 0) { newState.pickResults = null; newState.pickGeom = null; } } return newState; }); }); _defineProperty(_this, "highlightFeature", function (key, feature) { var layer = { id: "pick-feature-selection", role: LayerRole.SELECTION }; _this.props.addLayerFeatures(layer, [feature], true); _this.setState({ highlightedFeature: key }); }); _defineProperty(_this, "clearHighlight", function (key, feature) { if (_this.state.highlightedFeature === key) { _this.setState({ highlightFeature: null }); _this.props.clearLayer("pick-feature-selection"); } }); _defineProperty(_this, "onClose", function () { _this.setState(PickFeature.defaultState); _this.props.clearLayer("pick-feature-selection"); }); _this.state = PickFeature.defaultState; return _this; } _inherits(PickFeature, _React$Component); return _createClass(PickFeature, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var _this2 = this; if (this.state.pickGeom && this.state.pickGeom !== prevState.pickGeom) { var queryWmsLayers = []; var queryVectorLayers = []; if (this.props.layerFilter) { queryWmsLayers = [this.props.layers.find(function (l) { return l.url === _this2.props.layerFilter.url; })].filter(Boolean); } else { queryWmsLayers = IdentifyUtils.getQueryLayers(this.props.layers, this.props.map); queryVectorLayers = this.props.layers.filter(function (layer) { return layer.visibility && LayerRole.USERLAYER && (layer.type === 'vector' || layer.type === 'wfs'); }); } if (this.props.layerFilterFunc) { queryWmsLayers = queryWmsLayers.filter(this.props.layerFilterFunc); queryVectorLayers = queryVectorLayers.filter(this.props.layerFilterFunc); } if (isEmpty(queryWmsLayers) && isEmpty(queryVectorLayers)) { return; } this.setState(function (state) { var coordinates = _this2.props.pickGeomType === "Point" ? [[state.pickGeom.coordinates]] : state.pickGeom.coordinates; var maxX = coordinates[0][0][0]; var maxY = coordinates[0][0][1]; for (var i = 1; i < coordinates[0].length; ++i) { if (coordinates[0][i][0] > maxX) { maxX = coordinates[0][i][0]; maxY = coordinates[0][i][1]; } } var reqId = uuidv4(); var getPixelFromCoordinate = MapUtils.getHook(MapUtils.GET_PIXEL_FROM_COORDINATES_HOOK); var clickPos = getPixelFromCoordinate([maxX, maxY], false); if (!isEmpty(queryWmsLayers)) { queryWmsLayers.forEach(function (layer) { var request = null; if (_this2.props.pickGeomType === 'Point') { var _this2$props$layerFil; request = IdentifyUtils.buildRequest(layer, ((_this2$props$layerFil = _this2.props.layerFilter) === null || _this2$props$layerFil === void 0 ? void 0 : _this2$props$layerFil.name) || layer.queryLayers.join(","), state.pickGeom.coordinates, _this2.props.map); } else if (_this2.props.pickGeomType === 'Polygon') { var _this2$props$layerFil2; var filter = VectorLayerUtils.geoJSONGeomToWkt(_this2.state.pickGeom); request = IdentifyUtils.buildFilterRequest(layer, ((_this2$props$layerFil2 = _this2.props.layerFilter) === null || _this2$props$layerFil2 === void 0 ? void 0 : _this2$props$layerFil2.name) || layer.queryLayers.join(","), filter, _this2.props.map); } else { return; } IdentifyUtils.sendRequest(request, function (response) { return _this2.handleIdentifyResponse(response, reqId, layer, request.params.info_format); }); }); } var pickResults = []; if (!isEmpty(queryVectorLayers)) { var olMap = MapUtils.getHook(MapUtils.GET_MAP); var layerMap = queryVectorLayers.reduce(function (res, layer) { return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, layer.id, layer)); }, {}); var format = new ol.format.GeoJSON(); if (_this2.props.pickGeomType === 'Point') { olMap.forEachFeatureAtPixel(clickPos, function (feature, layer) { var _layer$get; var layerid = layer === null || layer === void 0 || (_layer$get = layer.get) === null || _layer$get === void 0 ? void 0 : _layer$get.call(layer, 'id'); if (layerid in layerMap) { var featureObj = format.writeFeatureObject(feature); var layername = layerMap[layerid].name; pickResults.push({ layer: layername, feature: featureObj }); } }); } else if (_this2.props.pickGeomType === 'Polygon') { var extent = ol.extent.boundingExtent(coordinates[0]); olMap.getLayers().forEach(function (layer) { if (!(layer.get('id') in layerMap)) { return; } layer.getSource().forEachFeatureIntersectingExtent(extent, function (feature) { var intersects = false; if (feature.getGeometry().getType() === "Point") { intersects = pointInPolygon(feature.getGeometry().getCoordinates(), coordinates[0]); } else if (feature.getGeometry().getType() === "LineString") { intersects = true; // TODO } else if (feature.getGeometry().getType() === "Polygon") { intersects = polygonIntersectTest(feature.getGeometry().getCoordinates()[0], coordinates[0]); } if (!intersects) { return; } var featureObj = format.writeFeatureObject(feature); var layername = layerMap[layer.get('id')].name; pickResults.push({ layer: layername, feature: featureObj }); }); }); } } return { pickResults: pickResults, clickPos: clickPos, pendingQueries: queryWmsLayers.length, reqId: reqId }; }); } } }, { key: "render", value: function render() { var _this3 = this; var resultsMenu = null; if (this.state.pickResults) { resultsMenu = /*#__PURE__*/React.createElement(PopupMenu, { className: "PickFeatureMenu", key: "PickResultMenu", onClose: this.onClose, x: this.state.clickPos[0], y: this.state.clickPos[1] }, this.state.pendingQueries === 0 ? this.state.pickResults.map(function (entry) { var _entry$feature$displa; var key = (entry.mapName || "") + "." + entry.layer + ":" + entry.feature.id; return /*#__PURE__*/React.createElement("div", { key: key, onClick: function onClick() { return _this3.props.featurePicked(entry.layer, entry.feature, entry.mapName); }, onMouseOut: function onMouseOut() { return _this3.clearHighlight(key, entry.feature); }, onMouseOver: function onMouseOver() { return _this3.highlightFeature(key, entry.feature); } }, entry.layer + ": " + ((_entry$feature$displa = entry.feature.displayname) !== null && _entry$feature$displa !== void 0 ? _entry$feature$displa : entry.feature.id)); }) : /*#__PURE__*/React.createElement("div", { className: "pick-feature-menu-querying" }, /*#__PURE__*/React.createElement(Spinner, null), LocaleUtils.tr("pickfeature.querying"))); } return [resultsMenu, /*#__PURE__*/React.createElement(MapSelection, { active: true, geomType: this.props.pickGeomType, geometry: this.state.pickGeom, geometryChanged: function geometryChanged(geom) { return _this3.setState({ pickGeom: geom }); }, key: "MapSelection", styleOptions: this.props.highlightStyle })]; } }]); }(React.Component); _defineProperty(PickFeature, "propTypes", { addLayerFeatures: PropTypes.func, clearLayer: PropTypes.func, /** Optional: Function which accepts a GeoJSON feature and returns whether it should be accepted (true) or discarded (false) */ featureFilter: PropTypes.func, featurePicked: PropTypes.func, /** The style used for highlighting filter geometries. */ highlightStyle: PropTypes.shape({ /* Stroke color rgba array, i.e. [255, 0, 0, 0.5] */ strokeColor: PropTypes.array, /* Stroke width */ strokeWidth: PropTypes.number, /* Stroke dash/gap pattern array. Empty for solid line. */ strokeDash: PropTypes.array, /* Fill color rgba array, i.e. [255, 0, 0, 0.33] */ fillColor: PropTypes.array }), /** Optional: Restrict pick to specified layer name */ layerFilter: PropTypes.shape({ url: PropTypes.string, name: PropTypes.string }), /** Optional: Filter function to restrict pick layers */ layerFilterFunc: PropTypes.func, layers: PropTypes.array, map: PropTypes.object, /** Pick geometry type: Point, Polygon, ... (default: Point) */ pickGeomType: PropTypes.string }); _defineProperty(PickFeature, "defaultProps", { pickGeomType: 'Point', highlightStyle: { strokeColor: [0, 0, 0], fillColor: [255, 255, 0, 0.25] } }); _defineProperty(PickFeature, "defaultState", { pickGeom: null, pickResults: null, clickPos: null, highlightedFeature: null, pendingQueries: 0, reqId: null }); export default connect(function (state) { return { layers: state.layers.flat, map: state.map }; }, { addLayerFeatures: addLayerFeatures, clearLayer: clearLayer })(PickFeature);