UNPKG

qwc2

Version:
330 lines (329 loc) 12.4 kB
/** * 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 ol from 'openlayers'; import ConfigUtils from './ConfigUtils'; import CoordinatesUtils from "./CoordinatesUtils"; import LocaleUtils from './LocaleUtils'; var MeasureUtils = { getFormattedBearingValue: function getFormattedBearingValue() { var azimuth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var format = ConfigUtils.getConfigProp("bearingFormat") || "bearing"; var precision = ConfigUtils.getConfigProp("bearingPrecision") || 0; var roundToPrecision = function roundToPrecision(val) { return Math.round((val + Number.EPSILON) * Math.pow(10, precision)) / Math.pow(10, precision); }; var normalizedAzimuth = (azimuth % 360 + 360) % 360; if (format === "bearing") { var bearing = ""; if (normalizedAzimuth >= 0 && normalizedAzimuth < 90) { bearing = "N ".concat(this.degToDms(normalizedAzimuth), " E"); } else if (normalizedAzimuth > 90 && normalizedAzimuth <= 180) { bearing = "S ".concat(this.degToDms(180 - normalizedAzimuth), " E"); } else if (normalizedAzimuth > 180 && normalizedAzimuth < 270) { bearing = "S ".concat(this.degToDms(normalizedAzimuth - 180), " W"); } else if (normalizedAzimuth >= 270 && normalizedAzimuth <= 360) { bearing = "N ".concat(this.degToDms(360 - normalizedAzimuth), " W"); } return bearing; } if (format === "azimuth") { return "".concat(roundToPrecision(normalizedAzimuth), "\xB0"); } if (format === "azimuth180") { if (normalizedAzimuth <= 180) { return "".concat(roundToPrecision(normalizedAzimuth), "\xB0"); } else { return "".concat(roundToPrecision(normalizedAzimuth - 360), "\xB0"); } } if (format === "bearingEW") { if (normalizedAzimuth < 180) { return "".concat(roundToPrecision(normalizedAzimuth), "\xB0E"); } else if (normalizedAzimuth > 180) { return "".concat(roundToPrecision(360 - normalizedAzimuth), "\xB0W"); } else { return "180°"; } } return "".concat(roundToPrecision(normalizedAzimuth), "\xB0"); }, formatDuration: function formatDuration(valueSeconds) { return new Date(valueSeconds * 1000).toISOString().slice(11, 19); }, formatMeasurement: function formatMeasurement(valueMetric, isArea) { var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'metric'; var decimals = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1; var withUnit = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true; if (decimals < 0) { decimals = ConfigUtils.getConfigProp("measurementPrecision", null, 2); } var result = ''; var unitlabel = unit; switch (unit) { case 'metric': if (isArea) { if (valueMetric > 1000000) { result = LocaleUtils.toLocaleFixed(valueMetric / 1000000, decimals); unitlabel = 'km²'; } else if (valueMetric > 10000) { result = LocaleUtils.toLocaleFixed(valueMetric / 10000, decimals); unitlabel = 'ha'; } else { result = LocaleUtils.toLocaleFixed(valueMetric, decimals); unitlabel = 'm²'; } } else { if (valueMetric > 1000) { result = LocaleUtils.toLocaleFixed(valueMetric / 1000, decimals); unitlabel = 'km'; } else { result = LocaleUtils.toLocaleFixed(valueMetric, decimals); unitlabel = 'm'; } } break; case 'imperial': if (isArea) { if (valueMetric > 2.58999 * 1000000) { result = LocaleUtils.toLocaleFixed(valueMetric * 0.000000386102159, decimals); unitlabel = 'mi²'; } else if (valueMetric > 4046.86) { result = LocaleUtils.toLocaleFixed(valueMetric * 0.0001, decimals); unitlabel = 'acre'; } else { result = LocaleUtils.toLocaleFixed(valueMetric * 10.7639, decimals); unitlabel = 'ft²'; } } else { if (valueMetric > 1609.34) { result = LocaleUtils.toLocaleFixed(valueMetric * 0.000621371, decimals); unitlabel = 'mi'; } else { result = LocaleUtils.toLocaleFixed(valueMetric * 3.28084, decimals); unitlabel = 'ft'; } } break; case 'm': result = LocaleUtils.toLocaleFixed(valueMetric, decimals); break; case 'ft': result = LocaleUtils.toLocaleFixed(valueMetric * 3.28084, decimals); break; case 'km': result = LocaleUtils.toLocaleFixed(valueMetric * 0.001, decimals); break; case 'mi': result = LocaleUtils.toLocaleFixed(valueMetric * 0.000621371, decimals); break; case 'sqm': result = LocaleUtils.toLocaleFixed(valueMetric, decimals); unitlabel = 'm²'; break; case 'sqft': result = LocaleUtils.toLocaleFixed(valueMetric * 10.7639, decimals); unitlabel = 'ft²'; break; case 'sqkm': result = LocaleUtils.toLocaleFixed(valueMetric * 0.000001, decimals); unitlabel = 'km²'; break; case 'sqmi': result = LocaleUtils.toLocaleFixed(valueMetric * 0.000000386102159, decimals); unitlabel = 'mi²'; break; case 'ha': result = LocaleUtils.toLocaleFixed(valueMetric * 0.0001, decimals); break; case 'acre': result = LocaleUtils.toLocaleFixed(valueMetric * 0.000247105381467, decimals); break; default: result = LocaleUtils.toLocaleFixed(valueMetric, decimals); break; } if (withUnit) { result += ' ' + unitlabel; } return result; }, getFormattedLength: function getFormattedLength(unit, length) { var decimals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2; var withUnit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; var result = ''; switch (unit) { case 'm': result = LocaleUtils.toLocaleFixed(length, decimals); break; case 'ft': result = LocaleUtils.toLocaleFixed(length * 3.28084, decimals); break; case 'km': result = LocaleUtils.toLocaleFixed(length * 0.001, decimals); break; case 'mi': result = LocaleUtils.toLocaleFixed(length * 0.000621371, decimals); break; default: result = LocaleUtils.toLocaleFixed(length, decimals); break; } if (withUnit) { result += ' ' + unit; } return result; }, convertLength: function convertLength(length, fromUnit, toUnit) { var lengthMeters = length; switch (fromUnit) { case 'm': lengthMeters = length; break; case 'ft': lengthMeters = length * 0.3048; break; case 'km': lengthMeters = length * 1000; break; case 'mi': lengthMeters = length * 1609.34; break; default: lengthMeters = length; break; } switch (toUnit) { case 'm': return lengthMeters; case 'ft': return lengthMeters * 3.28084; case 'km': return lengthMeters * 0.001; case 'mi': return lengthMeters * 0.000621371; default: return lengthMeters; } }, getFormattedArea: function getFormattedArea(unit, area) { var decimals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2; var withUnit = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; var result = ''; var unitlabel = unit; switch (unit) { case 'sqm': result = LocaleUtils.toLocaleFixed(area, decimals); unitlabel = 'm²'; break; case 'sqft': result = LocaleUtils.toLocaleFixed(area * 10.7639, decimals); unitlabel = 'ft²'; break; case 'sqkm': result = LocaleUtils.toLocaleFixed(area * 0.000001, decimals); unitlabel = 'km²'; break; case 'sqmi': result = LocaleUtils.toLocaleFixed(area * 0.000000386102159, decimals); unitlabel = 'mi²'; break; case 'ha': result = LocaleUtils.toLocaleFixed(area * 0.0001, decimals); break; case 'acre': result = LocaleUtils.toLocaleFixed(area * 0.000247105381467, decimals); break; default: result = LocaleUtils.toLocaleFixed(area, decimals); break; } if (withUnit) { result += ' ' + unitlabel; } return result; }, degToDms: function degToDms(deg) { // convert decimal deg to minutes and seconds var d = Math.floor(deg); var minfloat = (deg - d) * 60; var m = Math.floor(minfloat); var secfloat = (minfloat - m) * 60; var s = Math.floor(secfloat); return "" + d + "° " + m + "' " + s + "'' "; }, updateFeatureMeasurements: function updateFeatureMeasurements(feature, geomType, featureCrs, settings) { var _settings$displayCrs; var geodesic = ConfigUtils.getConfigProp("geodesicMeasurements"); var measurements = { lenUnit: settings.lenUnit, areaUnit: settings.areaUnit }; var displayCrs = (_settings$displayCrs = settings.displayCrs) !== null && _settings$displayCrs !== void 0 ? _settings$displayCrs : featureCrs; feature.set('label', ''); feature.set('segment_labels', undefined); var geom = feature.getGeometry(); if (geomType === 'Point') { feature.set('label', CoordinatesUtils.getFormattedCoordinate(geom.getCoordinates(), featureCrs, displayCrs)); } else if (geomType === 'LineString') { var lengths = MeasureUtils.computeSegmentLengths(geom.getCoordinates(), featureCrs, geodesic); measurements.segment_lengths = lengths; measurements.length = lengths.reduce(function (sum, len) { return sum + len; }, 0); feature.set('segment_labels', lengths.map(function (length) { return MeasureUtils.formatMeasurement(length, false, settings.lenUnit); })); } else if (["Ellipse", "Polygon", "Square", "Box"].includes(geomType)) { var area = MeasureUtils.computeArea(geom, featureCrs, geodesic); measurements.area = area; feature.set('label', MeasureUtils.formatMeasurement(area, true, settings.areaUnit)); } else if (geomType === 'Circle') { var radius = geom.getRadius(); measurements.radius = radius; feature.set('label', "r = " + MeasureUtils.formatMeasurement(radius, false, settings.lenUnit)); } else if (geomType === 'Bearing') { var coo = geom.getCoordinates(); measurements.bearing = CoordinatesUtils.calculateAzimuth(coo[0], coo[1], featureCrs); feature.set('label', MeasureUtils.getFormattedBearingValue(measurements.bearing)); } feature.set('measurements', measurements); }, computeSegmentLengths: function computeSegmentLengths(coordinates, featureCrs, geodesic) { var lengths = []; var units = CoordinatesUtils.getUnits(featureCrs); if (geodesic || units === 'degrees') { var wgsCoo = coordinates.map(function (coo) { return CoordinatesUtils.reproject(coo, featureCrs, "EPSG:4326"); }); for (var i = 0; i < wgsCoo.length - 1; ++i) { lengths.push(ol.sphere.getDistance(wgsCoo[i], wgsCoo[i + 1])); } } else { var conv = units === 'feet' ? 0.3048 : 1; for (var _i = 0; _i < coordinates.length - 1; ++_i) { var dx = coordinates[_i + 1][0] - coordinates[_i][0]; var dy = coordinates[_i + 1][1] - coordinates[_i][1]; lengths.push(Math.sqrt(dx * dx + dy * dy) * conv); } } return lengths; }, computeArea: function computeArea(geometry, featureCrs, geodesic) { var units = CoordinatesUtils.getUnits(featureCrs); if (geodesic || units === 'degrees') { return ol.sphere.getArea(geometry, { projection: featureCrs }); } else { var conv = units === 'feet' ? 0.3048 : 1; return geometry.getArea() * conv * conv; } } }; export default MeasureUtils;