kafka配置、序列化warn修复
This commit is contained in:
parent
61a99a6ca6
commit
13ccf02967
@ -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<Point> {
|
||||
|
||||
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<Point> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取坐标的格式化字符串(用于文本表示)
|
||||
*/
|
||||
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) + ")";
|
||||
}
|
||||
}
|
||||
@ -37,8 +37,6 @@ public class PolygonDeserializer extends JsonDeserializer<Polygon> {
|
||||
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<Polygon> {
|
||||
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<Polygon> {
|
||||
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<Coordinate> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析坐标数组格式
|
||||
*/
|
||||
|
||||
@ -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<Polygon> {
|
||||
|
||||
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<Polygon> {
|
||||
|
||||
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<Polygon> {
|
||||
|
||||
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<Polygon> {
|
||||
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<Polygon> {
|
||||
// 直接写 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();
|
||||
}
|
||||
}
|
||||
@ -23,9 +23,7 @@ public class PolygonTypeHandler implements TypeHandler<Polygon> {
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user