Free Support Forum - aspose.com

Unable to create a valid polygon by excluding invalid points

Dear Aspose Support

We are continuing to experience a problem when using the overlap method -
polygon1.Overlaps(polygon2) throws an exception.

We now test the IsValid() flag of each polygon prior to calling the Overlaps() method. Whilst this avoids the exception, nevertheless it leaves us with the problem of having managed to create an invalid polygon in the first place.

As a potential solution we attempted to create a polygon on a point by point basis and test the validity of the polygon after the addition of each new point, see code snippet below.


    // We test each point prior to adding it to the listOfValidPoints to see if it would result in
    // an invalid polygon, and if it does, we exclude the point otherwise we include it.
    // Returns true if valid and false if not.
    private bool CheckWhetherPolygonIsValid(Point nextPoint)
    {
        // Note: points is a temporary list, listOfValidPoints only contains valid points
        List<Point> points = new List<Point>(listOfValidPoints);
        if (nextPoint.X != endPoint.X && nextPoint.Y != endPoint.Y)
        {
            points.Add(nextPoint);
        }
        points.Add(endPoint);
        if (points.Count >= 4) // We need 4 for a simple triangle because the start point and end point are the same
        {
            LinearRing ring = new LinearRing(points);
            Polygon shape = new Polygon(ring);
            if (!shape.IsValid)
            {
                var x = nextPoint;  // breakpoint for debugger
            }
            else
            {
                var x = nextPoint;  // breakpoint for debugger
            }
            return shape.IsValid;
        }
        else
        {
            return true;
        }
    }

This seems to work in as much that it excludes points from the RingCoverage which would result in the formation of an invalid polygon. However, we have noticed (anecdotally but not conclusively) that after detecting a point which would make the polygon invalid, all subsequent points also seem to make the polygon invalid.

I was expecting to be able to subsequently call the Overlaps method on the resultant valid polygon. However it seems that once the first invalid point has been detected, it is not possible to form a valid polygon simply by adding further valid points.

Thanks

Steve

Hello, @steve.cooper!

Thank you for sharing the code. This seems, the “listOfValidPoints” can be a degenerate triangle. Or some invalid point may appear in the list before you do the check.

Please, consider the following code to add more validations:

    // re-check list of valid points
    var ringPoints = new List<Point>(listOfValidPoints);
    ringPoints.Add(listOfValidPoints.First());
    Polygon validShape = new Polygon(new LinearRing(ringPoints));
    var okValidPints = validShape.IsValid;
    Console.WriteLine(okValidPints);

    // re-check end point
    var okEndPoint = endPoint == listOfValidPoints.First();
    Console.WriteLine(okEndPoint);

Thanks