GeoTools使用--jts处理线的自相交问题

jts处理线的自相交问题

  做图形处理的时候,经常遇到面的边线有自相交的交点,当进行面面合并、拆分时就会报错:
com.vividsolutions.jts.geom.TopologyException: side location conflict [ (1.3808092156632675E7, 4827228.3856722275, NaN) ]

这里使用面的边线进行自相交,获取线的所有点(包含相交点),然后在重新生成多边形面
处理前的图形:
GeoTools使用--jts处理线的自相交问题_第1张图片

处理代码:

 public static void main(String[] args) {
        String geometryStr="POLYGON ((13798650 4826450, 13812600 4827600, 13798100 4813550, 13802450 4813800, 13807600 4814350, 13804700 4813000, 13811400 4812900, 13813400 4817500, 13810000 4818350, 13813400 4813900, 13811750 4820300, 13807500 4828350, 13805000 4828000, 13802750 4827650, 13798650 4826450))";
        Geometry geometry1 = JTSNewUtil.geometry(geometryStr);
        if(geometry1 instanceof MultiPolygon){
            int numGeometries = geometry1.getNumGeometries();
            List<Polygon> polygons=new ArrayList<>();
            for(int i=0;i<numGeometries;i++){
                Geometry geometryN = geometry1.getGeometryN(i);
                Polygon polygon = getPolygonByInteriorPoint((Polygon) geometryN);
                polygons.add(polygon);
            }
            geometry1=geometry1.getFactory().createMultiPolygon(polygons.toArray(new Polygon[]{}));
        }else if(geometry1 instanceof  Polygon){
            geometry1 = getPolygonByInteriorPoint((Polygon) geometry1);
        }
        System.out.println(geometry1.toText());
    }

	/**
     * 使用jts的两线相交方法解决线的自相交问题
     * @param geometry1
     * @return
     */
    private static Polygon getPolygonByInteriorPoint(Polygon geometry1) {
        LineString exteriorRing = geometry1.getExteriorRing();
        Coordinate[] coordinates = exteriorRing.union(exteriorRing).getCoordinates();
        return geometry1.getFactory().createPolygon(coordinates);
    }

处理后图形:
GeoTools使用--jts处理线的自相交问题_第2张图片

你可能感兴趣的:(java,geotools,jts,geotool)