jts 将单个多边形转换为多边形

jts convert single polygon to multipolygon

大家好,我正在使用带有 geotools 快照 21 的 JTS 1.15:

我编写了一个以多边形集合作为输入参数的方法,输出方法是多多边形几何体。这工作正常,无一例外: 我还有一个联合标志,如果重叠或接触,它将联合我的多边形。如果现在所有多边形都重叠,则只会返回一个多边形。但是之后就会出现ClassCastException。

无法将单个多边形转换为多边形

有没有简单的方法可以将单个多边形转换为多边形? 我找不到几何 jts uml 图,但我发现了 Polygons 使用 Polygonal 接口扩展了 Geometry class Multipolygon 扩展了 GeometryCollection,它扩展了 Geometry 并且还具有 Polygonal 接口。

JTS Geometry Jacadoc中提到了以下内容: 叠加方法

The overlay methods return the most specific class possible to represent the result. If the result is homogeneous, a Point, LineString, or Polygon will be returned if the result contains a single element; otherwise, a MultiPoint, MultiLineString, or MultiPolygon will be returned. If the result is heterogeneous a GeometryCollection will be returned.

我写了一个简单的字符串操纵器方法来更改我的 wkt 字符串,但是还有其他方法可以实现这个目标吗?

这是我的方法

public static MultiPolygon createSPMultiPolygon(Collection<Polygon> polygons, boolean dissolveResult) {


    // if emty collection is returned
    if (polygons.size() == 0){
        //emty collection returns emty MultiPolygon

        GeometryFactory geomFactory = new GeometryFactory();
        MultiPolygon fail = geomFactory.createMultiPolygon();

        return fail;
    }

    //it will be assumed that all polygons have the same epsg code

    int epsgCode = polygons.iterator().next().getSRID();
    prec = new PrecisionModel(1000000)
    //creates the factory object
    GeometryFactory geomFactory = new GeometryFactory(prec, epsgCode);

    MultiPolygon result = null;


    // main task

    result = (MultiPolygon) geomFactory.buildGeometry(polygons);


        //mergedOptions
        if (dissolveResult) {

            try {
                result = (MultiPolygon) result.union();
                // there will be an exception if result or merge is a single polygon that polygon cant be cast to Multipolygon
            } catch (ClassCastException e) {

                // DO SOMETHING ELSE
            }

        }

    }

我正在使用以下 Maven 包:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>21-SNAPSHOT</geotools.version>
</properties>
<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-geometry</artifactId>
  <version>${geotools.version}</version>
</dependency>
<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-epsg-hsql</artifactId>
  <version>${geotools.version}</version>
</dependency>
<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-epsg-wkt </artifactId>
  <version>${geotools.version}</version>
</dependency>

我正在使用以下示例数据和辅助函数:

    String touches1_wkt = "POLYGON ((7.896407750956972 49.73405361813658, 8.14886306623246 49.73405361813658, 8.14886306623246 49.73405361813658, 8.14886306623246 49.46768344296402, 7.902371262341433 49.46569560583586, 7.896407750956972 49.73405361813658))";
    String touches2_wkt = "POLYGON ((8.14886306623246 49.61478339044737, 8.39137919586718 49.616771227575526, 8.39137919586718 49.616771227575526, 8.399330544379795 49.35835240091558, 8.14886306623246 49.35835240091558, 8.14886306623246 49.46768344296402, 8.14886306623246 49.61478339044737))";


    Polygon touch1 = (Polygon) createSPGeom(touches1_wkt, 4326);
    Polygon touch2 = (Polygon) createSPGeom(touches2_wkt, 4326);

    Collection<Polygon> polyCollection = new ArrayList<>();
    polyCollection.add(touch1);
    polyCollection.add(touch2);

    MultiPolygon multi = createSPMultiPolygon(polyCollection, true);

使用以下帮助方法进行 wkt 阅读:

     public static Geometry createSPGeom(String wktString, Integer epsgCode){

    Geometry returnedGeom =null;
    prec = new PrecisionModel(1000000);

    GeometryFactory geomFactory = new GeometryFactory(prec, epsgCode);
    WKTReader wktReader = new WKTReader(geomFactory);

    try {
        returnedGeom = wktReader.read(wktString);
    } catch (ParseException e) {
        // if wktString is invalid, an emty point geometry is used
        returnedGeom = geomFactory.createPoint();
    }

    return returnedGeom;
}

提前感谢您的帮助

您的 ClassCastException 似乎可以通过检查对 union() returns 的调用是否为多边形来处理。如果是这样,您可以使用仅包含一个多边形的数组调用 GeometryFactorys 的 createMultiPolygon。[1]

  1. https://github.com/locationtech/jts/blob/master/modules/core/src/main/java/org/locationtech/jts/geom/GeometryFactory.java#L326

正如GeoMesaJim所说,这是一个简单的问题,只需将多边形提升为多多边形即可。

GeometryFactory gf = new GeometryFactory();
MultiPolygon mPoly;
if (p instanceOf Polygon){
  Polygon[] polys = new Polygon[1];
  polys[0] = p;
  mPoly = gf.createMultiPolygon(polys);
} else {
   mPoly = p;
}
System.out.println(mPoly);