Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-2203: Prevent starting or completing a measurement when out of map bounds (#3959)

Tom1edcda328ab1
diff --git a/web/js/components/map/ol-measure-tool.js b/web/js/components/map/ol-measure-tool.js
index f30106f7a..525547748 100644
--- a/web/js/components/map/ol-measure-tool.js
+++ b/web/js/components/map/ol-measure-tool.js
@@ -18,6 +18,7 @@ import {
Stroke as OlStyleStroke,
Style as OlStyle,
} from 'ol/style';
+import { transform } from 'ol/proj';
import {
toggleMeasureActive as toggleMeasureActiveAction,
updateMeasurements as updateMeasurementsAction,
@@ -37,6 +38,8 @@ import {
MAP_DISABLE_CLICK_ZOOM,
MAP_ENABLE_CLICK_ZOOM,
} from '../../util/constants';
+import { areCoordinatesWithinExtent } from '../../modules/location-search/util';
+
const { events } = util;
@@ -57,7 +60,7 @@ function OlMeasureTool (props) {
let twoFingerTouchListener;
const {
- map, olMap, crs, unitOfMeasure, toggleMeasureActive, updateMeasurements, projections,
+ map, olMap, crs, unitOfMeasure, toggleMeasureActive, updateMeasurements, projections, proj,
} = props;
useEffect(() => {
@@ -175,6 +178,8 @@ function OlMeasureTool (props) {
crs={crs}
unitOfMeasure={unitOfMeasure}
onRemove={removeFeature}
+ olMap={olMap}
+ proj={proj}
/>
), overlay.getElement());
};
@@ -229,6 +234,12 @@ function OlMeasureTool (props) {
source,
type,
style: drawStyles,
+ condition(e) {
+ const pixel = [e.originalEvent.x, e.originalEvent.y];
+ const coord = olMap.getCoordinateFromPixel(pixel);
+ const tCoord = transform(coord, crs, 'EPSG:4326');
+ return areCoordinatesWithinExtent(proj, tCoord);
+ },
});
olMap.addInteraction(draw);
if (!vectorLayers[crs]) {
@@ -333,6 +344,7 @@ const mapStateToProps = (state) => {
crs,
unitOfMeasure,
projections,
+ proj,
};
};
diff --git a/web/js/components/measure-tool/measure-tooltip.js b/web/js/components/measure-tool/measure-tooltip.js
index 428f15677..a09763697 100644
--- a/web/js/components/measure-tool/measure-tooltip.js
+++ b/web/js/components/measure-tool/measure-tooltip.js
@@ -5,8 +5,10 @@ import {
LineString as OlLineString,
Polygon as OlGeomPolygon,
} from 'ol/geom';
+import { transform } from 'ol/proj';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
+import { areCoordinatesWithinExtent } from '../../modules/location-search/util';
import {
getGeographicLibDistance,
getGeographicLibArea,
@@ -27,6 +29,7 @@ export default function MeasureTooltip(props) {
unitOfMeasure,
geometry,
onRemove,
+ proj,
} = props;
const activeStaticClass = active
@@ -101,16 +104,34 @@ export default function MeasureTooltip(props) {
}
};
- return (
- <div className={`tooltip-measure tooltip-custom-black ${activeStaticClass}`}>
- <span dangerouslySetInnerHTML={{ __html: getMeasurementValue() }} />
- {!active && (
- <span className="close-tooltip" onClick={onRemove} onTouchEnd={onRemove}>
- <FontAwesomeIcon icon="times" fixedWidth />
- </span>
- )}
- </div>
- );
+ const checkGeographicCoordValidity = (val) => val.indexOf('NaN') < 0;
+
+ const checkPolarCoordValidity = () => {
+ const coordinates = geometry.flatCoordinates;
+
+ // Distance & Area measurement coordinates are stored differently, so identify based on geometry type
+ const yCoord = geometry instanceof OlGeomPolygon ? coordinates[coordinates.length - 4] : coordinates[coordinates.length - 2];
+ const xCoord = geometry instanceof OlGeomPolygon ? coordinates[coordinates.length - 3] : coordinates[coordinates.length - 1];
+ const tCoord = transform([xCoord, yCoord], crs, 'EPSG:4326');
+ return areCoordinatesWithinExtent(proj, tCoord);
+ };
+
+ const tooltipValue = getMeasurementValue();
+ const coordinatesAreValid = crs === 'EPSG:4326' ? checkGeographicCoordValidity(tooltipValue) : checkPolarCoordValidity();
+
+ if (coordinatesAreValid) {
+ return (
+ <div className={`tooltip-measure tooltip-custom-black ${activeStaticClass}`}>
+ <span dangerouslySetInnerHTML={{ __html: tooltipValue }} />
+ {!active && (
+ <span className="close-tooltip" onClick={onRemove} onTouchEnd={onRemove}>
+ <FontAwesomeIcon icon="times" fixedWidth />
+ </span>
+ )}
+ </div>
+ );
+ }
+ return null;
}
MeasureTooltip.defaultProps = {
@@ -122,4 +143,5 @@ MeasureTooltip.propTypes = {
geometry: PropTypes.object,
onRemove: PropTypes.func,
unitOfMeasure: PropTypes.string,
+ proj: PropTypes.object,
};
diff --git a/web/js/modules/location-search/util.js b/web/js/modules/location-search/util.js
index 7862b0df3..7d975c2af 100644
--- a/web/js/modules/location-search/util.js
+++ b/web/js/modules/location-search/util.js
@@ -39,7 +39,7 @@ export function areCoordinatesWithinExtent(proj, coordinates) {
const { maxExtent, crs } = proj.selected;
const extent = crs === 'EPSG:4326' ? FULL_MAP_EXTENT : maxExtent;
const coord = crs === 'EPSG:4326' ? coordinates : transform(coordinates, 'EPSG:4326', crs);
- return containsCoordinate(extent, coord);
+ return containsCoordinate(extent, coord); // expects X then Y!
}
/**