1
13693261870
2024-10-08 084d07a8dfbde06f3b96557e446bfdd098f65aba
src/main/java/com/se/simu/service/ResultService.java
@@ -8,7 +8,6 @@
import com.se.simu.domain.dto.ResultDto;
import com.se.simu.helper.GdalHelper;
import lombok.extern.slf4j.Slf4j;
import nonapi.io.github.classgraph.utils.FileUtils;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.WarpOptions;
@@ -250,7 +249,6 @@
    }
    private static List<String> getFiles(String path, String suffix) {
        List<String> files = new ArrayList<>();
        getFiles(files, new File(path), suffix);
@@ -281,11 +279,13 @@
    private static void setWaterData(LayerDto layer, List<String> files) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MILLISECOND, 0);
        for (String file : files) {
            String fileName = new File(file).getName();
            String fileName = getNameWithExt(file);
            int hour = Integer.parseInt(fileName.substring(0, 2));
            int minute = Integer.parseInt(fileName.substring(2, 2));
            int second = Integer.parseInt(fileName.substring(4, 2));
            int minute = Integer.parseInt(fileName.substring(2, 4));
            int second = Integer.parseInt(fileName.substring(4, 6));
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
@@ -298,10 +298,10 @@
    }
    private static void setWaterHeight(LayerDto layer, List<String> files) {
        for (int i = 0, c = files.size(); i < c; i++) {
        files.parallelStream().forEach(file->{
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                ds = gdal.Open(file, gdalconst.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return;
                double[] mm = new double[2];
@@ -310,7 +310,7 @@
            } finally {
                if (null != ds) ds.delete();
            }
        }
        });
        layer.getExtension().setMinHeight(getMinVal(layer.getExtension().getMinHeight() - 1, 1000));
        layer.getExtension().setMaxHeight(getMaxVal(layer.getExtension().getMaxHeight() + 1, 1000));
    }
@@ -346,7 +346,7 @@
    }
    private static String getNameWithExt(String file) {
        return file.substring(file.lastIndexOf(File.separator), file.lastIndexOf("."));
        return file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf("."));
    }
    private static void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) {
@@ -461,7 +461,35 @@
    }
    private static void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
        //
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphic = image.createGraphics();
        Color transparent = new Color(0, 0, 0, 0);
        graphic.setColor(transparent);
        graphic.clearRect(0, 0, width, height);
        // 用 R通道表示,流向为归一化的二维向量(x,y),G通道表示为 x *255 , B通道表示为 y * 255
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int offset = x + y * width;
                float fx = getFloatValue(vxBuffer[offset]);
                float fy = getFloatValue(vyBuffer[offset]);
                if (Float.isNaN(fx) && Float.isNaN(fy) || (fx == 0 && fy == 0)) continue;
                fx = Float.isNaN(fx) ? 0 : fx;
                fy = Float.isNaN(fy) ? 0 : fy;
                double dr = Math.sqrt(Math.pow(fx, 2) + Math.pow(fy, 2));
                int r = (int) (dr / 4 * 255);
                int g = (int) ((fx / dr * 0.5 + 0.5) * 255);
                int b = (int) ((fy / dr * 0.5 + 0.5) * 255);
                Color color = new Color(getSafeValue(r), getSafeValue(g), getSafeValue(b), 127);
                //image.setRGB(x, y, color.getRGB());
                graphic.drawImage(image, x, y, 1, 1, color, null);
            }
        }
        graphic.dispose();
        savePng(image, png);
    }
    private static float getFloatValue(float val) {
@@ -475,6 +503,9 @@
        return val;
    }
    /**
     * 元数据
     */
    private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
        String json = JSONUtil.toJsonPrettyStr(layer);
        String filePath = dto.getOutPath() + File.separator + "layer.json";