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

Invalid number of points in LinearRing #511

Closed
Ryszard-Trojnacki opened this issue Oct 11, 2023 · 2 comments
Closed

Invalid number of points in LinearRing #511

Ryszard-Trojnacki opened this issue Oct 11, 2023 · 2 comments

Comments

@Ryszard-Trojnacki
Copy link

I'm trying to create LinearRing (Polygon) from array of CoordinatesXY but I get exception:
IllegalArgumentException: Invalid number of points in LinearRing (found 3 - must be 0 or >= 4)

Why there is a minimum of 4 points? Isn't triangle a valid LinearRing?

Another thing - Java implementation (JTS) requires only 3 points.

JavaScript constant definition and validate method:

LinearRing.MINIMUM_VALID_SIZE = 4

validateConstruction() {
    if (!this.isEmpty() && !super.isClosed.call(this)) 
      throw new IllegalArgumentException('Points of LinearRing do not form a closed linestring')
    
    if (this.getCoordinateSequence().size() >= 1 && this.getCoordinateSequence().size() < LinearRing.MINIMUM_VALID_SIZE) 
      throw new IllegalArgumentException('Invalid number of points in LinearRing (found ' + this.getCoordinateSequence().size() + ' - must be 0 or >= 4)')
    
  }

Java constant definition and validate method:

public class LinearRing extends LineString {
  public static final int MINIMUM_VALID_SIZE = 3;

  private void validateConstruction() {
    if (!isEmpty() && ! super.isClosed()) {
      throw new IllegalArgumentException("Points of LinearRing do not form a closed linestring");
    }
    if (getCoordinateSequence().size() >= 1 && getCoordinateSequence().size() < MINIMUM_VALID_SIZE) {
      throw new IllegalArgumentException("Invalid number of points in LinearRing (found "
      		+ getCoordinateSequence().size() + " - must be 0 or >= " + MINIMUM_VALID_SIZE + ")");
    }
  }
}

Shouldn't this be changed to 3 like in Java code?

@Ryszard-Trojnacki
Copy link
Author

Ryszard-Trojnacki commented Oct 11, 2023

I see that in Java this code was changed from 4 to 3.

My workaround is to add this to override minimum valid size:

LinearRing.MINIMUM_VALID_SIZE=3;

@bjornharrtell
Copy link
Owner

@Ryszard-Trojnacki I guess the definition is that it needs to be a closed ring in which case a triangle would be defined by 4 points. As seen in the pull request in JTS it was lowered with the intent to be able to create invalid rings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants