dcb
2025-05-08 18f1c98549e29323ff1a89ef3ed670ffb9ed0764
修复转png时rgb值溢出问题
已修改3个文件
96 ■■■■ 文件已修改
src/main/java/com/se/nsl/service/ResolveService.java 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/service/TestService.java 70 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-dev.yml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/service/ResolveService.java
@@ -240,6 +240,26 @@
     * 调用zarr2tif
     */
    private String callZarr2tif(SimuData data) throws Exception {
        String zarr2tifBat = config.getZarr2tifBat();
        File dir = new File(zarr2tifBat).getParentFile();
        String configJson = new File(dir, "config.json").getAbsolutePath();
        String cmd = String.format("%s \"%s\"", zarr2tifBat, configJson);
        String res = callBat(cmd);
        File resultTifDir = new File(dir, "result_tif");
        File targetDir = new File(config.getInPath(), data.getInPath() + File.separator + "depth");
        System.out.println("targetDir:" + targetDir);
        File[] files = resultTifDir.listFiles();
        for (File file : files) {
            File target = new File(targetDir, file.getName());
            Files.copy(file.toPath(), target.toPath());
        }
        return res;
    }
    /*private String callZarr2tif(SimuData data) throws Exception {
        File uwBat = new File(config.getUwSolverBat());
        String zarrFile = uwBat.getParent() + File.separator + "result.zarr";
        String inPath = config.getInPath() + File.separator + data.getInPath();
@@ -249,7 +269,7 @@
        String cmd = String.format("%s \"%s\" \"%s\" \"%s\" \"%s\"", config.getZarr2tifBat(), "depth", zarrFile, terrainFile, waterPath);
        return callBat(cmd);
    }
    }*/
    private String callBat(String cmd) {
        try {
src/main/java/com/se/nsl/service/TestService.java
@@ -87,7 +87,7 @@
            copeTerrain(dto, layer);
            //copeBuilding(dto, layer);
            List<BuildingDepthVo> buildings = copeWater(dto, layer);
            //copeFlow(dto, layer);
            copeFlow(dto, layer);
            copeLayerJson(dto, layer);
            //copeRainFallJson(dto, layer);
            //copeBuildingDepthJson(dto, buildings);
@@ -347,7 +347,8 @@
                        g = val / 256;
                        b = val % 256;
                    }
                    g = ComHelper.getSafeValue(g);
                    b = ComHelper.getSafeValue(b);
                    Color color = new Color(r, g, b, 127);
                    image.setRGB(x, y, color.getRGB());
                }
@@ -564,9 +565,8 @@
    }
    public void copeFlow(ResultDto dto, LayerDto layer) {
        List<String> files = getFiles(dto.getFlowPath(), ".tif");
        if (null == files || files.size() != layer.getWaters().getData().size()) return;
        List<String> files = layer.getWaters().getFiles();
        if (null == files || files.isEmpty()) return;
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
@@ -603,17 +603,69 @@
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            float[] depthBuffer = new float[width * height];
            float[] vxBuffer = new float[width * height];
            float[] vyBuffer = new float[width * height];
            ds.GetRasterBand(1).ReadRaster(0, 0, width, height, depthBuffer);
            ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vxBuffer);
            ds.GetRasterBand(3).ReadRaster(0, 0, width, height, vyBuffer);
            createFlowPng(depthBuffer, vxBuffer, vyBuffer, png, width, height);
        } finally {
            if (null != ds) ds.delete();
        }
    }
    public void createFlowPng(float[] depthBuffer, float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int offset = x + y * width;
                float depth = ComHelper.getFloatValue(depthBuffer[offset]);
                float fx = ComHelper.getFloatValue(vxBuffer[offset]);
                float fy = ComHelper.getFloatValue(vyBuffer[offset]);
                if (Float.isNaN(fx) && Float.isNaN(fy) || (fx < MIN_VAL && fy < MIN_VAL)) continue;
                if (depth == 0) {
                    fx = 0f;
                    fy = 0f;
                } else {
                    fx = Float.isNaN(fx) ? 0 : fx / depth;
                    fy = Float.isNaN(fy) ? 0 : fy / depth;
                }
                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(ComHelper.getSafeValue(r), ComHelper.getSafeValue(g), ComHelper.getSafeValue(b), 127);
                image.setRGB(x, y, color.getRGB());
            }
        }
        ComHelper.savePng(image, png);
    }
/*
    public void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
            ds.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer);
            ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vyBuffer);
            ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vxBuffer);
            ds.GetRasterBand(3).ReadRaster(0, 0, width, height, vyBuffer);
            createFlowPng(vxBuffer, vyBuffer, png, width, height);
        } finally {
            if (null != ds) ds.delete();
        }
    }
*/
    public void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
    /*public void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
@@ -635,7 +687,7 @@
            }
        }
        ComHelper.savePng(image, png);
    }
    }*/
    public void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
        layer.getWaters().setFiles(null);
src/main/resources/application-dev.yml
@@ -142,8 +142,8 @@
  solverBat: D:\other\simu\uwsolver\run_solver.bat
  sww2tifBat: D:\other\simu\uwsolver\sww2tif.bat
  uwSolverBat: D:\other\simu\CudaUWSolver.Demo.NoVis.20250430\start.bat
  zarr2tifBat: D:\other\simu\zarr2tif1.0\start.bat
  #zarr2tifBat: D:\other\simu\zarr2tif-2.0-mkl\start.bat
#  zarr2tifBat: D:\other\simu\zarr2tif1.0\start.bat
  zarr2tifBat: D:\other\simu\zarr2tif-2.0-mkl\start.bat
  createRainfall: '"C:\Program Files\Python310\python.exe" D:\terrait\NslServer\data\CreatRainfall.py'
  rainfallTitle: Station Longitude Latitude Year Month Day Hour Minute Intensity
  rainfallSite: beijing