dcb
2025-06-24 8c7daad24082b796f1f49b4e2b3f18d2d3c058bf
src/main/java/com/se/nsl/service/ResolveService.java
@@ -69,6 +69,8 @@
    @Resource
    TestService testService;
    private ObjectMapper mapper = new ObjectMapper();
    Integer DIGIT = 1000000;
    SimpleDateFormat YYYYMDHM = new SimpleDateFormat("yyyy M d H m ");
@@ -165,6 +167,7 @@
        AreaType at = AreaType.of(areaType);
        String terrainTif = config.getSourceDem();
        String landuseTif = config.getSourceLanduse();
        String stationTif = null;
        if (at == AreaType.KEY_DITCH) {
            File keyDitchDir = new File(config.getKeyDitch());
            String areaName = simu.getAreaName();
@@ -179,6 +182,9 @@
                    }
                    if (name.toLowerCase().contains("landuse")) {
                        landuseTif = file.getAbsolutePath();
                    }
                    if (name.toLowerCase().contains("station")) {
                        stationTif = file.getAbsolutePath();
                    }
                }
            }
@@ -195,17 +201,24 @@
        String terrainFile = inPath + File.separator + config.getTerrainFile();
        Dataset dsDem = gdal.Open(terrainTif, gdalconstConstants.GA_ReadOnly);
        ComHelper.Resample(dsDem, null, terrainFile, null, wkt, null, null);
        ComHelper.cutAndResample(dsDem, null, terrainFile, null, wkt, null, null);
        dsDem.delete();
        String landuseFile = inPath + File.separator + config.getLanduseFile();
        Dataset dsLanduse = gdal.Open(landuseTif, gdalconstConstants.GA_ReadOnly);
        ComHelper.Resample(dsLanduse, null, landuseFile, null, wkt, null, null);
        ComHelper.cutAndResample(dsLanduse, null, landuseFile, null, wkt, null, null);
        dsLanduse.delete();
        if (stationTif != null) {
            String stationFile = inPath + File.separator + "Station.tif";
            Dataset dsStation = gdal.Open(stationTif, gdalconstConstants.GA_ReadOnly);
            ComHelper.cutAndResample(dsStation, null, stationFile, null, wkt, null, null);
            dsStation.delete();
        }
    }
    public void updateTif(Simu simu, SimuData data2) throws IOException {
    public void updateTif(Simu simu, SimuData data2) {
        Dataset ds = gdal.Open(config.getSourceLanduse(), gdalconstConstants.GA_Update); // 以读写模式打开TIFF文件
        Band band = ds.GetRasterBand(1);
@@ -307,8 +320,6 @@
     * 调用UWSolver
     */
    public String callUwSolver(Simu simu, SimuData data) throws Exception {
        File uwBat = new File(config.getUwSolverBat());
        int duration = 3600 * data.getDuration(); // 秒数
        if (null != data.getRainfalls() && data.getRainfalls().size() > 1) {
            List<Rainfall> rainfalls = data.getRainfalls();
@@ -332,13 +343,14 @@
        Short type = simu.getType();
        SimulateType simulateType = SimulateType.of(type);
        String saveMode = simulateType.getSaveMode();
        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile, saveName, duration, saveFrames, saveMode);
        String startTime = TimeFormatUtil.formatDate(data.getStartTime());
        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile,
                saveName, duration, saveFrames, saveMode, startTime);
        vo.getResult().setSave_filter(config.getSaveFilter());
        String configFile = config.getInPath() + File.separator + data.getInPath() + File.separator + data.getInPath() + ".json";
        ComHelper.writeJson(configFile, JSON.toJSONString(vo));
//        ComHelper.writeJson(configFile, JSON.toJSONString(vo));
        mapper.writeValue(new File(configFile), vo);
        String cmd = String.format("%s \"%s\"", config.getUwSolverBat(), configFile);
        return callBat2(cmd);
    }
@@ -351,10 +363,9 @@
        String geotiffDir = inPath + File.separator + "depth";
        String terrainFile = inPath + File.separator + config.getTerrainFile();
        String jsonPath = inPath + File.separator + "zarr2tif.json";
        Zarr2Tif zarr2Tif = new Zarr2Tif(zarrFile, geotiffDir, terrainFile, data.getStartTime());
        ComHelper.writeJson(jsonPath, JSON.toJSONString(zarr2Tif));
        Zarr2Tif zarr2Tif = new Zarr2Tif(zarrFile, geotiffDir, terrainFile, Collections.emptyList());
//        ComHelper.writeJson(jsonPath, JSON.toJSONString(zarr2Tif));
        mapper.writeValue(new File(jsonPath), zarr2Tif);
        String cmd = String.format("%s \"%s\"", config.getZarr2tifBat(), jsonPath);
        return callBat2(cmd);
@@ -471,11 +482,10 @@
    }
    private List<Double> getValues(String csvPath) throws Exception {
        if (!new File(csvPath).exists()) return null;
        if (!new File(csvPath).exists()) return Collections.emptyList();
        List<String> list = Files.readAllLines(Paths.get(csvPath));
        list.remove(0);
        //list.remove(list.size() - 1);
        return list.stream()
                .map(s -> new BigDecimal(s).setScale(6, RoundingMode.HALF_DOWN).doubleValue())
@@ -547,7 +557,6 @@
        File dem = new File(serviceNameDir, "DEM.tif");
        resultDto.setTerrainFile(dem.getAbsolutePath());
        File newDepthDir = new File(serviceNameDir + File.separator + "depth_" + currentTime);
        File[] files = newDepthDir.listFiles();
        resultDto.setWaterPath(newDepthDir.getAbsolutePath());
        LayerDto layerDto = new LayerDto(config.getVer(), 4548, config.getSizes());
        String newLayerJsonName = "layer_" + currentTime + ".json";
@@ -556,25 +565,14 @@
        return newLayerJsonName;
    }
