Ausgabe:
Teilen Sie ein Polygon in genau zwei gültige Polygone mithilfe eines LineStrings, der sie schneidet.
Mein Code:
Code: Select all
dividePolygon() {
try {
const featureGroup = new L.FeatureGroup(this.layers);
this.map.addLayer(featureGroup);
this.selectedPlots.forEach(plot => {
featureGroup.addLayer(plot);
});
const drawControl = new L.Control.Draw({
edit: {
featureGroup: featureGroup,
},
draw: {
polyline: {
shapeOptions: {
color: 'red',
weight: 4,
},
},
},
});
this.map.addControl(drawControl);
this.map.on(L.Draw.Event.CREATED, (e: any) => {
const drawnLineLayer = e.layer as L.Polyline;
featureGroup.eachLayer((layer: L.Layer) => {
const plotGeoJSON = (layer as L.Polygon).toGeoJSON() as Feature
;
const drawnLineGeoJSON = drawnLineLayer.toGeoJSON() as Feature;
const intersectionPoints = turf.lineIntersect(drawnLineGeoJSON, plotGeoJSON);
if (intersectionPoints.features.length > 0) {
console.log("Original Polygon Coordinates:", plotGeoJSON.geometry.coordinates);
console.log("Intersection Points: ", intersectionPoints);
// Slice the line segment inside the polygon
const slicedLine = turf.lineSlice(
turf.point(intersectionPoints.features[0].geometry.coordinates),
turf.point(intersectionPoints.features[1].geometry.coordinates),
drawnLineGeoJSON
);
console.log("Sliced Line Coordinates:", slicedLine);
const splitPolygons = this.splitPolygonWithLine(plotGeoJSON, slicedLine);
if (splitPolygons && splitPolygons.length === 2) {
// Updated polygons on the map
const newPlot1Layer = L.geoJSON(splitPolygons[0]).addTo(this.map);
const newPlot2Layer = L.geoJSON(splitPolygons[1]).addTo(this.map);
// Remove the original polygon from the map
this.map.removeLayer(layer);
} else {
console.error('Error: The polygon was not split into exactly two polygons.');
}
}
});
});
} catch (error) {
console.error('Error while dividing the polygon:', error);
}
}
splitPolygonWithLine(polygon: Feature, line: Feature): Feature[] | null {
if (!turf.booleanIntersects(line, polygon)) {
console.error("Error: Line does not intersect the polygon.");
return null;
}
try {
const flippedPolygon = turf.flip(polygon);
const flippedLine = turf.flip(line);
const polygonBoundary = turf.polygonToLine(flippedPolygon) as Feature;
// Split the polygon boundary using the slicing line
const splitLines = turf.lineSplit(polygonBoundary, flippedLine);
if (splitLines.features.length < 2) {
console.error("Error: Unable to split the polygon boundary into two parts.");
return null;
}
// Combine split lines with the slicing line to form the full boundary
const combinedLines = turf.featureCollection([
...splitLines.features,
flippedLine,
]);
const splitPolygons = turf.polygonize(combinedLines);
// Ensure polygons are closed loops
splitPolygons.features.forEach(poly => {
const coords = poly.geometry.coordinates[0];
if (coords[0][0] !== coords[coords.length - 1][0] ||
coords[0][1] !== coords[coords.length - 1][1]) {
coords.push(coords[0]); // Close the loop if necessary
}
});
const validPolygons = splitPolygons.features.filter(poly => {
const area = turf.area(poly);
return area > 0 && turf.booleanWithin(turf.centroid(poly), flippedPolygon);
});
// Ensure we have exactly two valid polygons
if (validPolygons.length === 2) {
return validPolygons.map(poly => turf.flip(poly));
} else {
console.error("Error: Did not generate exactly two valid polygons. Found:", validPolygons.length);
return null;
}
} catch (error) {
console.error("Error during polygon split:", error);
return null;
}
}
Nachdem ich die Teilung durchgeführt und die Methode turf.polygonize ausgeführt habe, erhalte ich nur ein Polygon statt der erwarteten zwei. Der Fehler, auf den ich stoße, ist:
Code: Select all
Error: Did not generate exactly two valid polygons. Found: 1