|
@@ -5,7 +5,9 @@ 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.LineSegment;
|
|
|
+import org.locationtech.jts.geom.GeometryFactory;
|
|
|
+import org.locationtech.jts.geom.LineString;
|
|
|
+import org.locationtech.jts.geom.PrecisionModel;
|
|
|
|
|
|
import java.sql.CallableStatement;
|
|
|
import java.sql.PreparedStatement;
|
|
@@ -20,22 +22,23 @@ import java.util.regex.Pattern;
|
|
|
* @author 欧阳劲驰
|
|
|
* @since 1.0.0
|
|
|
*/
|
|
|
-@MappedTypes({LineSegment.class})
|
|
|
+@MappedTypes({LineString.class})
|
|
|
@MappedJdbcTypes(JdbcType.VARCHAR)
|
|
|
-public class GeomLineSegmentTypeHandlePg extends BaseTypeHandler<LineSegment> {
|
|
|
+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);
|
|
|
|
|
|
/**
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
@Override
|
|
|
- public void setNonNullParameter(PreparedStatement ps, int i, LineSegment segment, JdbcType jdbcType)
|
|
|
+ public void setNonNullParameter(PreparedStatement ps, int i, LineString lineString, JdbcType jdbcType)
|
|
|
throws SQLException {
|
|
|
- //将 LineSegment 序列化为 [[x1,y1],[x2,y2]] 字符串
|
|
|
- if (segment != null) {
|
|
|
- Coordinate p0 = segment.getCoordinate(0);
|
|
|
- Coordinate p1 = segment.getCoordinate(1);
|
|
|
+ //将 LineString 序列化为 [[x1,y1],[x2,y2]] 字符串
|
|
|
+ if (lineString != null) {
|
|
|
+ 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);
|
|
|
ps.setString(i, value);
|
|
|
} else {
|
|
@@ -48,7 +51,7 @@ public class GeomLineSegmentTypeHandlePg extends BaseTypeHandler<LineSegment> {
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
@Override
|
|
|
- public LineSegment getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ public LineString getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
return parseString(rs.getString(columnName));
|
|
|
}
|
|
|
|
|
@@ -56,7 +59,7 @@ public class GeomLineSegmentTypeHandlePg extends BaseTypeHandler<LineSegment> {
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
@Override
|
|
|
- public LineSegment getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ public LineString getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
return parseString(rs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
@@ -64,7 +67,7 @@ public class GeomLineSegmentTypeHandlePg extends BaseTypeHandler<LineSegment> {
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
@Override
|
|
|
- public LineSegment getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ public LineString getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
return parseString(cs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
@@ -75,7 +78,7 @@ public class GeomLineSegmentTypeHandlePg extends BaseTypeHandler<LineSegment> {
|
|
|
* @return 线段
|
|
|
* @throws SQLException sql异常
|
|
|
*/
|
|
|
- private LineSegment parseString(String value) throws SQLException {
|
|
|
+ private LineString parseString(String value) throws SQLException {
|
|
|
//非空校验
|
|
|
if (value == null || value.isEmpty()) return null;
|
|
|
//正则匹配
|
|
@@ -87,8 +90,8 @@ public class GeomLineSegmentTypeHandlePg extends BaseTypeHandler<LineSegment> {
|
|
|
double y0 = Double.parseDouble(matcher.group(2));
|
|
|
double x1 = Double.parseDouble(matcher.group(3));
|
|
|
double y1 = Double.parseDouble(matcher.group(4));
|
|
|
- //构建线段
|
|
|
- return new LineSegment(x0, y0, x1, y1);
|
|
|
+ //构建线
|
|
|
+ return factory.createLineString(new Coordinate[]{new Coordinate(x0, y0), new Coordinate(x1, y1)});
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new SQLException("坐标格式无效: " + value, e);
|
|
|
}
|