Interactive interface for browsing global, full-resolution satellite imagery
get measure tool working for distance and area
Jason Kente03a23b1630d
diff --git a/web/css/button.css b/web/css/button.cssindex f3f20c718..4176e2d3c 100644--- a/web/css/button.css+++ b/web/css/button.css@@ -46,17 +46,3 @@cursor: not-allowed;}-.wv-measure-button {- position: absolute;- bottom: 180px;- right: 10px;- padding: 6px 10px;-}-.modal-dialog.toolbar-modal.measure-tool-modal {- top: unset;- right: 70px;- bottom: 157px;- & .modal-body {- padding: 0;- }-}diff --git a/web/css/measure.css b/web/css/measure.cssnew file mode 100644index 000000000..962146c9b--- /dev/null+++ b/web/css/measure.css@@ -0,0 +1,51 @@+.wv-measure-button {+ position: absolute;+ bottom: 180px;+ right: 10px;+ padding: 6px 10px;+}+.modal-dialog.toolbar-modal.measure-tool-modal {+ top: unset;+ right: 70px;+ bottom: 157px;+ & .modal-body {+ padding: 0;+ }+}++/* TODO selector name is too generic */+.tooltip {+ position: relative;+ background: rgba(0, 0, 0, 0.5);+ border-radius: 4px;+ color: #fff;+ padding: 4px 8px;+ opacity: 0.75;+ white-space: nowrap;+}+.tooltip-measure {+ opacity: 1;+ font-weight: 600;+}+.tooltip-static {+ background-color: #fc3;+ color: #000;+ border: 1px solid #fff;+ font-weight: 700;+ font-size: 16px;+ opacity: 0.9;+}+.tooltip-measure::before,+.tooltip-static::before {+ border-top: 6px solid rgba(0, 0, 0, 0.5);+ border-right: 6px solid transparent;+ border-left: 6px solid transparent;+ content: "";+ position: absolute;+ bottom: -6px;+ margin-left: -7px;+ left: 50%;+}+.tooltip-static::before {+ border-top-color: #fc3;+}diff --git a/web/js/app.js b/web/js/app.jsindex 6183e2e85..d84775cfc 100644--- a/web/js/app.js+++ b/web/js/app.js@@ -73,6 +73,7 @@ import '../css/dateselector.css';import '../css/tooltip.css';import '../css/mobile.css';import '../css/modal.css';+import '../css/measure.css';import '../css/list.css';import '../css/vectorMeta.css';import '../pages/css/document.css';diff --git a/web/js/map/measure/measure.js b/web/js/map/measure/measure.jsnew file mode 100644index 000000000..df27043c8--- /dev/null+++ b/web/js/map/measure/measure.js@@ -0,0 +1,201 @@+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/Draw';+import { Vector as VectorLayer } from 'ol/layer';+import { Vector as VectorSource } from 'ol/source';+import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';++export function measure (map) {+ const self = {};+ let draw;+ let sketch;+ let helpTooltipElement;+ let helpTooltip;+ let measureTooltipElement;+ let measureTooltip;+ let allMeasureTooltips = [];+ let drawChangeListener;+ const continuePolygonMsg = 'Click to continue drawing <br/> Double-click to complete.';+ const continueLineMsg = 'Click to continue drawing <br/> Double-click to complete.';+ const source = new VectorSource();+ const vector = getVectorLayer(source);++ /**+ * Handle pointer move.+ */+ function pointerMoveHandler (evt) {+ if (evt.dragging) {+ return;+ }+ let helpMsg = 'Click to start drawing';++ if (sketch) {+ const geom = (sketch.getGeometry());+ if (geom instanceof Polygon) {+ helpMsg = continuePolygonMsg;+ } else if (geom instanceof LineString) {+ helpMsg = continueLineMsg;+ }+ }+ helpTooltipElement.innerHTML = helpMsg;+ helpTooltip.setPosition(evt.coordinate);+ helpTooltipElement.classList.remove('hidden');+ };++ function createHelpTooltip() {+ if (helpTooltipElement) {+ helpTooltipElement.parentNode.removeChild(helpTooltipElement);+ }+ helpTooltipElement = document.createElement('div');+ helpTooltipElement.className = 'tooltip hidden';+ helpTooltip = new Overlay({+ element: helpTooltipElement,+ offset: [15, 0],+ positioning: 'center-left'+ });+ map.addOverlay(helpTooltip);+ }++ function createMeasureTooltip() {+ if (measureTooltipElement) {+ measureTooltipElement.parentNode.removeChild(measureTooltipElement);+ }+ measureTooltipElement = document.createElement('div');+ measureTooltipElement.className = 'tooltip tooltip-measure';+ measureTooltip = new Overlay({+ element: measureTooltipElement,+ offset: [0, -15],+ positioning: 'bottom-center'+ });+ allMeasureTooltips.push(measureTooltip);+ map.addOverlay(measureTooltip);+ }++ function drawStartCallback (evt) {+ let tooltipCoord; // = evt.coordinate;+ sketch = evt.feature;+ drawChangeListener = sketch.getGeometry().on('change', (evt) => {+ const geom = evt.target;+ let output;+ if (geom instanceof Polygon) {+ output = formatArea(geom);+ tooltipCoord = geom.getInteriorPoint().getCoordinates();+ } else if (geom instanceof LineString) {+ output = formatLength(geom);+ tooltipCoord = geom.getLastCoordinate();+ }+ measureTooltipElement.innerHTML = output;+ measureTooltip.setPosition(tooltipCoord);+ });+ };++ function drawEndCallback () {+ measureTooltipElement.className = 'tooltip tooltip-static';+ measureTooltip.setOffset([0, -7]);+ sketch = null;+ measureTooltipElement = null;+ createMeasureTooltip();+ map.removeOverlay(helpTooltip);+ map.removeInteraction(draw);+ unByKey(drawChangeListener);+ };++ /**+ * Add the map interactionn+ * @param {String} measureType+ */+ function addInteraction(measureType) {+ const type = (measureType === 'area' ? 'Polygon' : 'LineString');+ draw = getDraw(source, type);+ vector.setMap(map);+ map.addInteraction(draw);+ createMeasureTooltip();+ createHelpTooltip();+ draw.on('drawstart', drawStartCallback, this);+ draw.on('drawend', drawEndCallback, this);+ }++ self.initMeasurement = (type) => {+ map.on('pointermove', pointerMoveHandler);+ addInteraction(type);+ };++ self.clearMeasurements = () => {+ allMeasureTooltips.forEach(tooltip => {+ map.removeOverlay(tooltip);+ });+ allMeasureTooltips = [];+ map.removeInteraction(draw);+ vector.getSource().clear();+ vector.setMap(null);+ };++ return self;+}++const formatLength = (line) => {+ const length = getLength(line);+ if (length > 100) {+ return (Math.round(length / 1000 * 100) / 100) + ' ' + 'km';+ } else {+ return (Math.round(length * 100) / 100) + ' ' + 'm';+ }+};++const formatArea = (polygon) => {+ const area = getArea(polygon);+ if (area > 10000) {+ return `${(Math.round(area / 1000000 * 100) / 100)} km<sup>2</sup>`;+ } else {+ return `${Math.round(area * 100) / 100} m<sup>2</sup>`;+ }+};++const getVectorLayer = (source) => {+ return new VectorLayer({+ source,+ style: new Style({+ fill: new Fill({+ color: 'rgba(255, 255, 255, 0.2)'+ }),+ stroke: new Stroke({+ color: '#ffcc33',+ width: 2+ }),+ image: new CircleStyle({+ radius: 7,+ fill: new Fill({+ color: '#ffcc33'+ })+ })+ })+ });+};++const getDraw = (source, type) => {+ return new Draw({+ source,+ type,+ style: new Style({+ fill: new Fill({+ color: 'rgba(255, 255, 255, 0.2)'+ }),+ stroke: new Stroke({+ color: 'rgba(0, 0, 0, 0.5)',+ lineDash: [10, 10],+ width: 2+ }),+ image: new CircleStyle({+ radius: 5,+ stroke: new Stroke({+ color: 'rgba(0, 0, 0, 0.7)'+ }),+ fill: new Fill({+ color: 'rgba(255, 255, 255, 0.2)'+ })+ })+ })+ });+};diff --git a/web/js/map/ui.js b/web/js/map/ui.jsindex a81719b0f..a60b0b520 100644--- a/web/js/map/ui.js+++ b/web/js/map/ui.js@@ -26,6 +26,7 @@ import { MapRunningData } from './runningdata';import { mapPrecacheTile } from './precachetile';import { mapUtilZoomAction, getActiveLayerGroup } from './util';import { mapCompare } from './compare/compare';+import { measure } from './measure/measure';import { CALCULATE_RESPONSIVE_STATE } from 'redux-responsive';import { LOCATION_POP_ACTION } from '../redux-location-state-customs';import { CHANGE_PROJECTION } from '../modules/projection/constants';@@ -135,13 +136,13 @@ export function mapui(models, config, store, ui) {case SELECT_DATE:return updateDate();case measureConstants.MEASURE_DISTANCE:- console.log('distance!');+ self.selected.measureTool.initMeasurement('distance');return;case measureConstants.MEASURE_AREA:- console.log('area!');+ self.selected.measureTool.initMeasurement('area');return;case measureConstants.CLEAR_MEASUREMENTS:- console.log('clear!');+ self.selected.measureTool.clearMeasurements();}};/*@@ -157,6 +158,7 @@ export function mapui(models, config, store, ui) {// it is possible that config.projections somehow becomes array-like.lodashForOwn(config.projections, function(proj) {var map = createMap(proj);+ map.measureTool = measure(map);self.proj[proj.id] = map;});models.map.events.on('update-layers', reloadLayers);diff --git a/web/js/modules/combine-reducers.js b/web/js/modules/combine-reducers.jsindex 8ff963541..258fbe21e 100644--- a/web/js/modules/combine-reducers.js+++ b/web/js/modules/combine-reducers.js@@ -37,6 +37,7 @@ import {import dataDownloadReducer from './data/reducers';import { get as lodashGet, assign as lodashAssign } from 'lodash';import { imageDownloadReducer } from './image-download/reducers';+import { measureReducer } from './measure/reducers';import { LOCATION_POP_ACTION } from '../redux-location-state-customs';function lastAction(state = null, action) {@@ -125,7 +126,8 @@ const reducers = {shortLink,notificationsRequest,lastAction: lastAction,- location: locationReducer+ location: locationReducer,+ measure: measureReducer};const appReducer = combineReducers(reducers);/**diff --git a/web/js/modules/measure/actions.js b/web/js/modules/measure/actions.jsindex 810e83909..cd76f3dab 100644--- a/web/js/modules/measure/actions.js+++ b/web/js/modules/measure/actions.js@@ -1,6 +1,8 @@import {MEASURE_DISTANCE,MEASURE_AREA,+ ADD_DISTANCE_MEASUREMENT,+ ADD_AREA_MEASUREMENT,CLEAR_MEASUREMENTS} from './constants';@@ -14,6 +16,18 @@ export function measureArea() {type: MEASURE_AREA};}+export function addDistanceMeasurement(measurement) {+ return {+ type: ADD_DISTANCE_MEASUREMENT,+ value: measurement+ };+}+export function addAreaMeasurement(measurement) {+ return {+ type: ADD_AREA_MEASUREMENT,+ value: measurement+ };+}export function clearMeasurements() {return {type: CLEAR_MEASUREMENTSdiff --git a/web/js/modules/measure/constants.js b/web/js/modules/measure/constants.jsindex 9b961e332..851bc5d63 100644--- a/web/js/modules/measure/constants.js+++ b/web/js/modules/measure/constants.js@@ -1,3 +1,5 @@export const MEASURE_DISTANCE = 'MAP/MEASURE_DISTANCE';export const MEASURE_AREA = 'MAP/MEASURE_AREA';-export const CLEAR_MEASUREMENTS = 'MAP/MEASURE_AREA';+export const ADD_DISTANCE_MEASUREMENT = 'MAP/ADD_DISTANCE_MEASUREMENT';+export const ADD_AREA_MEASUREMENT = 'MAP/ADD_AREA_MEASUREMENT';+export const CLEAR_MEASUREMENTS = 'MAP/CLEAR_MEASUREMENTS';diff --git a/web/js/modules/measure/reducers.js b/web/js/modules/measure/reducers.jsindex 8921fb2a0..f71b4d42e 100644--- a/web/js/modules/measure/reducers.js+++ b/web/js/modules/measure/reducers.js@@ -2,23 +2,29 @@ import { assign as lodashAssign } from 'lodash';import {MEASURE_DISTANCE,MEASURE_AREA,+ ADD_DISTANCE_MEASUREMENT,+ ADD_AREA_MEASUREMENT,CLEAR_MEASUREMENTS} from './constants';const INITIAL_STATE = {- distanceSketches: [],- areaSketches: []+ distanceMeasurements: [],+ areaMeasurements: []};-export default function measureReducer(state = INITIAL_STATE, action) {+export function measureReducer(state = INITIAL_STATE, action) {switch (action.type) {+ case MEASURE_AREA:+ return state;case MEASURE_DISTANCE:+ return state;+ case ADD_DISTANCE_MEASUREMENT:return lodashAssign({}, state, {- distanceSketches: action.sketches+ distanceMeasurements: action.value});- case MEASURE_AREA:+ case ADD_AREA_MEASUREMENT:return lodashAssign({}, state, {- areaSketches: action.sketches+ areaMeasurements: action.value});case CLEAR_MEASUREMENTS:return lodashAssign({}, state, INITIAL_STATE);