qwc2
Version:
QGIS Web Client
367 lines (365 loc) • 18.5 kB
JavaScript
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 _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 _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 _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 2017-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 ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import classNames from 'classnames';
import isEmpty from 'lodash.isempty';
import ol from 'openlayers';
import PropTypes from 'prop-types';
import { v4 as uuidv4 } from 'uuid';
import { LayerRole } from '../../actions/layers';
import { setSnappingConfig } from '../../actions/map';
import Icon from '../../components/Icon';
import { BottomToolPortalContext } from '../../components/PluginsContainer';
import Spinner from '../../components/widgets/Spinner';
import IdentifyUtils from '../../utils/IdentifyUtils';
import LocaleUtils from '../../utils/LocaleUtils';
import MapUtils from '../../utils/MapUtils';
import VectorLayerUtils from '../../utils/VectorLayerUtils';
import SnapInteraction from './SnapInteraction';
import './style/SnappingSupport.css';
/**
* Snapping support for the map component.
*/
var SnappingSupport = /*#__PURE__*/function (_React$Component) {
function SnappingSupport(props) {
var _this;
_classCallCheck(this, SnappingSupport);
_this = _callSuper(this, SnappingSupport, [props]);
_defineProperty(_this, "state", {
reqId: null,
// FeatureInfo request ID
invalid: true,
// Whether the feature cache needs to be rebuilt
havesnaplayers: false,
// Whether there are any snaplayers
drawing: false // Whether a drawing interaction is active,
});
_defineProperty(_this, "snapToEdge", function (snappingConfig) {
return snappingConfig.active === true || snappingConfig.active === 'edge';
});
_defineProperty(_this, "snapToVertex", function (snappingConfig) {
return snappingConfig.active === true || snappingConfig.active === 'vertex';
});
_defineProperty(_this, "toggleSnap", function (mode) {
var active = _this.props.mapObj.snapping.active;
if (mode === 'edge') {
if (active === true) {
active = 'vertex';
} else if (active === 'edge') {
active = false;
} else if (active === 'vertex') {
active = true;
} else {
active = 'edge';
}
} else if (mode === 'vertex') {
if (active === true) {
active = 'edge';
} else if (active === 'vertex') {
active = false;
} else if (active === 'edge') {
active = true;
} else {
active = 'vertex';
}
}
_this.props.setSnappingConfig(true, active);
});
_defineProperty(_this, "handleInteractionAdded", function (ev) {
if (_this.inEventHandler) {
return;
}
_this.inEventHandler = true;
_this.addSnapInteractionIfNeeded(ev.target);
_this.inEventHandler = false;
});
_defineProperty(_this, "handleInteractionRemoved", function (ev) {
if (_this.inEventHandler) {
return;
}
_this.inEventHandler = true;
// If the removed interaction is the snap interaction, which should always be
// the last interaction, remove the interaction preceding the snap interaction
if (ev.element === _this.snapInteraction) {
ev.target.pop();
}
_this.addSnapInteractionIfNeeded(ev.target);
_this.inEventHandler = false;
});
_defineProperty(_this, "addSnapInteractionIfNeeded", function (interactions) {
// Just to be sure
interactions.remove(_this.snapInteraction);
// If there is any draw or modify interaction, add snapping interaction
var added = false;
if (_this.props.mapObj.snapping.enabled) {
for (var i = 0; i < interactions.getLength(); ++i) {
var interaction = interactions.item(i);
if (interaction instanceof ol.interaction.Draw || interaction instanceof ol.interaction.Modify || interaction instanceof ol.interaction.Select) {
interactions.push(_this.snapInteraction);
added = true;
break;
}
}
}
_this.setState({
drawing: added
});
});
_defineProperty(_this, "refreshFeatureCache", function (force) {
if (!_this.state.invalid && !force || !_this.state.drawing) {
return;
}
_this.source.clear();
var themeLayer = _this.props.layers.find(function (layer) {
return layer.role === LayerRole.THEME;
});
if (!_this.props.theme || !themeLayer || !_this.state.drawing) {
return;
}
var snappingConfig = _this.props.theme.snapping || {};
// Gather snapping layers based on visibility scale ranges
var scale = MapUtils.computeForZoom(_this.props.mapObj.scales, _this.props.mapObj.zoom);
var snapLayers = (snappingConfig.snaplayers || []).reduce(function (res, cur) {
if (cur.min !== undefined && cur.min !== null && scale < cur.min) {
// Below scale range
return res;
}
if (cur.max !== undefined && cur.max !== null && scale >= cur.max) {
// Above scale range
return res;
}
return [].concat(_toConsumableArray(res), [cur.name]);
}, []);
// Gather local snap layers
var snapToWfs = scale < snappingConfig.wfsMaxScale;
var localLayers = [];
_this.props.layers.forEach(function (layer) {
if (layer.role === LayerRole.USERLAYER && (layer.type === 'vector' || layer.type === "wfs" && snapToWfs)) {
var olLayer = _this.props.map.getLayers().getArray().find(function (l) {
return l.get('id') === layer.id;
});
if (olLayer && olLayer.getSource() && olLayer.getSource().getFeaturesInExtent) {
localLayers.push(olLayer);
}
}
});
_this.setState({
reqId: null,
havesnaplayers: !isEmpty(snapLayers) || !isEmpty(localLayers)
});
if (!_this.snapInteraction.getMap() || !_this.snapInteraction.getActive()) {
return;
}
if (snapLayers.length === 0) {
_this.addLocalSnapFeatures(localLayers);
return;
}
var xmin = _this.props.mapObj.bbox.bounds[0];
var ymin = _this.props.mapObj.bbox.bounds[1];
var xmax = _this.props.mapObj.bbox.bounds[2];
var ymax = _this.props.mapObj.bbox.bounds[3];
var filterGeom = VectorLayerUtils.geoJSONGeomToWkt({
type: 'Polygon',
coordinates: [[[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax], [xmin, ymin]]]
});
var options = {
LAYERATTRIBS: JSON.stringify(snapLayers.reduce(function (res, key) {
return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, key, []));
}, {})),
with_htmlcontent: false,
with_bbox: false,
feature_count: snappingConfig.featureCount || 500
};
var request = IdentifyUtils.buildFilterRequest(themeLayer, snapLayers.join(","), filterGeom, _this.props.mapObj, options);
var reqId = uuidv4();
_this.setState({
reqId: reqId
});
IdentifyUtils.sendRequest(request, function (response) {
if (_this.state.reqId !== reqId) {
return;
}
if (response) {
var result = IdentifyUtils.parseXmlResponse(response, _this.props.mapObj.projection, themeLayer);
var features = Object.values(result).reduce(function (res, cur) {
return [].concat(_toConsumableArray(res), _toConsumableArray(cur));
}, []);
var format = new ol.format.GeoJSON();
var olFeatures = format.readFeatures({
type: "FeatureCollection",
features: features.map(function (feature) {
return _objectSpread(_objectSpread({}, feature), {}, {
id: uuidv4()
});
})
});
_this.source.addFeatures(olFeatures);
// Add features from local layers
_this.addLocalSnapFeatures(localLayers);
_this.setState({
invalid: false,
reqId: null,
havesnaplayers: true
});
} else {
_this.setState({
reqId: null
});
}
});
});
_defineProperty(_this, "addLocalSnapFeatures", function (localLayers) {
var extent = _this.props.mapObj.bbox.bounds;
var projection = ol.proj.get(_this.props.mapObj.projection);
localLayers.forEach(function (olLayer) {
var olFeatures = olLayer.getSource().getFeaturesInExtent(extent, projection);
_this.source.addFeatures(olFeatures);
});
});
_this.source = new ol.source.Vector();
_this.snapInteraction = new SnapInteraction({
source: _this.source,
edge: _this.snapToEdge(props.mapObj.snapping),
vertex: _this.snapToVertex(props.mapObj.snapping)
});
_this.snapInteraction.setActive(_this.props.mapObj.snapping.active);
_this.inEventHandler = false;
props.map.getInteractions().on('add', _this.handleInteractionAdded);
props.map.getInteractions().on('remove', _this.handleInteractionRemoved);
MapUtils.registerHook(MapUtils.GET_SNAPPED_COORDINATES_FROM_PIXEL_HOOK, function (pixel) {
var coo = props.map.getCoordinateFromPixel(pixel);
var snapResult = _this.snapInteraction.snapTo(pixel, coo, props.map);
return snapResult ? props.map.getCoordinateFromPixel(snapResult.vertexPixel) : coo;
});
return _this;
}
_inherits(SnappingSupport, _React$Component);
return _createClass(SnappingSupport, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this2 = this;
if (this.state.drawing && (this.props.mapObj.bbox !== prevProps.mapObj.bbox || this.props.theme !== prevProps.theme)) {
this.setState({
invalid: true
});
this.refreshFeatureCache(true);
} else if (this.state.drawing && this.props.layers !== prevProps.layers) {
var layersChanged = this.props.layers.find(function (layer) {
if (layer.role === LayerRole.THEME) {
var prev = prevProps.layers.find(function (prevLayer) {
return layer.id === prevLayer.id;
});
return !prev || layer.rev !== prev.rev;
} else if (layer.role === LayerRole.USERLAYER && layer.type === 'vector') {
var _prev = prevProps.layers.find(function (prevLayer) {
return layer.id === prevLayer.id;
});
return !_prev || _prev.features !== layer.features;
}
return false;
});
if (layersChanged) {
this.setState({
invalid: true
});
// Delay to avoid refreshing the cache before QGIS Server can pick up the new feature
setTimeout(function () {
_this2.refreshFeatureCache(true);
}, 1500);
}
}
if (this.props.mapObj.snapping.active !== prevProps.mapObj.snapping.active || this.state.drawing !== prevState.drawing) {
this.snapInteraction.setActive(this.props.mapObj.snapping.active !== false);
this.snapInteraction.setSnapEdge(this.snapToEdge(this.props.mapObj.snapping));
this.snapInteraction.setSnapVertex(this.snapToVertex(this.props.mapObj.snapping));
if (this.props.mapObj.snapping.active) {
this.refreshFeatureCache();
}
}
}
}, {
key: "render",
value: function render() {
var _this3 = this;
if (!this.state.drawing || !this.props.mapObj.snapping.enabled) {
return null;
}
var disabled = !this.state.havesnaplayers || this.props.mapObj.snapping.active === false;
var className = classNames({
"snapping-toolbar": true,
"snapping-toolbar-inactive": disabled
});
var snapEdge = this.snapToEdge(this.props.mapObj.snapping);
var snapVertex = this.snapToVertex(this.props.mapObj.snapping);
return /*#__PURE__*/ReactDOM.createPortal(/*#__PURE__*/React.createElement("div", {
className: className
}, this.state.reqId !== null ? /*#__PURE__*/React.createElement(Spinner, null) : /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("button", {
className: "button" + (snapVertex ? " pressed" : ""),
onClick: function onClick() {
return _this3.toggleSnap('vertex');
},
title: LocaleUtils.tr("snapping.vertex")
}, /*#__PURE__*/React.createElement(Icon, {
icon: "snap_vertex",
size: "large"
})), /*#__PURE__*/React.createElement("button", {
className: "button" + (snapEdge ? " pressed" : ""),
onClick: function onClick() {
return _this3.toggleSnap('edge');
},
title: LocaleUtils.tr("snapping.edge")
}, /*#__PURE__*/React.createElement(Icon, {
icon: "snap_edge",
size: "large"
}))), "\xA0", this.state.reqId ? LocaleUtils.tr("snapping.loading") : LocaleUtils.tr("snapping.snappingenabled")), this.context);
}
}]);
}(React.Component);
_defineProperty(SnappingSupport, "contextType", BottomToolPortalContext);
_defineProperty(SnappingSupport, "propTypes", {
layers: PropTypes.array,
map: PropTypes.object,
mapObj: PropTypes.object,
setSnappingConfig: PropTypes.func,
task: PropTypes.string,
theme: PropTypes.object
});
export default connect(function (state) {
return {
layers: state.layers.flat,
mapObj: state.map,
task: state.task.id,
theme: state.theme.current
};
}, {
setSnappingConfig: setSnappingConfig
})(SnappingSupport);