1

我们正在使用 JTS Geometry Suite、GeoTools (ShapefileDataStore) 和 Hibernate Spatial 将具有 3D 坐标的多多边形 Shapefile 导入 o​​racle 空间。在 Oracle Spatial 中,我们希望它们以 2D 形式存储。

我发现的 onyl(而且非常慢)方法如下,使用 WKBWriter 和 WKBReader:

private static Geometry convert2D(Geometry geometry3D) {
    // create a 2D WKBWriter
    WKBWriter writer = new WKBWriter(2);
    byte[] binary = writer.write(geometry3D);
    WKBReader reader = new WKBReader(factory);
    Geometry geometry2D= null;
    try {
        geometry2D= reader.read(binary);
    } catch (ParseException e) {
        log.error("error reading wkb", e);
    }
    return geometry2D;
}

有人知道将几何图形从 3D 转换为 2D 的更有效方法吗?

4

2 回答 2

1

我找到了一个方法:

  1. 创建一个CoordinateArraySequence强制使用 2D实例的新Coordinate实例
  2. 创建一个新CoordinateArraySequenceFactory的生成新的自定义CoodinateArraySequence
  3. 创建一个 GeometryFactory 的新实例,它使用新的CoordinateFactory并使用它来重新创建几何:

    private static Geometry convert2D(Geometry geometry3D) {
        GeometryFactory geoFactory = new GeometryFactory(
            geometry3d.getPrecisionModel(), geometry3d.getSRID(), CoordinateArraySequence2DFactory.instance());
        if (geometry3D instanceOf Point) {
           return geoFactory.createPoint(geometry3D.getCoordinateSequence());
        } else if (geometry3D instanceOf Point) {
        //...  
        //...
        //...
        throw new IllegalArgumentException("Unsupported geometry type: ".concat(geometry3d.getClass().getName());
    }
    

祝你好运!!

于 2014-12-12T13:07:16.873 回答
1

我没有测试WKBWriterandWKBReader但这是另一种简单的方法:

  • 创建几何图形的副本
  • 将所有坐标设置为 2D

简单代码:

private static Geometry convert2D(Geometry g3D){
    // copy geometry
    Geometry g2D = (Geometry) g3D.clone();
    // set new 2D coordinates
    for(Coordinate c : g2D.getCoordinates()){
        c.setCoordinate(new Coordinate(c.x, c.y));
    }
    return g2D;
}
于 2016-07-13T07:06:51.880 回答