qwc2
Version:
QGIS Web Client
223 lines (218 loc) • 13.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 _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 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 2016 GeoSolutions Sas
* Copyright 2016-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 { changeMeasurementState } from '../../actions/measurement';
import FeatureStyles from '../../utils/FeatureStyles';
import MeasureUtils from '../../utils/MeasureUtils';
/**
* Measurement support for the map component.
*/
var MeasurementSupport = /*#__PURE__*/function (_React$Component) {
function MeasurementSupport(props) {
var _this;
_classCallCheck(this, MeasurementSupport);
_this = _callSuper(this, MeasurementSupport, [props]);
_defineProperty(_this, "addDrawInteraction", function (newProps) {
_this.reset();
// Create a layer to draw on
_this.measureLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
zIndex: 1000000
});
_this.props.map.addLayer(_this.measureLayer);
var geometryType = newProps.measurement.geomType;
if (geometryType === 'Bearing') {
geometryType = 'LineString';
}
// create an interaction to draw with
_this.drawInteraction = new ol.interaction.Draw({
stopClick: true,
source: _this.measureLayer.getSource(),
condition: function condition(event) {
return event.originalEvent.buttons === 1;
},
type: geometryType,
style: function style() {
return _this.modifyInteraction ? [] : FeatureStyles.sketchInteraction();
}
});
_this.drawInteraction.on('drawstart', function (ev) {
_this.leaveTemporaryPickMode();
_this.measureLayer.getSource().clear();
_this.sketchFeature = ev.feature;
_this.sketchFeature.setStyle(_this.featureStyleFunction);
_this.sketchFeature.on('change', function (evt) {
return _this.updateMeasurementResults(evt.target);
});
});
_this.drawInteraction.on('drawend', function () {
_this.updateMeasurementResults(_this.sketchFeature, false);
_this.enterTemporaryPickMode();
});
_this.props.map.addInteraction(_this.drawInteraction);
});
_defineProperty(_this, "reset", function () {
if (_this.drawInteraction !== null) {
_this.props.map.removeInteraction(_this.drawInteraction);
_this.drawInteraction = null;
_this.leaveTemporaryPickMode();
_this.props.map.removeLayer(_this.measureLayer);
_this.measureLayer = null;
_this.sketchFeature = null;
}
});
_defineProperty(_this, "enterTemporaryPickMode", function () {
_this.modifyInteraction = new ol.interaction.Modify({
features: new ol.Collection([_this.sketchFeature]),
condition: function condition(event) {
return event.originalEvent.buttons === 1;
},
insertVertexCondition: function insertVertexCondition() {
return _this.props.measurement.geomType === 'Bearing' ? false : true;
},
deleteCondition: function deleteCondition(event) {
return ol.events.condition.shiftKeyOnly(event) && ol.events.condition.singleClick(event);
},
style: function style(feature) {
// Hack to get cursor position over geometry...
if (_this.props.measurement.pickPositionCallback) {
clearTimeout(_this.pickPositionCallbackTimeout);
_this.props.measurement.pickPositionCallback(feature.getGeometry().getCoordinates());
}
return FeatureStyles.sketchInteraction();
}
});
_this.props.map.on('pointermove', _this.clearPickPosition);
_this.modifyInteraction.on('modifyend', function () {
_this.updateMeasurementResults(_this.sketchFeature, false);
});
_this.props.map.addInteraction(_this.modifyInteraction);
});
_defineProperty(_this, "leaveTemporaryPickMode", function () {
if (_this.modifyInteraction) {
_this.props.map.un('pointermove', _this.clearPickPosition);
_this.props.map.removeInteraction(_this.modifyInteraction);
_this.modifyInteraction = null;
}
});
_defineProperty(_this, "clearPickPosition", function () {
if (_this.props.measurement.pickPositionCallback) {
clearTimeout(_this.pickPositionCallbackTimeout);
// Works because style function clears timeout if marker is rendered, i.e. if mouse is over measure geometry
_this.pickPositionCallbackTimeout = setTimeout(function () {
if (_this.props.measurement.pickPositionCallback) {
_this.props.measurement.pickPositionCallback(null);
}
}, 50);
}
});
_defineProperty(_this, "updateMeasurementResults", function (feature) {
var drawing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var geomType = _this.props.measurement.geomType;
var settings = {
lenUnit: _this.props.measurement.lenUnit,
areaUnit: _this.props.measurement.areaUnit,
displayCrs: _this.props.displayCrs
};
MeasureUtils.updateFeatureMeasurements(feature, geomType, _this.props.projection, settings);
// Only one segment for bearing measurement
if (geomType === 'Bearing' && feature.getGeometry().getCoordinates().length > 2) {
_this.drawInteraction.finishDrawing();
}
_this.measureLayer.getSource().changed();
_this.props.changeMeasurementState(_objectSpread({
geomType: _this.props.measurement.geomType,
drawing: drawing,
coordinates: feature.getGeometry().getCoordinates()
}, structuredClone(feature.get('measurements'))));
});
_defineProperty(_this, "featureStyleFunction", function (feature) {
var geometryFunction = function geometryFunction(f) {
if (f.getGeometry().getType() === "Point") {
return new ol.geom.MultiPoint([f.getGeometry().getCoordinates()]);
} else if (f.getGeometry().getType() === "LineString") {
return new ol.geom.MultiPoint(f.getGeometry().getCoordinates());
}
return new ol.geom.MultiPoint(f.getGeometry().getCoordinates()[0]);
};
var opts = {};
if (_this.props.measurement.geomType === 'LineString') {
opts.headmarker = _this.props.measurement.lineHeadMarker;
opts.tailmarker = _this.props.measurement.lineTailMarker;
} else if (_this.props.measurement.geomType === 'Bearing') {
opts.headmarker = _this.props.measurement.bearingHeadMarker;
opts.tailmarker = _this.props.measurement.bearingTailMarker;
}
return [].concat(_toConsumableArray(FeatureStyles.measureInteraction(feature, opts)), [FeatureStyles.measureInteractionVertex({
geometryFunction: geometryFunction
})]);
});
_this.pickPositionCallbackTimeout = null;
_this.measureLayer = null;
return _this;
}
_inherits(MeasurementSupport, _React$Component);
return _createClass(MeasurementSupport, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.props.measurement.geomType && this.props.measurement.geomType !== prevProps.measurement.geomType) {
this.addDrawInteraction(this.props);
} else if (!this.props.measurement.geomType) {
this.reset();
} else if (this.sketchFeature && (this.props.measurement.lenUnit !== prevProps.measurement.lenUnit || this.props.measurement.areaUnit !== prevProps.measurement.areaUnit || this.props.displayCrs !== prevProps.displayCrs)) {
this.updateMeasurementResults(this.sketchFeature, this.props.measurement.drawing);
}
}
}, {
key: "render",
value: function render() {
return null;
}
}]);
}(React.Component);
_defineProperty(MeasurementSupport, "propTypes", {
changeMeasurementState: PropTypes.func,
displayCrs: PropTypes.string,
map: PropTypes.object,
measurement: PropTypes.object,
projection: PropTypes.string
});
export default connect(function (state) {
return {
displayCrs: state.map.displayCrs,
measurement: state.measurement
};
}, {
changeMeasurementState: changeMeasurementState
})(MeasurementSupport);