in modules/edit-modes/src/lib/split-polygon-mode.ts [148:200]
splitPolygon(tentativeFeature: TentativeFeature, props: ModeProps<FeatureCollection>) {
const selectedGeometry = this.getSelectedGeometry(props);
const featureIndex = props.selectedIndexes[0];
const modeConfig = props.modeConfig || {};
// Default gap in between the polygon
let { gap = 0.1, units = 'centimeters' } = modeConfig;
if (gap === 0) {
gap = 0.1;
units = 'centimeters';
}
const buffer = turfBuffer(tentativeFeature, gap, { units });
// @ts-expect-error turf types diff
const updatedGeometry = turfDifference(selectedGeometry, buffer);
if (!updatedGeometry) {
// eslint-disable-next-line no-console,no-undef
console.warn('Canceling edit. Split Polygon erased');
return null;
}
const { type, coordinates } = updatedGeometry.geometry;
let updatedCoordinates = [];
if (type === 'Polygon') {
// Update the coordinates as per Multipolygon
updatedCoordinates = coordinates.map((c) => [c]);
} else {
// Handle Case when Multipolygon has holes
updatedCoordinates = coordinates.reduce((agg, prev) => {
prev.forEach((p) => {
// @ts-expect-error revisit coordinates type here
agg.push([p]);
});
return agg;
}, []);
}
// Update the type to Mulitpolygon
const updatedData = new ImmutableFeatureCollection(props.data).replaceGeometry(featureIndex, {
type: 'MultiPolygon',
coordinates: updatedCoordinates,
});
const editAction: GeoJsonEditAction = {
updatedData: updatedData.getObject(),
editType: 'split',
editContext: {
featureIndexes: [featureIndex],
},
};
return editAction;
}