Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

rhumb line distance measurement for length

Jason Kent83c741edc879
diff --git a/web/css/checkbox.css b/web/css/checkbox.css
index 226cfbd54..4a4f7526b 100644
--- a/web/css/checkbox.css
+++ b/web/css/checkbox.css
@@ -1,5 +1,9 @@
.wv-checkbox {
position: relative;
+ & input[type="checkbox"] {
+ margin: 3px 1px;
+ vertical-align: middle;
+ }
}
.wv-checkbox input {
position: absolute;
diff --git a/web/css/measure.css b/web/css/measure.css
index 3a08d523a..f9f34ffd9 100644
--- a/web/css/measure.css
+++ b/web/css/measure.css
@@ -11,6 +11,9 @@
& .modal-body {
padding: 0;
}
+ & .form-check-label {
+ margin-left: 24px;
+ }
}
.measure-unit-toggle {
diff --git a/web/css/util.css b/web/css/util.css
index f3508b287..efbbc167e 100644
--- a/web/css/util.css
+++ b/web/css/util.css
@@ -208,8 +208,3 @@ button[data-tooltip] {
display: table;
clear: both;
}
-
-input[type="checkbox"] {
- margin: 3px 1px;
- vertical-align: middle;
-}
diff --git a/web/js/components/measure-tool/measure-menu.js b/web/js/components/measure-tool/measure-menu.js
index 1a685784f..7a633a0a8 100644
--- a/web/js/components/measure-tool/measure-menu.js
+++ b/web/js/components/measure-tool/measure-menu.js
@@ -3,7 +3,8 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { onToggle } from '../../modules/modal/actions';
import IconList from '../util/list';
-import { changeUnits } from '../../modules/measure/actions';
+import { changeUnits, useGreatCircle } from '../../modules/measure/actions';
+import { FormGroup, Label, Input } from 'reactstrap';
// import googleTagManager from 'googleTagManager';
const OPTIONS_ARRAY = [
@@ -31,7 +32,8 @@ class MeasureMenu extends Component {
constructor(props) {
super(props);
this.state = {
- units: props.units
+ units: props.units,
+ useGreatCircleMeasurements: props.useGreatCircleMeasurements
};
}
@@ -44,12 +46,29 @@ class MeasureMenu extends Component {
unitToggle(evt) {
const { checked } = evt.target;
const units = checked ? 'mi' : 'km';
- this.props.toggleUnits(units);
+ this.props.onToggleUnits(units);
+ }
+
+ useGreatCircle(evt) {
+ const { checked } = evt.target;
+ this.props.onToggleUseGreatCircle(checked);
}
render() {
return (
<>
+ <FormGroup check>
+ <Label check>
+ <Input
+ id="great-circle-toggle"
+ type="checkbox"
+ onChange={this.useGreatCircle.bind(this)}
+ defaultChecked={this.state.useGreatCircleMeasurements}
+ />
+ {' '} Great circle? <i className="fas fa-info-circle"></i>
+ </Label>
+ </FormGroup>
+
<div className="measure-unit-toggle custom-control custom-switch">
<label htmlFor="unit-toggle">km</label>
<input
@@ -72,12 +91,16 @@ class MeasureMenu extends Component {
const mapStateToProps = (state, ownProps) => ({
map: state.map,
- units: state.measure.units
+ units: state.measure.units,
+ useGreatCircleMeasurements: state.measure.useGreatCircleMeasurements
});
const mapDispatchToProps = (dispatch, ownProps) => ({
- toggleUnits: (units) => {
+ onToggleUnits: (units) => {
dispatch(changeUnits(units));
},
+ onToggleUseGreatCircle: (value) => {
+ dispatch(useGreatCircle(value));
+ },
onCloseModal: () => {
dispatch(onToggle());
}
@@ -91,6 +114,8 @@ export default connect(
MeasureMenu.propTypes = {
map: PropTypes.object,
onCloseModal: PropTypes.func,
- toggleUnits: PropTypes.func,
- units: PropTypes.string
+ onToggleUnits: PropTypes.func,
+ onToggleUseGreatCircle: PropTypes.func,
+ units: PropTypes.string,
+ useGreatCircleMeasurements: PropTypes.bool
};
diff --git a/web/js/map/measure/measure.js b/web/js/map/measure/ui.js
similarity index 72%
rename from web/js/map/measure/measure.js
rename to web/js/map/measure/ui.js
index 47523970e..6493efa03 100644
--- a/web/js/map/measure/measure.js
+++ b/web/js/map/measure/ui.js
@@ -1,17 +1,21 @@
import { unByKey } from 'ol/Observable';
import Overlay from 'ol/Overlay';
import { getArea, getLength } from 'ol/sphere';
-import { MultiLineString, LineString, Polygon } from 'ol/geom';
+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 arc from 'arc';
-
-const kilometer = 1000;
-const mile = 5280;
-const metersToFeet = (meters) => meters * 3.28084;
-const squareMetersToFeet = (squareMeters) => squareMeters * 10.76391;
+import {
+ ftPerMile,
+ metersPerkilometer,
+ metersToFeet,
+ squareMetersToFeet,
+ roundAndLocale,
+ transformLineStringArc,
+ transformPolygonArc,
+ getRhumbLineDistance
+} from './util.js';
export function measure (map, mapUiEvents) {
let draw;
@@ -27,10 +31,10 @@ export function measure (map, mapUiEvents) {
let allMeasureTooltips = {};
let allGeometries = {};
let unitOfMeasure = 'km';
+ let useGreatCircle = false;
const self = {};
const source = new VectorSource();
const projection = map.getView().getProjection().getCode();
- const referenceProjection = 'EPSG:4326';
const areaBgFill = new Fill({
color: 'rgba(213, 78, 33, 0.1)'
});
@@ -155,6 +159,8 @@ export function measure (map, mapUiEvents) {
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]);
@@ -169,68 +175,18 @@ export function measure (map, mapUiEvents) {
*/
function styleGeometryFn (feature) {
const geometry = feature.getGeometry();
+ if (!useGreatCircle) {
+ return geometry;
+ }
if (geometry instanceof LineString) {
- return transformLineStringArc(geometry);
+ return transformLineStringArc(geometry, projection);
}
if (geometry instanceof Polygon) {
- return transformPolygonArc(geometry);
+ return transformPolygonArc(geometry, projection);
}
return geometry;
};
- /**
- * 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
- */
- function transformLineStringArc (geom) {
- 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) => {
- coords.push(arcGeom.coords);
- });
- });
- return new MultiLineString(coords).transform(referenceProjection, 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
- */
- function transformPolygonArc (geom) {
- 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) => {
- coords = coords.concat(arcGeom.coords);
- });
- }
- return new Polygon([coords]).transform(referenceProjection, projection);
- };
-
/**
* Set the innerHTML of the given tooltip element to the formatted length/area
* measruement of the given geometry
@@ -248,33 +204,22 @@ export function measure (map, mapUiEvents) {
element.innerHTML = measurement;
}
- /**
- * Convert and format raw measurements to two decimal points
- * @param {*} measurement
- * @param {*} factor
- * @return {String} - The measurement, converted based on factor and locale
- */
- const roundAndLocale = (measurement, factor) => {
- factor = factor || 1;
- return (Math.round(measurement / factor * 100) / 100).toLocaleString();
- };
-
/**
*
* @param {*} line
* @return {String} - The formatted distance measurement
*/
const getFormattedLength = (line) => {
- const metricLength = getLength(line, { projection });
+ const metricLength = useGreatCircle ? getLength(line, { projection }) : getRhumbLineDistance(line);
if (unitOfMeasure === 'km') {
return metricLength > 100
- ? `${roundAndLocale(metricLength, kilometer)} km`
+ ? `${roundAndLocale(metricLength, metersPerkilometer)} km`
: `${roundAndLocale(metricLength)} m`;
}
if (unitOfMeasure === 'mi') {
const imperialLength = metersToFeet(metricLength);
- return imperialLength > (mile / 4)
- ? `${roundAndLocale(imperialLength, mile)} mi`
+ return imperialLength > (ftPerMile / 4)
+ ? `${roundAndLocale(imperialLength, ftPerMile)} mi`
: `${roundAndLocale(imperialLength)} ft`;
}
};
@@ -285,20 +230,36 @@ export function measure (map, mapUiEvents) {
* @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, 1000000)} km<sup>2</sup>`
+ ? `${roundAndLocale(metricArea, sqMeterPerKilometer)} km<sup>2</sup>`
: `${roundAndLocale(metricArea)} m<sup>2</sup>`;
}
if (unitOfMeasure === 'mi') {
const imperialArea = squareMetersToFeet(metricArea);
- return imperialArea > (27878400 / 8)
- ? `${roundAndLocale(imperialArea, 27878400)} mi<sup>2</sup>`
+ 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
+ */
+ function recalculateAllMeasurements () {
+ for (const id in allMeasureTooltips) {
+ const geomForTooltip = allGeometries[id];
+ const tooltipElement = allMeasureTooltips[id].element.children[0];
+ tooltipElement.innerHtml = setMeasurement(geomForTooltip, tooltipElement);
+ geomForTooltip.changed();
+ allMeasureTooltips[id].setOffset([0, -7]);
+ }
+ }
+
/**
* Initiate a measurement interaction of the given measureType ('distance' or 'area')
* @param {String} measureType
@@ -330,14 +291,14 @@ export function measure (map, mapUiEvents) {
* Convert unit of measurement on all existing measurments and those created after
* @param {String} unit - Unit of measurement: 'km' or 'mi'
*/
- self.toggleUnits = (unit) => {
+ self.changeUnits = (unit) => {
unitOfMeasure = unit;
- for (const id in allMeasureTooltips) {
- const geomForTooltip = allGeometries[id];
- const tooltipElement = allMeasureTooltips[id].element.children[0];
- tooltipElement.innerHtml = setMeasurement(geomForTooltip, tooltipElement);
- allMeasureTooltips[id].setOffset([0, -7]);
- }
+ recalculateAllMeasurements();
+ };
+
+ self.useGreatCircleMeasurements = (value) => {
+ useGreatCircle = value;
+ recalculateAllMeasurements();
};
/**
diff --git a/web/js/map/measure/util.js b/web/js/map/measure/util.js
new file mode 100644
index 000000000..b004b8ad9
--- /dev/null
+++ b/web/js/map/measure/util.js
@@ -0,0 +1,95 @@
+import { MultiLineString, Polygon } from 'ol/geom';
+import arc from 'arc';
+
+const equatorialRadiusMeters = 6378137;
+const referenceProjection = 'EPSG:4326';
+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;
+
+/**
+ * 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) => {
+ coords.push(arcGeom.coords);
+ });
+ });
+ return new MultiLineString(coords).transform(referenceProjection, 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
+ */
+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) => {
+ coords = coords.concat(arcGeom.coords);
+ });
+ }
+ return new Polygon([coords]).transform(referenceProjection, projection);
+};
+
+/**
+ *
+ */
+export function getRhumbLineDistance(lineString) {
+ const c1 = lineString.getFirstCoordinate();
+ const c2 = lineString.getLastCoordinate();
+ const lat1 = toRadians(c1[1]);
+ const lat2 = toRadians(c2[1]);
+ const dLat = lat2 - lat1;
+ const dPsi = Math.log(Math.tan(Math.PI / 4 + lat2 / 2) / Math.tan(Math.PI / 4 + lat1 / 2));
+ let dLon = toRadians(c2[0] - c1[0]);
+ // E-W course becomes ill-conditioned with 0/0
+ const q = Math.abs(dPsi) > 10e-12 ? dLat / dPsi : Math.cos(lat1);
+ // if dLon over 180 degrees take shorter rhumb across anti-meridian:
+ if (Math.abs(dLon) > Math.PI) {
+ dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
+ }
+ return Math.sqrt(dLat * dLat + q * q * dLon * dLon) * equatorialRadiusMeters;
+};
+
+/**
+ * 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();
+};
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index 6847cf333..bcf8cb701 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -26,13 +26,13 @@ import { MapRunningData } from './runningdata';
import { mapPrecacheTile } from './precachetile';
import { mapUtilZoomAction, getActiveLayerGroup } from './util';
import { mapCompare } from './compare/compare';
-import { measure } from './measure/measure';
+import { measure } from './measure/ui';
import { CALCULATE_RESPONSIVE_STATE } from 'redux-responsive';
import { LOCATION_POP_ACTION } from '../redux-location-state-customs';
import { CHANGE_PROJECTION } from '../modules/projection/constants';
import { SELECT_DATE } from '../modules/date/constants';
import { openCustomContent } from '../modules/modal/actions';
-import { CHANGE_UNITS } from '../modules/measure/constants';
+import { CHANGE_UNITS, USE_GREAT_CIRCLE } from '../modules/measure/constants';
import VectorMetaTable from '../components/vector-metadata/table';
import Cache from 'cachai';
import * as layerConstants from '../modules/layers/constants';
@@ -139,6 +139,8 @@ export function mapui(models, config, store, ui) {
return onResize();
case CHANGE_UNITS:
return toggleMeasurementUnits(action.value);
+ case USE_GREAT_CIRCLE:
+ return useGreatCircleMeasurements(action.value);
case SELECT_DATE:
return updateDate();
}
@@ -839,7 +841,13 @@ export function mapui(models, config, store, ui) {
const toggleMeasurementUnits = (units) => {
for (const proj in measureTools) {
- measureTools[proj].toggleUnits(units);
+ measureTools[proj].changeUnits(units);
+ }
+ };
+
+ const useGreatCircleMeasurements = (value) => {
+ for (const proj in measureTools) {
+ measureTools[proj].useGreatCircleMeasurements(value);
}
};
diff --git a/web/js/modules/measure/actions.js b/web/js/modules/measure/actions.js
index 782cfa026..8bf7abdbb 100644
--- a/web/js/modules/measure/actions.js
+++ b/web/js/modules/measure/actions.js
@@ -1,8 +1,14 @@
-import { CHANGE_UNITS } from './constants';
+import { CHANGE_UNITS, USE_GREAT_CIRCLE } from './constants';
-export function changeUnits(unit) {
+export function changeUnits(units) {
return {
type: CHANGE_UNITS,
- value: unit
+ value: units
+ };
+}
+export function useGreatCircle(value) {
+ return {
+ type: USE_GREAT_CIRCLE,
+ value
};
}
diff --git a/web/js/modules/measure/constants.js b/web/js/modules/measure/constants.js
index 63dd43a24..b3e0f372e 100644
--- a/web/js/modules/measure/constants.js
+++ b/web/js/modules/measure/constants.js
@@ -1 +1,2 @@
export const CHANGE_UNITS = 'MEASURE/CHANGE_UNITS';
+export const USE_GREAT_CIRCLE = 'MEASURE/USE_GREAT_CIRCLE';
diff --git a/web/js/modules/measure/reducers.js b/web/js/modules/measure/reducers.js
index a49e45808..cc07439ee 100644
--- a/web/js/modules/measure/reducers.js
+++ b/web/js/modules/measure/reducers.js
@@ -1,8 +1,9 @@
import { assign as lodashAssign } from 'lodash';
-import { CHANGE_UNITS } from './constants';
+import { CHANGE_UNITS, USE_GREAT_CIRCLE } from './constants';
const defaultState = {
- units: 'km'
+ units: 'km',
+ useGreatCircleMeasurements: false
};
export default function measureReducer(state = defaultState, action) {
@@ -11,6 +12,10 @@ export default function measureReducer(state = defaultState, action) {
return lodashAssign({}, state, {
units: action.value
});
+ case USE_GREAT_CIRCLE:
+ return lodashAssign({}, state, {
+ useGreatCircleMeasurements: action.value
+ });
default:
return state;
}