How to create a polygon in JTS when we have list of coordinate?

Piscean picture Piscean · Jul 4, 2011 · Viewed 27.7k times · Source

We can create a LineString using coordinates list like this:

     Geometry g1 = new GeometryFactory().createLineString(coordinates);

How can we create a polygon using coordinates list?

Thanks in advance.

Answer

bugmenot123 picture bugmenot123 · Apr 24, 2015

The accepted answer might have still been valid (still awkward) in 2012 but nowadays you should really do it simply like this:

// Create a GeometryFactory if you don't have one already
GeometryFactory geometryFactory = new GeometryFactory();

// Simply pass an array of Coordinate or a CoordinateSequence to its method
Polygon polygonFromCoordinates = geometryFactory.createPolygon(coordinates);