Java 当我们有坐标列表时,如何在 JTS 中创建多边形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6570017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:49:47  来源:igfitidea点击:

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

javageometrycomputational-geometryjts

提问by Piscean

We can create a LineString using coordinates list like this:

我们可以使用坐标列表创建一个 LineString,如下所示:

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

How can we create a polygon using coordinates list?

我们如何使用坐标列表创建多边形?

Thanks in advance.

提前致谢。

采纳答案by Piscean

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

我希望它会有所帮助:)

回答by Chetan Gole

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

你看过他们的文档吗?看看 - 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.

我认为这是非常直接的。我希望这能解决你的问题。

回答by bugmenot123

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

接受的答案在 2012 年可能仍然有效(仍然很尴尬),但现在你真的应该这样做:

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