Quellcode durchsuchen

+ bd 围栏闯禁事件,bd 围栏绘制

chen.cheng vor 9 Monaten
Ursprung
Commit
d4503ba711

+ 6 - 0
pom.xml

@@ -33,6 +33,7 @@
         <jwt.version>0.9.1</jwt.version>
         <forest.version>1.5.9</forest.version>
         <cffu.version>1.0.0-Alpha19</cffu.version>
+        <jts.version>1.18.2</jts.version>
     </properties>
 
     <!-- 依赖声明 -->
@@ -194,6 +195,11 @@
                 <artifactId>cffu</artifactId>
                 <version>${cffu.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.locationtech.jts</groupId>
+                <artifactId>jts-core</artifactId>
+                <version>${jts.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 

+ 8 - 3
ruoyi-common/pom.xml

@@ -52,13 +52,13 @@
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
         </dependency>
-  
+
         <!-- JSON工具类 -->
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>
-        
+
         <!-- 阿里JSON解析器 -->
         <dependency>
             <groupId>com.alibaba.fastjson2</groupId>
@@ -119,6 +119,11 @@
             <artifactId>javax.servlet-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.locationtech.jts</groupId>
+            <artifactId>jts-core</artifactId>
+        </dependency>
+
     </dependencies>
 
-</project>
+</project>

+ 144 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/geo/GeoUtils.java

@@ -0,0 +1,144 @@
+package com.ruoyi.common.utils.geo;
+
+import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.GeometryFactory;
+import org.locationtech.jts.geom.Point;
+import org.locationtech.jts.geom.Polygon;
+import org.locationtech.jts.io.ParseException;
+import org.locationtech.jts.io.WKTReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+public class GeoUtils {
+    private static final Logger log = LoggerFactory.getLogger(GeoUtils.class);
+    /**
+     * geometryFactory
+     */
+    private static final GeometryFactory geometryFactory = new GeometryFactory();
+
+    /**
+     * Gets poly center.
+     * POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))
+     *
+     * @param wktPolygon the wkt polygon
+     * @return the poly center
+     * @author chen.cheng
+     */
+    public static Point getPolyCenter(String wktPolygon) {
+        // 创建WKTReader对象
+        WKTReader reader = new WKTReader(geometryFactory);
+        // 从WKT字符串中读取几何对象
+        Geometry geometry = null;
+        try {
+            geometry = reader.read(wktPolygon);
+        } catch (ParseException e) {
+            log.info("Invalid WKT string: " + e.getMessage());
+        }
+        // 获取中心点
+        return geometry.getCentroid();
+    }
+
+    /**
+     * 判断指定的GPS点是否在电子围栏内
+     *
+     * @param fencePointsList 包含电子围栏的经纬度数据的列表,格式为 "经度,纬度"
+     * @param pointStr        指定的GPS点,格式为 "经度,纬度"
+     * @return 如果在电子围栏内则返回true,否则返回false
+     */
+    public static boolean isPointInGeoFence(List<String> fencePointsList, String pointStr) {
+        // 将电子围栏的经纬度数据转换为坐标数组
+        Coordinate[] fencePoints = parseCoordinates(fencePointsList);
+
+        // 将指定的GPS点转换为坐标
+        Coordinate targetPoint = parseCoordinate(pointStr);
+
+        // 创建电子围栏多边形
+        Polygon geoFencePolygon = createPolygon(fencePoints);
+
+        // 创建指定的GPS点
+        Point point = geometryFactory.createPoint(targetPoint);
+
+        // 检查指定的GPS点是否在电子围栏内
+        return geoFencePolygon.contains(point);
+    }
+
+    /**
+     * 判断指定的GPS点是否在电子围栏内
+     *
+     * @param geoFencePolygon geo fence polygon
+     * @param pointStr        指定的GPS点,格式为 "经度,纬度"
+     * @return 如果在电子围栏内则返回true ,否则返回false
+     * @since 2.0.0
+     */
+    public static boolean isPointInGeoFence(Polygon geoFencePolygon, String pointStr) {
+        // 将指定的GPS点转换为坐标
+        Coordinate testPoint = parseCoordinate(pointStr);
+
+        // 创建指定的GPS点
+        Point point = geometryFactory.createPoint(testPoint);
+
+        // 检查指定的GPS点是否在电子围栏内
+        return geoFencePolygon.contains(point);
+    }
+
+    /**
+     * 判断指定的GPS点是否在电子围栏内
+     *
+     * @param fencePointsList 包含电子围栏的经纬度数据的列表,格式为 "经度,纬度"
+     * @return 如果在电子围栏内则返回true,否则返回false
+     */
+    public static Polygon getPointInGeoFence(List<String> fencePointsList) {
+        // 将电子围栏的经纬度数据转换为坐标数组
+        Coordinate[] fencePoints = parseCoordinates(fencePointsList);
+        // 创建电子围栏
+        return createPolygon(fencePoints);
+    }
+
+
+    /**
+     * 根据GPS点集合创建多边形
+     *
+     * @param coordinates GPS点集合
+     * @return 多边形对象
+     */
+    private static Polygon createPolygon(Coordinate[] coordinates) {
+        // Ensure the polygon is closed by adding the first coordinate at the end if necessary
+        if (!coordinates[0].equals(coordinates[coordinates.length - 1])) {
+            Coordinate[] closedCoordinates = new Coordinate[coordinates.length + 1];
+            System.arraycopy(coordinates, 0, closedCoordinates, 0, coordinates.length);
+            closedCoordinates[closedCoordinates.length - 1] = coordinates[0];
+            coordinates = closedCoordinates;
+        }
+        return geometryFactory.createPolygon(coordinates);
+    }
+
+    /**
+     * 将包含经纬度数据的列表转换为坐标数组
+     *
+     * @param pointsList 包含经纬度数据的列表
+     * @return 坐标数组
+     */
+    private static Coordinate[] parseCoordinates(List<String> pointsList) {
+        Coordinate[] coordinates = new Coordinate[pointsList.size()];
+        for (int i = 0; i < pointsList.size(); i++) {
+            coordinates[i] = parseCoordinate(pointsList.get(i));
+        }
+        return coordinates;
+    }
+
+    /**
+     * 将经纬度数据字符串转换为坐标
+     *
+     * @param pointStr 经纬度数据字符串,格式为 "经度,纬度"
+     * @return 坐标
+     */
+    private static Coordinate parseCoordinate(String pointStr) {
+        String[] parts = pointStr.split(",");
+        double lon = Double.parseDouble(parts[0]);
+        double lat = Double.parseDouble(parts[1]);
+        return new Coordinate(lon, lat);
+    }
+}

+ 4 - 0
ruoyi-system/pom.xml

@@ -30,6 +30,10 @@
             <groupId>io.foldright</groupId>
             <artifactId>cffu</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.locationtech.jts</groupId>
+            <artifactId>jts-core</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

+ 20 - 21
ruoyi-system/src/main/java/com/ruoyi/bd/service/impl/BdFenceInfoServiceImpl.java

@@ -1,7 +1,10 @@
 package com.ruoyi.bd.service.impl;
 
 import java.util.List;
+
 import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.utils.geo.GeoUtils;
+import org.locationtech.jts.geom.Point;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.ruoyi.bd.mapper.BdFenceInfoMapper;
@@ -10,87 +13,83 @@ import com.ruoyi.bd.service.IBdFenceInfoService;
 
 /**
  * 围栏基础信息Service业务层处理
- * 
+ *
  * @author ruoyi
  * @date 2024-10-14
  */
 @Service
-public class BdFenceInfoServiceImpl implements IBdFenceInfoService 
-{
+public class BdFenceInfoServiceImpl implements IBdFenceInfoService {
     @Autowired
     private BdFenceInfoMapper bdFenceInfoMapper;
 
     /**
      * 查询围栏基础信息
-     * 
+     *
      * @param id 围栏基础信息主键
      * @return 围栏基础信息
      */
     @Override
-    public BdFenceInfo selectBdFenceInfoById(Long id)
-    {
+    public BdFenceInfo selectBdFenceInfoById(Long id) {
         return bdFenceInfoMapper.selectBdFenceInfoById(id);
     }
 
     /**
      * 查询围栏基础信息列表
-     * 
+     *
      * @param bdFenceInfo 围栏基础信息
      * @return 围栏基础信息
      */
     @Override
-    public List<BdFenceInfo> selectBdFenceInfoList(BdFenceInfo bdFenceInfo)
-    {
+    public List<BdFenceInfo> selectBdFenceInfoList(BdFenceInfo bdFenceInfo) {
         return bdFenceInfoMapper.selectBdFenceInfoList(bdFenceInfo);
     }
 
     /**
      * 新增围栏基础信息
-     * 
+     *
      * @param bdFenceInfo 围栏基础信息
      * @return 结果
      */
     @Override
-    public int insertBdFenceInfo(BdFenceInfo bdFenceInfo)
-    {
+    public int insertBdFenceInfo(BdFenceInfo bdFenceInfo) {
         bdFenceInfo.setCreateTime(DateUtils.getNowDate());
+        Point polyCenter = GeoUtils.getPolyCenter(bdFenceInfo.getPoly());
+        bdFenceInfo.setCenterLat(polyCenter.getY());
+        bdFenceInfo.setCenterLng(polyCenter.getX());
         return bdFenceInfoMapper.insertBdFenceInfo(bdFenceInfo);
     }
 
     /**
      * 修改围栏基础信息
-     * 
+     *
      * @param bdFenceInfo 围栏基础信息
      * @return 结果
      */
     @Override
-    public int updateBdFenceInfo(BdFenceInfo bdFenceInfo)
-    {
+    public int updateBdFenceInfo(BdFenceInfo bdFenceInfo) {
         bdFenceInfo.setUpdateTime(DateUtils.getNowDate());
         return bdFenceInfoMapper.updateBdFenceInfo(bdFenceInfo);
     }
 
     /**
      * 批量删除围栏基础信息
-     * 
+     *
      * @param ids 需要删除的围栏基础信息主键
      * @return 结果
      */
     @Override
-    public int deleteBdFenceInfoByIds(Long[] ids)
-    {
+    public int deleteBdFenceInfoByIds(Long[] ids) {
         return bdFenceInfoMapper.deleteBdFenceInfoByIds(ids);
     }
 
     /**
      * 删除围栏基础信息信息
-     * 
+     *
      * @param id 围栏基础信息主键
      * @return 结果
      */
     @Override
-    public int deleteBdFenceInfoById(Long id)
-    {
+    public int deleteBdFenceInfoById(Long id) {
         return bdFenceInfoMapper.deleteBdFenceInfoById(id);
     }
 }

+ 7 - 7
ruoyi-system/src/main/resources/mapper/bd/BdFenceInfoMapper.xml

@@ -3,7 +3,7 @@
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.ruoyi.bd.mapper.BdFenceInfoMapper">
-    
+
     <resultMap type="BdFenceInfo" id="BdFenceInfoResult">
         <result property="id"    column="id"    />
         <result property="defenceName"    column="defence_name"    />
@@ -22,11 +22,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
     <select id="selectBdFenceInfoList" parameterType="BdFenceInfo" resultMap="BdFenceInfoResult">
         <include refid="selectBdFenceInfoVo"/>
-        <where>  
+        <where>
             <if test="defenceName != null  and defenceName != ''"> and defence_name like concat('%', #{defenceName}, '%')</if>
         </where>
     </select>
-    
+
     <select id="selectBdFenceInfoById" parameterType="Long" resultMap="BdFenceInfoResult">
         <include refid="selectBdFenceInfoVo"/>
         where id = #{id}
@@ -46,7 +46,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="defenceName != null">#{defenceName},</if>
-            <if test="poly != null">#{poly},</if>
+            <if test="poly != null">ST_GeomFromText(#{poly}),</if>
             <if test="centerLng != null">#{centerLng},</if>
             <if test="centerLat != null">#{centerLat},</if>
             <if test="updateTime != null">#{updateTime},</if>
@@ -60,7 +60,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         update bd_fence_info
         <trim prefix="SET" suffixOverrides=",">
             <if test="defenceName != null">defence_name = #{defenceName},</if>
-            <if test="poly != null">poly = #{poly},</if>
+            <if test="poly != null">poly = ST_GeomFromText(#{poly}),</if>
             <if test="centerLng != null">center_lng = #{centerLng},</if>
             <if test="centerLat != null">center_lat = #{centerLat},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -76,9 +76,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </delete>
 
     <delete id="deleteBdFenceInfoByIds" parameterType="String">
-        delete from bd_fence_info where id in 
+        delete from bd_fence_info where id in
         <foreach item="id" collection="array" open="(" separator="," close=")">
             #{id}
         </foreach>
     </delete>
-</mapper>
+</mapper>