From c738483367653c6485ddc9a6dcdea019ad08cc63 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 31 十月 2024 17:44:02 +0800
Subject: [PATCH] 根据坐标查询积水深度

---
 src/main/java/com/se/simu/helper/ShpHelper.java           |   37 --------
 src/main/java/com/se/simu/domain/po/SimuPo.java           |   11 ++
 src/main/java/com/se/simu/controller/WaterController.java |   20 ++++
 src/main/java/com/se/simu/service/SimuService.java        |   13 +++
 src/main/java/com/se/simu/helper/GdalHelper.java          |   41 ++++++++++
 src/main/java/com/se/simu/service/WaterService.java       |  100 ++++++++++++++----------
 6 files changed, 142 insertions(+), 80 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/WaterController.java b/src/main/java/com/se/simu/controller/WaterController.java
index fea5fc0..68e77d5 100644
--- a/src/main/java/com/se/simu/controller/WaterController.java
+++ b/src/main/java/com/se/simu/controller/WaterController.java
@@ -1,8 +1,12 @@
 package com.se.simu.controller;
 
+import com.se.simu.domain.po.SimuPo;
 import com.se.simu.helper.WebHelper;
+import com.se.simu.service.SimuService;
 import com.se.simu.service.WaterService;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
@@ -25,6 +29,9 @@
 @RestController
 @RequestMapping("/waterlogging")
 public class WaterController {
+    @Resource
+    SimuService simuService;
+
     @Resource
     WaterService waterService;
 
@@ -121,14 +128,21 @@
 
     @ApiOperation(value = "鏍规嵁鍧愭爣鏌ヨ绉按娣卞害")
     @GetMapping("/{serviceName}/getWaterHeight")
-    public Double getWaterHeight(@PathVariable String serviceName, Double x, Double y, Long timestamp, HttpServletResponse res) {
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "serviceName", value = "鏈嶅姟鍚�", dataType = "String", paramType = "path", example = "20241010095328"),
+            @ApiImplicitParam(name = "x", value = "X", dataType = "double", paramType = "query", example = "116.6447998"),
+            @ApiImplicitParam(name = "y", value = "Y", dataType = "double", paramType = "query", example = "39.8868915"),
+            @ApiImplicitParam(name = "timestamp", value = "鏃堕棿鎴�", dataType = "long", paramType = "query", example = "1730217660000")
+    })
+    public Double getWaterHeight(@PathVariable String serviceName, double x, double y, long timestamp, HttpServletResponse res) {
         try {
-            if (!validate(serviceName, res)) {
+            SimuPo simu = simuService.getSimuByServiceName(serviceName);
+            if (null == simu) {
                 return null;
             }
 
             // 鏍规嵁鏈嶅姟鍚�+鏃堕棿鎴�+鍧愭爣锛屾煡璇㈠搴旂殑绉按娣卞害
-            return waterService.getWaterHeight(serviceName, x, y, timestamp);
+            return waterService.getWaterHeight(simu, x, y, timestamp);
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
             return null;
diff --git a/src/main/java/com/se/simu/domain/po/SimuPo.java b/src/main/java/com/se/simu/domain/po/SimuPo.java
index 6eaf338..df621b2 100644
--- a/src/main/java/com/se/simu/domain/po/SimuPo.java
+++ b/src/main/java/com/se/simu/domain/po/SimuPo.java
@@ -29,6 +29,9 @@
     @ApiModelProperty("鍚嶇О")
     private String name;
 
+    @ApiModelProperty("鏈嶅姟鍚�")
+    private String serviceName;
+
     @ApiModelProperty("鏁版嵁(JSON)")
     private String data;
 
@@ -91,6 +94,14 @@
         this.name = name;
     }
 
+    public String getServiceName() {
+        return serviceName;
+    }
+
+    public void setServiceName(String serviceName) {
+        this.serviceName = serviceName;
+    }
+
     public String getData() {
         return data;
     }
diff --git a/src/main/java/com/se/simu/helper/GdalHelper.java b/src/main/java/com/se/simu/helper/GdalHelper.java
index 1601ea1..3d996ff 100644
--- a/src/main/java/com/se/simu/helper/GdalHelper.java
+++ b/src/main/java/com/se/simu/helper/GdalHelper.java
@@ -6,6 +6,7 @@
 import org.gdal.gdal.gdal;
 import org.gdal.gdalconst.gdalconst;
 import org.gdal.ogr.*;
+import org.gdal.osr.CoordinateTransformation;
 import org.gdal.osr.SpatialReference;
 import org.gdal.osr.osr;
 
@@ -199,4 +200,44 @@
 
         return point;
     }
+
+    /**
+     * 杞崲涓篧GS84鍧愭爣
+     */
+    public static Geometry toWgs84(SpatialReference sr, double x, double y) {
+        Geometry point = new Geometry(ogr.wkbPoint);
+        point.AssignSpatialReference(sr);
+        point.AddPoint(x, y);
+
+        point.TransformTo(GdalHelper.SR4326);
+        //point.SwapXY();
+
+        return point;
+    }
+
+    /**
+     * WGS84杞崲涓虹洰鏍囧潗鏍�
+     */
+    public static double[] fromWgs84(SpatialReference sr, double x, double y) {
+        // https://blog.csdn.net/weixin_34910922/article/details/129208661
+        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
+        if (sr.IsProjected() != 1) {
+            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+        }
+
+        return ct.TransformPoint(x, y);
+    }
+
+    /**
+     * WGS84杞崲涓虹洰鏍囧潗鏍�
+     */
+    public static int fromWgs84(SpatialReference sr, Geometry point) {
+        // https://blog.csdn.net/weixin_34910922/article/details/129208661
+        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
+        if (sr.IsProjected() != 1) {
+            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+        }
+
+        return point.TransformTo(sr);
+    }
 }
