Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

move things to util

Jason Kent90b5abd0a79a
diff --git a/web/js/map/measure/ui.js b/web/js/map/measure/ui.js
index 6493efa03..9fab7a9c5 100644
--- a/web/js/map/measure/ui.js
+++ b/web/js/map/measure/ui.js
@@ -1,20 +1,15 @@
import { unByKey } from 'ol/Observable';
import Overlay from 'ol/Overlay';
-import { getArea, getLength } from 'ol/sphere';
import { LineString, Polygon } from 'ol/geom';
import { Draw } from 'ol/interaction/';
import { Vector as VectorLayer } from 'ol/layer';
import { Vector as VectorSource } from 'ol/source';
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
import {
- ftPerMile,
- metersPerkilometer,
- metersToFeet,
- squareMetersToFeet,
- roundAndLocale,
transformLineStringArc,
transformPolygonArc,
- getRhumbLineDistance
+ getFormattedLength,
+ getFormattedArea
} from './util.js';
export function measure (map, mapUiEvents) {
@@ -43,7 +38,6 @@ export function measure (map, mapUiEvents) {
lineJoin: 'round',
width: 5
});
- // const solidOrange = 'rgba(255, 100, 6, 1)';
const vectorStyles = [
new Style({
fill: areaBgFill,
@@ -152,15 +146,13 @@ export function measure (map, mapUiEvents) {
} else if (geom instanceof LineString) {
tooltipCoord = geom.getLastCoordinate();
}
- setMeasurement(geom, measureTooltipElement);
+ setMeasurementTooltip(geom, measureTooltipElement);
measureTooltip.setPosition(tooltipCoord);
});
};
function drawEndCallback (evt) {
const featureGeom = evt.feature.getGeometry();
- // const metricLength = getLength(featureGeom, { projection });
- // const geomLength = featureGeom.getLength();
allGeometries[featureGeom.ol_uid] = featureGeom;
measureTooltipElement.className = 'tooltip-measure tooltip-static';
measureTooltip.setOffset([0, -7]);
@@ -193,59 +185,17 @@ export function measure (map, mapUiEvents) {
* @param {*} geometry
* @param {*} element
*/
- function setMeasurement(geometry, element) {
+ function setMeasurementTooltip(geometry, element) {
let measurement;
if (geometry instanceof Polygon) {
- measurement = getFormattedArea(geometry);
+ measurement = getFormattedArea(geometry, projection, unitOfMeasure, useGreatCircle);
}
if (geometry instanceof LineString) {
- measurement = getFormattedLength(geometry);
+ measurement = getFormattedLength(geometry, projection, unitOfMeasure, useGreatCircle);
}
element.innerHTML = measurement;
}
- /**
- *
- * @param {*} line
- * @return {String} - The formatted distance measurement
- */
- const getFormattedLength = (line) => {
- const metricLength = useGreatCircle ? getLength(line, { projection }) : getRhumbLineDistance(line);
- if (unitOfMeasure === 'km') {
- return metricLength > 100
- ? `${roundAndLocale(metricLength, metersPerkilometer)} km`
- : `${roundAndLocale(metricLength)} m`;
- }
- if (unitOfMeasure === 'mi') {
- const imperialLength = metersToFeet(metricLength);
- return imperialLength > (ftPerMile / 4)
- ? `${roundAndLocale(imperialLength, ftPerMile)} mi`
- : `${roundAndLocale(imperialLength)} ft`;
- }
- };
-
- /**
- *
- * @param {*} polygon
- * @return {String} - The formatted area measurement
- */
- const getFormattedArea = (polygon) => {
- const sqFtPerSqMile = 27878400;
- const sqMeterPerKilometer = 1000000;
- const metricArea = getArea(polygon, { projection });
- if (unitOfMeasure === 'km') {
- return metricArea > 10000
- ? `${roundAndLocale(metricArea, sqMeterPerKilometer)} km<sup>2</sup>`
- : `${roundAndLocale(metricArea)} m<sup>2</sup>`;
- }
- if (unitOfMeasure === 'mi') {
- const imperialArea = squareMetersToFeet(metricArea);
- return imperialArea > (sqFtPerSqMile / 8)
- ? `${roundAndLocale(imperialArea, sqFtPerSqMile)} mi<sup>2</sup>`
- : `${roundAndLocale(imperialArea)} ft<sup>2</sup>`;
- }
- };
-
/**
* Go through every tooltip and recalculate the measurement based on
* current settings of unit of measurement and great circle
@@ -254,7 +204,7 @@ export function measure (map, mapUiEvents) {
for (const id in allMeasureTooltips) {
const geomForTooltip = allGeometries[id];
const tooltipElement = allMeasureTooltips[id].element.children[0];
- tooltipElement.innerHtml = setMeasurement(geomForTooltip, tooltipElement);
+ tooltipElement.innerHtml = setMeasurementTooltip(geomForTooltip, tooltipElement);
geomForTooltip.changed();
allMeasureTooltips[id].setOffset([0, -7]);
}
@@ -296,6 +246,9 @@ export function measure (map, mapUiEvents) {
recalculateAllMeasurements();
};
+ /**
+ *
+ */
self.useGreatCircleMeasurements = (value) => {
useGreatCircle = value;
recalculateAllMeasurements();
diff --git a/web/js/map/measure/util.js b/web/js/map/measure/util.js
index b004b8ad9..a8482fdab 100644
--- a/web/js/map/measure/util.js
+++ b/web/js/map/measure/util.js
@@ -1,35 +1,38 @@
import { MultiLineString, Polygon } from 'ol/geom';
+import { getArea, getLength } from 'ol/sphere';
import arc from 'arc';
const equatorialRadiusMeters = 6378137;
const referenceProjection = 'EPSG:4326';
+const metersPerkilometer = 1000;
+const ftPerMile = 5280;
+const sqFtPerSqMile = 27878400;
+const sqMeterPerKilometer = 1000000;
+const metersToFeet = (meters) => meters * 3.28084;
+const squareMetersToFeet = (sqMeters) => sqMeters * 10.76391;
const toRadians = (degrees) => degrees * (Math.PI / 180);
-export const metersPerkilometer = 1000;
-export const ftPerMile = 5280;
-export const metersToFeet = (meters) => meters * 3.28084;
-export const squareMetersToFeet = (sqMeters) => sqMeters * 10.76391;
+const getArcLine = (p1, p2) => {
+// The number of additional coordinate points to add to a LineString when
+// converting it to conform to a great circle arc.
+ const arcLineResolution = 50;
+ const start = { x: p1[0], y: p1[1] };
+ const end = { x: p2[0], y: p2[1] };
+ const arcGen = new arc.GreatCircle(start, end);
+ return arcGen.Arc(arcLineResolution, { offset: 10 });
+};
/**
- * Transforms a LineString of two points to a MultiLineString of multiple points
- * applying a great circle arc transformation
- * @param {*} geom - the geometry object to apply great circle arc transformation to
- */
+ * Transforms a LineString of two points to a MultiLineString of multiple points
+ * applying a great circle arc transformation
+ * @param {*} geom - the geometry object to apply great circle arc transformation to
+ */
export function transformLineStringArc (geom, projection) {
const coords = [];
const transformedGeom = geom.clone().transform(projection, referenceProjection);
+
transformedGeom.forEachSegment((segStart, segEnd) => {
- const start = {
- x: segStart[0],
- y: segStart[1]
- };
- const end = {
- x: segEnd[0],
- y: segEnd[1]
- };
- const arcGen = new arc.GreatCircle(start, end);
- const arcline = arcGen.Arc(25, { offset: 10 });
- arcline.geometries.forEach((arcGeom) => {
+ getArcLine(segStart, segEnd).geometries.forEach((arcGeom) => {
coords.push(arcGeom.coords);
});
});
@@ -37,32 +40,69 @@ export function transformLineStringArc (geom, projection) {
};
/**
- * Transforms a Polygon to one with addiitonal points on each edge to account for
- * great circle arc
- * @param {*} geom - the geometry object to apply great circle arc transformation to
- */
+ * Transforms a Polygon into one with addiitonal points on each edge to account for
+ * great circle arc
+ * @param {*} geom - the geometry object to apply great circle arc transformation to
+ */
export function transformPolygonArc (geom, projection) {
let coords = [];
const transformedGeom = geom.clone().transform(projection, referenceProjection);
const polyCoords = transformedGeom.getCoordinates()[0];
for (let i = 0; i < polyCoords.length - 1; i++) {
- const start = {
- x: polyCoords[i][0],
- y: polyCoords[i][1]
- };
- const end = {
- x: polyCoords[i + 1][0],
- y: polyCoords[i + 1][1]
- };
- const arcGen = new arc.GreatCircle(start, end);
- const arcline = arcGen.Arc(25, { offset: 10 });
- arcline.geometries.forEach((arcGeom) => {
+ const arcLine = getArcLine(polyCoords[i], polyCoords[i + 1]);
+ arcLine.geometries.forEach((arcGeom) => {
coords = coords.concat(arcGeom.coords);
});
}
return new Polygon([coords]).transform(referenceProjection, projection);
};
+/**
+ *
+ * @param {*} line
+ * @return {String} - The formatted distance measurement
+ */
+export function getFormattedLength(line, projection, unitOfMeasure, useGreatCircle) {
+ const metricLength = useGreatCircle
+ ? getLength(line, { projection })
+ : getRhumbLineDistance(line);
+
+ if (unitOfMeasure === 'km') {
+ return metricLength > 100
+ ? `${roundAndLocale(metricLength, metersPerkilometer)} km`
+ : `${roundAndLocale(metricLength)} m`;
+ }
+ if (unitOfMeasure === 'mi') {
+ const imperialLength = metersToFeet(metricLength);
+ return imperialLength > (ftPerMile / 4)
+ ? `${roundAndLocale(imperialLength, ftPerMile)} mi`
+ : `${roundAndLocale(imperialLength)} ft`;
+ }
+};
+
+/**
+ *
+ * @param {*} polygon
+ * @return {String} - The formatted area measurement
+ */
+export function getFormattedArea(polygon, projection, unitOfMeasure, useGreatCircle) {
+ const metricArea = useGreatCircle
+ ? getArea(polygon, { projection })
+ : getRhumbLineArea(polygon);
+
+ if (unitOfMeasure === 'km') {
+ return metricArea > 10000
+ ? `${roundAndLocale(metricArea, sqMeterPerKilometer)} km<sup>2</sup>`
+ : `${roundAndLocale(metricArea)} m<sup>2</sup>`;
+ }
+ if (unitOfMeasure === 'mi') {
+ const imperialArea = squareMetersToFeet(metricArea);
+ return imperialArea > (sqFtPerSqMile / 8)
+ ? `${roundAndLocale(imperialArea, sqFtPerSqMile)} mi<sup>2</sup>`
+ : `${roundAndLocale(imperialArea)} ft<sup>2</sup>`;
+ }
+};
+
/**
*
*/
@@ -83,12 +123,16 @@ export function getRhumbLineDistance(lineString) {
return Math.sqrt(dLat * dLat + q * q * dLon * dLon) * equatorialRadiusMeters;
};
+export function getRhumbLineArea(polygon) {
+ return 10000000;
+}
+
/**
- * Convert and format raw measurements to two decimal points
- * @param {*} measurement
- * @param {*} factor
- * @return {String} - The measurement, converted based on factor and locale
- */
+ * Convert and format raw measurements to two decimal points
+ * @param {*} measurement
+ * @param {*} factor
+ * @return {String} - The measurement, converted based on factor and locale
+ */
export function roundAndLocale (measurement, factor) {
factor = factor || 1;
return (Math.round(measurement / factor * 100) / 100).toLocaleString();