月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2023-09-23 4869f5a2ce4b8c2f063e1a3af8bf092a915a774b
src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -7,6 +7,7 @@
import com.moon.server.entity.data.PublishEntity;
import com.moon.server.helper.GeoHelper;
import com.moon.server.helper.PathHelper;
import com.moon.server.helper.WebHelper;
import com.moon.server.service.all.WebSocketService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -41,6 +42,11 @@
    /**
     * 测试
     *
     * POINT (26.72 -48.58)
     * LINESTRING(18.44 -46.69,20.14 -45.93)
     * POLYGON ((5.11 -41.64,7.68 -43.59,4.73 -43.3,5.11 -41.64))
     * POLYGON ((42.6 -40.3,49.1 -40.4,49.1 -65.7,46.3 -65.6,42.6 -40.3))
     */
    public List<AnalysisResultEntity> test(Geometry geo, Integer size) {
        List<AnalysisResultEntity> rs = new ArrayList<>();
@@ -144,6 +150,7 @@
    private void openRaster(AnalysisResultEntity entity, String filePath, Geometry geo, int size) {
        Dataset ds = null;
        try {
            // filePath = "D:\\Moon\\data\\DOM\\test.tif"
            ds = gdal.Open(filePath);
            if (null == ds) {
                throw new Exception("打开栅格数据失败");
@@ -214,9 +221,13 @@
            for (int j = 1; j <= bandCount; j++) {
                double[] pixelValues = new double[1];
                ds.GetRasterBand(j).ReadRaster(xPixel, yPixel, 1, 1, pixelValues);
                vals.add(pixelValues[0]);
                if (!Double.isNaN(pixelValues[0])) {
                    vals.add(WebHelper.round(pixelValues[0], 3));
                }
            }
            entity.addPoint(xy[0], xy[1], len, vals);
            if (vals.size() > 0) {
                entity.addPoint(xy[0], xy[1], len, vals);
            }
        }
    }
@@ -229,20 +240,20 @@
        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]), rotationX = transform[2], rotationY = transform[4];
        double[] env = new double[4];
        geo.GetEnvelope(env);
        entity.addPoint(geo.Centroid().GetX(), geo.Centroid().GetY(), 0, null);
        int xMinPixel = Math.max((int) Math.floor((env[0] - minX) / pixelWidth), 1);
        int yMinPixel = Math.max((int) Math.floor((maxY - env[3]) / pixelHeight), 1);
        int xMaxPixel = Math.min((int) Math.floor((env[1] - minX) / pixelWidth), xSize);
        int yMaxPixel = Math.min((int) Math.floor((maxY - env[2]) / pixelHeight), ySize);
        int bandCount = ds.getRasterCount();
        int width = Math.abs(xMaxPixel - xMinPixel);
        int height = Math.abs(yMaxPixel - yMinPixel);
        if (width < 1 || height < 1 || xMaxPixel < 1 || yMaxPixel < 1) {
        if (xMaxPixel < 1 || yMaxPixel < 1 || xMaxPixel - xMinPixel < 0 || yMaxPixel - yMinPixel < 0) {
            setError(entity, "查询范围无效");
            return;
        }
        int bandCount = ds.getRasterCount();
        int width = xMaxPixel - xMinPixel;
        int height = yMaxPixel - yMinPixel;
        if (width * height > StaticData.I64 * StaticData.I64) {
            readRasterForBlocks(entity, ds, bandCount, xMinPixel, yMinPixel, width, height);
            return;
@@ -259,8 +270,8 @@
     * 按照块读取栅格数据
     */
    private void readRasterForBlocks(AnalysisResultEntity entity, Dataset ds, int bandCount, int xMinPixel, int yMinPixel, int width, int height) {
        List<Integer> xList = getSamples(xMinPixel, width);
        List<Integer> yList = getSamples(yMinPixel, height);
        List<Integer> xList = getSamples(xMinPixel, width - 1);
        List<Integer> yList = getSamples(yMinPixel, height - 1);
        double[] pixelValues = new double[1];
        for (int i = 1; i <= bandCount; i++) {
@@ -268,7 +279,9 @@
            for (Integer x : xList) {
                for (Integer y : yList) {
                    ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, pixelValues);
                    list.add(pixelValues[0]);
                    if (!Double.isNaN(pixelValues[0])) {
                        list.add(pixelValues[0]);
                    }
                }
            }
            setBandVals(entity, list);
@@ -307,7 +320,9 @@
        List<Double> list = new ArrayList<>();
        for (double val : pixelValues) {
            list.add(val);
            if (!Double.isNaN(val)) {
                list.add(val);
            }
        }
        setBandVals(entity, list);
    }
@@ -316,6 +331,10 @@
     * 设置Band值
     */
    private void setBandVals(AnalysisResultEntity entity, List<Double> list) {
        if (null == list || list.isEmpty()) {
            return;
        }
        double min = Collections.min(list);
        double max = Collections.max(list);
        double avg = list.stream().mapToDouble(Double::valueOf).average().getAsDouble();