qwc2
Version:
QGIS Web Client
292 lines (289 loc) • 14.2 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 _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 ol from 'openlayers';
import PropTypes from 'prop-types';
import FeatureStyles from '../utils/FeatureStyles';
import MapUtils from '../utils/MapUtils';
import MeasureUtils from '../utils/MeasureUtils';
var MapSelection = /*#__PURE__*/function (_React$Component) {
function MapSelection(props) {
var _this;
_classCallCheck(this, MapSelection);
_this = _callSuper(this, MapSelection, [props]);
_defineProperty(_this, "state", {
geometry: null,
modifiers: {
alt: false,
ctrl: false,
shift: false
}
});
_defineProperty(_this, "addDrawInteraction", function () {
// cleanup old interaction
if (_this.drawInteraction) {
_this.removeDrawInteraction();
}
if (_this.props.geomType === "DragBox") {
_this.drawInteraction = new ol.interaction.DragBox({
className: 'selection-drag-box',
condition: _this.props.drawCondition
});
_this.drawInteraction.on('boxend', function () {
_this.updateSelectionState(_this.drawInteraction.getGeometry());
});
} else {
var typeMap = {
Point: "Point",
LineString: "LineString",
Polygon: "Polygon",
Circle: "Circle",
Box: "Circle"
};
if (!typeMap[_this.props.geomType]) {
// Unknown geom type
return;
}
// create an interaction to draw with
_this.drawInteraction = new ol.interaction.Draw({
stopClick: true,
source: _this.selectionLayer.getSource(),
condition: _this.props.drawCondition,
type: typeMap[_this.props.geomType],
style: function style(feature) {
return FeatureStyles[_this.props.styleName](feature, _objectSpread(_objectSpread({}, _this.props.styleOptions), {}, {
circleRadius: 0
}));
},
geometryFunction: _this.props.geomType === "Box" ? ol.interaction.createBox() : undefined
});
_this.drawInteraction.on('drawstart', function (evt) {
// clear previous sketches
_this.selectionLayer.getSource().clear();
evt.feature.on('change', function () {
return _this.updateMeasurements(evt.feature);
});
}, _this);
_this.drawInteraction.on('drawend', function (evt) {
_this.updateSelectionState(evt.feature.getGeometry());
}, _this);
}
_this.map.addInteraction(_this.drawInteraction);
window.addEventListener('keydown', _this.checkModifier);
window.addEventListener('keyup', _this.checkModifier);
if (_this.props.cursor) {
_this.map.getViewport().style.cursor = _this.props.cursor;
}
});
_defineProperty(_this, "removeDrawInteraction", function () {
if (_this.drawInteraction !== null) {
_this.map.removeInteraction(_this.drawInteraction);
_this.drawInteraction = null;
}
window.removeEventListener('keydown', _this.checkModifier);
window.removeEventListener('keyup', _this.checkModifier);
_this.map.getViewport().style.cursor = '';
});
_defineProperty(_this, "checkModifier", function (ev) {
var down = ev.type === "keydown";
_this.setState(function (state) {
return {
modifiers: {
alt: ev.key === 'Alt' ? down : state.modifiers.alt,
ctrl: ev.key === 'Control' ? down : state.modifiers.ctrl,
shift: ev.key === 'Shift' ? down : state.modifiers.shift
}
};
});
});
_defineProperty(_this, "updateSelectionState", function (geometry) {
if (!geometry) {
return;
}
var coords = _this.props.geomType === 'Circle' ? null : geometry.getCoordinates();
if (_this.props.geomType === "Circle") {
// Also store poligonized circle
var center = geometry.getCenter();
var radius = geometry.getRadius();
var deg2rad = Math.PI / 180;
var polycords = [Array.apply(null, Array(91)).map(function (item, index) {
return [center[0] + radius * Math.cos(4 * index * deg2rad), center[1] + radius * Math.sin(4 * index * deg2rad)];
})];
_this.setState({
geometry: {
type: "Polygon",
coordinates: polycords,
center: center,
radius: radius
}
});
} else if (_this.props.geomType === "DragBox" || _this.props.geomType === "Box") {
var boxcoords = [[Math.min(coords[0][0][0], coords[0][2][0]), Math.min(coords[0][0][1], coords[0][2][1]), Math.max(coords[0][0][0], coords[0][2][0]), Math.max(coords[0][0][1], coords[0][2][1])]];
_this.setState({
geometry: {
type: "Polygon",
coordinates: boxcoords
}
});
} else {
_this.setState({
geometry: {
type: _this.props.geomType,
coordinates: coords
}
});
}
});
_defineProperty(_this, "updateMeasurements", function (feature) {
if (!_this.props.measure) {
return;
}
var settings = {
lenUnit: 'metric',
areaUnit: 'metric'
};
MeasureUtils.updateFeatureMeasurements(feature, _this.props.geomType, _this.props.projection, settings);
});
_this.drawInteraction = null;
// create a layer to draw on
_this.selectionLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
zIndex: 1000000,
style: function style(feature) {
return FeatureStyles[_this.props.styleName](feature, _this.props.styleOptions);
}
});
_this.selectionLayer.setVisible(!_this.props.hideGeometry);
return _this;
}
_inherits(MapSelection, _React$Component);
return _createClass(MapSelection, [{
key: "componentDidMount",
value: function componentDidMount() {
this.map = MapUtils.getHook(MapUtils.GET_MAP);
this.map.addLayer(this.selectionLayer);
if (this.props.active) {
this.addDrawInteraction();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (this.props.hideGeometry !== prevProps.hideGeometry) {
this.selectionLayer.setVisible(!this.props.hideGeometry);
}
if (this.props.geomType !== prevProps.geomType) {
this.selectionLayer.getSource().clear();
}
if (this.props.active && !prevProps.active || this.props.active && this.props.geomType !== prevProps.geomType) {
this.addDrawInteraction();
} else if (!this.props.active && prevProps.active) {
this.removeDrawInteraction();
}
if (this.state.geometry !== prevState.geometry) {
if (this.state.geometry !== this.props.geometry) {
this.props.geometryChanged(this.state.geometry, _objectSpread({}, this.state.modifiers));
}
}
if (this.props.geometry !== prevProps.geometry && this.props.geometry !== this.state.geometry) {
if (!this.props.geometry) {
this.selectionLayer.getSource().clear();
} else {
var feature = this.selectionLayer.getSource().getFeatures()[0];
if (!feature) {
feature = new ol.Feature();
this.selectionLayer.getSource().addFeature(feature);
}
if (this.props.geometry.type === "Point") {
feature.setGeometry(new ol.geom.Point(this.props.geometry.coordinates));
} else if (this.props.geometry.type === "MultiPoint") {
feature.setGeometry(new ol.geom.Multi(this.props.geometry.coordinates));
} else if (this.props.geometry.type === "LineString") {
feature.setGeometry(new ol.geom.LineString(this.props.geometry.coordinates));
} else if (this.props.geometry.type === "MultiLineString") {
feature.setGeometry(new ol.geom.MultiLineString(this.props.geometry.coordinates));
} else if (this.props.geometry.type === "Polygon") {
if (this.props.geometry.center && this.props.geometry.radius) {
feature.setGeometry(new ol.geom.Circle(this.props.geometry.center, this.props.geometry.radius));
} else {
feature.setGeometry(new ol.geom.Polygon(this.props.geometry.coordinates));
}
} else if (this.props.geometry.type === "MultiPolygon") {
feature.setGeometry(new ol.geom.MultiPolygon(this.props.geometry.coordinates));
}
this.setState({
geometry: this.props.geometry
});
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.map.removeLayer(this.selectionLayer);
this.removeDrawInteraction();
}
}, {
key: "render",
value: function render() {
return null;
}
}]);
}(React.Component);
_defineProperty(MapSelection, "propTypes", {
/** Whether the selection tool is active */
active: PropTypes.bool,
/** Optional, a css-cursor to use when drawing */
cursor: PropTypes.string,
/** The draw interaction condition. */
drawCondition: PropTypes.func,
/** The selection geometry type (Point, LineString, Polygon, Circle, DragBox, Box) */
geomType: PropTypes.string,
/** Initial geometry or geometry to update. */
geometry: PropTypes.object,
/** The callback which is invoked with a drawn geometry. */
geometryChanged: PropTypes.func,
/** Whether to hide the current selection (except while drawing). */
hideGeometry: PropTypes.bool,
/** Whether to show measurements while drawing. */
measure: PropTypes.bool,
projection: PropTypes.string,
/** Optional: the selection feature style name. */
styleName: PropTypes.string,
/** Optional: the selection feature style options. */
styleOptions: PropTypes.object
});
_defineProperty(MapSelection, "defaultProps", {
drawCondition: function drawCondition(event) {
return event.originalEvent.buttons === 1;
},
styleName: 'default',
styleOptions: {}
});
export default connect(function (state) {
return {
projection: state.map.projection
};
}, {})(MapSelection);