I have a series of points I would like to connect into a polygon but it isn't drawing correctly, You'll see the problem.

Coordinate[] xyarray = xycoords.toCoordinateArray();                                                                    

LinearRing shell = geomFactory.createLinearRing(xyarray);                                                               
Polygon poly = geomFactory.createPolygon(shell, new LinearRing[0]);                                                     

Geometry perimeterline = geomFactory.createPolygon(xyarray);                                                            
GeometryCollector xypoints = new GeometryCollector();                                                                   
GeometryCollection xygeoms = xypoints.collect();                                                                        

File xyshp = new File("perimeterline.shp");                                                                             
File xyshx = new File("perimeterline.shx");                                                                             
FileOutputStream xysshp = new FileOutputStream(xyshp);                                                                  
FileOutputStream xysshx = new FileOutputStream(xyshx);                                                                  
ShapefileWriter xywriter = new ShapefileWriter(xysshp.getChannel(), xysshx.getChannel());                               
xywriter.write(xygeoms, ShapeType.POLYGON);  

enter image description here

  • Without knowing what is in xycoords it's hard to tell what is up, but why do you collect the polygon? and why are you using the inner workings of shapefile writer rather than a ShapefileDatastore?
    – Ian Turton
    Commented Apr 8, 2020 at 12:36
  • 1
    A polygon is defined by an ordered set of vertices that walk the perimeter of the figure. You don't have a polygon in the vertices, so the result is a mess. It seems as if you could point that vertex stream to a convex hull generator, and then you'd get a polygon.
    – Vince
    Commented Apr 8, 2020 at 13:18
  • Ok I'll try that, thanks Commented Apr 8, 2020 at 14:26

1 Answer 1


In the end I took the centroid of the bounding box and calculated the azimuth of every point to sort the coordinates and it then drew in order.

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.