月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-14 8f3d45ded35c23d233b9891d3d69de2635f2a125
栅格分析按照Band进行计算~
已修改2个文件
174 ■■■■■ 文件已修改
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/service/data/RasterAnalysisService.java 131 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java
@@ -2,7 +2,6 @@
import com.moon.server.helper.WebHelper;
import java.io.DataOutput;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@@ -17,6 +16,9 @@
    public AnalysisResultEntity() {
        this.code = 200;
        this.minList = new ArrayList<>();
        this.avgList = new ArrayList<>();
        this.maxList = new ArrayList<>();
        this.points = new ArrayList<>();
    }
@@ -28,13 +30,22 @@
        this.points.add(point);
    }
    /**
     * 添加Band值
     */
    public void addBandVals(double min, double avg, double max) {
        this.minList.add(WebHelper.round(min, 3));
        this.avgList.add(WebHelper.round(avg, 3));
        this.maxList.add(WebHelper.round(max, 3));
    }
    private String layerName;
    private Double min;
    private List<Double> minList;
    private Double avg;
    private List<Double> avgList;
    private Double max;
    private List<Double> maxList;
    private int code;
@@ -50,28 +61,28 @@
        this.layerName = layerName;
    }
    public Double getMin() {
        return min;
    public List<Double> getMinList() {
        return minList;
    }
    public void setMin(Double min) {
        this.min = WebHelper.round(min, 3);
    public void setMinList(List<Double> minList) {
        this.minList = minList;
    }
    public Double getAvg() {
        return avg;
    public List<Double> getAvgList() {
        return avgList;
    }
    public void setAvg(Double avg) {
        this.avg = avg;
    public void setAvgList(List<Double> avgList) {
        this.avgList = avgList;
    }
    public Double getMax() {
        return max;
    public List<Double> getMaxList() {
        return maxList;
    }
    public void setMax(Double max) {
        this.max = max;
    public void setMaxList(List<Double> maxList) {
        this.maxList = maxList;
    }
    public int getCode() {
src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -14,11 +14,7 @@
import org.gdal.gdal.gdal;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.ogr;
import org.gdal.osr.SpatialReference;
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;
@@ -133,16 +129,11 @@
        int xPixel = (int) Math.floor((x - minX) / pixelWidth);
        int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
        List<double[]> list = new ArrayList<>();
        for (int i = 1; i <= bandCount; i++) {
            double[] pixelValues = new double[size * size];
            Band band = ds.GetRasterBand(i);
            band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
            list.add(pixelValues);
        }
        processResult(entity, list);
    }
    /**
@@ -210,8 +201,7 @@
                }
            }
        }
        processResult(list, entity);
        // processResult(list, entity)
    }
    /**
@@ -233,16 +223,12 @@
        int geoWidth = Math.abs(xMaxPixel - xMinPixel);
        int geoHeight = Math.abs(yMaxPixel - yMinPixel);
        List<double[]> list = new ArrayList<>();
        for (int i = 1; i <= bandCount; i++) {
            Band band = ds.GetRasterBand(i);
            double[] pixelValues = new double[geoWidth * geoHeight];
            band.ReadRaster(xMinPixel, yMinPixel, geoWidth, geoHeight, pixelValues);
            ds.GetRasterBand(i).ReadRaster(xMinPixel, yMinPixel, geoWidth, geoHeight, pixelValues);
            list.add(pixelValues);
            setBandVals(entity, pixelValues);
        }
        processResult(entity, list);
    }
    /**
@@ -254,75 +240,22 @@
    }
    /**
     * 处理结果
     * 设置Band值
     */
    private void processResult(AnalysisResultEntity entity, List<double[]> list) {
        if (null == list || list.isEmpty()) {
    private void setBandVals(AnalysisResultEntity entity, double[] pixelValues) {
        if (null == pixelValues || pixelValues.length == 0) {
            return;
        }
        List<Double> rs = new ArrayList<>();
        for (double[] ds : list) {
            if (null != ds && ds.length > 0) {
                for (double d : ds) {
                    rs.add(d);
                }
            }
        List<Double> list = new ArrayList<>();
        for (double val : pixelValues) {
            list.add(val);
        }
        processResult(rs, entity);
    }
    /**
     * 处理结果
     */
    private void processResult(List<Double> rs, AnalysisResultEntity entity) {
        if (null == rs || rs.isEmpty()) {
            return;
        }
        double avg = rs.stream().mapToDouble(Double::valueOf).average().getAsDouble();
        entity.setMin(Collections.min(rs));
        entity.setMax(Collections.max(rs));
        entity.setAvg(avg);
    }
    public void processClippedDataByLine(String imagePath, String geometryString) {
        // 注册GDAL驱动
        gdal.AllRegister();
        // 打开栅格图像数据集
        Dataset dataset = gdal.Open(imagePath);
        if (dataset == null) {
            throw new RuntimeException("Failed to open raster dataset.");
        }
        Geometry geometry = Geometry.CreateFromWkt(geometryString);
        Dataset clippedDataset = clipRaster(dataset, geometry);
        int width = clippedDataset.GetRasterXSize();
        int height = clippedDataset.GetRasterYSize();
        int bandCount = clippedDataset.getRasterCount();
        List<double[]> sum = new ArrayList<>();
        for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) {
            Band band = clippedDataset.GetRasterBand(bandIndex);
            double[] pixelValues = new double[width * height];
            band.ReadRaster(0, 0, width, height, pixelValues);
            sum.add(pixelValues);
        }
    }
    public static Dataset clipRaster(Dataset dataset, Geometry geometry) {
        Vector<String> warpOptions = new Vector<>();
        warpOptions.add("-crop_to_cutline");
        warpOptions.add("-cutline");
        warpOptions.add(geometry.ExportToWkt());
        warpOptions.add("-dstalpha");
        return gdal.Warp("", new Dataset[]{dataset}, new WarpOptions(warpOptions));
        double min = Collections.min(list);
        double max = Collections.max(list);
        double avg = list.stream().mapToDouble(Double::valueOf).average().getAsDouble();
        entity.addBandVals(min, avg, max);
    }
    /**
@@ -370,4 +303,42 @@
        return list;
    }
    private void processClippedDataByLine(String imagePath, String geometryString) {
        // 注册GDAL驱动
        gdal.AllRegister();
        // 打开栅格图像数据集
        Dataset dataset = gdal.Open(imagePath);
        if (dataset == null) {
            throw new RuntimeException("Failed to open raster dataset.");
        }
        Geometry geometry = Geometry.CreateFromWkt(geometryString);
        Dataset clippedDataset = clipRaster(dataset, geometry);
        int width = clippedDataset.GetRasterXSize();
        int height = clippedDataset.GetRasterYSize();
        int bandCount = clippedDataset.getRasterCount();
        List<double[]> sum = new ArrayList<>();
        for (int bandIndex = 1; bandIndex <= bandCount; bandIndex++) {
            Band band = clippedDataset.GetRasterBand(bandIndex);
            double[] pixelValues = new double[width * height];
            band.ReadRaster(0, 0, width, height, pixelValues);
            sum.add(pixelValues);
        }
    }
    private Dataset clipRaster(Dataset dataset, Geometry geometry) {
        Vector<String> warpOptions = new Vector<>();
        warpOptions.add("-crop_to_cutline");
        warpOptions.add("-cutline");
        warpOptions.add(geometry.ExportToWkt());
        warpOptions.add("-dstalpha");
        return gdal.Warp("", new Dataset[]{dataset}, new WarpOptions(warpOptions));
    }
}