【GeoTools】polygon 转 multipolygon

使用GeoTools写入shape文件时,由于结果集中包含polygon和multipolygon两种类型,“the_geom”字段不好指定。使用如下方法可以把polygon转为multipolygon。

        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
        WKTReader reader = new WKTReader( geometryFactory );
        Polygon p = (Polygon) reader.read("POLYGON ((115.05948119466 22.8626134301296, 115.06225289824 22.8626134301296, 115.06225289824 22.8523347587268, 115.05925618009766 22.85237940437907, 115.05948119466 22.8626134301296))");
        GeometryFactory gf = new GeometryFactory();
        MultiPolygon mPoly = null;
        if (p instanceof Polygon){
            Polygon[] polys = new Polygon[1];
            polys[0] = p;
            mPoly = gf.createMultiPolygon(polys);
        }
        System.out.println(mPoly);

参考文章

1,jts convert single polygon to multipolygon

https://stackoverflow.com/questions/54946097/jts-convert-single-polygon-to-multipolygon

 

你可能感兴趣的:(GeoTools)