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/service/WaterService.java |  100 ++++++++++++++++++++++++++++---------------------
 1 files changed, 57 insertions(+), 43 deletions(-)

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