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

Pis*_*ean 14 java geometry jts computational-geometry

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

     Geometry g1 = new GeometryFactory().createLineString(coordinates);
Run Code Online (Sandbox Code Playgroud)

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

提前致谢.

bug*_*123 28

在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);
Run Code Online (Sandbox Code Playgroud)

  • JTS是2D.坐标对象具有第三个字段,但它始终是NaN.坐标不是JTS中的几何.点将是,它只有x和y. (2认同)
  • 是的,bugmenot123 是对的,使用当前版本的 JTS,您不需要创建一个 linearRing 来创建多边形,一个简单的 createPolygon 就足够了。请记住,坐标必须形成一个封闭的环(第一个点和最后一个点相同),否则您将得到一个例外。 (2认同)

Pis*_*ean 12

使用以下代码行:

 GeometryFactory fact = new GeometryFactory();
 LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
 Polygon poly = new Polygon(linear, null, fact);
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助:)

  • 我想知道为什么你需要2个GeometryFactories来制作一个多边形? (10认同)
  • 如果要设置特定的SRID,则必须以这种方式创建GeometryFactory:GeometryFactory fac = new GeometryFactory(new PrecisionModel(),_ scan_);,将_srid_替换为所需的SRID. (2认同)