in modules/layers/src/mode-handlers/split-polygon-handler.ts [124:176]
splitPolygon() {
const selectedGeometry = this.getSelectedGeometry();
const tentativeFeature = this.getTentativeFeature();
const featureIndex = this.getSelectedFeatureIndexes()[0];
const modeConfig = this.getModeConfig() || {};
// Default gap in between the polygon
let { gap = 0.1, units = 'centimeters' } = modeConfig;
if (gap === 0) {
gap = 0.1;
units = 'centimeters';
}
// @ts-expect-error turf type diff
const buffer = turfBuffer(tentativeFeature, gap, { units });
// @ts-expect-error turf type diff
const updatedGeometry = turfDifference(selectedGeometry, buffer);
this._setTentativeFeature(null);
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) => {
agg.push([p]);
});
return agg;
}, [] as any[]);
}
// Update the type to Mulitpolygon
const updatedData = this.getImmutableFeatureCollection().replaceGeometry(featureIndex, {
type: 'MultiPolygon',
coordinates: updatedCoordinates,
});
const editAction: EditAction = {
updatedData: updatedData.getObject(),
editType: 'split',
featureIndexes: [featureIndex],
editContext: null,
};
return editAction;
}