From 13ccf02967f540a28787595e57c4c344d915574fb1f2492b8049fccee5a8f9bb Mon Sep 17 00:00:00 2001 From: attiya <2413103649@qq.com> Date: Tue, 6 Jan 2026 09:14:23 +0800 Subject: [PATCH] =?UTF-8?q?kafka=E9=85=8D=E7=BD=AE=E3=80=81=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96warn=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operations/handler/PointSerializer.java | 31 +--------- .../handler/PolygonDeserializer.java | 44 +------------- .../operations/handler/PolygonSerializer.java | 60 +++---------------- .../handler/PolygonTypeHandler.java | 2 - 4 files changed, 11 insertions(+), 126 deletions(-) diff --git a/ebike-operations/src/main/java/com/cdzy/operations/handler/PointSerializer.java b/ebike-operations/src/main/java/com/cdzy/operations/handler/PointSerializer.java index 1acbb27..8cdc234 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/handler/PointSerializer.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/handler/PointSerializer.java @@ -4,13 +4,11 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import lombok.extern.slf4j.Slf4j; -import org.locationtech.jts.geom.*; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Point; import org.locationtech.jts.io.WKTWriter; import java.io.IOException; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.util.Locale; /** * JTS Point 序列化器 - 将 Point 序列化为 JSON @@ -19,9 +17,6 @@ import java.util.Locale; @Slf4j public class PointSerializer extends JsonSerializer { - private static final DecimalFormat COORD_FORMAT = new DecimalFormat("#.##########", - DecimalFormatSymbols.getInstance(Locale.US)); - private final WKTWriter wktWriter = new WKTWriter(); @Override @@ -109,26 +104,4 @@ public class PointSerializer extends JsonSerializer { } } } - - /** - * 获取坐标的格式化字符串(用于文本表示) - */ - private String getFormattedCoordinateString(double value) { - synchronized (COORD_FORMAT) { - return COORD_FORMAT.format(value); - } - } - - /** - * 生成 PostgreSQL 点格式的字符串(兼容性) - */ - private String toPostgresPointFormat(Point point) { - if (point == null || point.isEmpty()) { - return ""; - } - - Coordinate coordinate = point.getCoordinate(); - return "(" + getFormattedCoordinateString(coordinate.x) + - "," + getFormattedCoordinateString(coordinate.y) + ")"; - } } \ No newline at end of file diff --git a/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonDeserializer.java b/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonDeserializer.java index 78a2bee..6756dc8 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonDeserializer.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonDeserializer.java @@ -37,8 +37,6 @@ public class PolygonDeserializer extends JsonDeserializer { String type = node.get("type").asText().toLowerCase(); if ("polygon".equals(type)) { result = parseFromCustomFormat(node); - } else if ("polygon".equals(type)) { // GeoJSON 格式 - result = parseFromGeoJSON(node); } } // 格式2: 直接坐标数组 [[x1,y1], [x2,y2], ...] @@ -61,7 +59,7 @@ public class PolygonDeserializer extends JsonDeserializer { 2. GeoJSON格式: {"type":"Polygon","coordinates":[[[x1,y1],[x2,y2],...]]} 3. 坐标数组: [[x1,y1],[x2,y2],...] 4. WKT字符串: "POLYGON((x1 y1, x2 y2, ...))" - 5. PGpolygon格式: "((x1,y1),(x2,y2),...)" """); + 5. PGpolygon格式: "((x1,y1),(x2,y2),...)\""""); } return result; @@ -116,46 +114,6 @@ public class PolygonDeserializer extends JsonDeserializer { return createPolygonFromCoordinates(coordinates); } - /** - * 解析 GeoJSON 格式 - */ - private Polygon parseFromGeoJSON(JsonNode node) throws IOException { - JsonNode coordinatesNode = node.get("coordinates"); - if (coordinatesNode == null) { - throw new IOException("coordinates 字段缺失"); - } - - if (!coordinatesNode.isArray() || coordinatesNode.isEmpty()) { - throw new IOException("coordinates 字段必须是非空数组"); - } - - // GeoJSON 格式: coordinates 是三维数组 [[[x1,y1], [x2,y2], ...]] - JsonNode firstRing = coordinatesNode.get(0); - if (!firstRing.isArray()) { - throw new IOException("GeoJSON coordinates 第一环必须是数组"); - } - - List coordinates = new ArrayList<>(); - for (int i = 0; i < firstRing.size(); i++) { - JsonNode coordNode = firstRing.get(i); - if (!coordNode.isArray()) { - throw new IOException("GeoJSON 坐标 " + i + " 必须是数组格式"); - } - if (coordNode.size() < 2) { - throw new IOException("GeoJSON 坐标 " + i + " 至少需要2个值(经度和纬度)"); - } - try { - double x = coordNode.get(0).asDouble(); - double y = coordNode.get(1).asDouble(); - coordinates.add(new Coordinate(x, y)); - } catch (NumberFormatException e) { - throw new IOException("GeoJSON 坐标 " + i + " 格式错误: " + coordNode, e); - } - } - - return createPolygonFromCoordinates(coordinates); - } - /** * 解析坐标数组格式 */ diff --git a/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonSerializer.java b/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonSerializer.java index d564883..1d2b258 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonSerializer.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonSerializer.java @@ -10,9 +10,6 @@ import org.locationtech.jts.geom.Polygon; import org.locationtech.jts.io.WKTWriter; import java.io.IOException; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.util.Locale; /** * JTS Polygon 序列化器 - 将 Polygon 序列化为 JSON @@ -21,13 +18,10 @@ import java.util.Locale; @Slf4j public class PolygonSerializer extends JsonSerializer { - private static final DecimalFormat COORD_FORMAT = new DecimalFormat("#.##########", - DecimalFormatSymbols.getInstance(Locale.US)); - private final WKTWriter wktWriter = new WKTWriter(); @Override - public void serialize(Polygon polygon, JsonGenerator gen, SerializerProvider serializers) + public void serialize(Polygon polygon, JsonGenerator gen, SerializerProvider serializers) throws IOException { if (polygon == null) { gen.writeNull(); @@ -103,13 +97,13 @@ public class PolygonSerializer extends JsonSerializer { try { // 获取外环 - LinearRing exteriorRing = (LinearRing) polygon.getExteriorRing(); + LinearRing exteriorRing = polygon.getExteriorRing(); if (exteriorRing == null) { throw new IOException("多边形外环为空"); } return exteriorRing.getCoordinates(); - + } catch (Exception e) { throw new IOException("获取多边形坐标失败: " + e.getMessage(), e); } @@ -134,16 +128,15 @@ public class PolygonSerializer extends JsonSerializer { try { String wkt = wktWriter.write(polygon); - - // 转换为与 PGpolygon 相似的格式 + if (wkt.startsWith("POLYGON")) { // 提取坐标部分 String coordsPart = wkt.substring(wkt.indexOf("((") + 2, wkt.lastIndexOf("))")); String[] points = coordsPart.split(", "); - + gen.writeStartObject(); gen.writeStringField("type", "polygon"); - + gen.writeArrayFieldStart("coordinates"); for (String point : points) { String[] xy = point.split(" "); @@ -151,7 +144,7 @@ public class PolygonSerializer extends JsonSerializer { try { double x = Double.parseDouble(xy[0]); double y = Double.parseDouble(xy[1]); - + gen.writeStartArray(); gen.writeNumber(x); gen.writeNumber(y); @@ -167,47 +160,10 @@ public class PolygonSerializer extends JsonSerializer { // 直接写 WKT 字符串 gen.writeString(wkt); } - + } catch (Exception e) { log.error("WKT 序列化失败: {}", e.getMessage()); throw new IOException("Polygon 序列化失败", e); } } - - /** - * 获取坐标的格式化字符串 - */ - private String getFormattedCoordinateString(double value) { - synchronized (COORD_FORMAT) { - return COORD_FORMAT.format(value); - } - } - - /** - * 生成 PGpolygon 格式的字符串(兼容性) - */ - private String toPGpolygonFormat(Coordinate[] coordinates) { - if (coordinates == null || coordinates.length == 0) { - return ""; - } - - StringBuilder sb = new StringBuilder(); - sb.append("(("); - - for (int i = 0; i < coordinates.length; i++) { - Coordinate coord = coordinates[i]; - if (coord == null) continue; - - sb.append(getFormattedCoordinateString(coord.x)) - .append(",") - .append(getFormattedCoordinateString(coord.y)); - - if (i < coordinates.length - 1) { - sb.append("),("); - } - } - - sb.append("))"); - return sb.toString(); - } } \ No newline at end of file diff --git a/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonTypeHandler.java b/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonTypeHandler.java index fab7d70..6380bf9 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonTypeHandler.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/handler/PolygonTypeHandler.java @@ -23,9 +23,7 @@ public class PolygonTypeHandler implements TypeHandler { private static final int WGS84_SRID = 4326; private final GeometryFactory geometryFactory = new GeometryFactory(); private final WKTReader wktReader = new WKTReader(geometryFactory); - private final WKTWriter wktWriter = new WKTWriter(); private final WKBReader wkbReader = new WKBReader(geometryFactory); - private final WKBWriter wkbWriter = new WKBWriter(); @Override public void setParameter(PreparedStatement ps, int i, Polygon parameter, JdbcType jdbcType)