From 9ba86955948dff0655ce7f028beddfdce2bad0b4 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 11 九月 2023 12:12:41 +0800
Subject: [PATCH] 栅格分析-分析点数据

---
 src/main/java/com/moon/server/service/data/RasterAnalysisService.java |  172 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 120 insertions(+), 52 deletions(-)

diff --git a/src/main/java/com/moon/server/service/data/RasterAnalysisService.java b/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
index fcd5f35..9088003 100644
--- a/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
+++ b/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -1,6 +1,12 @@
 package com.moon.server.service.data;
 
+import com.moon.server.entity.all.StaticData;
 import com.moon.server.entity.data.AnalysisResultEntity;
+import com.moon.server.entity.data.MetaEntity;
+import com.moon.server.entity.data.PublishEntity;
+import com.moon.server.helper.PathHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.gdal.gdal.Band;
 import org.gdal.gdal.Dataset;
 import org.gdal.gdal.WarpOptions;
@@ -9,6 +15,8 @@
 import org.gdal.ogr.ogr;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Vector;
@@ -20,67 +28,127 @@
  */
 @Service
 public class RasterAnalysisService {
-    public List<AnalysisResultEntity> analysisPoint(String wkt, Integer pixel) {
+    @Resource
+    PathHelper pathHelper;
+
+    @Resource
+    PublishService publishService;
+
+    private final static Log log = LogFactory.getLog(RasterAnalysisService.class);
+
+    /**
+     * 鍒嗘瀽鐐�
+     */
+    public List<AnalysisResultEntity> analysisPoint(Geometry point, int size) {
         List<AnalysisResultEntity> rs = new ArrayList<>();
-        //
 
-        return rs;
-    }
-
-    public List<AnalysisResultEntity> analysisPolyline(String wkt) {
-        List<AnalysisResultEntity> rs = new ArrayList<>();
-        //
-
-        return rs;
-    }
-    public List<AnalysisResultEntity> analysisPolygon(String wkt) {
-        List<AnalysisResultEntity> rs = new ArrayList<>();
-        //
-
-        return rs;
-    }
-
-
-    public double processPoint(String imagePath, String geometryString, int size) {
-        // 鎵撳紑鏍呮牸鍥惧儚鏁版嵁闆�
-        Dataset dataset = gdal.Open(imagePath);
-        if (dataset == null) {
-            throw new RuntimeException("Failed to open raster dataset.");
+        List<PublishEntity> pubs = publishService.selectRaster();
+        if (null == pubs || pubs.isEmpty()) {
+            return rs;
         }
 
-        Geometry geometry = Geometry.CreateFromWkt(geometryString);
+        for (PublishEntity pub : pubs) {
+            AnalysisResultEntity entity = new AnalysisResultEntity();
+            entity.setLayerName(pub.getName());
 
-        double[] geoTransform = dataset.GetGeoTransform();
-        double minX = geoTransform[0];
-        double pixelWidth = geoTransform[1];
-        double rotationX = geoTransform[2];
-        double maxY = geoTransform[3];
-        double rotationY = geoTransform[4];
-        double pixelHeight = geoTransform[5];
-
-        double x = geometry.GetX();
-        double y = geometry.GetY();
-
-        int xPixel = (int) Math.floor((x - minX) / pixelWidth);
-        int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
-
-        int bandCount = dataset.getRasterCount();
-
-        List<double[]> sum = new ArrayList<>();
-        for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) {
-            double[] pixelValues = new double[size * size];
-            Band band = dataset.GetRasterBand(bandIndex);
-            band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
-
-            sum.add(pixelValues);
+            processPoint(entity, pub, point, size);
+            rs.add(entity);
         }
 
-        // 閲婃斁璧勬簮
-        gdal.GDALDestroyDriverManager();
-
-        return average(calculateAverage(sum));
+        return rs;
     }
 
+    /**
+     * 璁剧疆閿欒
+     */
+    private void setError(AnalysisResultEntity entity, String info) {
+        entity.setCode(StaticData.I500);
+        entity.setInfo(info);
+    }
+
+    /**
+     * 澶勭悊鐐�
+     */
+    private void processPoint(AnalysisResultEntity entity, PublishEntity pub, Geometry point, int size) {
+        List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId());
+        if (null == metas || metas.isEmpty()) {
+            setError(entity, "鎵句笉鍒板彂甯冩暟鎹�");
+            return;
+        }
+
+        String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath();
+        File file = new File(filePath);
+        if (!file.exists() || file.isDirectory()) {
+            setError(entity, "婧愭暟鎹笉瀛樺湪");
+            return;
+        }
+
+        processPoint(entity, filePath, point, size);
+    }
+
+    /**
+     * 澶勭悊鐐�
+     */
+    public void processPoint(AnalysisResultEntity entity, String filePath, Geometry point, int size) {
+        Dataset ds = null;
+        try {
+            ds = gdal.Open(filePath);
+            if (null == ds) {
+                throw new Exception("鎵撳紑鏍呮牸鏁版嵁澶辫触");
+            }
+
+            double x = point.GetX(), y = point.GetY();
+            double[] transform = ds.GetGeoTransform();
+            // double rotationX = transform[2]; double rotationY = transform[4]
+            double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5];
+
+            int xPixel = (int) Math.floor((x - minX) / pixelWidth);
+            int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
+
+            int bandCount = ds.getRasterCount();
+
+            List<double[]> sum = new ArrayList<>();
+            for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) {
+                double[] pixelValues = new double[size * size];
+                Band band = ds.GetRasterBand(bandIndex);
+                band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
+
+                sum.add(pixelValues);
+            }
+
+            //return average(calculateAverage(sum));
+        } catch (Exception ex) {
+            setError(entity, ex.getMessage());
+            log.error(ex.getMessage(), ex);
+        } finally {
+            //  gdal.GDALDestroyDriverManager()
+            if (null != ds) {
+                ds.delete();
+            }
+        }
+    }
+
+    /**
+     * 鍒嗘瀽绾�
+     */
+    public List<AnalysisResultEntity> analysisPolyline(Geometry polyline) {
+        List<AnalysisResultEntity> rs = new ArrayList<>();
+        //
+
+        return rs;
+    }
+
+    /**
+     * 鍒嗘瀽闈�
+     */
+    public List<AnalysisResultEntity> analysisPolygon(Geometry polygon) {
+        List<AnalysisResultEntity> rs = new ArrayList<>();
+        //
+
+        return rs;
+    }
+
+
     public List<Double> processLine(String imagePath, String geometryString) {
         // 鎵撳紑鏍呮牸鍥惧儚鏁版嵁闆�
         Dataset dataset = gdal.Open(imagePath);

--
Gitblit v1.9.3