From 236b037b0d8d2673bb8f1f00013983459c6e669c Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 14 九月 2023 16:57:24 +0800
Subject: [PATCH] 修改栅格分析的线分析功能-2

---
 src/main/java/com/moon/server/entity/all/StaticData.java              |    2 
 src/main/java/com/moon/server/service/data/RasterAnalysisService.java |   76 +++++++++++--------------
 src/main/java/com/moon/server/helper/GeoHelper.java                   |   32 +++++-----
 src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java   |   32 ++++++----
 4 files changed, 72 insertions(+), 70 deletions(-)

diff --git a/src/main/java/com/moon/server/entity/all/StaticData.java b/src/main/java/com/moon/server/entity/all/StaticData.java
index 99b97d3..5a4389b 100644
--- a/src/main/java/com/moon/server/entity/all/StaticData.java
+++ b/src/main/java/com/moon/server/entity/all/StaticData.java
@@ -40,6 +40,8 @@
 
     public final static int ONE_HUNDRED_THOUSAND = 100000;
 
+    public static final double D05 = 0.05;
+
     public static final double D90 = 90.0;
 
     public static final double D100 = 100.0;
diff --git a/src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java b/src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java
index d73a689..a1c1d1b 100644
--- a/src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java
+++ b/src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java
@@ -23,8 +23,8 @@
     /**
      * 娣诲姞鐐�
      */
-    public void addPoint(Double x, Double y, Double val) {
-        Point point = new Point(x, y, val);
+    public void addPoint(Double x, Double y, List<Double> vals) {
+        Point point = new Point(x, y, vals);
         this.points.add(point);
     }
 
@@ -63,7 +63,7 @@
     }
 
     public void setAvg(Double avg) {
-        this.avg = WebHelper.round(avg, 3);
+        this.avg = avg;
     }
 
     public Double getMax() {
@@ -71,7 +71,7 @@
     }
 
     public void setMax(Double max) {
-        this.max = WebHelper.round(max, 3);
+        this.max = max;
     }
 
     public int getCode() {
@@ -90,20 +90,28 @@
         this.info = info;
     }
 
