From 6365456e5476830267fde30914a1b260d2ec6462 Mon Sep 17 00:00:00 2001
From: xingjinshuang <xingjs@qq.com>
Date: 星期四, 26 十二月 2024 15:20:23 +0800
Subject: [PATCH] @xingjs@20241226@添加查询管线工具类,修改解决查询站点接口token存在无法获取的情况,解决后可正常获取token,并查询出需要的站点;解析shp文件获取站点范围

---
 src/main/java/com/se/simu/utils/ShpToolUtils.java        |  171 ++++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/se/simu/controller/SimuController.java |   22 ++++-
 2 files changed, 187 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java
index 0f3d346..63327f0 100644
--- a/src/main/java/com/se/simu/controller/SimuController.java
+++ b/src/main/java/com/se/simu/controller/SimuController.java
@@ -1,5 +1,6 @@
 package com.se.simu.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.se.simu.config.PropertiesConfig;
 import com.se.simu.domain.dto.GeDb;
@@ -11,6 +12,7 @@
 import com.se.simu.domain.vo.R;
 import com.se.simu.domain.vo.SimuVo;
 import com.se.simu.service.*;
+import com.se.simu.utils.ShpToolUtils;
 import io.swagger.annotations.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
@@ -208,12 +210,24 @@
             // todo: 瑙f瀽鑼冨洿鏂囦欢 鏍规嵁涓嶅悓鐨勭殑鏍煎紡鏂囦欢杩涜涓嶅悓鐨勮В鏋愶紝鑾峰彇闇�瑕佽绠楃殑鑼冨洿鍊�
             //  涓婁紶鏍煎紡锛�.shp/.tiff/.img/.geojson
             //  閲嶆柊缁檝o璧嬪��
+            // 1 璇诲彇shp鏂囦欢锛岃幏鍙栬寖鍥村��
+            JSONObject jsonObject = ShpToolUtils.readShp(floodFile);
+            // 2 鑾峰彇jsonObject涓殑鑼冨洿鍊�
+            vo.setMinx(jsonObject.getDouble("minX"));
+            vo.setMaxx(jsonObject.getDouble("maxX"));
+            vo.setMiny(jsonObject.getDouble("minY"));
+            vo.setMaxy(jsonObject.getDouble("maxY"));
         } catch (Exception e) {
             log.error("瑙f瀽鑼冨洿鏂囦欢澶辫触");
+            if (null == vo.getMinx() || null == vo.getMiny() || null == vo.getMaxx() || null == vo.getMaxy()) {
+                return fail("瑙f瀽鑼冨洿鏂囦欢澶辫触锛侀�夋嫨鑼冨洿涓嶈兘涓虹┖锛岃閲嶆柊閫夋嫨鏂囦欢锛�", false);
+            }
         }
 
         String stationFile = vo.getStationFile();
         log.info("绔欑偣鏂囦欢shp鍦板潃锛歿}", floodFile);
+
+
         String stationRainFile = vo.getStationRainFile();
         log.info("绔欑偣闆ㄩ噺CSV鏂囦欢鍦板潃锛歿}", floodFile);
         // TODO: 2024/12/24 鏍规嵁杩欎簺鏂囦欢鐨勫湴鍧�锛岃幏鍙栨枃浠跺唴瀹癸紝鍒涘缓浠跨湡瑙嗗浘
@@ -225,21 +239,16 @@
             if (null == vo.getDuration() || vo.getDuration() < 1 || vo.getDuration() > 10080) {
                 return fail("浠跨湡鏃堕暱涓嶈兘涓虹┖锛屼笖鍙栧�煎湪1~10080涔嬮棿");
             }
-            if (null == vo.getMinx() || null == vo.getMiny() || null == vo.getMaxx() || null == vo.getMaxy()) {
-                return fail("閫夋嫨鑼冨洿涓嶈兘涓虹┖", false);
-            }
             if (null == vo.getPid() || vo.getPid() < 0) {
                 vo.setPid(0);
             }
             if (null == vo.getNum() || vo.getNum() < 1) {
                 vo.setNum(simuService.getMaxId() + 1);
             }
-            if (null == vo.getPid() || vo.getPid() < 0) {
-                vo.setPid(0);
-            }
             if (null == vo.getStartTime()) {
                 vo.setStartTime(new Date());
             }
+            // 闃叉睕浣滀笟
             if (vo.getPid() > 0) {
                 SimuPo pp = simuService.getSimuByPid(vo.getPid());
                 if (null == pp) {
@@ -261,6 +270,7 @@
                     return fail("闃叉睕鑼冨洿涓嶈兘涓虹┖", false);
                 }
             }
