jts将单面转换为多面

时间:2019-03-01 13:54:41

标签: casting geometry geotools jts

大家好,我正在将JTS 1.15与geotools快照21结合使用:

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

不能将单个多边形投射为MultiPolygon

有没有简单的方法可以将单个多边形投射到MultiPolygon中? 我找不到几何JTS UML图,但发现了 多边形通过接口Polygonal扩展了Geometry类 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读取的helpfer方法:

     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;
}

感谢您的提前帮助

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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);