简体   繁体   中英

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

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.

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);

Use these line of codes:

 GeometryFactory fact = new GeometryFactory();
 LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
 Polygon poly = new Polygon(linear, null, fact);

I hope it will help :)

Have you seen their documentation ? Take a look - http://www.vividsolutions.com/jts/javadoc/com/vividsolutions/jts/geom/Polygon.html

I think this is very much straight forward. I hope this will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM