UNPKG

qwc2

Version:
242 lines (241 loc) 12.3 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 commonEsriWktLookup = { "EPSG:4326": 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]', "EPSG:3857": 'PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]' }; 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; }, getProjectionConfig: function getProjectionConfig(projection) { var _config$projection; var config = ConfigUtils.getConfigProp("projections").reduce(function (res, entry) { var _entry$format, _entry$addDirection, _entry$swapLonLat; res[entry.code] = { format: (_entry$format = entry.format) !== null && _entry$format !== void 0 ? _entry$format : 'decimal', addDirection: (_entry$addDirection = entry.addDirection) !== null && _entry$addDirection !== void 0 ? _entry$addDirection : 'none', swapLonLat: (_entry$swapLonLat = entry.swapLonLat) !== null && _entry$swapLonLat !== void 0 ? _entry$swapLonLat : false }; return res; }, {}); return (_config$projection = config[projection]) !== null && _config$projection !== void 0 ? _config$projection : { format: 'decimal', addDirection: 'none', swapLonLat: false }; }, 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]; }, getEsriWktFromCrs: function getEsriWktFromCrs(crsStr) { var epsgCode = crsStr.startsWith("EPSG:") ? crsStr : CoordinatesUtils.fromOgcUrnCrs(crsStr); var projections = ConfigUtils.getConfigProp("projections") || []; var configProjection = projections.find(function (proj) { return proj.code === epsgCode; }); if (configProjection && configProjection.esriWkt) { return configProjection.esriWkt; } if (commonEsriWktLookup[epsgCode]) { return commonEsriWktLookup[epsgCode]; } /* eslint-disable-next-line */ console.warn("No ESRI WKT definition found for ".concat(epsgCode, ". Shapefile export may not include projection information. Consider adding an 'esriWkt' property to the projection in config.json.")); return null; }, getFormattedCoordinate: function getFormattedCoordinate(coo, srcCrs) { var _options$decimals; var dstCrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var units = CoordinatesUtils.getUnits(dstCrs !== null && dstCrs !== void 0 ? dstCrs : srcCrs); var decimals = (_options$decimals = options.decimals) !== null && _options$decimals !== void 0 ? _options$decimals : CoordinatesUtils.getPrecision(dstCrs !== null && dstCrs !== void 0 ? dstCrs : srcCrs); var _ref = function () { var config = CoordinatesUtils.getProjectionConfig(dstCrs !== null && dstCrs !== void 0 ? dstCrs : srcCrs); return { format: units !== 'degrees' ? 'decimal' : config.format, addDirection: config.addDirection, swapLonLat: config.swapLonLat }; }(), format = _ref.format, addDirection = _ref.addDirection, swapLonLat = _ref.swapLonLat; if (srcCrs && dstCrs && srcCrs !== dstCrs) { coo = CoordinatesUtils.reproject(coo, srcCrs, dstCrs); } if (swapLonLat) { coo = [coo[1], coo[0]]; } var toDMS = function toDMS(coord) { var deg = Math.floor(Math.abs(coord)); var minFull = (Math.abs(coord) - deg) * 60; var min = Math.floor(minFull); var sec = ((minFull - min) * 60).toFixed(decimals); return "".concat(deg, "\xB0 ").concat(min, "' ").concat(sec, "\""); }; var toDM = function toDM(coord) { var deg = Math.floor(Math.abs(coord)); var min = ((Math.abs(coord) - deg) * 60).toFixed(decimals); return "".concat(deg, "\xB0 ").concat(min, "'"); }; var formatCoordinate = function formatCoordinate(value, isLat) { var direction = ''; if (addDirection !== 'none') { if (isLat) { direction = value >= 0 ? 'N' : 'S'; } else { direction = value >= 0 ? 'E' : 'W'; } } var formatted; switch (format) { case 'dms': formatted = toDMS(value, decimals); break; case 'dm': formatted = toDM(value, decimals); break; default: formatted = LocaleUtils.toLocaleFixed(Math.abs(value), decimals); } if (addDirection === 'prefix') { return "".concat(direction).concat(formatted); } else if (addDirection === 'suffix') { return "".concat(formatted).concat(direction); } return formatted; }; return coo.map(function (coord, idx) { return formatCoordinate(coord, swapLonLat ? idx === 0 : idx === 1); }).join(", "); } }; export default CoordinatesUtils;