-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ERA-7681] - Map Drawing Tools #725
Changes from 5 commits
95b8762
4c7a435
045b1de
8a71a46
d2b315d
a7b0d7c
7705bfa
91be1e8
81234d7
5cd996c
d444c15
30b52ee
964d485
ee0a97f
008f6fa
98d5d8c
bf4670e
101b815
437326b
ee3ccf1
d228b94
f5e1ff1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
REACT_APP_DAS_HOST=https://develop.pamdas.org | ||
REACT_APP_GA_TRACKING_ID=UA-12413928-16 | ||
REACT_APP_MOCK_EVENTS_API=true | ||
REACT_APP_MOCK_EVENTS_API=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { memo, useEffect } from 'react'; | ||
|
||
import { withMap } from '../EarthRangerMap'; | ||
|
||
import { useMapLayer, useMapSource } from '../hooks'; | ||
|
||
import { linePaint, lineLayout, circlePaint, fillLayout, fillPaint, symbolPaint, symbolLayout } from './layerStyles'; | ||
|
||
export const RULER_POINTS_LAYER_ID = 'RULER_POINTS_LAYER_ID'; | ||
|
||
export const LAYER_IDS = { | ||
POINTS: 'draw-layer-points', | ||
LINES: 'draw-layer-lines', | ||
LABELS: 'draw-layer-labels', | ||
FILL: 'draw-layer-fill', | ||
}; | ||
|
||
const SOURCE_IDS = { | ||
LINE_SOURCE: 'map-line-source', | ||
FILL_SOURCE: 'map-fill-source', | ||
}; | ||
|
||
const MapDrawingLayers = (props) => { | ||
const { | ||
data, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestion -- I'm already in the process of contextifying everything so that works out 🙂 Data vs explicitly naming the properties...I have no strong preference, I thought it would be easier to just pass the full hook return value down rather than destructure and assign as individual props. I'll play with it. |
||
map, | ||
} = props; | ||
|
||
useMapSource(SOURCE_IDS.LINE_SOURCE, data?.drawnLineSegments, { type: 'geojson' }); | ||
useMapSource(SOURCE_IDS.FILL_SOURCE, data?.fillPolygon, { type: 'geojson' }); | ||
|
||
const circleLayer = useMapLayer(LAYER_IDS.POINTS, 'circle', SOURCE_IDS.LINE_SOURCE, circlePaint); | ||
useMapLayer(LAYER_IDS.LINES, 'line', SOURCE_IDS.LINE_SOURCE, linePaint, lineLayout); | ||
useMapLayer(LAYER_IDS.LABELS, 'symbol', SOURCE_IDS.LINE_SOURCE, symbolPaint, symbolLayout); | ||
useMapLayer(LAYER_IDS.FILL, 'fill', SOURCE_IDS.FILL_SOURCE, fillPaint, fillLayout); | ||
|
||
useEffect(() => { | ||
if (circleLayer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we make sure that map is defined too? |
||
const onCircleMouseEnter = () => map.getCanvas().style.cursor = 'pointer'; | ||
const onCircleMouseLeave = () => map.getCanvas().style.cursor = ''; | ||
|
||
map.on('mouseenter', LAYER_IDS.POINTS, onCircleMouseEnter); | ||
JoshuaVulcan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
map.on('mouseleave', LAYER_IDS.POINTS, onCircleMouseLeave); | ||
|
||
return () => { | ||
map.off('mouseenter', LAYER_IDS.POINTS, onCircleMouseEnter); | ||
map.off('mouseleave', LAYER_IDS.POINTS, onCircleMouseLeave); | ||
}; | ||
} | ||
}, [circleLayer, map]); | ||
|
||
return null; | ||
}; | ||
|
||
export default memo(withMap(MapDrawingLayers)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { DRAWING_MODES } from '.'; | ||
import { createLineSegmentGeoJsonForCoords, createFillPolygonForCoords } from './utils'; | ||
|
||
export const useDrawToolGeoJson = (points = [], cursorCoords = null, drawMode = DRAWING_MODES.POLYGON) => { | ||
const drawnLinePoints = useMemo(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't memoization costs add latency since the user may move the cursor fast? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe so! |
||
cursorCoords | ||
? [...points, cursorCoords] | ||
: [...points], | ||
[points, cursorCoords]); | ||
|
||
const shouldCalcPolygonData = drawMode === DRAWING_MODES.POLYGON && drawnLinePoints.length > 2; | ||
|
||
const fillPolygonPoints = useMemo(() => | ||
shouldCalcPolygonData | ||
? [...drawnLinePoints, drawnLinePoints.at(0)] | ||
: null, [drawnLinePoints, shouldCalcPolygonData]); | ||
|
||
const geoJsonObject = useMemo(() => { | ||
if (drawnLinePoints.length < 2) return null; | ||
|
||
const data = { | ||
drawnLineSegments: createLineSegmentGeoJsonForCoords(drawnLinePoints), | ||
}; | ||
|
||
if (fillPolygonPoints) { | ||
data.fillPolygon = createFillPolygonForCoords(fillPolygonPoints); | ||
} | ||
|
||
return data; | ||
|
||
}, [drawnLinePoints, fillPolygonPoints]); | ||
|
||
return geoJsonObject; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import React, { memo, Fragment, useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { Popup } from 'react-mapbox-gl'; | ||
import debounce from 'lodash/debounce'; | ||
import noop from 'lodash/noop'; | ||
import length from '@turf/length'; | ||
import PropTypes from 'prop-types'; | ||
import isEqual from 'react-fast-compare'; | ||
|
||
import { calcPositiveBearing } from '../utils/location'; | ||
import { withMap } from '../EarthRangerMap'; | ||
|
||
import { useDrawToolGeoJson } from '../MapDrawingTools/hooks'; | ||
import { useMapEventBinding } from '../hooks'; | ||
|
||
import styles from './styles.module.scss'; | ||
import MapLayers from './MapLayers'; | ||
|
||
import { LAYER_IDS } from './MapLayers'; | ||
|
||
export const RULER_POINTS_LAYER_ID = 'RULER_POINTS_LAYER_ID'; | ||
|
||
export const DRAWING_MODES = { | ||
POLYGON: 'polygon', | ||
LINE: 'line', | ||
}; | ||
|
||
const MapDrawingTools = (props) => { | ||
const { | ||
children, | ||
drawing = true, | ||
drawingMode = DRAWING_MODES.POLYGON, | ||
onChange = noop, | ||
onClickPoint = noop, | ||
onClickFill = noop, | ||
points, | ||
onClickLine = noop, | ||
onClickLabel = noop, | ||
} = props; | ||
|
||
const [pointerLocation, setPointerLocation] = useState(null); | ||
|
||
const cursorPopupCoords = useMemo(() => pointerLocation ? [pointerLocation.lng, pointerLocation.lat] : points[points.length - 1], [pointerLocation, points]); | ||
const data = useDrawToolGeoJson(points, (drawing && cursorPopupCoords), drawingMode); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it for the dataContainer, in passing up the data on eventing. for the |
||
const dataContainer = useRef(data); | ||
|
||
const lineLength = useMemo(() => { | ||
if (!data?.drawnLineSegments) return null; | ||
|
||
return `${length(data.drawnLineSegments).toFixed(2)}km`; | ||
}, [data?.drawnLineSegments]); | ||
|
||
const showLayer = pointerLocation || points.length; | ||
|
||
const onMapClick = useCallback(debounce((e) => { | ||
const { lngLat } = e; | ||
e.preventDefault(); | ||
e.originalEvent.stopPropagation(); | ||
onChange([...points, [lngLat.lng, lngLat.lat]], dataContainer.current); | ||
}, 100), [onChange, points]); | ||
|
||
|
||
const onMapDblClick = useCallback((e) => { | ||
onMapClick(e); | ||
|
||
e.preventDefault(); | ||
e.originalEvent.stopPropagation(); | ||
onMapClick.cancel(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ensures we don't have funny sync-related issues where a second click fires. It's ported over from the existing ruler code |
||
|
||
}, [onMapClick]); | ||
|
||
const onMouseMove = useCallback((e) => { | ||
setPointerLocation(e.lngLat); | ||
}, []); | ||
|
||
useMapEventBinding('click', onClickLine, LAYER_IDS.LINES); | ||
useMapEventBinding('click', onClickPoint, LAYER_IDS.POINTS); | ||
useMapEventBinding('click', onClickLabel, LAYER_IDS.LABELS); | ||
useMapEventBinding('click', onClickFill, LAYER_IDS.FILL); | ||
|
||
useMapEventBinding('mousemove', onMouseMove, null, drawing); | ||
useMapEventBinding('dblclick', onMapDblClick, null, drawing); | ||
useMapEventBinding('click', onMapClick, null, drawing); | ||
|
||
useEffect(() => { | ||
dataContainer.current = data; | ||
}, [dataContainer, data]); | ||
|
||
if (!showLayer) return null; | ||
|
||
return <> | ||
{drawing && <CursorPopup coords={cursorPopupCoords} lineLength={lineLength} points={points} />} | ||
<MapLayers data={data} /> | ||
{children} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the purpose of supporting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For ❇️ the future ❇️ but it's trivial to add/remove |
||
</>; | ||
}; | ||
|
||
export default memo(withMap(MapDrawingTools)); | ||
|
||
PropTypes.propTypes = { | ||
points: PropTypes.array, | ||
}; | ||
|
||
const CursorPopup = (props) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be cleaner to have this one in its own file, with its own tests and all. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! |
||
const { coords, points, lineLength } = props; | ||
|
||
const popupClassName = `${styles.popup} ${styles.notDone}`; | ||
const popupOffset = [-8, 0]; | ||
const popupAnchorPosition = 'right'; | ||
|
||
const popupLocationAndPreviousPointAreIdentical = isEqual(coords, points[points.length - 1]); | ||
const showPromptForSecondPoint = popupLocationAndPreviousPointAreIdentical && points.length === 1; | ||
return <Popup className={popupClassName} offset={popupOffset} coordinates={coords} anchor={popupAnchorPosition}> | ||
{points.length === 0 && <p>Click to start</p>} | ||
{!!points.length && <Fragment> | ||
{showPromptForSecondPoint && <div> | ||
<p>Select another point</p> | ||
</div>} | ||
{!showPromptForSecondPoint && <Fragment> | ||
<p>Bearing: {calcPositiveBearing(points[points.length - 1], coords).toFixed(2)}°</p> | ||
<p>Distance: {lineLength}</p> | ||
{!!points.length && <> | ||
<small>Click to add a point.<br />Hit "enter" or "return" to complete.</small> | ||
</>} | ||
</Fragment>} | ||
</Fragment>} | ||
</Popup>; | ||
}; | ||
|
||
/* | ||
points, | ||
drawing, | ||
drawingMode, | ||
onChange | ||
onComplete | ||
onClickPoint | ||
onClickFill | ||
onClickLine | ||
onClickLabel | ||
*/ | ||
|
||
|
||
/* how to show popup when completed | ||
how to show popup when clicking individual point | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DEFAULT_SYMBOL_PAINT } from '../constants'; | ||
|
||
export const linePaint = { | ||
'line-color': 'orange', | ||
'line-dasharray': [2, 4], | ||
'line-width': 2, | ||
}; | ||
|
||
export const lineLayout = { | ||
'line-join': 'round', | ||
'line-cap': 'round', | ||
}; | ||
|
||
export const symbolLayout = { | ||
'text-allow-overlap': true, | ||
'icon-allow-overlap': true, | ||
'symbol-placement': 'line-center', | ||
'text-font': ['Open Sans Regular'], | ||
'text-field': ['get', 'lengthLabel'], | ||
}; | ||
|
||
export const symbolPaint = { | ||
...DEFAULT_SYMBOL_PAINT, | ||
'text-halo-width': 2, | ||
}; | ||
|
||
export const circlePaint = { | ||
'circle-radius': 5, | ||
'circle-color': 'orange', | ||
}; | ||
|
||
export const fillLayout = { visibility: 'visible' }; | ||
export const fillPaint = { 'fill-color': 'red', 'fill-opacity': 0.4 }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { lineString, polygon } from '@turf/helpers'; | ||
import length from '@turf/length'; | ||
import lineSegment from '@turf/line-segment'; | ||
|
||
export const createLineSegmentGeoJsonForCoords = (coords) => { | ||
const lineSegments = lineSegment(lineString(coords)); | ||
|
||
lineSegments.features = lineSegments.features.map(feature => { | ||
const lineLength = length(feature); | ||
const lengthLabel = `${lineLength.toFixed(2)}km`; | ||
|
||
return { | ||
...feature, | ||
properties: { | ||
...feature.properties, | ||
length: lineLength, | ||
lengthLabel, | ||
} | ||
}; | ||
}); | ||
|
||
return lineSegments; | ||
}; | ||
|
||
export const createFillPolygonForCoords = (coords) => polygon([coords]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we have this trivial method just to keep it in utils with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we reuse
LAYER_IDS.POINTS
for the ruler?