in modules/layers/src/mode-handlers/split-polygon-handler.ts [17:63]
calculateGroundCoords(clickSequence: any, groundCoords: any) {
const modeConfig = this.getModeConfig();
if (!modeConfig || !modeConfig.lock90Degree || !clickSequence.length) {
return groundCoords;
}
if (clickSequence.length === 1) {
// if first point is clicked, then find closest polygon point and build ~90deg vector
const firstPoint = clickSequence[0];
const selectedGeometry = this.getSelectedGeometry();
// @ts-expect-error turf type diff
const feature = turfPolygonToLine(selectedGeometry);
const lines = feature.type === 'FeatureCollection' ? feature.features : [feature];
let minDistance = Number.MAX_SAFE_INTEGER;
let closestPoint = null;
// If Multipolygon, then we should find nearest polygon line and stick split to it.
lines.forEach((line) => {
const snapPoint = nearestPointOnLine(line, firstPoint);
const distanceFromOrigin = turfDistance(snapPoint, firstPoint);
if (minDistance > distanceFromOrigin) {
minDistance = distanceFromOrigin;
closestPoint = snapPoint;
}
});
if (closestPoint) {
// closest point is used as 90degree entry to the polygon
const lastBearing = turfBearing(firstPoint, closestPoint);
const currentDistance = turfDistance(firstPoint, groundCoords, { units: 'meters' });
return turfDestination(firstPoint, currentDistance, lastBearing, {
units: 'meters',
}).geometry.coordinates;
}
return groundCoords;
}
// Allow only 90 degree turns
const lastPoint = clickSequence[clickSequence.length - 1];
const [approximatePoint] = generatePointsParallelToLinePoints(
clickSequence[clickSequence.length - 2],
lastPoint,
groundCoords
);
// align point with current ground
const nearestPt = nearestPointOnLine(lineString([lastPoint, approximatePoint]), groundCoords)
.geometry.coordinates;
return nearestPt;
}