Problem beim Teilen eines Polygons in zwei Teile mithilfe von LineString mit Turf.jsJavaScript

Javascript-Forum
Guest
 Problem beim Teilen eines Polygons in zwei Teile mithilfe von LineString mit Turf.js

Post by Guest »

Ich arbeite an einem Projekt, bei dem ich ein Polygon mithilfe eines LineStrings in zwei verschiedene Polygone aufteilen muss. Ich verwende Turf.js, um geometrische Operationen wie Schnittmenge, Teilung und Polygonisierung durchzuführen. Obwohl sichergestellt wird, dass die Eingabegeometrien korrekt erscheinen, kann die Funktion turf.polygonize keine zwei gültigen Polygone generieren und gibt nur eines aus. Nachfolgend finden Sie die Herausforderung und den Code, den ich implementiert habe. Unten ist der Code, den ich bisher implementiert habe, und die Ausgabe, die ich erreichen möchte.
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;
}
}
Hauptproblem:
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
Auch wenn die Schnittpunkte und Teilungslinien korrekt erscheinen, führt der letzte Schritt des Kombinierens und Polygonisierens der Linien nicht zum gewünschten Ergebnis.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post