org.geotools.geometry.jts.JTS.makeValid()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(2.0k)|赞(0)|评价(0)|浏览(678)

本文整理了Java中org.geotools.geometry.jts.JTS.makeValid()方法的一些代码示例,展示了JTS.makeValid()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JTS.makeValid()方法的具体详情如下:
包路径:org.geotools.geometry.jts.JTS
类名称:JTS
方法名:makeValid

JTS.makeValid介绍

[英]Given a potentially invalid polygon it rebuilds it as a list of valid polygons, eventually removing the holes
[中]给定一个可能无效的多边形,它会将其重建为有效多边形的列表,最终删除孔

代码示例

代码示例来源:origin: geotools/geotools

/**
 * If validation is requested, scan the geometries and build valid polygons (in case they
 * aren't) by also removing holes.
 *
 * @param geometriesList
 * @return
 */
private List<Polygon> validateGeometries(List<Polygon> geometriesList) {
  if (forceValid && (geometriesList.size() > 0)) {
    List<Polygon> validated = new ArrayList<Polygon>(geometriesList.size());
    for (int i = 0; i < geometriesList.size(); i++) {
      Polygon polygon = geometriesList.get(i);
      if (!polygon.isValid()) {
        List<Polygon> validPolygons = JTS.makeValid(polygon, true);
        validated.addAll(validPolygons);
      } else {
        validated.add(polygon);
      }
    }
    geometriesList = validated;
  }
  return geometriesList;
}

代码示例来源:origin: geotools/geotools

@Test
  public void testMakeValid() throws Exception {
    // An invalid polygon similar to this one
    //
    // *----*
    // |    |
    // *----*----*
    //      |    |
    //      *----*
    //
    // Will be split into 2 separate polygons through the makeValid method
    final int[] xPoints = {0, 5, 5, 5, 10, 10, 5, 0};
    final int[] yPoints = {0, 0, 5, 10, 10, 5, 5, 5};
    final int nPoints = xPoints.length;

    final Shape shape = new java.awt.Polygon(xPoints, yPoints, nPoints);
    final LinearRing geom = (LinearRing) JTS.toGeometry(shape);
    final GeometryFactory factory = new GeometryFactory();
    final org.locationtech.jts.geom.Polygon polygon = factory.createPolygon(geom);
    assertFalse(polygon.isValid());

    final List<org.locationtech.jts.geom.Polygon> validPols = JTS.makeValid(polygon, false);

    assertEquals(2, validPols.size());
    org.locationtech.jts.geom.Polygon polygon1 = validPols.get(0);
    org.locationtech.jts.geom.Polygon polygon2 = validPols.get(1);
    assertEquals(5, polygon1.getNumPoints());
    assertEquals(5, polygon2.getNumPoints());
  }
}

相关文章