【问题标题】:Geotools calculate intersection area between two geometries neededGeotools 计算所需的两个几何图形之间的相交面积
【发布时间】:2018-07-13 10:07:01
【问题描述】:

我是 Geotools 的新手,我创建了两个几何图形(例如两个多边形),我想计算其中一个几何图形的交集区域的百分比。

    //First polygon
        GeometryFactory geometryFactory1 = JTSFactoryFinder.getGeometryFactory();

        Coordinate[] coords1  =
           new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2),
                             new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) };

        LinearRing ring1 = geometryFactory.createLinearRing( coords );
        LinearRing holes[] = null; 
        Polygon polygon1 = geometryFactory.createPolygon(ring1, holes );
    // Second polygon
        GeometryFactory geometryFactory2 = JTSFactoryFinder.getGeometryFactory();

        Coordinate[] coords2  =
           new Coordinate[] {new Coordinate(2, 0), new Coordinate(2, 2),
                             new Coordinate(1, 1), new Coordinate(4, 2), new Coordinate(2, 0) };

        LinearRing ring2 = geometryFactory.createLinearRing( coords );
        LinearRing holes[] = null; 
        Polygon polygon2 = geometryFactory.createPolygon(ring2, holes );
    // test if polygon2 is inside polygon1
        boolean test = polygon1.contains(polygon2);

有人知道如何计算多边形 1(或圆)内多边形 2 的百分比吗?有什么算法可以计算几何之间的交集面积吗?

【问题讨论】:

    标签: java geometry polygon geotools


    【解决方案1】:

    您需要计算交点,然后计算其面积,最后计算比率

    Geometry intersect = polygon1.intersection(polygon2);
    double areaRatio = 100.0*intersect.getArea() / polygon2.getArea();
    
    System.out.println("ratio: "+areaRatio + "%");
    

    话虽如此,您需要在计算交点之前使用polygon1.isValid()polygon2.isValid() 确保几何图形有效。 polygon2 的样本数据是自相交的,因此相交操作失败并显示

    com.vividsolutions.jts.geom.TopologyException: 发现非节点 LINESTRING ( 2.0 0.0, 2.0 2.0 ) 和 LINESTRING ( 1.0 1.0, 2.5 1.5 ) [ (2.0, 1.3333333333333333, NaN) ]

    【讨论】:

      猜你喜欢
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2015-01-25
      • 2016-08-16
      • 2021-08-15
      • 2015-07-29
      • 1970-01-01
      相关资源
      最近更新 更多