+            // 寮�濮嬫ā鎷熻绠�
             boolean flag = simuFilesService.createByfiles(vo);
             return success(flag, flag ? "鎴愬姛" : "澶辫触");
         } catch (Exception ex) {
diff --git a/src/main/java/com/se/simu/utils/ShpToolUtils.java b/src/main/java/com/se/simu/utils/ShpToolUtils.java
new file mode 100644
index 0000000..ab44469
--- /dev/null
+++ b/src/main/java/com/se/simu/utils/ShpToolUtils.java
@@ -0,0 +1,171 @@
+package com.se.simu.utils;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import org.gdal.gdal.gdal;
+import org.gdal.ogr.*;
+import org.gdal.osr.CoordinateTransformation;
+import org.gdal.osr.SpatialReference;
+
+import java.util.Arrays;
+
+/**
+ * shp 宸ュ叿瀹炵敤绋嬪簭
+ *
+ * @author xingjinshuang@smartearth.cn
+ * @date 2024/12/26
+ */
+public class ShpToolUtils {
+
+    // 澹版槑涓虹被鎴愬憳鍙橀噺
+    private static double minX = Double.MAX_VALUE;
+    private static double maxX = Double.MIN_VALUE;
+    private static double minY = Double.MAX_VALUE;
+    private static double maxY = Double.MIN_VALUE;
+
+    // 鐢ㄤ簬瀛樺偍鎵�鏈夎浆鎹㈠悗鐨勭粡绾害
+    private static JSONArray coordinatesArray = new JSONArray();
+
+    /**
+     * 闃呰 SHP
+     *
+     * @param strVectorFile str 鍚戦噺鏂囦欢
+     * @return {@link JSONObject}
+     */
+    public static JSONObject readShp(String strVectorFile) {
+        // 娉ㄥ唽鎵�鏈夌殑椹卞姩
+        ogr.RegisterAll();
+        // 涓轰簡鏀寔涓枃璺緞锛岃娣诲姞涓嬮潰杩欏彞浠g爜
+        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
+
+        // 璇诲彇鏁版嵁锛岃繖閲屼互ESRI鐨剆hp鏂囦欢涓轰緥
+        String strDriverName = "ESRI Shapefile";
+        org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
+
+        if (oDriver == null) {
+            System.out.println(strDriverName + " 椹卞姩涓嶅彲鐢紒\n");
+            return null;
+        }
+
+        // 鎵撳紑鏁版嵁婧�
+        DataSource dataSource = oDriver.Open(strVectorFile);
+        if (dataSource == null) {
+            System.out.println("鏃犳硶鎵撳紑 Shapefile 鏂囦欢锛�");
+            return null;
+        }
+
+        Layer layer = dataSource.GetLayer(0);
+
+        // 鑾峰彇鍥惧眰鐨勮寖鍥达紙鍒濇鐨勭煩褰㈣寖鍥达級
+        double[] layerExtent = layer.GetExtent();
+        System.out.println("鍒濆鍥惧眰鑼冨洿锛歮inx:" + layerExtent[0] + ", maxx:" + layerExtent[1] + ", miny:" + layerExtent[2] + ", maxy:" + layerExtent[3]);
+
+        // 鑾峰彇鍥惧眰鐨勭┖闂村弬鑰�
+        SpatialReference layerSpatialRef = layer.GetSpatialRef();
+
+        // 鍒涘缓鐩爣绌洪棿鍙傝�� (EPSG:4326)
+        SpatialReference targetSpatialRef = new SpatialReference();
+        targetSpatialRef.ImportFromEPSG(4326); // EPSG:4326 鏄� WGS84
+
+        // 鍒涘缓鍧愭爣杞崲瀵硅薄
+        CoordinateTransformation coordTransform = CoordinateTransformation.CreateCoordinateTransformation(layerSpatialRef, targetSpatialRef);
+
+        // 閬嶅巻姣忎釜 feature 鑾峰彇鍧愭爣鐐�
+        for (int i = 0; i < layer.GetFeatureCount(); i++) {
+            Feature feature = layer.GetFeature(i);
+            Geometry geometry = feature.GetGeometryRef();
+            // 鍒ゆ柇鍑犱綍绫诲瀷骞跺鐞�
+            if (geometry != null) {
+                if (geometry.GetGeometryType() == ogr.wkbPoint) {
+                    // 鍗曚釜鐐圭殑澶勭悊
+                    processPointGeometry(geometry, coordTransform);
+                } else if (geometry.GetGeometryType() == ogr.wkbMultiPoint) {
+                    // 澶氫釜鐐圭殑澶勭悊
+                    System.out.println("geometry = " + geometry);
+                    //processMultiPointGeometry(geometry, coordTransform);
+                }
+            }
+        }
+        // 鎵撳嵃杞崲鍚庣殑鐭╁舰鑼冨洿锛堢粡绾害锛�
+        System.out.println("鎵�鏈夌偣鐨勭粡绾害鐭╁舰鑼冨洿锛歮inX = " + minX + ", maxX = " + maxX + ", minY = " + minY + ", maxY = " + maxY);
+        JSONObject json = new JSONObject();
+        json.put("minX", minX);
+        json.put("maxX", maxX);
+        json.put("minY", minY);
+        json.put("maxY", maxY);
+        return json;
+    }
+
+    // 澶勭悊鍗曚釜鐐圭殑鍑犱綍浣�
+    private static void processPointGeometry(Geometry geometry, CoordinateTransformation coordTransform) {
+        double x = geometry.GetX();
+        double y = geometry.GetY();
+
+        // 鍒涘缓鍖呭惈 3 涓厓绱犵殑鏁扮粍锛孼 鍧愭爣鍙互璁句负 0
+        double[] coords = new double[]{x, y, 0}; // Z 鍧愭爣榛樿鍊间负 0
+        //System.out.println("鍘熷鍧愭爣1锛歝oords = " + Arrays.toString(coords));
+
+        // 杞崲鍧愭爣
+        coordTransform.TransformPoint(coords);
+        //System.out.println("杞崲鍚庣殑鍧愭爣锛歝oords = " + Arrays.toString(coords));
+
+        // 鍧愭爣椤哄簭鏄� [latitude, longitude]锛屼氦鎹㈤『搴忎负 [longitude, latitude]
+        double longitude = coords[0];
+        double latitude = coords[1];
+        //System.out.println("杞崲鍚庣殑缁忕含搴︼細longitude = " + longitude + ", latitude = " + latitude);
+        // 鍒涘缓涓�涓� JSON 瀵硅薄淇濆瓨缁忕含搴︿俊鎭�
+        JSONObject coordObj = new JSONObject();
+        coordObj.put("lat", longitude);
+        coordObj.put("lon", latitude);
+        // 灏嗘 JSON 瀵硅薄娣诲姞鍒� JSONArray 涓�
+        coordinatesArray.add(coordObj);
+
+        // 鏇存柊鐭╁舰杈圭晫锛堟澶勪娇鐢ㄥ叏灞�鍙橀噺鎴栬繑鍥炲�艰繘琛岃绠楋級
+        updateRectangleBounds(coords);
+    }
+
+    // 澶勭悊澶氱偣鐨勫嚑浣曚綋
+    private static void processMultiPointGeometry(Geometry geometry, CoordinateTransformation coordTransform) {
+        int numPoints = geometry.GetGeometryCount();
+        for (int j = 0; j < numPoints; j++) {
+            Geometry pointGeometry = geometry.GetGeometryRef(j);
+            double x = pointGeometry.GetX();
+            double y = pointGeometry.GetY();
+
+            // 鍒涘缓鍖呭惈 3 涓厓绱犵殑鏁扮粍锛孼 鍧愭爣鍙互璁句负 0
+            double[] coords = new double[]{x, y, 0}; // Z 鍧愭爣榛樿鍊间负 0
+            System.out.println("鍘熷鍧愭爣2锛歝oords = " + Arrays.toString(coords));
+
+            // 杞崲鍧愭爣
+            coordTransform.TransformPoint(coords);
+            System.out.println("杞崲鍚庣殑鍧愭爣锛歝oords = " + Arrays.toString(coords));
+
+            // 鍧愭爣椤哄簭鏄� [latitude, longitude]锛屼氦鎹㈤『搴忎负 [longitude, latitude]
+            double longitude = coords[0];
+            double latitude = coords[1];
+            System.out.println("杞崲鍚庣殑缁忕含搴︼細longitude = " + longitude + ", latitude = " + latitude);
+
+            // 鏇存柊鐭╁舰杈圭晫
+            updateRectangleBounds(coords);
+        }
+    }
+
+    // 鏇存柊鐭╁舰鐨勮竟鐣屽��
+    private static void updateRectangleBounds(double[] coords) {
+        double x = coords[0];
+        double y = coords[1];
+
+        // 鏇存柊鏈�灏忔渶澶у��
+        minX = Math.min(minX, x);
+        maxX = Math.max(maxX, x);
+        minY = Math.min(minY, y);
+        maxY = Math.max(maxY, y);
+    }
+
+    public static void main(String[] args) {
+        // 璇诲彇shp鏂囦欢
+        readShp("D:\\0a_project\\model\\shp\\闆ㄩ噺绔欑偣鏁版嵁\\闆ㄩ噺绔欑偣_4548\\闆ㄩ噺绔欑偣_4548.shp");
+        System.out.println(coordinatesArray.toString()); // Pretty print with indent
+
+    }
+}

--
Gitblit v1.9.3