4

I'm trying to create a polygon and calculate it's area using

    final GeometryFactory gf = new GeometryFactory();

    final ArrayList<Coordinate> points = new ArrayList<Coordinate>();
    points.add(new Coordinate(0,0));
    points.add(new Coordinate(0,1));
    points.add(new Coordinate(1,0));
    points.add(new Coordinate(1,1));
    points.add(new Coordinate(0,0));


    final Polygon polygon = gf.createPolygon(new LinearRing(new CoordinateArraySequence(points
        .toArray(new Coordinate[points.size()])), gf), null);

    System.out.println(polygon.getArea());

And I get the error:

 points must form a closed linestring

When I tried to use the points :

points.add(new Coordinate(0,0));
points.add(new Coordinate(0,1));
points.add(new Coordinate(1,0));
points.add(new Coordinate(1,1));
points.add(new Coordinate(0,0));

I've received area = 0 .

I'd like to know what I've got to do in order to calculate the area correctly without getting any errors.

0

1 Answer 1

8

In the conversion world, what you've built is known as a "bowtie".

As-built

If you really want that shape, you need to conform to topology rules by making a multipart polygon with "left hand rule" part vertices {0,0},{0.5,0.5},{0,1},{0,0} and {0.5,0.5},{1,0},{1,1},{0.5,0.5} (reverse the order [or just swap vertices 2 & 3 in each part] to generate "right hand rule" polygons). enter image description here

If you wanted a rectangle, then you need to order the vertices so that no line segment crosses another (in the direction appropriate to your software):

//Left hand rule 
points.add(new Coordinate(0,0));
points.add(new Coordinate(1,0));
points.add(new Coordinate(1,1));
points.add(new Coordinate(0,1));
points.add(new Coordinate(0,0));

//Right Hand Rule
points.add(new Coordinate(0,0));
points.add(new Coordinate(0,1));
points.add(new Coordinate(1,1));
points.add(new Coordinate(1,0));
points.add(new Coordinate(0,0));

LHR Rectangle

2
  • 1
    Right, and because the question is about jts, it does not care about the ring orientation and for jts POLYGON ((0 0,0 1,1 1,1 0,0 0)) is just as valid as POLYGON ((0 0,1 0,1 1,0 1,0 0)). But do not draw butterflies or hour glasses.
    – user30184
    Commented Aug 29, 2014 at 14:49
  • Thanks for the great answer and the detailed explanation !
    – itamar
    Commented Aug 29, 2014 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.