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.JsonSerializer;
|
||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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 org.locationtech.jts.io.WKTWriter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DecimalFormat;
|
|
||||||
import java.text.DecimalFormatSymbols;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JTS Point 序列化器 - 将 Point 序列化为 JSON
|
* JTS Point 序列化器 - 将 Point 序列化为 JSON
|
||||||
@ -19,9 +17,6 @@ import java.util.Locale;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class PointSerializer extends JsonSerializer<Point> {
|
public class PointSerializer extends JsonSerializer<Point> {
|
||||||
|
|
||||||
private static final DecimalFormat COORD_FORMAT = new DecimalFormat("#.##########",
|
|
||||||
DecimalFormatSymbols.getInstance(Locale.US));
|
|
||||||
|
|
||||||
private final WKTWriter wktWriter = new WKTWriter();
|
private final WKTWriter wktWriter = new WKTWriter();
|
||||||
|
|
||||||
@Override
|
@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();
|
String type = node.get("type").asText().toLowerCase();
|
||||||
if ("polygon".equals(type)) {
|
if ("polygon".equals(type)) {
|
||||||
result = parseFromCustomFormat(node);
|
result = parseFromCustomFormat(node);
|
||||||
} else if ("polygon".equals(type)) { // GeoJSON 格式
|
|
||||||
result = parseFromGeoJSON(node);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 格式2: 直接坐标数组 [[x1,y1], [x2,y2], ...]
|
// 格式2: 直接坐标数组 [[x1,y1], [x2,y2], ...]
|
||||||
@ -61,7 +59,7 @@ public class PolygonDeserializer extends JsonDeserializer<Polygon> {
|
|||||||
2. GeoJSON格式: {"type":"Polygon","coordinates":[[[x1,y1],[x2,y2],...]]}
|
2. GeoJSON格式: {"type":"Polygon","coordinates":[[[x1,y1],[x2,y2],...]]}
|
||||||
3. 坐标数组: [[x1,y1],[x2,y2],...]
|
3. 坐标数组: [[x1,y1],[x2,y2],...]
|
||||||
4. WKT字符串: "POLYGON((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;
|
return result;
|
||||||
@ -116,46 +114,6 @@ public class PolygonDeserializer extends JsonDeserializer<Polygon> {
|
|||||||
return createPolygonFromCoordinates(coordinates);
|
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 org.locationtech.jts.io.WKTWriter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DecimalFormat;
|
|
||||||
import java.text.DecimalFormatSymbols;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JTS Polygon 序列化器 - 将 Polygon 序列化为 JSON
|
* JTS Polygon 序列化器 - 将 Polygon 序列化为 JSON
|
||||||
@ -21,9 +18,6 @@ import java.util.Locale;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class PolygonSerializer extends JsonSerializer<Polygon> {
|
public class PolygonSerializer extends JsonSerializer<Polygon> {
|
||||||
|
|
||||||
private static final DecimalFormat COORD_FORMAT = new DecimalFormat("#.##########",
|
|
||||||
DecimalFormatSymbols.getInstance(Locale.US));
|
|
||||||
|
|
||||||
private final WKTWriter wktWriter = new WKTWriter();
|
private final WKTWriter wktWriter = new WKTWriter();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -103,7 +97,7 @@ public class PolygonSerializer extends JsonSerializer<Polygon> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 获取外环
|
// 获取外环
|
||||||
LinearRing exteriorRing = (LinearRing) polygon.getExteriorRing();
|
LinearRing exteriorRing = polygon.getExteriorRing();
|
||||||
if (exteriorRing == null) {
|
if (exteriorRing == null) {
|
||||||
throw new IOException("多边形外环为空");
|
throw new IOException("多边形外环为空");
|
||||||
}
|
}
|
||||||
@ -135,7 +129,6 @@ public class PolygonSerializer extends JsonSerializer<Polygon> {
|
|||||||
try {
|
try {
|
||||||
String wkt = wktWriter.write(polygon);
|
String wkt = wktWriter.write(polygon);
|
||||||
|
|
||||||
// 转换为与 PGpolygon 相似的格式
|
|
||||||
if (wkt.startsWith("POLYGON")) {
|
if (wkt.startsWith("POLYGON")) {
|
||||||
// 提取坐标部分
|
// 提取坐标部分
|
||||||
String coordsPart = wkt.substring(wkt.indexOf("((") + 2, wkt.lastIndexOf("))"));
|
String coordsPart = wkt.substring(wkt.indexOf("((") + 2, wkt.lastIndexOf("))"));
|
||||||
@ -173,41 +166,4 @@ public class PolygonSerializer extends JsonSerializer<Polygon> {
|
|||||||
throw new IOException("Polygon 序列化失败", e);
|
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 static final int WGS84_SRID = 4326;
|
||||||
private final GeometryFactory geometryFactory = new GeometryFactory();
|
private final GeometryFactory geometryFactory = new GeometryFactory();
|
||||||
private final WKTReader wktReader = new WKTReader(geometryFactory);
|
private final WKTReader wktReader = new WKTReader(geometryFactory);
|
||||||
private final WKTWriter wktWriter = new WKTWriter();
|
|
||||||
private final WKBReader wkbReader = new WKBReader(geometryFactory);
|
private final WKBReader wkbReader = new WKBReader(geometryFactory);
|
||||||
private final WKBWriter wkbWriter = new WKBWriter();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameter(PreparedStatement ps, int i, Polygon parameter, JdbcType jdbcType)
|
public void setParameter(PreparedStatement ps, int i, Polygon parameter, JdbcType jdbcType)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user