diff --git a/src/main/java/com/se/simu/helper/ShpHelper.java b/src/main/java/com/se/simu/helper/ShpHelper.java
index f9f8778..b46120d 100644
--- a/src/main/java/com/se/simu/helper/ShpHelper.java
+++ b/src/main/java/com/se/simu/helper/ShpHelper.java
@@ -7,13 +7,9 @@
 import com.se.simu.domain.dto.GeLayer;
 import lombok.extern.slf4j.Slf4j;
 import org.gdal.ogr.*;
-import org.gdal.osr.CoordinateTransformation;
 import org.gdal.osr.SpatialReference;
-import org.gdal.osr.osr;
 
-import java.math.BigDecimal;
 import java.sql.Timestamp;
-import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Map;
@@ -71,7 +67,7 @@
         }
     }
 
-    private static void createFields(Layer layer,Map<String, Object> map) {
+    private static void createFields(Layer layer, Map<String, Object> map) {
         for (String key : map.keySet()) {
             Object val = map.get(key);
             switch (val.getClass().getTypeName()) {
@@ -125,8 +121,8 @@
     private static Geometry createPolygon(SpatialReference sr, Double minx, Double miny, Double maxx, Double maxy) {
         String epsg = sr.GetAuthorityCode(null);
         if (!("4326".equals(epsg) || "4490".equals(epsg))) {
-            double[] dmin = fromWgs84(sr, minx, miny);
-            double[] dmax = fromWgs84(sr, maxx, maxy);
+            double[] dmin = GdalHelper.fromWgs84(sr, minx, miny);
+            double[] dmax = GdalHelper.fromWgs84(sr, maxx, maxy);
             minx = dmin[0];
             miny = dmin[1];
             maxx = dmax[0];
@@ -302,32 +298,5 @@
 
     private String getEpsg(SpatialReference sr) {
         return sr.GetAuthorityCode(null);
-    }
-
-    /**
-     * 杞崲涓篧GS84鍧愭爣
-     */
-    public static Geometry toWgs84(SpatialReference sr, double x, double y) {
-        Geometry point = new Geometry(ogr.wkbPoint);
-        point.AssignSpatialReference(sr);
-        point.AddPoint(x, y);
-
-        point.TransformTo(GdalHelper.SR4326);
-        //point.SwapXY();
-
-        return point;
-    }
-
-    /**
-     * WGS84杞崲涓虹洰鏍囧潗鏍�
-     */
-    public static double[] fromWgs84(SpatialReference sr, double x, double y) {
-        // https://blog.csdn.net/weixin_34910922/article/details/129208661
-        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
-        if (sr.IsProjected() != 1) {
-            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
-        }
-
-        return ct.TransformPoint(x, y);
     }
 }
diff --git a/src/main/java/com/se/simu/service/SimuService.java b/src/main/java/com/se/simu/service/SimuService.java
index e888c5c..540f488 100644
--- a/src/main/java/com/se/simu/service/SimuService.java
+++ b/src/main/java/com/se/simu/service/SimuService.java
@@ -135,6 +135,18 @@
         return simuMapper.selectOne(wrapper);
     }
 
+    public SimuPo getSimuByServiceName(String serviceName) {
+        if (StringHelper.isEmpty(serviceName)) {
+            return null;
+        }
+
+        QueryWrapper<SimuPo> wrapper = new QueryWrapper<>();
+        wrapper.eq("service_name", serviceName);
+        wrapper.last("limit 1");
+
+        return simuMapper.selectOne(wrapper);
+    }
+
     public boolean create(CreateSimuVo vo) {
         Date now = new Date();
         String date = StringHelper.YMDHMS2_FORMAT.format(now);
@@ -147,6 +159,7 @@
         initPath(data);
 
         SimuPo simu = new SimuPo(vo.getNum(), vo.getPid(), vo.getName(), JSONUtil.toJsonStr(data), 0, vo.getBak());
+        simu.setServiceName(date);
         simu.setCreateTime(new Timestamp(now.getTime()));
 
         int rows = simuMapper.insert(simu);
diff --git a/src/main/java/com/se/simu/service/WaterService.java b/src/main/java/com/se/simu/service/WaterService.java
index 1c05a40..19a4736 100644
--- a/src/main/java/com/se/simu/service/WaterService.java
+++ b/src/main/java/com/se/simu/service/WaterService.java
@@ -1,13 +1,17 @@
 package com.se.simu.service;
 
+import cn.hutool.json.JSONUtil;
 import com.se.simu.config.PropertiesConfig;
+import com.se.simu.domain.po.DataPo;
+import com.se.simu.domain.po.SimuPo;
 import com.se.simu.domain.vo.*;
+import com.se.simu.helper.GdalHelper;
 import lombok.extern.slf4j.Slf4j;
-import org.gdal.gdal.Band;
 import org.gdal.gdal.Dataset;
 import org.gdal.gdal.gdal;
-import org.gdal.gdalconst.gdalconstConstants;
+import org.gdal.gdalconst.gdalconst;
 import org.gdal.osr.SpatialReference;
+import org.gdal.osr.osr;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -25,6 +29,7 @@
  */
 @Slf4j
 @Service
+@SuppressWarnings("ALL")
 public class WaterService {
     @Resource
     PropertiesConfig config;
@@ -123,64 +128,73 @@
     }
 
     /**
-     * 鏍规嵁鍧愭爣鏌ヨ绉按娣卞害
+     * 鏍规嵁鍧愭爣鏌ヨ绉按娣卞害:gdalconst.GA_Update
      */
-    public Double getWaterHeight(String serviceName, Double x, Double y, Long timestamp) {
-        String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "waters"
+    public Double getWaterHeight(SimuPo simu, double x, double y, Long timestamp) {
+        String filePath = config.getOutPath() + File.separator + simu.getServiceName() + File.separator + "waters"
                 + File.separator + timestamp + File.separator + "water.tif";
 
-        // 棣栧厛鏄墍鏈塯dal绋嬪簭閮介渶瑕佺殑娉ㄥ唽璇彞
-        gdal.AllRegister();
-        // 璇诲彇褰卞儚
-        Dataset hDataset = gdal.Open(filePath, gdalconstConstants.GA_Update);
+        Dataset ds = null;
+        try {
+            ds = gdal.Open(filePath, gdalconst.GA_ReadOnly);
+            if (null == ds || ds.getRasterCount() < 1) {
+                return null;
+            }
+            if (null == ds.GetSpatialRef()) {
+                ds.SetSpatialRef(getSpatialRef(simu));
+            }
 
-        // 璁剧疆鏁版嵁闆嗙殑鎶曞奖
-        SpatialReference srs = new SpatialReference();
-        srs.ImportFromEPSG(4548);
-        hDataset.SetProjection(srs.ExportToWkt());
+            double[] gt = ds.GetGeoTransform();
+            double[] xy = GdalHelper.fromWgs84(ds.GetSpatialRef(), x, y);
+            int[] XY = coordinates2ColRow(gt, xy[0], xy[1]);
 
-        //鑾峰彇鏍呮牸鏁伴噺
-        int numBands=hDataset.GetRasterCount();
-        System.out.println("RasterCount: " + numBands);
-        //鏋勯�犱豢灏勫彉鎹㈠弬鏁版暟缁勶紝骞惰幏鍙栨暟鎹�
-        double[] gt = new double[6];
-        hDataset.GetGeoTransform(gt);
-        System.out.println("浠垮皠鍙樻崲鍙傛暟"+ Arrays.toString(gt));
+            if (XY[0] < 0 || XY[1] < 0 || XY[0] > ds.getRasterXSize() || XY[1] > ds.getRasterYSize()) {
+                return null;
+            }
 
-        //缁忕含搴﹁浆鎹负鏍呮牸鍍忕礌鍧愭爣
-        int[] ColRow=Coordinates2ColRow(gt,x,y);
+            double[] values = new double[1];
+            ds.GetRasterBand(1).ReadRaster(XY[0], XY[1], 1, 1, values);
+            double val = values[0];
 
-        //鍒ゆ柇鏄惁鍧愭爣瓒呭嚭鑼冨洿
-        if(ColRow[0]<0||ColRow[1]<0||ColRow[0]>hDataset.getRasterXSize()||ColRow[1]>hDataset.getRasterYSize()){
-            System.out.println(Arrays.toString(ColRow)+"鍧愭爣鍊艰秴鍑烘爡鏍艰寖鍥达紒");
+            return isValid(val) ? val : null;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
             return null;
+        } finally {
+            if (null != ds) ds.delete();
         }
+    }
 
-        //鑾峰彇璇ョ偣瀵瑰簲鐨勬尝娈电殑鍊�
-        Band band = hDataset.GetRasterBand(1);
-        double[] values = new double[1];
-        band.ReadRaster(ColRow[0], ColRow[1], 1, 1, values);
-        double value = values[0];
+    private SpatialReference getSpatialRef(SimuPo simu) {
+        DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class);
 
-        //閲婃斁璧勬簮
-        hDataset.delete();
-        return value;
+        SpatialReference sr = new SpatialReference();
+        sr.ImportFromEPSG(data.getEpsg());
+        sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
+
+        return sr;
     }
 
     /**
      * 灏嗗湴鍥惧潗鏍囪浆鎹负鏍呮牸鍍忕礌鍧愭爣
+     *
      * @param gt 浠垮皠鍙樻崲鍙傛暟
-     * @param X 妯潗鏍�
-     * @param Y 绾靛潗鏍�
-     * @return
+     * @param x  妯潗鏍�
+     * @param y  绾靛潗鏍�
+     * @return 鍍忕礌鍧愭爣
      */
-    public int[] Coordinates2ColRow(double[] gt, double X, double Y){
+    public int[] coordinates2ColRow(double[] gt, double x, double y) {
         int[] ints = new int[2];
-        //鍚戜笅鍙栨暣,濡傛灉鍚戜笂鍙栨暣浼氬鑷磋绠楃粨鏋滃亸澶э紝浠庤�屽湪鍚庨潰璇诲彇鍒伴偦杩戝儚鍏冪殑鏁版嵁
-        double  Yline = Math.floor(((Y - gt[3])*gt[1] - (X-gt[0])*gt[4]) / (gt[5]*gt[1]-gt[2]*gt[4]));
-        double  Xpixel = Math.floor((X-gt[0] - Yline*gt[2])/gt[1]);
-        ints[0] = new Double(Xpixel).intValue();
-        ints[1] = new Double(Yline).intValue();
-        return ints;
+
+        // 鍚戜笅鍙栨暣,濡傛灉鍚戜笂鍙栨暣浼氬鑷磋绠楃粨鏋滃亸澶э紝浠庤�屽湪鍚庨潰璇诲彇鍒伴偦杩戝儚鍏冪殑鏁版嵁
+        //Double col = Math.floor(((y - gt[3]) * gt[1] - (x - gt[0]) * gt[4]) / (gt[5] * gt[1] - gt[2] * gt[4]));
+        Double col = Math.floor((y * gt[1] - x * gt[4] + gt[0] * gt[4] - gt[3] * gt[1]) / (gt[5] * gt[1] - gt[2] * gt[4]));
+        Double row = Math.floor((x - gt[0] - col * gt[2]) / gt[1]);
+
+        return new int[]{row.intValue(), col.intValue()};
+    }
+
+    public static boolean isValid(double val) {
+        return !Double.isNaN(val) && val > Integer.MIN_VALUE;
     }
 }

--
Gitblit v1.9.3