UNPKG

qwc2-lts

Version:
151 lines 7.86 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 _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 _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 2015 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 ol from 'openlayers'; import Proj4js from 'proj4'; import ConfigUtils from './ConfigUtils'; import LocaleUtils from './LocaleUtils'; var crsLabels = { "EPSG:4326": "WGS 84", "EPSG:3857": "WGS 84 / Pseudo Mercator" }; var CoordinatesUtils = { setCrsLabels: function setCrsLabels(labels) { Object.assign(crsLabels, labels); }, getAvailableCRS: function getAvailableCRS() { var crsList = {}; for (var a in Proj4js.defs) { if (Object.prototype.hasOwnProperty.call(Proj4js.defs, a)) { crsList[a] = { label: crsLabels[a] || a }; } } return crsList; }, getUnits: function getUnits(projection) { var proj = ol.proj.get(projection); return proj.getUnits() || 'degrees'; }, getPrecision: function getPrecision(projection) { var _precisions$projectio; var precisions = ConfigUtils.getConfigProp("projections").reduce(function (res, entry) { var _entry$precision; return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, entry.code, (_entry$precision = entry.precision) !== null && _entry$precision !== void 0 ? _entry$precision : 0)); }, {}); return (_precisions$projectio = precisions[projection]) !== null && _precisions$projectio !== void 0 ? _precisions$projectio : CoordinatesUtils.getUnits(projection) === 'degrees' ? 4 : 0; }, getAxisOrder: function getAxisOrder(projection) { var axis = ol.proj.get(projection).getAxisOrientation(); return axis || 'enu'; }, reproject: function reproject(point, source, dest) { if (source === dest) { return _toConsumableArray(point); } if (source && dest) { var transformed = null; try { transformed = ol.proj.transform(point, source, dest); } catch (e) { transformed = [0, 0]; } return transformed; } return null; }, /** * Reprojects a bounding box. * * @param bbox {array} [minx, miny, maxx, maxy] * @param source {string} SRS of the given bbox * @param dest {string} SRS of the returned bbox * * @return {array} [minx, miny, maxx, maxy] */ reprojectBbox: function reprojectBbox(bbox, source, dest) { if (source === dest) { return _toConsumableArray(bbox); } var sw = CoordinatesUtils.reproject([bbox[0], bbox[1]], source, dest); var ne = CoordinatesUtils.reproject([bbox[2], bbox[3]], source, dest); return [].concat(_toConsumableArray(sw), _toConsumableArray(ne)); }, calculateAzimuth: function calculateAzimuth(p1, p2, pj) { var p1proj = CoordinatesUtils.reproject(p1, pj, 'EPSG:4326'); var p2proj = CoordinatesUtils.reproject(p2, pj, 'EPSG:4326'); var lon1 = p1proj[0] * Math.PI / 180.0; var lat1 = p1proj[1] * Math.PI / 180.0; var lon2 = p2proj[0] * Math.PI / 180.0; var lat2 = p2proj[1] * Math.PI / 180.0; var dLon = lon2 - lon1; var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon); var azimuth = (Math.atan2(y, x) * 180.0 / Math.PI + 360) % 360; return azimuth; }, /** * Extend an extent given another one * * @param extent1 {array} [minx, miny, maxx, maxy] * @param extent2 {array} [minx, miny, maxx, maxy] * * @return {array} [minx, miny, maxx, maxy] */ extendExtent: function extendExtent(extent1, extent2) { return [Math.min(extent1[0], extent2[0]), Math.min(extent1[1], extent2[1]), Math.max(extent1[2], extent2[2]), Math.max(extent1[3], extent2[3])]; }, /** * Check extent validity * * @param extent {array} [minx, miny, maxx, maxy] * * @return {bool} */ isValidExtent: function isValidExtent(extent) { return extent.length === 4 && !(extent.indexOf(Infinity) !== -1 || extent.indexOf(-Infinity) !== -1 || extent.indexOf(NaN) !== -1 || extent[0] >= extent[2] || extent[1] >= extent[3]); }, fromOgcUrnCrs: function fromOgcUrnCrs(crsStr) { if (crsStr.endsWith(":CRS84")) { return "EPSG:4326"; } var parts = crsStr.split(":"); return "EPSG:" + parts.slice(-1); }, toOgcUrnCrs: function toOgcUrnCrs(crsStr) { var parts = crsStr.split(":"); return "urn:ogc:def:crs:" + parts[0] + "::" + parts[1]; }, getFormattedCoordinate: function getFormattedCoordinate(coo, srcCrs) { var dstCrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var decimals = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1; if (srcCrs && dstCrs && srcCrs !== dstCrs) { coo = CoordinatesUtils.reproject(coo, srcCrs, dstCrs); } if (decimals < 0) { decimals = CoordinatesUtils.getPrecision(dstCrs !== null && dstCrs !== void 0 ? dstCrs : srcCrs); } return coo.map(function (ord) { return LocaleUtils.toLocaleFixed(ord, decimals); }).join(", "); } }; export default CoordinatesUtils;