zl程序教程

您现在的位置是:首页 >  其他

当前栏目

geotools外接矩形

2023-04-18 15:39:40 时间

geom.getEnvelope() 得到外接矩形,不一定是面积最小;可以对多边形的每一条边求外接矩形,然后比较得到最小外接矩形

参考:https://blog.csdn.net/qq_40985985/article/details/127034254

ArrayList<Polygon> polys = new ArrayList<Polygon>();
        Polygon bbox = (Polygon) geom.getEnvelope();
        polys.add(bbox);
        geoms.add(geom.getEnvelope());
        List<Geometry> geoms = new ArrayList<Geometry>();
        //Polygon poly = null;
        Geometry  polyUnion = null;
        int i=0;
        while(itr.hasNext()){
            SimpleFeature sf = itr.next();
            //MultiPolygon geom = (MultiPolygon)sf.getDefaultGeometry();
            Geometry geom = (Geometry) sf.getDefaultGeometry();
            Polygon bbox = (Polygon) geom.getEnvelope();
            //polys.add(bbox);
            geoms.add(geom.getEnvelope());
            if(i==0){
                polyUnion = bbox
            }else {
                polyUnion = polyUnion.union(bbox);
            }
            i++;
        }
        itr.close();
//
shpReadWrite.CreateNewShapeFile

求并集。。

为什么要这么迂回呢?为什么不直接对原始多边形求并集,然后求外接矩形。。

        List<Geometry> geoms = new ArrayList<Geometry>();
        Geometry  polyUnion = null;
        int i=0;
        while(itr.hasNext()){
            SimpleFeature sf = itr.next();
            Geometry geom = (Geometry) sf.getDefaultGeometry();
            if(i==0){
                Geometry geom2 = geom.intersection(geom);//避免自交
                polyUnion = geom2;
            }else {
                Geometry geom2 = geom.intersection(geom);//避免自交
                polyUnion = polyUnion.union(geom2);
            }
            i++;
        }
        itr.close();

 s.geom.TopologyException: found non-noded intersection between LINESTRING........geotools空间分析存在自相交情况时的错误解决:先和自己自交,再求并集。。

>>更简单的办法:

SimpleFeatureCollection fc = contentFeatureSource.getFeatures();

double minX = fc.getBounds().getMinX();

...