UNPKG

qwc2

Version:
188 lines (182 loc) 11 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 _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 ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer'; import VectorSource from '@giro3d/giro3d/sources/VectorSource'; import ol from 'openlayers'; import PropTypes from 'prop-types'; import { Group } from 'three'; import { GLTFLoader } from 'three/addons/loaders/GLTFLoader'; import { CSS2DObject } from 'three/addons/renderers/CSS2DRenderer'; import pinModel from '../../resources/pin.glb'; import CoordinatesUtils from '../../utils/CoordinatesUtils'; import FeatureStyles from '../../utils/FeatureStyles'; import { collectSearchProviders } from '../../utils/SearchProviders'; import VectorLayerUtils from '../../utils/VectorLayerUtils'; import SearchWidget from '../widgets/SearchWidget'; var SearchField3D = /*#__PURE__*/function (_React$Component) { function SearchField3D() { var _this; _classCallCheck(this, SearchField3D); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, SearchField3D, [].concat(args)); _defineProperty(_this, "state", { searchProviders: {} }); _defineProperty(_this, "searchResultSelected", function (result) { var _result$crs, _result$feature, _result$crs3; var sceneContext = _this.props.sceneContext; sceneContext.removeLayer("__searchHighlight"); sceneContext.removeSceneObject("__searchMarker"); if (!result) { return; } var mapCrs = sceneContext.mapCrs; var scenePos = CoordinatesUtils.reproject([result.x, result.y], (_result$crs = result.crs) !== null && _result$crs !== void 0 ? _result$crs : mapCrs, mapCrs); // Add highlight geometry if (result.feature && ((_result$feature = result.feature) === null || _result$feature === void 0 || (_result$feature = _result$feature.geometry) === null || _result$feature === void 0 ? void 0 : _result$feature.type) !== "Point") { var _result$crs2; var format = new ol.format.GeoJSON(); var olFeatures = format.readFeatures(result.feature, { dataProjection: (_result$crs2 = result.crs) !== null && _result$crs2 !== void 0 ? _result$crs2 : mapCrs, featureProjection: mapCrs }); var highlightLayer = new ColorLayer({ source: new VectorSource({ data: olFeatures, format: format, style: function style(feat) { return FeatureStyles["default"](feat, {}); } }) }); sceneContext.addLayer("__searchHighlight", highlightLayer); } // Zoom to bounds var bounds = result.feature ? VectorLayerUtils.computeFeatureBBox(result.feature) : CoordinatesUtils.reprojectBbox(result.bbox, (_result$crs3 = result.crs) !== null && _result$crs3 !== void 0 ? _result$crs3 : mapCrs, mapCrs); // Adjust bounds so that we do not zoom further than 1:searchMinScaleDenom var bbWidth = bounds[2] - bounds[0]; var bbHeight = bounds[3] - bounds[1]; var sceneRect = _this.props.sceneContext.scene.viewport.getBoundingClientRect(); // Compute maximum allowed dimensions at the given scale var px2m = 0.0254 / 96; var minWidth = sceneRect.width * px2m * _this.props.searchOptions.minScaleDenom; var minHeight = sceneRect.height * px2m * _this.props.searchOptions.minScaleDenom; var scaleFactor = Math.max(bbWidth / minWidth, bbHeight / minHeight); if (scaleFactor < 1) { var bbCenterX = 0.5 * (bounds[0] + bounds[2]); var bbCenterY = 0.5 * (bounds[1] + bounds[3]); bounds = [bbCenterX - minWidth / 2, bbCenterY - minHeight / 2, bbCenterX + minWidth / 2, bbCenterY + minHeight / 2]; } sceneContext.setViewToExtent(bounds, 0); // Add pin and label at result position above terrain sceneContext.getTerrainHeightFromDTM(scenePos).then(function (terrainHeight) { var loader = new GLTFLoader(); loader.load(pinModel, function (gltf) { var _result$label; var searchMarker = new Group(); // Add pin var pin = gltf.scene; pin.position.x = scenePos[0]; pin.position.y = scenePos[1]; pin.position.z = terrainHeight; pin.rotation.x = Math.PI / 2; pin.updateMatrixWorld(); searchMarker.add(pin); // Add label var labelEl = document.createElement("span"); labelEl.innerText = (_result$label = result.label) !== null && _result$label !== void 0 ? _result$label : result.text; labelEl.className = "map3d-search-label"; var label = new CSS2DObject(labelEl); label.position.set(scenePos[0], scenePos[1], terrainHeight + 2); label.updateMatrixWorld(); searchMarker.add(label); sceneContext.addSceneObject("__searchMarker", searchMarker); // Scale search marker with distance var scaleSearchMarker = function scaleSearchMarker() { var distance = sceneContext.scene.view.camera.position.distanceTo(pin.position) / 30; var scale = Math.max(20, distance); label.position.z = terrainHeight + 2 * scale; label.updateMatrixWorld(); pin.scale.set(scale, scale, scale); pin.updateMatrixWorld(); }; sceneContext.scene.view.controls.addEventListener('change', scaleSearchMarker); searchMarker.addEventListener('removed', function () { var _labelEl$parentNode, _labelEl$parentNode$r; sceneContext.scene.view.controls.removeEventListener('change', scaleSearchMarker); // The label DOM element is not removed when the searchMarker group is removed from the instance (_labelEl$parentNode = labelEl.parentNode) === null || _labelEl$parentNode === void 0 || (_labelEl$parentNode$r = _labelEl$parentNode.removeChild) === null || _labelEl$parentNode$r === void 0 || _labelEl$parentNode$r.call(_labelEl$parentNode, labelEl); }); }); }); }); return _this; } _inherits(SearchField3D, _React$Component); return _createClass(SearchField3D, [{ key: "componentDidMount", value: function componentDidMount() { this.componentDidUpdate({}); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this$props$sceneCont, _prevProps$sceneConte; if (((_this$props$sceneCont = this.props.sceneContext) === null || _this$props$sceneCont === void 0 ? void 0 : _this$props$sceneCont.colorLayers) !== ((_prevProps$sceneConte = prevProps.sceneContext) === null || _prevProps$sceneConte === void 0 ? void 0 : _prevProps$sceneConte.colorLayers)) { var layers = Object.values(this.props.sceneContext.colorLayers); this.setState({ searchProviders: collectSearchProviders(this.props.theme, layers) }); } } }, { key: "render", value: function render() { return /*#__PURE__*/React.createElement(SearchWidget, { queryGeometries: true, resultSelected: this.searchResultSelected, searchParams: { mapcrs: this.props.sceneContext.mapCrs, displaycrs: this.props.sceneContext.mapCrs }, searchProviders: Object.values(this.state.searchProviders), value: "" }); } }]); }(React.Component); _defineProperty(SearchField3D, "propTypes", { sceneContext: PropTypes.object, searchOptions: PropTypes.shape({ minScaleDenom: PropTypes.number }), theme: PropTypes.object }); export default connect(function (state) { return { theme: state.theme.current }; })(SearchField3D);