|
@@ -25,8 +25,37 @@ import java.sql.SQLException;
|
|
|
@MappedTypes({LineString.class})
|
|
|
@MappedJdbcTypes(JdbcType.VARCHAR)
|
|
|
public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
- GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4490);
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ private static final GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4490);
|
|
|
+ private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析
|
|
|
+ *
|
|
|
+ * @param lineStr 值
|
|
|
+ * @return 线段
|
|
|
+ * @throws SQLException sql异常
|
|
|
+ */
|
|
|
+ public static LineString parseLineString(String lineStr) throws SQLException {
|
|
|
+ //非空校验
|
|
|
+ if (lineStr == null || lineStr.isEmpty()) return null;
|
|
|
+ try {
|
|
|
+ //序列化
|
|
|
+ double[][] coordinates = objectMapper.readValue(lineStr, double[][].class);
|
|
|
+ //判断格式
|
|
|
+ if (coordinates.length == 2 && coordinates[0].length == 2 && coordinates[1].length == 2) {
|
|
|
+ //获取值
|
|
|
+ double x0 = coordinates[0][0];
|
|
|
+ double y0 = coordinates[0][1];
|
|
|
+ double x1 = coordinates[1][0];
|
|
|
+ double y1 = coordinates[1][1];
|
|
|
+ //构建线
|
|
|
+ return factory.createLineString(new Coordinate[]{new Coordinate(x0, y0), new Coordinate(x1, y1)});
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new SQLException("线段格式无效: " + lineStr);
|
|
|
+ }
|
|
|
+ throw new SQLException("线段格式无效: " + lineStr);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* {@inheritDoc}
|
|
@@ -35,7 +64,7 @@ public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
public void setNonNullParameter(PreparedStatement ps, int i, LineString lineString, JdbcType jdbcType)
|
|
|
throws SQLException {
|
|
|
//将 LineString 序列化为 [[x1,y1],[x2,y2]] 字符串
|
|
|
- if (lineString != null) {
|
|
|
+ if (lineString != null && 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]]", p0.x, p0.y, p1.x, p1.y);
|
|
@@ -51,7 +80,7 @@ public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
*/
|
|
|
@Override
|
|
|
public LineString getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
- return parseString(rs.getString(columnName));
|
|
|
+ return rs.wasNull() ? null : parseLineString(rs.getString(columnName));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -59,7 +88,7 @@ public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
*/
|
|
|
@Override
|
|
|
public LineString getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
- return parseString(rs.getString(columnIndex));
|
|
|
+ return rs.wasNull() ? null : parseLineString(rs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -67,35 +96,6 @@ public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
*/
|
|
|
@Override
|
|
|
public LineString getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
- return parseString(cs.getString(columnIndex));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 解析
|
|
|
- *
|
|
|
- * @param value 值
|
|
|
- * @return 线段
|
|
|
- * @throws SQLException sql异常
|
|
|
- */
|
|
|
- private LineString parseString(String value) throws SQLException {
|
|
|
- //非空校验
|
|
|
- if (value == null || value.isEmpty()) return null;
|
|
|
- try {
|
|
|
- //序列化
|
|
|
- double[][] coordinates = objectMapper.readValue(value, double[][].class);
|
|
|
- //判断格式
|
|
|
- if (coordinates.length == 2 && coordinates[0].length == 2 && coordinates[1].length == 2) {
|
|
|
- //获取值
|
|
|
- double x0 = coordinates[0][0];
|
|
|
- double y0 = coordinates[0][1];
|
|
|
- double x1 = coordinates[1][0];
|
|
|
- double y1 = coordinates[1][1];
|
|
|
- //构建线
|
|
|
- return factory.createLineString(new Coordinate[]{new Coordinate(x0, y0), new Coordinate(x1, y1)});
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- throw new SQLException("线段格式无效: " + value);
|
|
|
- }
|
|
|
- throw new SQLException("线段格式无效: " + value);
|
|
|
+ return cs.wasNull() ? null : parseLineString(cs.getString(columnIndex));
|
|
|
}
|
|
|
}
|