George George - 1 year ago 102
Java Question

create polygon from array list points - coordinate sequence?

I have an array list with coordinates:

List<Coordinate> coords;


I want to create a polygon based on these values.

I am trying:

GeometryFactory geometryFactory = new GeometryFactory();
Polygon polyg = geometryFactory.createPolygon(coords);


but it shows that it wants CoordinateSequence:

The method createPolygon(CoordinateSequence) in the type GeometryFactory is not applicable for the arguments (List<Coordinate>)


If I try to create a CoordinateSequence it shows a bucnh of methods and I am not sure how to proceed (or if sequence is needed anyway).

Answer Source

You can use an array of points too.

See http://docs.geotools.org/stable/userguide/library/jts/geometry.html for an example.

Here is some sample code:

  ArrayList<Coordinate> points = new ArrayList<Coordinate>();
  points.add(new Coordinate(longitude, latitude));
  ...  
  points.add(new Coordinate(lon, lat));
  ...
  //make sure to close the linear ring
  points.add(new Coordinate(longitude, latitude));
  poly = geometryFactory.createPolygon((Coordinate[]) points.toArray(new Coordinate[] {}));
  valid = poly.isValid();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download