注册

dmV8数据库,使用java代码转化空间类型,"SYSGEO2"."ST_POINT","SYSGEO2"."ST_POLYGON"。程序运行异常

ql.t 2025/01/10 293 1

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:V8
【操作系统】:X86
【CPU】:
【问题描述】*:dmV8数据库,使用java代码转化空间类型,"SYSGEO2"."ST_POINT","SYSGEO2"."ST_POLYGON"。程序运行异常,获取不到数据,如下是具体描述:

# 一、Point处理geo工具类代码:

public class MysqlGeoPointTypeHandler extends BaseTypeHandler<Point> {
private static TargetDataSourceUtil targetDataSourceUtil;

@Autowired
public void setDataSourceUtil(TargetDataSourceUtil targetDataSourceUtil) {
MysqlGeoPointTypeHandler.targetDataSourceUtil = targetDataSourceUtil;
}

@Override
public void setNonNullParameter(PreparedStatement ps, int i, Point parameter, JdbcType jdbcType) throws SQLException {
try {
Connection connection = ps.getConnection();
Struct struct = convertToBytesDM(parameter, connection);
ps.setObject(i, struct);

} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public Point getNullableResult(ResultSet rs, String columnName) throws SQLException {
Struct struct = (Struct) rs.getObject(columnName);
return convertToGeoDM(struct);

}

@Override
public Point getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Struct struct = (Struct) rs.getObject(columnIndex);
return convertToGeoDM(struct);
}

@Override
public Point getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Struct struct = (Struct) cs.getObject(columnIndex);
return convertToGeoDM(struct);

}

private Struct convertToBytesDM(Geometry geometry, Connection connection) throws IOException, SQLException {
DmdbConnection dmdbConnection = connection.unwrap(DmdbConnection.class);
WKBWriter writer = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN);
byte[] bytes = writer.write(geometry);

// 设置SRID为4326
byte[] wkb = new byte[bytes.length + 8];
ByteBuffer.wrap(wkb)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(4326)
.put(bytes);

DmdbBlob blob = DmdbBlob.newInstanceOfLocal(wkb, dmdbConnection);
Object[] attrs = new Object[]{blob};
Struct struct = dmdbConnection.createStruct("SYSGEO2.ST_POINT", attrs);
return struct;
}

private Point convertToGeoDM(Struct struct) {
if (struct == null) {
return null;
}
try {

Object[] attrs = struct.getAttributes(); //获取结构体中的成员
Blob blob = (Blob) attrs[0]; //获取GEO_WKB属性信息
WKBReader reader = new WKBReader();
byte[] geomBytes = blob.getBytes(1, (int) blob.length());
Geometry geometry = reader.read(geomBytes);
return (Point) geometry;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

报错信息如下:

org.locationtech.jts.io.ParseException: Unknown WKB type 0
at org.locationtech.jts.io.WKBReader.readGeometry(WKBReader.java:282)
at org.locationtech.jts.io.WKBReader.read(WKBReader.java:191)
at org.locationtech.jts.io.WKBReader.read(WKBReader.java:159)
at cn.com.highlander.hscp.starget.server.typehandler.MysqlGeoPointTypeHandler.convertToGeoDM(MysqlGeoPointTypeHandler.java:117)
at cn.com.highlander.hscp.starget.server.typehandler.MysqlGeoPointTypeHandler.getNullableResult(MysqlGeoPointTypeHandler.java:64)
at cn.com.highlander.hscp.starget.server.typehandler.MysqlGeoPointTypeHandler.getNullableResult(MysqlGeoPointTypeHandler.java:38)
at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:86)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getPropertyMappingValue(DefaultResultSetHandler.java:530)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:495)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:418)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:366)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:337)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:310)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:202)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:66)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:80)
at jdk.internal.reflect.GeneratedMethodAccessor120.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at jdk.proxy2/jdk.proxy2.$Proxy361.query(Unknown Source)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:65)

具体报错的行数:

Geometry geometry = reader.read(geomBytes);

二、Polygon处理geo工具类代码:

