2025-10-21 14:57:52 +08:00
|
|
|
|
package com.cdzy.operations.handler;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2025-10-22 14:51:17 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-10-21 14:57:52 +08:00
|
|
|
|
import org.postgresql.geometric.PGpoint;
|
|
|
|
|
|
import org.postgresql.geometric.PGpolygon;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* PGpolygon 反序列化器 - 将 JSON 反序列化为 PGpolygon
|
|
|
|
|
|
*/
|
2025-10-22 14:51:17 +08:00
|
|
|
|
@Slf4j
|
2025-10-21 14:57:52 +08:00
|
|
|
|
public class PGpolygonDeserializer extends JsonDeserializer<PGpolygon> {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public PGpolygon deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
|
|
|
|
|
JsonNode node = p.getCodec().readTree(p);
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
if (node.isNull()) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
PGpolygon result = null;
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
// 格式1: 自定义格式 {"type": "polygon", "coordinates": [[x1,y1], [x2,y2], ...]}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
if (node.isObject() && node.has("type")) {
|
|
|
|
|
|
String type = node.get("type").asText();
|
|
|
|
|
|
|
|
|
|
|
|
if ("polygon".equals(type)) {
|
|
|
|
|
|
result = parseFromCustomFormat(node);
|
|
|
|
|
|
} else if ("Polygon".equals(type)) {
|
|
|
|
|
|
result = parseFromGeoJSON(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 格式2: 直接坐标数组 [[x1,y1], [x2,y2], ...]
|
|
|
|
|
|
else if (node.isArray()) {
|
|
|
|
|
|
result = parseFromArray(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 格式3: 字符串格式 "((x1 y1, x2 y2, ...))"
|
|
|
|
|
|
else if (node.isTextual()) {
|
|
|
|
|
|
result = parseFromString(node.asText());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 格式4: 对象格式但没有 type 字段,尝试解析 coordinates
|
|
|
|
|
|
else if (node.isObject() && node.has("coordinates")) {
|
|
|
|
|
|
result = parseFromCustomFormat(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
|
|
throw new IOException("""
|
|
|
|
|
|
不支持的 PGpolygon JSON 格式,支持的格式包括:
|
|
|
|
|
|
1. 自定义格式: {"type":"polygon","coordinates":[[x1,y1],[x2,y2],...]}
|
|
|
|
|
|
2. GeoJSON格式: {"type":"Polygon","coordinates":[[[x1,y1],[x2,y2],...]]}
|
|
|
|
|
|
3. 坐标数组: [[x1,y1],[x2,y2],...]
|
|
|
|
|
|
4. 字符串格式: "((x1 y1, x2 y2, ...))\"""");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
} catch (Exception e) {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
System.err.println("!!! PGpolygonDeserializer 反序列化失败 !!!");
|
|
|
|
|
|
System.err.println("错误信息: " + e.getMessage());
|
|
|
|
|
|
System.err.println("输入 JSON: " + node);
|
2025-10-21 14:57:52 +08:00
|
|
|
|
throw new IOException("PGpolygon 反序列化失败: " + e.getMessage(), e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Class<PGpolygon> handledType() {
|
|
|
|
|
|
return PGpolygon.class;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解析自定义格式
|
|
|
|
|
|
*/
|
|
|
|
|
|
private PGpolygon parseFromCustomFormat(JsonNode node) throws IOException {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
JsonNode coordinatesNode = node.get("coordinates");
|
2025-10-22 14:51:17 +08:00
|
|
|
|
if (coordinatesNode == null) {
|
|
|
|
|
|
throw new IOException("coordinates 字段缺失");
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (!coordinatesNode.isArray()) {
|
|
|
|
|
|
throw new IOException("coordinates 字段必须是数组类型");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
List<PGpoint> points = new ArrayList<>();
|
2025-10-22 14:51:17 +08:00
|
|
|
|
for (int i = 0; i < coordinatesNode.size(); i++) {
|
|
|
|
|
|
JsonNode coordNode = coordinatesNode.get(i);
|
|
|
|
|
|
|
|
|
|
|
|
if (!coordNode.isArray()) {
|
|
|
|
|
|
throw new IOException("坐标 " + i + " 必须是数组格式");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (coordNode.size() < 2) {
|
|
|
|
|
|
throw new IOException("坐标 " + i + " 至少需要2个值(经度和纬度)");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-10-21 14:57:52 +08:00
|
|
|
|
double x = coordNode.get(0).asDouble();
|
|
|
|
|
|
double y = coordNode.get(1).asDouble();
|
2025-10-22 14:51:17 +08:00
|
|
|
|
PGpoint point = new PGpoint(x, y);
|
|
|
|
|
|
points.add(point);
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
throw new IOException("坐标 " + i + " 格式错误: " + coordNode, e);
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
return createPGpolygonFromPoints(points);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解析 GeoJSON 格式
|
|
|
|
|
|
*/
|
|
|
|
|
|
private PGpolygon parseFromGeoJSON(JsonNode node) throws IOException {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
JsonNode coordinatesNode = node.get("coordinates");
|
2025-10-22 14:51:17 +08:00
|
|
|
|
if (coordinatesNode == null) {
|
|
|
|
|
|
throw new IOException("coordinates 字段缺失");
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (!coordinatesNode.isArray() || coordinatesNode.isEmpty()) {
|
|
|
|
|
|
throw new IOException("coordinates 字段必须是非空数组");
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
// GeoJSON 格式: coordinates 是三维数组 [[[x1,y1], [x2,y2], ...]]
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
List<PGpoint> points = new ArrayList<>();
|
2025-10-22 14:51:17 +08:00
|
|
|
|
for (int i = 0; i < coordinatesNode.size(); i++) {
|
|
|
|
|
|
JsonNode ringNode = coordinatesNode.get(i);
|
|
|
|
|
|
if (!ringNode.isArray()) {
|
|
|
|
|
|
throw new IOException("GeoJSON coordinates 第一环必须是数组");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!ringNode.isArray()) {
|
|
|
|
|
|
throw new IOException("GeoJSON 坐标 " + i + " 必须是数组格式");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ringNode.size() < 2) {
|
|
|
|
|
|
throw new IOException("GeoJSON 坐标 " + i + " 至少需要2个值(经度和纬度)");
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
double x = ringNode.get(0).asDouble();
|
|
|
|
|
|
double y = ringNode.get(1).asDouble();
|
|
|
|
|
|
PGpoint point = new PGpoint(x, y);
|
|
|
|
|
|
points.add(point);
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
throw new IOException("GeoJSON 坐标 " + i + " 格式错误: " + ringNode, e);
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
return createPGpolygonFromPoints(points);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解析坐标数组格式
|
|
|
|
|
|
*/
|
|
|
|
|
|
private PGpolygon parseFromArray(JsonNode node) throws IOException {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
List<PGpoint> points = new ArrayList<>();
|
2025-10-22 14:51:17 +08:00
|
|
|
|
for (int i = 0; i < node.size(); i++) {
|
|
|
|
|
|
JsonNode coordNode = node.get(i);
|
|
|
|
|
|
|
|
|
|
|
|
if (!coordNode.isArray()) {
|
|
|
|
|
|
throw new IOException("坐标 " + i + " 必须是数组格式");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (coordNode.size() < 2) {
|
|
|
|
|
|
throw new IOException("坐标 " + i + " 至少需要2个值(经度和纬度)");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-10-21 14:57:52 +08:00
|
|
|
|
double x = coordNode.get(0).asDouble();
|
|
|
|
|
|
double y = coordNode.get(1).asDouble();
|
2025-10-22 14:51:17 +08:00
|
|
|
|
PGpoint point = new PGpoint(x, y);
|
|
|
|
|
|
points.add(point);
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
throw new IOException("坐标 " + i + " 格式错误: " + coordNode, e);
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
return createPGpolygonFromPoints(points);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解析字符串格式
|
|
|
|
|
|
*/
|
|
|
|
|
|
private PGpolygon parseFromString(String polygonString) throws IOException {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (polygonString == null || polygonString.trim().isEmpty()) {
|
|
|
|
|
|
throw new IOException("多边形字符串为空");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
try {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
PGpolygon polygon = new PGpolygon(polygonString);
|
|
|
|
|
|
System.out.println("字符串解析成功");
|
|
|
|
|
|
return polygon;
|
2025-10-21 14:57:52 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
throw new IOException("PGpolygon 字符串格式解析失败: " + polygonString, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从点列表创建 PGpolygon
|
|
|
|
|
|
*/
|
|
|
|
|
|
private PGpolygon createPGpolygonFromPoints(List<PGpoint> points) throws IOException {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (points == null) {
|
|
|
|
|
|
throw new IOException("点列表为 null");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (points.size() < 3) {
|
|
|
|
|
|
throw new IOException("多边形至少需要3个点,当前只有 " + points.size() + " 个点");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查点是否为 null
|
|
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
|
|
if (points.get(i) == null) {
|
|
|
|
|
|
throw new IOException("第 " + i + " 个点为 null");
|
|
|
|
|
|
}
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
// 构建正确的多边形字符串格式
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
|
|
PGpoint point = points.get(i);
|
2025-10-22 14:51:17 +08:00
|
|
|
|
if (i == 0) {
|
|
|
|
|
|
sb.append("("); // 开始内括号
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sb.append("(").append(point.x).append(",").append(point.y).append(")");
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
if (i < points.size() - 1) {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
sb.append(",");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
sb.append(")"); // 结束内括号
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 确保多边形闭合
|
2025-10-21 14:57:52 +08:00
|
|
|
|
PGpoint firstPoint = points.get(0);
|
|
|
|
|
|
PGpoint lastPoint = points.get(points.size() - 1);
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
if (firstPoint.x != lastPoint.x || firstPoint.y != lastPoint.y) {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
log.info("多边形未闭合,添加首点以闭合");
|
|
|
|
|
|
sb.append(",(").append(firstPoint.x).append(",").append(firstPoint.y).append(")");
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
2025-10-22 14:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String polygonString = sb.toString();
|
|
|
|
|
|
|
|
|
|
|
|
return new PGpolygon(polygonString);
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-21 14:57:52 +08:00
|
|
|
|
} catch (Exception e) {
|
2025-10-22 14:51:17 +08:00
|
|
|
|
throw new IOException("创建 PGpolygon 失败: " + e.getMessage(), e);
|
2025-10-21 14:57:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|