From f3a8ace95bb141026b694b70df4d41bf284c6a7d Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期三, 11 三月 2026 21:50:11 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/Robotaxi
---
xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java | 39 ++++++++++++++++++++++++++++-----------
1 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java b/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java
index de03958..4e1b063 100644
--- a/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java
+++ b/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java
@@ -46,10 +46,7 @@
import javax.annotation.Resource;
import java.awt.geom.Point2D;
import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -729,14 +726,34 @@
* @return
*/
private org.locationtech.jts.geom.Point calculateCentroid(Coordinate[] coordinates) {
- GeometryFactory factory = new GeometryFactory();
- // 创建线性环 (LinearRing)
- LinearRing ring = factory.createLinearRing(coordinates);
- // 创建多边形 (Polygon)
- Polygon polygon = factory.createPolygon(ring);
- // 计算质心 (Centroid)
- org.locationtech.jts.geom.Point centroid = polygon.getCentroid();
+ // 1. 判空校验
+ if (coordinates == null || coordinates.length == 0) {
+ throw new ServiceException("坐标数组不能为空");
+ }
+ // 2. 过滤掉 null 坐标点
+ Coordinate[] validCoords = Arrays.stream(coordinates)
+ .filter(Objects::nonNull)
+ .toArray(Coordinate[]::new);
+
+ // 3. LinearRing 至少需要 4 个点(首尾相同),且必须闭合
+ if (validCoords.length < 4) {
+ throw new ServiceException("有效坐标点不足,至少需要4个点");
+ }
+
+ // 4. 确保首尾坐标相同(闭合)
+ Coordinate first = validCoords[0];
+ Coordinate last = validCoords[validCoords.length - 1];
+ if (!first.equals2D(last)) {
+ // 自动补上闭合点
+ validCoords = Arrays.copyOf(validCoords, validCoords.length + 1);
+ validCoords[validCoords.length - 1] = new Coordinate(first.x, first.y);
+ }
+
+ GeometryFactory factory = new GeometryFactory();
+ LinearRing ring = factory.createLinearRing(validCoords);
+ Polygon polygon = factory.createPolygon(ring);
+ org.locationtech.jts.geom.Point centroid = polygon.getCentroid();
System.out.println("中心点经度: " + centroid.getX());
System.out.println("中心点纬度: " + centroid.getY());
return centroid;
--
Gitblit v1.7.1