public class MysqlGeoPolygonTypeHandler extends BaseTypeHandler<Polygon> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Polygon parameter, JdbcType jdbcType) throws SQLException {
try {
Connection connection = ps.getConnection();
Struct struct = convertToBytesDM(parameter, connection);
ps.setObject(i, struct);
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public Polygon getNullableResult(ResultSet rs, String columnName) throws SQLException {
Struct struct = (Struct) rs.getObject(columnName);
return convertToGeoDM(struct);

}

@Override
public Polygon getNullableResult(ResultSet rs, int columnIndex) throws SQLException {

Struct struct = (Struct) rs.getObject(columnIndex);
return convertToGeoDM(struct);

}

@Override
public Polygon getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Struct struct = (Struct) cs.getObject(columnIndex);
return convertToGeoDM(struct);

}

private Struct convertToBytesDM(Geometry geometry, Connection connection) throws IOException {
DmdbConnection dmdbConnection = null;
try {
dmdbConnection = connection.unwrap(DmdbConnection.class);
} catch (SQLException e) {
e.printStackTrace();
}

WKBWriter writer = new WKBWriter();
byte[] bytes = writer.write(geometry);
DmdbBlob blob = DmdbBlob.newInstanceOfLocal(bytes, dmdbConnection); // 创建 BLOB 对象
Object[] attrs = new Object[]{blob};
// 创建 Struct 对象
Struct struct = dmdbConnection.createStruct("SYSGEO2.ST_POLYGON", attrs);
return struct;
}

private Polygon convertToGeoDM(Struct struct) {
if (struct == null) {
return null;
}
try {
Object[] attrs = struct.getAttributes();
Blob blob = (Blob) attrs[0];
WKBReader reader = new WKBReader();
byte[] geomBytes = blob.getBytes(1, (int) blob.length());
Geometry geometry = reader.read(geomBytes);
return (Polygon) geometry;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

报错信息如下:

org.locationtech.jts.io.ParseException: Unknown WKB type 0
at org.locationtech.jts.io.WKBReader.readGeometry(WKBReader.java:282)
at org.locationtech.jts.io.WKBReader.read(WKBReader.java:191)
at org.locationtech.jts.io.WKBReader.read(WKBReader.java:159)
at cn.com.highlander.hscp.starget.server.typehandler.MysqlGeoPolygonTypeHandler.convertToGeoDM(MysqlGeoPolygonTypeHandler.java:95)
at cn.com.highlander.hscp.starget.server.typehandler.MysqlGeoPolygonTypeHandler.getNullableResult(MysqlGeoPolygonTypeHandler.java:49)
at cn.com.highlander.hscp.starget.server.typehandler.MysqlGeoPolygonTypeHandler.getNullableResult(MysqlGeoPolygonTypeHandler.java:33)

具体报错的行数:

Geometry geometry = reader.read(geomBytes);

三、数据库sql运行结果:

sql查询:

select dmgeo2.ST_AsText(pos) as pos,dmgeo2.ST_AsText(env) as env from hscp.fixed_area_info limit 1;

执行结果:

pos列:POINT(119.21976789810944 39.35273897566665)
env列:POLYGON((119.21946165 39.35236969,119.21941787 39.35236969,119.21941787 39.35250337,119.21932849 39.35250337,119.21932849 39.35262518,119.21928368 39.35262518,119.21928368 39.35265979,119.21919694 39.35265979,119.21919694 39.35334214,119.21919804 39.35334214,119.21919804 39.35335085,119.21921357 39.35335085,119.21921357 39.35335258,119.21976334 39.35335258,119.21976334 39.35335085,119.2198281 39.35335085,119.2198281 39.35334214,119.21988785 39.35334214,119.21988785 39.35326184,119.21991718 39.35326184,119.21991718 39.35320633,119.22005825 39.35320633,119.22005825 39.35308632,119.2201325 39.35308632,119.2201325 39.3530625,119.22027734 39.3530625,119.22027734 39.35305831,119.22029505 39.35305831,119.22029505 39.35284971,119.22032273 39.35284971,119.22032273 39.3521344,119.21967453 39.3521344,119.21967453 39.35214224,119.21948366 39.35214224,119.21948366 39.35216158,119.21946165 39.35216158,119.21946165 39.35236969))
..........

回答 0
暂无回答
扫一扫
联系客服