月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-14 236b037b0d8d2673bb8f1f00013983459c6e669c
修改栅格分析的线分析功能-2
已修改4个文件
142 ■■■■ 文件已修改
src/main/java/com/moon/server/entity/all/StaticData.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/helper/GeoHelper.java 32 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/service/data/RasterAnalysisService.java 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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;
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;
        }
    }
}
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();
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;
    }
}