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

问题描述 投票:0回答:2

大家好我正在使用JTS 1.15和geotools快照21:

我用Polygon Collection作为输入参数编写了一个方法,输出方法是MultiPolygon Geometry。这是正常工作没有一个例外:我还有一个联合标志,如果重叠或触摸将联合我的多边形。如果现在所有多边形都重叠,则仅返回单个多边形。但是会有一个ClassCastException。

单个多边形无法转换为MultiPolygon

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

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

overlay方法返回可能表示结果的最具体的类。如果结果是同质的,如果结果包含单个元素,则返回Point,LineString或Polygon;否则,将返回MultiPoint,MultiLineString或MultiPolygon。如果结果是异构的,则将返回GeometryCollection。

我写了一个简单的字符串操纵器方法来改变我的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;
}

在此先感谢您的帮助

casting geometry geotools jts
2个回答
1
投票

正如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);

1
投票

看起来你的ClassCastException可以通过检查对union()的调用是否返回Polygon来处理。如果是这样,您可以使用仅包含一个Polygon的数组调用GeometryFactorys的createMultiPolygon。[1]

  1. https://github.com/locationtech/jts/blob/master/modules/core/src/main/java/org/locationtech/jts/geom/GeometryFactory.java#L326
© www.soinside.com 2019 - 2024. All rights reserved.