|
@@ -6,13 +6,11 @@ import org.apache.ibatis.type.BaseTypeHandler;
|
|
|
import org.apache.ibatis.type.JdbcType;
|
|
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
|
|
import org.apache.ibatis.type.MappedTypes;
|
|
|
-import org.locationtech.jts.geom.Coordinate;
|
|
|
import org.locationtech.jts.geom.Geometry;
|
|
|
-import org.locationtech.jts.geom.LineString;
|
|
|
+import org.locationtech.jts.geom.MultiLineString;
|
|
|
import org.locationtech.jts.geom.Point;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.math.BigDecimal;
|
|
|
import java.sql.CallableStatement;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
@@ -29,54 +27,6 @@ import java.sql.SQLException;
|
|
|
public class GeomTypeHandlePg extends BaseTypeHandler<Geometry> {
|
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
- @Override
|
|
|
- public void setNonNullParameter(PreparedStatement ps, int i, Geometry geometry, JdbcType jdbcType) throws SQLException {
|
|
|
- if (geometry instanceof Point) {
|
|
|
- //将 Point 序列化化为 [x,y] 字符串
|
|
|
- Point point = (Point) geometry;
|
|
|
- if (point.getCoordinate() != null) {
|
|
|
- String pointStr = String.format("[%s,%s]",
|
|
|
- BigDecimal.valueOf(point.getX()).toPlainString(),
|
|
|
- BigDecimal.valueOf(point.getY()).toPlainString()
|
|
|
- );
|
|
|
- ps.setString(i, pointStr);
|
|
|
- return;
|
|
|
- }
|
|
|
- } else if (geometry instanceof LineString) {
|
|
|
- //将 LineString 序列化为 [[x1,y1],[x2,y2]] 字符串
|
|
|
- LineString lineString = (LineString) geometry;
|
|
|
- if (lineString.getCoordinates() != null && lineString.getCoordinates().length == 2) {
|
|
|
- Coordinate p0 = lineString.getCoordinateN(0);
|
|
|
- Coordinate p1 = lineString.getCoordinateN(1);
|
|
|
- String value = String.format("[[%s,%s],[%s,%s]]",
|
|
|
- BigDecimal.valueOf(p0.x).toPlainString(),
|
|
|
- BigDecimal.valueOf(p0.y).toPlainString(),
|
|
|
- BigDecimal.valueOf(p1.x).toPlainString(),
|
|
|
- BigDecimal.valueOf(p1.y).toPlainString()
|
|
|
- );
|
|
|
- ps.setString(i, value);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- //处理空值
|
|
|
- ps.setNull(i, JdbcType.VARCHAR.TYPE_CODE);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Geometry getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
- return rs.wasNull() ? null : parseGeom(rs.getString(columnName));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Geometry getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
- return rs.wasNull() ? null : parseGeom(rs.getString(columnIndex));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Geometry getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
- return cs.wasNull() ? null : parseGeom(cs.getString(columnIndex));
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 解析
|
|
|
*
|
|
@@ -84,7 +34,7 @@ public class GeomTypeHandlePg extends BaseTypeHandler<Geometry> {
|
|
|
* @return 地理对象
|
|
|
* @throws SQLException sql异常
|
|
|
*/
|
|
|
- public Geometry parseGeom(String geomStr) throws SQLException {
|
|
|
+ public static Geometry parseGeom(String geomStr) throws SQLException {
|
|
|
//非空校验
|
|
|
if (geomStr == null || geomStr.isEmpty()) return null;
|
|
|
try {
|
|
@@ -92,11 +42,11 @@ public class GeomTypeHandlePg extends BaseTypeHandler<Geometry> {
|
|
|
if (jsonNode.isArray() && jsonNode.size() > 0) {
|
|
|
//点处理
|
|
|
if (jsonNode.get(0).isNumber() && jsonNode.get(1).isNumber()) {
|
|
|
- return GeomPointTypeHandlePg.parsePoint(geomStr);
|
|
|
+ return GeomPointTypeHandlePg.parseString(geomStr);
|
|
|
}
|
|
|
//线处理
|
|
|
else if (jsonNode.get(0).isArray()) {
|
|
|
- return GeomLineStringTypeHandlePg.parseLineString(geomStr);
|
|
|
+ return GeomMultiLineStringTypeHandlePg.parseString(geomStr);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -105,4 +55,54 @@ public class GeomTypeHandlePg extends BaseTypeHandler<Geometry> {
|
|
|
}
|
|
|
throw new SQLException("地理数据格式无效: " + geomStr);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从Geometry对象转数据库字符串
|
|
|
+ *
|
|
|
+ * @param geometry 点
|
|
|
+ * @return 字符串
|
|
|
+ * @throws SQLException sql异常
|
|
|
+ */
|
|
|
+ public static String toGeomString(Geometry geometry) throws SQLException {
|
|
|
+ if (geometry instanceof Point) {
|
|
|
+ Point point = (Point) geometry;
|
|
|
+ //转字符串
|
|
|
+ return GeomPointTypeHandlePg.toString(point);
|
|
|
+
|
|
|
+ } else if (geometry instanceof MultiLineString) {
|
|
|
+ MultiLineString multiLineString = (MultiLineString) geometry;
|
|
|
+ //转字符串
|
|
|
+ return GeomMultiLineStringTypeHandlePg.toString(multiLineString);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, Geometry geometry, JdbcType jdbcType) throws SQLException {
|
|
|
+ if (geometry != null && geometry.getCoordinate() != null) {
|
|
|
+ //将geom序列化化为字符串
|
|
|
+ String geomString = toGeomString(geometry);
|
|
|
+ if (geomString != null) {
|
|
|
+ ps.setString(i, geomString);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //处理空值
|
|
|
+ ps.setNull(i, JdbcType.VARCHAR.TYPE_CODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Geometry getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ return rs.wasNull() ? null : parseGeom(rs.getString(columnName));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Geometry getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ return rs.wasNull() ? null : parseGeom(rs.getString(columnIndex));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Geometry getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ return cs.wasNull() ? null : parseGeom(cs.getString(columnIndex));
|
|
|
+ }
|
|
|
}
|