Skip to content
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

Merged
merged 9 commits into from
Mar 25, 2024

Conversation

AbraaoAlves
Copy link
Contributor

With these tests we can see polyunion failing in some cases, for example:

  • for circles polygons with 17 or more steps or lower than 16

@juanpujol
Copy link
Owner

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));

@juanpujol juanpujol self-requested a review March 22, 2024 12:51
@juanpujol
Copy link
Owner

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?

@juanpujol juanpujol added this to the v0.1.3 milestone Mar 22, 2024
Comment on lines +91 to +92
mergeCandidate = union(mergeCandidate, targetFeature) as Feature<Polygon>;
processed.add(candidate.id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
}

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.

@lamartinecabral
Copy link

I've added a try catch block to avoid throwing and console.error instead.

@juanpujol I found out that another way to avoid throwing is to reduce the precision of the coordinates. Something like +value.toFixed(13) should be enough

@juanpujol
Copy link
Owner

@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.

@juanpujol juanpujol merged commit b426e87 into juanpujol:develop Mar 25, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants