Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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();
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
function isPolyInPoly(feature1: Polygon, feature2: Polygon) {
for (const coord1 of feature1.coordinates[0]) {
if (booleanPointInPolygon(coord1, feature2)) {
return true;
}
}
for (const coord2 of feature2.coordinates[0]) {
if (booleanPointInPolygon(coord2, feature1)) {
return true;
}
}
const doLinesIntersect = lineIntersect(polygonToLine(feature1), polygonToLine(feature2));
if (doLinesIntersect.features.length > 0) {
return true;
}
return false;
}
function isLineInPoly(polygon: Polygon, lineString: LineString) {
for (const coord of lineString.coordinates) {
if (booleanPointInPolygon(coord, polygon)) {
return true;
}
}
const doLinesIntersect = lineIntersect(lineString, polygonToLine(polygon));
if (doLinesIntersect.features.length > 0) {
return true;
}
return false;
}