UNPKG

qwc2

Version:
215 lines (214 loc) 11.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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } 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 _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 2019, norBIT GmbH. * 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 axios from 'axios'; import ol from 'openlayers'; import PropTypes from 'prop-types'; import { v4 as uuidv4 } from 'uuid'; import { LayerRole } from '../../actions/layers'; import FeatureStyles from "../../utils/FeatureStyles"; import IdentifyUtils from '../../utils/IdentifyUtils'; import MapUtils from '../../utils/MapUtils'; var SnapSupport = /*#__PURE__*/function (_React$Component) { function SnapSupport(props) { var _this; _classCallCheck(this, SnapSupport); _this = _callSuper(this, SnapSupport, [props]); _defineProperty(_this, "state", { mousePos: null }); _defineProperty(_this, "getMapMousePos", function (ev) { _this.setState({ mousePos: { coordinate: ev.coordinate, pixel: ev.pixel } }); }); _defineProperty(_this, "addSnapFeatures", function (geojson) { _this.reset(); var format = new ol.format.GeoJSON(); var features = format.readFeatures(geojson); var _iterator = _createForOfIteratorHelper(features), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var feature = _step.value; _this.snapSource.addFeature(feature); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } }); _defineProperty(_this, "addSnapInteraction", function () { _this.snapInteraction = new ol.interaction.Snap({ source: _this.snapSource }); _this.props.map.addInteraction(_this.snapInteraction); }); _defineProperty(_this, "getFeature", function () { _this.timeoutId = null; var layers = _this.props.layers.find(function (layer) { return layer.role === LayerRole.THEME; }); var queryLayers = _this.props.layers.reduce(function (accum, layer) { return layer.role === LayerRole.THEME ? accum.concat(layer.queryLayers) : accum; }, []).join(","); if (!layers || !queryLayers) { return; } var options = { info_format: 'text/xml', feature_count: 20, FI_POINT_TOLERANCE: 16, FI_LINE_TOLERANCE: 8, FI_POLYGON_TOLERANCE: 4 }; var request = IdentifyUtils.buildRequest(layers, queryLayers, _this.state.mousePos.coordinate, _this.props.mapObj, options); axios.get(request.url, { params: request.params }).then(function (response) { var results = IdentifyUtils.parseXmlResponse(response.data, _this.props.mapObj.projection, layers); var features = []; results.forEach(function (result) { var _iterator2 = _createForOfIteratorHelper(result), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var feature = _step2.value; if (feature.geometry) { feature.id = uuidv4(); features.push(feature); } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } }); if (!features) { return; } var geojson = { type: 'FeatureCollection', crs: { type: 'name', properties: { name: _this.props.mapObj.projection } }, features: features }; _this.addSnapFeatures(geojson); _this.addSnapInteraction(); }); }); _defineProperty(_this, "reset", function () { if (_this.snapInteractions) { _this.props.map.removeInteraction(_this.snapInteraction); } var _iterator3 = _createForOfIteratorHelper(_this.snapSource.getFeatures()), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var feature = _step3.value; _this.snapSource.removeFeature(feature); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } }); var geometryFunction = function geometryFunction(feature) { if (feature.getGeometry().getType() === "Point") { return new ol.geom.MultiPoint([feature.getGeometry().getCoordinates()]); } else if (feature.getGeometry().getType() === "LineString") { return new ol.geom.MultiPoint(feature.getGeometry().getCoordinates()); } return new ol.geom.MultiPoint(feature.getGeometry().getCoordinates()[0]); }; _this.snapSource = new ol.source.Vector(); _this.snapLayer = new ol.layer.Vector({ source: _this.snapSource, zIndex: 1000000, style: [FeatureStyles.interaction({}, true), FeatureStyles.interactionVertex({ geometryFunction: geometryFunction }, true)] }); _this.props.map.addLayer(_this.snapLayer); _this.curPos = null; return _this; } _inherits(SnapSupport, _React$Component); return _createClass(SnapSupport, [{ key: "componentDidMount", value: function componentDidMount() { MapUtils.getHook(MapUtils.ADD_POINTER_MOVE_LISTENER)(this.getMapMousePos); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { MapUtils.getHook(MapUtils.REMOVE_POINTER_MOVE_LISTENER)(this.getMapMousePos); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this2 = this; if (this.props.drawing && this.state.mousePos && (!this.curPos || Math.abs(this.state.mousePos.pixel[0] - this.curPos[0]) > 5 || Math.abs(this.state.mousePos.pixel[1] - this.curPos[1]) > 5)) { clearTimeout(this.timeoutId); this.curPos = this.state.mousePos.pixel; this.timeoutId = setTimeout(function () { return _this2.getFeature(); }, 500); } else if (!this.props.drawing && prevProps.drawing) { this.reset(); } } }, { key: "render", value: function render() { return null; } }]); }(React.Component); _defineProperty(SnapSupport, "propTypes", { drawing: PropTypes.bool, layers: PropTypes.array, map: PropTypes.object, mapObj: PropTypes.object }); var selector = function selector(state) { return { drawing: ["Redlining", "Measure", "Editing"].includes(state.task.id), mapObj: state.map, layers: state.layers.flat }; }; export default connect(selector, {})(SnapSupport);