|
@@ -1,5 +1,6 @@
|
|
|
package com.shkpr.service.alambizplugin.dbdao.pgtype;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.apache.ibatis.type.BaseTypeHandler;
|
|
|
import org.apache.ibatis.type.JdbcType;
|
|
|
import org.apache.ibatis.type.MappedJdbcTypes;
|
|
@@ -9,12 +10,11 @@ import org.locationtech.jts.geom.GeometryFactory;
|
|
|
import org.locationtech.jts.geom.LineString;
|
|
|
import org.locationtech.jts.geom.PrecisionModel;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.sql.CallableStatement;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* 地理线段类型处理器
|
|
@@ -25,9 +25,8 @@ import java.util.regex.Pattern;
|
|
|
@MappedTypes({LineString.class})
|
|
|
@MappedJdbcTypes(JdbcType.VARCHAR)
|
|
|
public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
- //正则表达式匹配 [[x1,y1],[x2,y2]] 格式
|
|
|
- private static final Pattern PATTERN = Pattern.compile("^\\[\\[\\s*(-?\\d+(\\.\\d+)?),\\s*(-?\\d+(\\.\\d+)?)\\s*],\\s*\\[\\s*(-?\\d+(\\.\\d+)?),\\s*(-?\\d+(\\.\\d+)?)\\s*]\\s*]$");
|
|
|
GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4490);
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
/**
|
|
|
* {@inheritDoc}
|
|
@@ -81,20 +80,21 @@ public class GeomLineStringTypeHandlePg extends BaseTypeHandler<LineString> {
|
|
|
private LineString parseString(String value) throws SQLException {
|
|
|
//非空校验
|
|
|
if (value == null || value.isEmpty()) return null;
|
|
|
- //正则匹配
|
|
|
- Matcher matcher = PATTERN.matcher(value);
|
|
|
- if (matcher.find()) {
|
|
|
- try {
|
|
|
+ try {
|
|
|
+ //序列化
|
|
|
+ double[][] coordinates = objectMapper.readValue(value, double[][].class);
|
|
|
+ //判断格式
|
|
|
+ if (coordinates.length == 2 && coordinates[0].length == 2 && coordinates[1].length == 2) {
|
|
|
//获取值
|
|
|
- double x0 = Double.parseDouble(matcher.group(1));
|
|
|
- double y0 = Double.parseDouble(matcher.group(2));
|
|
|
- double x1 = Double.parseDouble(matcher.group(3));
|
|
|
- double y1 = Double.parseDouble(matcher.group(4));
|
|
|
+ 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 (NumberFormatException e) {
|
|
|
- throw new SQLException("坐标格式无效: " + value, e);
|
|
|
}
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new SQLException("线段格式无效: " + value);
|
|
|
}
|
|
|
throw new SQLException("线段格式无效: " + value);
|
|
|
}
|