月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-11 92fd3ce2a44b27dd931ee68a0f604e1af6d1b498
src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -1,5 +1,6 @@
package com.moon.server.service.data;
import com.moon.server.entity.data.AnalysisResultEntity;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.WarpOptions;
@@ -15,79 +16,30 @@
/**
 * 栅格分析服务
 * @author LJW
 * @date 2023-9-1 6:16
 * @date 2023-09-01 6:16
 */
@Service
public class RasterAnalysisService {
    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");
    public List<AnalysisResultEntity> analysisPoint(String wkt, Integer pixel) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        return gdal.Warp("", new Dataset[]{dataset}, new WarpOptions(warpOptions));
        return rs;
    }
    public double[] processClippedDataByLine(String imagePath, String geometryString) {
        // 注册GDAL驱动
        gdal.AllRegister();
        // 打开栅格图像数据集
        Dataset dataset = gdal.Open(imagePath);
        if (dataset == null) {
            throw new RuntimeException("Failed to open raster dataset.");
        }
    public List<AnalysisResultEntity> analysisPolyline(String wkt) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        Geometry geometry = Geometry.CreateFromWkt(geometryString);
        return rs;
    }
    public List<AnalysisResultEntity> analysisPolygon(String wkt) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
        //
        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);
        }
        return calculateAverage(sum);
        return rs;
    }
    public double processClippedDataByPolygon(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();
        double[] bandSum = new double[bandCount];
        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);
            // 多波段归一化处理
            bandSum[bandIndex - 1] = average(pixelValues);
        }
        return average(bandSum);
    }
    public double processPoint(String imagePath, String geometryString, int size) {
        // 打开栅格图像数据集
@@ -219,6 +171,76 @@
        return average(calculateAverage(sum));
    }
    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));
    }
    public double[] 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);
        }
        return calculateAverage(sum);
    }
    public double processClippedDataByPolygon(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();
        double[] bandSum = new double[bandCount];
        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);
            // 多波段归一化处理
            bandSum[bandIndex - 1] = average(pixelValues);
        }
        return average(bandSum);
    }
    public static double average(double[] values) {
        double sum = 0;
        for (double value : values) {