-    public class Point {
+    public List<Point> getPoints() {
+        return points;
+    }
+
+    public void setPoints(List<Point> points) {
+        this.points = points;
+    }
+
+    public static class Point {
         private Double x;
 
         private Double y;
 
-        private Double val;
+        private List<Double> vals;
 
         public Point() {
         }
 
-        public Point(Double x, Double y, Double val) {
+        public Point(Double x, Double y, List<Double> vals) {
             this.x = x;
             this.y = y;
-            this.val = val;
+            this.vals = vals;
         }
 
         public Double getX() {
@@ -122,12 +130,12 @@
             this.y = y;
         }
 
-        public Double getVal() {
-            return val;
+        public List<Double> getVals() {
+            return vals;
         }
 
-        public void setVal(Double val) {
-            this.val = val;
+        public void setVals(List<Double> vals) {
+            this.vals = vals;
         }
     }
 }
diff --git a/src/main/java/com/moon/server/helper/GeoHelper.java b/src/main/java/com/moon/server/helper/GeoHelper.java
index 556c784..20ac8da 100644
--- a/src/main/java/com/moon/server/helper/GeoHelper.java
+++ b/src/main/java/com/moon/server/helper/GeoHelper.java
@@ -39,9 +39,20 @@
     }
 
     /**
-     * 鑾峰彇璺濈1
+     * 鑾峰彇璺濈
      */
-    public static double getDistance1(double lon1, double lat1, double lon2, double lat2) {
+    public static double getDistance(double x1, double y1, double x2, double y2) {
+        GeodeticCalculator geodeticCalculator = new GeodeticCalculator(crs104903);
+        geodeticCalculator.setStartingGeographicPoint(x1, y1);
+        geodeticCalculator.setDestinationGeographicPoint(x2, y2);
+
+        return geodeticCalculator.getOrthodromicDistance();
+    }
+
+    /**
+     * 鑾峰彇璺濈2
+     */
+    public static double getDistance2(double lon1, double lat1, double lon2, double lat2) {
         double radLat1 = Math.toRadians(lat1);
         double radLat2 = Math.toRadians(lat2);
         double a = radLat1 - radLat2;
@@ -52,17 +63,6 @@
     }
 
     /**
-     * 鑾峰彇璺濈2
-     */
-    public static double getDistance2(double x1, double y1, double x2, double y2) {
-        GeodeticCalculator geodeticCalculator = new GeodeticCalculator(crs104903);
-        geodeticCalculator.setStartingGeographicPoint(x1, y1);
-        geodeticCalculator.setDestinationGeographicPoint(x2, y2);
-
-        return geodeticCalculator.getOrthodromicDistance();
-    }
-
-    /**
      * 鑾峰彇鏂瑰悜瑙�
      */
     public static double getBearing(double x1, double y1, double x2, double y2) {
@@ -70,9 +70,9 @@
     }
 
     /**
-     * 鑾峰彇瑙掑害1
+     * 鑾峰彇瑙掑害
      */
-    public static double getAngle1(double x1, double y1, double x2, double y2) {
+    public static double getAngle(double x1, double y1, double x2, double y2) {
         DirectPosition2D p1 = new DirectPosition2D(crs104903, x1, y1);
         DirectPosition2D p2 = new DirectPosition2D(crs104903, x2, y2);
 
@@ -100,7 +100,7 @@
     /**
      * 鏍规嵁璺濈鍜岃搴﹁幏鍙栫洰鏍囩偣
      */
-    private static Point2D getPointByDistanceAndAngle(double x, double y, double angle, double distance) {
+    public static Point2D getPointByDistanceAndAngle(double x, double y, double angle, double distance) {
         DirectPosition2D p1 = new DirectPosition2D(x, y);
 
         GeodeticCalculator gc = new GeodeticCalculator();
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 abdca9d..8d3d40d 100644
--- a/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
+++ b/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -18,8 +18,10 @@
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.springframework.stereotype.Service;
 import org.geotools.referencing.CRS;
+import sun.awt.IconInfo;
 
 import javax.annotation.Resource;
+import java.awt.geom.Point2D;
 import java.io.File;
 import java.util.*;
 
@@ -147,52 +149,27 @@
      * 鍒嗘瀽绾�
      */
     public void analysisPolyline(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
-        /*Double len = geo.Length();
-
-
         double[] transform = ds.GetGeoTransform();
         // double rotationX = transform[2]; double rotationY = transform[4]
-        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
-
-        double buffer = Math.max(pixelWidth, pixelHeight) * size;
-        double[] env = new double[4];
-        geo = geo.Buffer(buffer);
-        geo.GetEnvelope(env);
-
-        int xMinPixel = (int) Math.floor((env[0] - minX) / pixelWidth);
-        int yMinPixel = (int) Math.floor((maxY - env[3]) / pixelHeight);
-        int xMaxPixel = (int) Math.floor((env[1] - minX) / pixelWidth);
-        int yMaxPixel = (int) Math.floor((maxY - env[2]) / pixelHeight);
+        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5];
 
         int bandCount = ds.getRasterCount();
-        int xMin = Math.min(xMinPixel, xMaxPixel);
-        int xMax = Math.max(xMinPixel, xMaxPixel);
-        int yMin = Math.min(yMinPixel, yMaxPixel);
-        int yMax = Math.max(yMinPixel, yMaxPixel);
-
-        List<Double> list = new ArrayList<>();
-        for (int y = yMin; y <= yMax; y++) {
-            for (int x = xMin; x <= xMax; x++) {
-                Geometry point = new Geometry(ogr.wkbPoint);
-                point.AddPoint(minX + pixelWidth * x, maxY - pixelHeight * y);
-                if (geo.Intersects(point)) {
-                    for (int i = 1; i <= bandCount; i++) {
-                        double[] values = new double[1];
-                        ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, values);
-                        list.add(values[0]);
-                    }
-                }
-            }
-        }
-
-        processResult(list, entity);*/
-
-        //double dis = GeoHelper.getDistance1()
-
         double lineDis = getPolylineDistance(geo);
         double distance = lineDis / Math.max(geo.GetPointCount(), size);
 
-        List<double[]> list = getPointsByDistance(geo, distance);
+        List<double[]> points = getPointsByDistance(geo, distance);
+        for (double[] xy : points) {
+            int xPixel = (int) Math.floor((xy[0] - minX) / pixelWidth);
+            int yPixel = (int) Math.floor((maxY - xy[1]) / Math.abs(pixelHeight));
+
+            List<Double> vals = new ArrayList<>();
+            for (int i = 1; i <= bandCount; i++) {
+                double[] pixelValues = new double[1];
+                ds.GetRasterBand(i).ReadRaster(xPixel, yPixel, 1, 1, pixelValues);
+                vals.add(pixelValues[0]);
+            }
+            entity.addPoint(xy[0], xy[1], vals);
+        }
     }
 
     /**
@@ -357,7 +334,7 @@
             double[] xy1 = geo.GetPoint(i);
             double[] xy2 = geo.GetPoint(i + 1);
 
-            dis += GeoHelper.getDistance2(xy1[0], xy1[1], xy2[0], xy2[1]);
+            dis += GeoHelper.getDistance(xy1[0], xy1[1], xy2[0], xy2[1]);
         }
 
         return dis;
@@ -368,14 +345,29 @@
      */
     private List<double[]> getPointsByDistance(Geometry geo, double distance) {
         List<double[]> list = new ArrayList<>();
-        for (int i = 0, c = geo.GetPointCount() - 1; i < c; i++) {
+
+        int c = geo.GetPointCount() - 1;
+        for (int i = 0; i < c; i++) {
             double[] xy1 = geo.GetPoint(i);
             double[] xy2 = geo.GetPoint(i + 1);
+            list.add(xy1);
 
-            double dis = GeoHelper.getDistance2(xy1[0], xy1[1], xy2[0], xy2[1]);
+            double lineDis = GeoHelper.getDistance(xy1[0], xy1[1], xy2[0], xy2[1]);
+            if (lineDis < distance) {
+                continue;
+            }
 
+            double d = distance;
+            double angle = GeoHelper.getAngle(xy1[0], xy1[1], xy2[0], xy2[1]);
+            while (d + distance * StaticData.D05 < lineDis) {
+                Point2D point = GeoHelper.getPointByDistanceAndAngle(xy1[0], xy1[1], angle, d);
+                list.add(new double[]{point.getX(), point.getY()});
+                d += distance;
+            }
         }
 
+        list.add(geo.GetPoint(c));
+
         return list;
     }
 }

--
Gitblit v1.9.3