Skip to content

Commit

Permalink
feat: export feature cutouts
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Jul 25, 2024
1 parent d5d7a4a commit 7af6a23
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
18 changes: 17 additions & 1 deletion client/src/pages/map/popups/Polygon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ import {
splitMultiPolygons,
} from '@services/utils'
import { useImportExport } from '@hooks/useImportExport'
import { filterPoints, filterPolys } from '@services/geoUtils'
import {
filterPoints,
filterPolys,
getFeatureCutouts,
} from '@services/geoUtils'
import { usePersist } from '@hooks/usePersist'

const { add, remove, updateProperty } = useShapes.getState().setters
Expand Down Expand Up @@ -312,6 +316,18 @@ export function PolygonPopup({
>
Export
</MenuItem>
<MenuItem
dense
onClick={() => {
useImportExport.setState({
feature: getFeatureCutouts(feature),
open: 'exportPolygon',
})
handleClose()
}}
>
Export Cutouts
</MenuItem>
<MenuItem
dense
onClick={() => {
Expand Down
40 changes: 40 additions & 0 deletions client/src/services/geoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,43 @@ export function filterPolys(
remove(value.geometry.type, key)
})
}

export function getFeatureCutouts(
feature: Feature<Polygon | MultiPolygon>,
): Feature {
const polygons: Polygon[] = []

if (feature.geometry.type === 'Polygon') {
feature.geometry.coordinates.forEach((polygon, i) => {
if (i > 0) {
polygons.push({
type: 'Polygon',
coordinates: [polygon],
})
}
})
} else if (feature.geometry.type === 'MultiPolygon') {
feature.geometry.coordinates.forEach((multi) => {
multi.forEach((polygon, i) => {
if (i > 0) {
polygons.push({
type: 'Polygon',
coordinates: [polygon],
})
}
})
})
}
return {
type: 'Feature',
id: `${feature.id}-cutouts`,
properties: feature.properties,
geometry:
polygons.length > 1
? {
type: 'MultiPolygon',
coordinates: polygons.map((p) => p.coordinates),
}
: polygons[0],
}
}

0 comments on commit 7af6a23

Please sign in to comment.