工作中发现的困扰很久的问题,多次尝试最终揭开
代码奉上
//String 转为 geolatte
public static org.geolatte.geom.Geometry changeStringToGeom(String s){
org.geolatte.geom.Geometry<?> geometry = Wkb.fromWkb(ByteBuffer.from(s));
return geometry;
}
//转换geolatte类型
public static Geometry changeGeolatteToLocationtech(org.geolatte.geom.Geometry geometry){
PositionSequence positions = geometry.getPositions();
int size = positions.size();
int num = 0;
List<double[]> list = new ArrayList<>();
while (true){
double[] pos = new double[3];
Position positionN = positions.getPositionN(num);
pos[0] = positionN.getCoordinate(0);
pos[1] = positionN.getCoordinate(1);
pos[2] = positionN.getCoordinate(2);
++num;
list.add(pos);
if(num == positions.size()){break;}
}
Coordinate[] c = new Coordinate[num];
num = 0;
for(Coordinate c1 :c){
c1 = null;
double[] doubles = list.get(num);
Coordinate c0 = new Coordinate(doubles[0],doubles[1],doubles[2]);
c[num] = c0;
num++;
}
GeometryFactory factory = new GeometryFactory();
LineString lineString = factory.createLineString(c);
Geometry geometry2 = factory.createGeometry(lineString);
return geometry2;
}
因为数据库固定数据为LineString 线 类型,所以只使用它来存储数据,如果数据库中有多种类型的图形,可以通过拆分数据并分多种情况判断,再一一对应放入点位。