//    private static void readDemData(File serviceNameDir, LayerDto layerDto) {
//        File dem = new File(serviceNameDir, "DEM.tif");
//        Dataset ds = gdal.Open(dem.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
//        Band band = ds.GetRasterBand(1);
//        int width = ds.getRasterXSize();
//        int height = ds.getRasterYSize();
//        float[] buffer = new float[width * height];
//        band.ReadRaster(0, 0, width, height, buffer);
//        layerDto.getTerrain().getVals().put(width + "_" + height, buffer);
//    }
    private static File generateNewZarr2TifJson(File serviceNameDir, long currentTime) throws IOException {
    private File generateNewZarr2TifJson(File serviceNameDir, long currentTime) throws IOException {
        File srcZarr2TifJson = new File(serviceNameDir, "zarr2tif.json");
        ObjectMapper mapper = new ObjectMapper();
        Zarr2Tif zarr2Tif = mapper.readValue(srcZarr2TifJson, Zarr2Tif.class);
        //修改zarr2tif对象中的字段
        String stamp = TimeFormatUtil.formatTime(currentTime, "yyyy-MM-dd HH:mm:ss");
        zarr2Tif.setStart_timestamp(stamp);
        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
//        String stamp = TimeFormatUtil.formatTime(currentTime, "yyyy-MM-dd HH:mm:ss");
//        zarr2Tif.setStart_timestamp(stamp);
//        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
        String newZarrPath = serviceNameDir + File.separator + "result.zarr";
        zarr2Tif.setZarr_file(newZarrPath);
        zarr2Tif.setGeotiff_dir(serviceNameDir + File.separator + "depth_" + currentTime);
        File newZarr2TifJson = new File(serviceNameDir, "zarr2tif_" + currentTime + ".json");
@@ -584,16 +582,19 @@
    private File generateNewZarrConfigFile(File serviceNameDir, String serviceName, long currentTime, File newDatFile) throws IOException {
        File configFile = new File(serviceNameDir, serviceName + ".json");
        String str = Files.readAllLines(configFile.toPath()).stream().collect(Collectors.joining());
        ConfigVo configVo = JSON.parseObject(str, ConfigVo.class);
        configVo.setDuration(300); //固定为5min
        ConfigVo configVo = mapper.readValue(configFile, ConfigVo.class);
        int simulateTime = 300; //模拟时间,默认为5min,即300s
        int intervalTime = 60; //每帧的间隔时间,默认为60s,60s生成一帧
        configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file
        ResultVo result = configVo.getResult();
        result.setSave_interval(60); //60s生成一帧
        result.setSave_frames(5); //保留5帧
        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
        Integer oldDuration = configVo.getDuration();
//        result.setSave_start(oldDuration); //起始时间要在上次时间的基础上开始
        configVo.setDuration(oldDuration + simulateTime); //固定为5min
        result.setSave_interval(intervalTime);
        result.setSave_frames(result.getSave_frames() + (simulateTime / intervalTime)); //保留5帧,在原来的基础上增加5帧
//        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
        String newZarrPath = serviceNameDir + File.separator + "result.zarr";
        result.setSave_name(newZarrPath);
        ObjectMapper mapper = new ObjectMapper();
        File newConfigFile = new File(serviceNameDir, currentTime + ".json");
        mapper.writeValue(newConfigFile, configVo);
        return newConfigFile;