【问题标题】:jts convert single polygon to multipolygonjts 将单个多边形转换为多多边形
【发布时间】:2019-07-23 13:45:03
【问题描述】:

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

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

单个多边形不能转换为 MultiPolygon

有没有一种简单的方法可以将单个多边形投射到 MultiPolygon 中? 我找不到几何 jts uml 图,但我发现 Polygons 使用接口 Polygonal 扩展了 Geometry 类 Multipolygon 扩展了 GeometryCollection,它扩展了 Geometry 并具有 Polygonal 接口。

JTS Geometry Jacadoc 中提到了以下内容: 覆盖方法

覆盖方法返回最具体的类来表示 结果。如果结果是齐次的,点、线串或多边形 如果结果包含单个元素,将返回;否则,一个 将返回 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);

使用以下 helpfer 方法进行 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


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2018-08-19
        • 1970-01-01
        • 2018-06-13
        • 1970-01-01
        相关资源
        最近更新 更多