-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing Merging circles polyunion #10
Conversation
Thanks @AbraaoAlves will see if I can test and fix polygons before merging. Here's a quick idea: interface GeoJSONFeature {
type: string;
geometry: {
type: string;
coordinates: number[][][]; // For Polygon
};
}
// Example GeoJSON data
const exampleGeoJSON: GeoJSONFeature = {
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0] // Closing point should be the same as the starting point
]
]
}
};
function isPolygonClosed(coordinates: number[][][]): boolean {
const firstPoint = coordinates[0][0];
const lastPoint = coordinates[0][coordinates[0].length - 1];
// Check if they are the same
return firstPoint[0] === lastPoint[0] && firstPoint[1] === lastPoint[1];
}
function fixClosedPolygon(feature: GeoJSONFeature): void {
if (!isPolygonClosed(feature.geometry.coordinates)) {
const firstPoint = feature.geometry.coordinates[0][0];
feature.geometry.coordinates[0].push(firstPoint); // Append first point to close the polygon
console.log("Polygon was not closed. Fixed.");
} else {
console.log("Polygon is already closed.");
}
}
// Testing
console.log("Before fixing:");
console.log(JSON.stringify(exampleGeoJSON, null, 2));
fixClosedPolygon(exampleGeoJSON);
console.log("After fixing:");
console.log(JSON.stringify(exampleGeoJSON, null, 2)); |
Ok, so I found the problem. It turns out the intersect check was throwing an error. But, if this fails doesn't mean it won't work, we should skip the merge for that instance. I've added a try catch block to avoid throwing and console.error instead. Also, change the directory structure and simplified the tests. @AbraaoAlves what do you think? |
mergeCandidate = union(mergeCandidate, targetFeature) as Feature<Polygon>; | ||
processed.add(candidate.id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mergeCandidate = union(mergeCandidate, targetFeature) as Feature<Polygon>; | |
processed.add(candidate.id); | |
const polygon = union(mergeCandidate, targetFeature) as Feature<Polygon>; | |
if (polygon.geometry.type === 'Polygon') { | |
mergeCandidate = polygon; | |
processed.add(candidate.id); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found out that when two polygons touch in a vertex, turf does not merge them. Instead, it returns a MultiPolygon.
@juanpujol I found out that another way to avoid throwing is to reduce the precision of the coordinates. Something like |
@lamartinecabral, if you have an improvement feel free to create an issue with a new PR. Right now, this PR fixes the main issue we had to deploy the lib to production. This works for me and if it works for you, please approve it and we can make it better on the next patch. Thank you. |
With these tests we can see polyunion failing in some cases, for example: