月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-09-11 92fd3ce2a44b27dd931ee68a0f604e1af6d1b498
修改栅格分析-1
已添加1个文件
已修改2个文件
287 ■■■■■ 文件已修改
src/main/java/com/moon/server/controller/data/RasterAnalysisController.java 84 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java 55 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/service/data/RasterAnalysisService.java 148 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/moon/server/controller/data/RasterAnalysisController.java
@@ -3,7 +3,7 @@
import com.moon.server.annotation.SysLog;
import com.moon.server.controller.all.BaseController;
import com.moon.server.entity.all.ResponseMsg;
import com.moon.server.entity.data.ImageEntity;
import com.moon.server.helper.StringHelper;
import com.moon.server.helper.WebHelper;
import com.moon.server.service.data.RasterAnalysisService;
import io.swagger.annotations.Api;
@@ -15,12 +15,8 @@
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * æ …格分析
@@ -34,40 +30,32 @@
    @Resource
    RasterAnalysisService rasterService;
    @SysLog()
    @ApiOperation(value = "根据geometry获取对应范围的灰度值")
    @PostMapping
    public ResponseMsg<Object> rasterAnalysis(@RequestBody List<ImageEntity> images) {
        // è¾“入的几何Geometry: POINT (75.772 2.498)
        String geometryString = images.get(0).getGeometryString();
        Geometry geometry = Geometry.CreateFromWkt(geometryString);
        for (ImageEntity i : images) {
            String path = i.getPath();
            if (geometry.GetGeometryType() == ogr.wkbPoint) {
                i.setResult(rasterService.processPoint(path, geometryString, i.getPointSize()));
            } else if (geometry.GetGeometryType() == ogr.wkbLineString) {
                i.setResult(rasterService.processLine(path, geometryString));
            } else if (geometry.GetGeometryType() == ogr.wkbPolygon) {
                i.setResult(rasterService.processPolygon(path, geometryString));
            }
        }
        return success(images);
    }
    private final static List<Integer> pixels = new ArrayList<>(Arrays.asList(1, 2, 4, 8, 16, 32, 64, 128, 256));
    @SysLog()
    @ApiOperation(value = "查询点分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wkt", value = "点WKT", dataType = "String", example = "")
            @ApiImplicitParam(name = "wkt", value = "点WKT", dataType = "String", example = "POINT (115.94927385452 32.3754479115071)"),
            @ApiImplicitParam(name = "pixel", value = "像素值", dataType = "Integer", example = "1")
    })
    @GetMapping(value = "/selectByPoint")
    public ResponseMsg<Object> selectByPoint(String wkt) {
    public ResponseMsg<Object> selectByPoint(String wkt, Integer pixel) {
        try {
            Map<String, Double> map = new HashMap<>(5);
            map.put("图层名", 0.0);
            // Map<String, Double> map = new HashMap<>(5);  map.put("图层名", 0.0);  return success(map)
            if (StringHelper.isEmpty(wkt)) {
                return fail("WKT字符串不能为空");
            }
            Geometry geo = Geometry.CreateFromWkt(wkt);
            if (null == geo || geo.GetGeometryType() != ogr.wkbPoint) {
                return fail("WKT字符串不正确");
            }
            if (null == pixel || !pixels.contains(pixel)) {
                return fail("像点值只能为:" + StringHelper.join(pixels, ", "));
            }
            return success(map);
            List<?> rs = rasterService.analysisPoint(wkt, pixel);
            return success(rs.size(), rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
@@ -76,17 +64,22 @@
    @SysLog()
    @ApiOperation(value = "查询线分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wkt", value = "线WKT", dataType = "String", example = "")
            @ApiImplicitParam(name = "wkt", value = "线WKT", dataType = "String", example = "LINESTRING(115.94927385452 32.3754479115071,121.989371092554 32.2766788010181,121.850621222894 29.6874200067864)")
    })
    @GetMapping(value = "/selectByPolyline")
    public ResponseMsg<Object> selectByPolyline(String wkt) {
        try {
            Map<String, List<Double>> map = new HashMap<>(5);
            List<Double> list = new ArrayList<>();
            list.add(0.0);
            map.put("图层名", list);
            if (StringHelper.isEmpty(wkt)) {
                return fail("WKT字符串不能为空");
            }
            Geometry geo = Geometry.CreateFromWkt(wkt);
            if (null == geo || geo.GetGeometryType() != ogr.wkbLineString) {
                return fail("WKT字符串不正确");
            }
            return success(map);
            List<?> rs = rasterService.analysisPolyline(wkt);
            return success(rs.size(), rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
@@ -95,15 +88,22 @@
    @SysLog()
    @ApiOperation(value = "查询面分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wkt", value = "面WKT", dataType = "String", example = "")
            @ApiImplicitParam(name = "wkt", value = "面WKT", dataType = "String", example = "POLYGON ((115.94927385452 32.3754479115071,121.989371092554 32.2766788010181,121.850621222894 29.6874200067864,115.9727267226 29.7835368627922,115.94927385452 32.3754479115071))")
    })
    @GetMapping(value = "/selectByPolygon")
    public ResponseMsg<Object> selectByPolygon(String wkt) {
        try {
            Map<String, Double> map = new HashMap<>(5);
            map.put("图层名", 0.0);
            if (StringHelper.isEmpty(wkt)) {
                return fail("WKT字符串不能为空");
            }
            Geometry geo = Geometry.CreateFromWkt(wkt);
            if (null == geo || geo.GetGeometryType() != ogr.wkbPolygon) {
                return fail("WKT字符串不正确");
            }
            return success(map);
            List<?> rs = rasterService.analysisPolygon(wkt);
            return success(rs.size(), rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
src/main/java/com/moon/server/entity/data/AnalysisResultEntity.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,55 @@
package com.moon.server.entity.data;
import java.io.Serializable;
/**
 * åˆ†æžç»“æžœ
 * @author WWW
 * @date 2023-09-11
 */
public class AnalysisResultEntity implements Serializable {
    private static final long serialVersionUID = -1237623414044281355L;
    public AnalysisResultEntity() {
    }
    private String layerName;
    private Double min;
    private Double avg;
    private Double max;
    public String getLayerName() {
        return layerName;
    }
    public void setLayerName(String layerName) {
        this.layerName = layerName;
    }
    public Double getMin() {
        return min;
    }
    public void setMin(Double min) {
        this.min = min;
    }
    public Double getAvg() {
        return avg;
    }
    public void setAvg(Double avg) {
        this.avg = avg;
    }
    public Double getMax() {
        return max;
    }
    public void setMax(Double max) {
        this.max = max;
    }
}
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) {