我们正在使用JTS几何套件,GeoTools(ShapefileDataStore)和Hibernate Spatial将具有3D坐标的多边形Shapefile导入到oracle空间。在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的更有效方法吗?
答案 0 :(得分:0)
我找到了一种方法:
CoordinateArraySequence
实例Coordinate
CoordinateArraySequenceFactory
,生成新的自定义CoodinateArraySequence
创建一个新的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());
}
答案 1 :(得分:0)
我没有测试WKBWriter
和WKBReader
,但这是另一种简单方法:
简单代码:
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;
}