-
Notifications
You must be signed in to change notification settings - Fork 32
Feat: Draw geometries on map #3903
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b1dc75c
Feature/3686-draw-on-map (#3785)
Magnusrm 834424e
Merge branch 'main' into feature/3686-draw-on-map
tina-ahm b703041
Feature/toolbar config (#3810)
tina-ahm a87803c
Merge branch 'main' into feature/3686-draw-on-map
tina-ahm efaa8e1
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm d0a0808
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm d966b70
Save-to-datamodel (#3855)
Magnusrm 91f88d8
Feat/3686-draw-on-map/edit-geometries (#3865)
Magnusrm 2c0faff
delete geos wip
Magnusrm 65e2e3a
add WKT support, replace deprecated WKT package
Magnusrm 65ca677
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm b44db26
update yarn.lock
Magnusrm 0a4d69f
remove caret from react-ealfet-draw version
Magnusrm fa4cb60
fix duplicate geometries being rendered
Magnusrm 8aa4108
Merge branch 'feature/3686-draw-on-map' into feature/3686-draw-on-map…
Magnusrm a6e22ec
debugging
Magnusrm 243be89
fix editing bug
Magnusrm 971580d
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm c0b5407
fix coderabbit comments
Magnusrm fdf69f0
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm 2845583
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm a3025ee
add cypress tests for drawing geometries on map
Magnusrm 67db162
use specific versioning
Magnusrm 32aeff4
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm 07e8a8b
clean up comments and simplify early return logic
Magnusrm bc06758
use strict equals
Magnusrm 098fe5e
attempt to fix cypress test
Magnusrm 414b3f9
Merge remote-tracking branch 'origin/main' into feature/3686-draw-on-map
Magnusrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
src/layout/Map/features/geometries/editable/MapEditGeometries.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import React, { useEffect, useRef } from 'react'; | ||
| import { FeatureGroup } from 'react-leaflet'; | ||
| import { EditControl } from 'react-leaflet-draw'; | ||
|
|
||
| import { geojsonToWKT } from '@terraformer/wkt'; | ||
| // Import GeoJSON type | ||
| import L from 'leaflet'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
| import type { Feature } from 'geojson'; | ||
|
|
||
| import { FD } from 'src/features/formData/FormDataWrite'; | ||
| import { ALTINN_ROW_ID } from 'src/features/formData/types'; | ||
| import { toRelativePath } from 'src/features/saveToGroup/useSaveToGroup'; | ||
| import { useLeafletDrawSpritesheetFix } from 'src/layout/Map/features/geometries/editable/useLeafletDrawSpritesheetFix'; | ||
| import { useMapParsedGeometries } from 'src/layout/Map/features/geometries/fixed/hooks'; | ||
| import { useDataModelBindingsFor } from 'src/utils/layout/hooks'; | ||
| import { useItemWhenType } from 'src/utils/layout/useNodeItem'; | ||
|
|
||
| interface FeatureWithId extends Feature { | ||
| properties: { | ||
| altinnRowId?: string; | ||
| }; | ||
| } | ||
| interface MapEditGeometriesProps { | ||
| baseComponentId: string; | ||
| } | ||
|
|
||
| export function MapEditGeometries({ baseComponentId }: MapEditGeometriesProps) { | ||
| const { geometryType } = useItemWhenType(baseComponentId, 'Map'); | ||
|
|
||
| const editRef = useRef<L.FeatureGroup>(null); | ||
|
|
||
| const geometryBinding = useDataModelBindingsFor(baseComponentId, 'Map')?.geometries; | ||
| const geometryDataBinding = useDataModelBindingsFor(baseComponentId, 'Map')?.geometryData; | ||
| const isEditableBinding = useDataModelBindingsFor(baseComponentId, 'Map')?.geometryIsEditable; | ||
| const geometryDataFieldName = geometryDataBinding?.field.split('.').pop(); | ||
| const isEditableFieldName = isEditableBinding?.field.split('.').pop(); | ||
| const initialGeometries = useMapParsedGeometries(baseComponentId)?.filter((g) => g.isEditable); | ||
|
Magnusrm marked this conversation as resolved.
|
||
|
|
||
| const geometryDataPath = toRelativePath(geometryBinding, geometryDataBinding); | ||
|
|
||
| const appendToList = FD.useAppendToList(); | ||
| const setLeafValue = FD.useSetLeafValue(); | ||
| const removeFromList = FD.useRemoveFromListCallback(); | ||
|
|
||
| const { toolbar } = useItemWhenType(baseComponentId, 'Map'); | ||
|
|
||
| useLeafletDrawSpritesheetFix(); | ||
|
|
||
| // Load initial data into the FeatureGroup on component mount | ||
| useEffect(() => { | ||
| const featureGroup = editRef.current; | ||
| if (featureGroup && initialGeometries) { | ||
| // Clear existing layers to prevent duplication if initialData changes | ||
| featureGroup.clearLayers(); | ||
|
|
||
| initialGeometries.forEach((item) => { | ||
| if (item.data && item.data.type === 'FeatureCollection') { | ||
| item.data.features.forEach((feature: Feature) => { | ||
| // Attach the unique ID to the feature's properties | ||
| const newFeature: FeatureWithId = { | ||
| ...feature, | ||
| properties: { | ||
| ...feature.properties, | ||
| altinnRowId: item.altinnRowId, | ||
| }, | ||
| }; | ||
|
|
||
| // Create a GeoJSON layer for the single feature and add it to the group | ||
| const leafletLayer = L.geoJSON(newFeature); | ||
| leafletLayer.eachLayer((layer) => { | ||
| featureGroup.addLayer(layer); | ||
| }); | ||
| }); | ||
| } else { | ||
| // Handle case where item.data is a single Feature / PolyLine / Polygon, etc. | ||
| const geoData = item.data; | ||
|
|
||
| const isFeature = 'type' in geoData && geoData.type === 'Feature'; | ||
|
|
||
| const newFeature: FeatureWithId = isFeature | ||
| ? { | ||
| ...(geoData as Feature), | ||
| properties: { | ||
| ...(geoData as Feature).properties, | ||
| altinnRowId: item.altinnRowId, | ||
| }, | ||
| } | ||
| : { | ||
| type: 'Feature', | ||
| geometry: geoData, | ||
| properties: { | ||
| altinnRowId: item.altinnRowId, | ||
| }, | ||
| }; | ||
|
Magnusrm marked this conversation as resolved.
|
||
|
|
||
| const leafletLayer = L.geoJSON(newFeature); | ||
| leafletLayer.eachLayer((layer) => { | ||
| featureGroup.addLayer(layer); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }, [initialGeometries]); | ||
|
|
||
| const onCreatedHandler = (e: L.DrawEvents.Created) => { | ||
| if (!geometryBinding || !geometryDataFieldName || !isEditableFieldName) { | ||
| return; | ||
| } | ||
|
|
||
| const uuid = uuidv4(); | ||
| const layer = e.layer; | ||
| const geo = layer.toGeoJSON(); | ||
|
|
||
| // Ensure the Leaflet layer object itself knows its ID for future edits | ||
| if (!layer.feature) { | ||
| layer.feature = { type: 'Feature', geometry: geo.geometry, properties: {} }; | ||
| } | ||
| layer.feature.properties = { | ||
| ...layer.feature.properties, | ||
| altinnRowId: uuid, | ||
| }; | ||
|
|
||
| let geoString = JSON.stringify(geo); | ||
| if (geometryType === 'WKT') { | ||
| geoString = geojsonToWKT(geo.geometry); | ||
| } | ||
|
|
||
| appendToList({ | ||
| reference: geometryBinding, | ||
| newValue: { | ||
| [ALTINN_ROW_ID]: uuid, | ||
| [geometryDataFieldName]: geoString, | ||
| [isEditableFieldName]: true, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const onEditedHandler = (e: L.DrawEvents.Edited) => { | ||
| if (!geometryBinding || !geometryDataBinding || !isEditableBinding) { | ||
| return; | ||
| } | ||
|
|
||
| e.layers.eachLayer((layer) => { | ||
| // @ts-expect-error - Leaflet's typings don't guarantee feature or properties exist, but we ensure they do in onCreatedHandler | ||
| const editedGeo = layer.toGeoJSON(); | ||
| const altinnRowId = editedGeo.properties?.altinnRowId; | ||
|
|
||
| let geoString = JSON.stringify(editedGeo); | ||
|
|
||
| if (geometryType === 'WKT') { | ||
| geoString = geojsonToWKT(editedGeo.geometry); | ||
| } | ||
|
|
||
| initialGeometries?.forEach((g, index) => { | ||
| if (g.altinnRowId === altinnRowId) { | ||
| const field = `${geometryBinding.field}[${index}].${geometryDataPath}`; | ||
| setLeafValue({ | ||
| reference: { dataType: geometryDataBinding?.dataType, field }, | ||
| newValue: geoString, | ||
| }); | ||
| } | ||
| }); | ||
|
Magnusrm marked this conversation as resolved.
|
||
| }); | ||
| }; | ||
|
|
||
| const onDeletedHandler = (e: L.DrawEvents.Deleted) => { | ||
| if (!geometryBinding) { | ||
| return; | ||
| } | ||
|
|
||
| e.layers.eachLayer((layer) => { | ||
| // @ts-expect-error - Leaflet's typings don't guarantee feature or properties exist, but we ensure they do in onCreatedHandler | ||
| const deletedGeo = layer.toGeoJSON(); | ||
| removeFromList({ | ||
| reference: geometryBinding, | ||
| callback: (item) => item[ALTINN_ROW_ID] === deletedGeo.properties?.altinnRowId, | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <FeatureGroup ref={editRef}> | ||
| <EditControl | ||
| position='topright' | ||
| onCreated={onCreatedHandler} | ||
| onEdited={onEditedHandler} | ||
| onDeleted={onDeletedHandler} | ||
| draw={{ | ||
| polyline: !!toolbar?.polyline, | ||
| polygon: !!toolbar?.polygon, | ||
| rectangle: !!toolbar?.rectangle, | ||
| circle: !!toolbar?.circle, | ||
| marker: !!toolbar?.marker, | ||
| circlemarker: false, | ||
| }} | ||
| /> | ||
| </FeatureGroup> | ||
| ); | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
src/layout/Map/features/geometries/editable/useLeafletDrawSpritesheetFix.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { useEffect } from 'react'; | ||
|
|
||
| import DrawSpriteSheetPng from 'leaflet-draw/dist/images/spritesheet.png'; | ||
| import DrawSpriteSheetPng2x from 'leaflet-draw/dist/images/spritesheet-2x.png'; | ||
|
|
||
| const STYLE_ID = 'leaflet-draw-spritesheet-override'; | ||
|
|
||
| /** | ||
| * Hook to fix leaflet-draw spritesheet paths by overriding the CSS with webpack-processed image URLs. | ||
| * This is needed because the default leaflet-draw.css references relative image paths that don't work | ||
| * after webpack processing. | ||
| */ | ||
| export function useLeafletDrawSpritesheetFix() { | ||
| useEffect(() => { | ||
| // Only inject the style once globally | ||
| if (document.getElementById(STYLE_ID)) { | ||
| return; | ||
| } | ||
|
|
||
| const style = document.createElement('style'); | ||
| style.id = STYLE_ID; | ||
| style.textContent = ` | ||
| .leaflet-draw-toolbar a { | ||
| background-image: url(${DrawSpriteSheetPng}) !important; | ||
| } | ||
| .leaflet-retina .leaflet-draw-toolbar a { | ||
| background-image: url(${DrawSpriteSheetPng2x}) !important; | ||
| } | ||
| `; | ||
| document.head.appendChild(style); | ||
| }, []); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.