dcb
2025-07-07 97629da6a002b534da10f828187fd6cd65941aa4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package com.se.nsl.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.se.nsl.config.PropertiesConfig;
import com.se.nsl.domain.dto.*;
import com.se.nsl.domain.po.Rainfall;
import com.se.nsl.domain.po.Simu;
import com.se.nsl.domain.po.SimuData;
import com.se.nsl.domain.vo.ConfigVo;
import com.se.nsl.helper.ComHelper;
import com.se.nsl.helper.GdalHelper;
import com.se.nsl.helper.StringHelper;
import com.se.nsl.helper.WebHelper;
import com.se.nsl.utils.AreaType;
import com.se.nsl.utils.RainFallUnit;
import com.se.nsl.utils.SimulateType;
import com.se.nsl.utils.TimeFormatUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.ogr;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
 
@Slf4j
@Service
@SuppressWarnings("ALL")
public class ResolveService {
    @Resource
    SimuService simuService;
 
    @Resource
    PropertiesConfig config;
 
    @Resource
    UwService uwService;
 
    @Resource
    TestService testService;
 
    private ObjectMapper mapper = new ObjectMapper();
 
    Integer DIGIT = 1000000;
 
    SimpleDateFormat YYYYMDHM = new SimpleDateFormat("yyyy M d H m ");
 
    List<String> MODES = new ArrayList<>(Arrays.asList("正态分布", "平均分布", "波动平均分布", "持续上升"));
 
    public int start(Simu simu) {
        Date now = new Date();
        String date = StringHelper.YMDHMS2_FORMAT.format(now);
 
        // 将WKT转换为Geometry对象
        Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
        if (geom.GetGeometryType() == ogr.wkbMultiPolygon) {
            geom = geom.GetGeometryRef(0);
        }
        double[] envelope = new double[4];
        geom.GetEnvelope(envelope);
 
        SimuData data = JSON.parseObject(simu.getData(), SimuData.class);
        data.setInPath(date);
        data.setOutPath(date);
        data.setEnvelope(envelope);
        data.setEpsg(config.getEpsg());
 
        simu.setData(JSON.toJSONString(data));
        simu.setServiceName(date);
        simu.setStatus(1); // 0-创建仿真,1-预处理,2-分析中,10-完成,20-出错
        simu.setUpdateTime(new Timestamp(now.getTime()));
 
        int rows = simuService.updateById(simu);
        if (rows > 0) {
            asyncCall(simu);
        }
 
        return rows;
    }
 
    private void asyncCall(Simu simu) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            @SneakyThrows
            public void run() {
                cope(simu);
            }
        });
        executor.shutdown();
    }
 
    private void cope(Simu simu) {
        try {
            SimuData data = JSONUtil.toBean(simu.getData(), SimuData.class);
 
            update(simu, 1, "初始化参数");
            initArgs(simu, data);
            createRainfallFile(simu, data);
 
            update(simu, 2, "调用求解器");
            callUwSolver(simu, data);
 
            update(simu, 3, "调用Zarr转Tif");
            callZarr2tif(data);
 
            update(simu, 4, "解析数据");
            createNsl(data);
 
            update(simu, 10, "完成");
            log.info("模拟完成");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            update(simu, 20, ex.getMessage());
        }
    }
 
    private void update(Simu simu, int status, String rs) {
        simu.setStatus(status);
        if (null != rs) simu.setResult(rs);
        simu.setUpdateTime(WebHelper.getCurrentTimestamp());
 
        simuService.updateById(simu);
    }
 
    /**
     * 初始化参数
     */
    public void initArgs(Simu simu, SimuData data) {
        String inPath = config.getInPath() + File.separator + data.getInPath();
        createDir(inPath);
        createDir(inPath + File.separator + "depth");
        createDir(inPath + File.separator + "velocity");
        createDir(config.getOutPath() + File.separator + data.getOutPath());
 
        Short areaType = simu.getAreaType();
        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();
            Optional<File> first = Arrays.stream(keyDitchDir.listFiles()).filter(f -> f.getName().equals(areaName)).findFirst();
            if (first.isPresent()) {
                File targetTifDir = first.get();
                File[] files = targetTifDir.listFiles();
                for (File file : files) {
                    String name = file.getName();
                    if (name.toLowerCase().contains("dem")) {
                        terrainTif = file.getAbsolutePath();
                    }
                    if (name.toLowerCase().contains("landuse")) {
                        landuseTif = file.getAbsolutePath();
                    }
                    if (name.toLowerCase().contains("station")) {
                        stationTif = file.getAbsolutePath();
                    }
                }
            }
        }
 
        Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
        if (geom.GetGeometryType() == ogr.wkbMultiPolygon) geom = geom.GetGeometryRef(0);
        SpatialReference dstSR = GdalHelper.createSpatialReference(config.getEpsg());
 
        CoordinateTransformation ct = CoordinateTransformation.CreateCoordinateTransformation(GdalHelper.SR4326, dstSR);
        geom.Transform(ct);
        String wkt = geom.ExportToWkt();
 
        String terrainFile = inPath + File.separator + config.getTerrainFile();
 
        Dataset dsDem = gdal.Open(terrainTif, gdalconstConstants.GA_ReadOnly);
        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.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) {
        Dataset ds = gdal.Open(config.getSourceLanduse(), gdalconstConstants.GA_Update); // 以读写模式打开TIFF文件
 
        Band band = ds.GetRasterBand(1);
        if (band.GetRasterDataType() != gdalconstConstants.GDT_Byte) {
            System.err.println("错误:非Byte类型数据");
            ds.delete();
            return;
        }
 
        int width = band.getXSize();
        int height = band.getYSize();
 
        // 读取Byte类型数据
        byte[] data = new byte[width * height];
        band.ReadRaster(0, 0, width, height, data);
 
        // 替换0值为8(注意Java的byte是有符号的,需处理0-255范围)
        for (int i = 0; i < data.length; i++) {
            if ((data[i] & 0xFF) == 0) {  // 无符号比较
                data[i] = (byte) 8;
            }
        }
 
        band.WriteRaster(0, 0, width, height, data); // 写回数据并保存
        band.SetNoDataValue(8.0);  // 设置新Nodata值
        band.FlushCache(); // 强制写入更改
        ds.delete();
    }
 
    private void createDir(String path) {
        File f = new File(path);
        if (f.exists() && f.isDirectory()) {
            FileUtil.del(f);
        }
        f.mkdirs();
    }
 
    public void createRainfallFile(Simu simu, SimuData data) throws Exception {
        List<Rainfall> rainfalls = data.getRainfalls();
        if (null == rainfalls || rainfalls.size() < 2) createRainfall(simu);
 
        String dat = config.getInPath() + File.separator + "Rainfalls" + File.separator + simu.getId() + ".dat";
        String rainfallFile = config.getInPath() + File.separator + data.getInPath() + File.separator + "rainfall.dat";
        if (new File(dat).exists()) {
            Files.copy(Paths.get(dat), Paths.get(rainfallFile), StandardCopyOption.REPLACE_EXISTING);
            return;
        }
 
        List<String> list = new ArrayList<>();
        list.add(config.getRainfallTitle());
 
        double centerX = ComHelper.getMinVal((data.getMinx() + data.getMaxx()) / 2, DIGIT);
        double centerY = ComHelper.getMinVal((data.getMiny() + data.getMaxy()) / 2, DIGIT);
        String prefix = config.getRainfallSite() + " " + centerX + " " + centerY + " ";
        //如果没有单位或者单位为mm/h则按照 mm/h计算,否则按照mm/5min计算
//        int unit = StringUtils.isEmpty(data.getIntensityUnit()) || "mm/h".equals(data.getIntensityUnit()) ? 60 : 5;
        String iu = data.getIntensityUnit();
        RainFallUnit rfUnit = RainFallUnit.of(iu);
        data.setIntensityUnit(rfUnit.getUnit());
        int unit = rfUnit.getC();
        int c = rainfalls.size() - 1;
        for (int i = 0; i < c; i++) {
            Rainfall r1 = rainfalls.get(i);
            Rainfall r2 = rainfalls.get(i + 1);
 
            list.addAll(calcRainfall(r1, r2, prefix, unit));
        }
        //list.add(prefix + YYYYMDHM.format(rainfalls.get(c).getTime()) + getMinVal(rainfalls.get(c).getIntensity() / unit, DIGIT));
        list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(rainfalls.get(c).getTime()), getMinVal(rainfalls.get(c).getIntensity() / unit, DIGIT)));
//        list.add(0, "1 " + (list.size() - 1));
 
        Files.write(Paths.get(rainfallFile), list, StandardCharsets.UTF_8);
    }
 
    public static double getMinVal(double val, double radix) {
        return ((long) Math.floor(val * radix)) / radix;
    }
 
    // beijing 116.0 40.0 2025 1 1 0 13 1.666666
    private List<String> calcRainfall(Rainfall r1, Rainfall r2, String prefix, int unit) {
        long mins = Math.abs(r2.getTime().getTime() - r1.getTime().getTime()) / (1000 * 60); // 计算分钟数
        double diff = (r2.getIntensity() - r1.getIntensity()) / mins / unit; //ComHelper.getMinVal((r1.getIntensity() - r2.getIntensity()) / mins / unit, DIGIT);
 
        Calendar cal = Calendar.getInstance();
        cal.setTime(r1.getTime());
 
        List<String> list = new ArrayList<>();
        double intensity = ComHelper.getMinVal(r1.getIntensity() / unit, DIGIT);
        for (int i = 0; i < mins; i++) {
            //list.add(prefix + YYYYMDHM.format(cal.getTime()) + getMinVal((intensity + diff * i), DIGIT));
            list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(cal.getTime()), getMinVal((intensity + diff * i), DIGIT)));
            cal.add(Calendar.MINUTE, 1);
        }
 
        return list;
    }
 
    /**
     * 调用UWSolver
     */
    public String callUwSolver(Simu simu, SimuData data) throws Exception {
        int duration = 3600 * data.getDuration(); // 秒数
        if (null != data.getRainfalls() && data.getRainfalls().size() > 1) {
            List<Rainfall> rainfalls = data.getRainfalls();
            int size = rainfalls.size();
            Rainfall last = rainfalls.get(size - 1);
            Rainfall first = rainfalls.get(0);
            Instant end = last.getTime().toInstant();
            Instant start = first.getTime().toInstant();
            long diff = ChronoUnit.SECONDS.between(end, start);
            duration = (int) (Math.abs(diff));
        }
 
        String inPath = config.getInPath() + File.separator + data.getInPath();
        String terrainFile = inPath + File.separator + config.getTerrainFile();
        String landuseFile = inPath + File.separator + config.getLanduseFile();
        String rainfallFile = (inPath + File.separator + "rainfall.dat");
        String saveName = inPath + File.separator + "result.zarr";
//        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile, saveName, duration, config.getSaveFrames());
        Double saveFrameInterval = config.getSaveFrameInterval();
        int saveFrames = (int) (duration / saveFrameInterval);
        Short type = simu.getType();
        SimulateType simulateType = SimulateType.of(type);
        String saveMode = simulateType.getSaveMode();
        String startTime = TimeFormatUtil.formatDate(data.getStartTime());
        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile,
                saveName, duration, saveFrames, saveMode, startTime);
        vo.setEvaporation(config.getEvaporation());
        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));
        mapper.writeValue(new File(configFile), vo);
        String cmd = String.format("%s \"%s\"", config.getUwSolverBat(), configFile);
        return callBat2(cmd);
    }
 
    /**
     * 调用zarr2tif
     */
    public String callZarr2tif(SimuData data) throws Exception {
        String inPath = config.getInPath() + File.separator + data.getInPath();
        String zarrFile = inPath + File.separator + "result.zarr";
        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, 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);
    }
 
    private String callBat2(String cmd) {
        try {
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", cmd);
            pb.redirectErrorStream(true); // 合并错误流到标准输出
 
            Process process = pb.start();
            process.getOutputStream().close();
 
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    log.info(line);
                }
            }
 
            int exitCode = process.waitFor();
 
            return "ok"; // sb.toString();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    private String callBat(String cmd) {
        try {
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", cmd);
            pb.redirectErrorStream(true); // 合并错误流到标准输出
 
            Process process = pb.start();
            process.getOutputStream().close();
 
            /*StringBuilder sb = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                    sb.append(line);
                }
            }*/
 
            int exitCode = process.waitFor();
 
            return "ok"; // sb.toString();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    private void createNsl(SimuData data) throws Exception {
        testService.test(data);
    }
 
    private void procTifs(String tifPath, String outPath, Date startTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
 
        for (File file : new File(tifPath).listFiles()) {
            if (!file.exists() || !file.isDirectory()) continue;
 
            File tif = new File(tifPath + "\\" + file.getName() + File.separator + "depth.tif");
            if (!tif.exists() || tif.isDirectory()) continue;
 
            calendar.add(Calendar.SECOND, 1);
            String newName = df.format(calendar.getTime());
            String newFile = outPath + File.separator + newName + ".tif";
 
            System.out.println(newFile);
            tif.renameTo(new File(newFile));
            file.delete();
        }
    }
 
    public String createRainfallCsv(String csvPath, String mode, double total, double intensity, int hours) {
        // python 脚本名.py <参数1-csv文件名> <参数2-降雨模式:正态分布|平均分布|波动平均分布|持续上升> <参数3-降雨总量> <参数4-最大雨强> <参数5-降雨时间(分钟)>
        String cmd = String.format("%s \"%s\" \"%s\" %f %f %d", config.getCreateRainfall(), csvPath, mode, total, intensity, hours * 60);
        return callBat(cmd);
    }
 
    public void createRainfall(Simu simu) throws Exception {
        SimuData data = JSON.parseObject(simu.getData(), SimuData.class);
        if (null == data.getMode() || MODES.contains(data.getMode())) data.setMode(MODES.get(0));
        if (StringUtils.isEmpty(data.getIntensityUnit())) data.setIntensityUnit("mm/h");
 
        Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
        if (geom.GetGeometryType() == ogr.wkbMultiPolygon) geom = geom.GetGeometryRef(0);
        double[] envelope = new double[4];
        geom.GetEnvelope(envelope);
        data.setEnvelope(envelope);
        data.setEpsg(config.getEpsg());
 
        String basePath = config.getInPath() + File.separator + "Rainfalls";
        if (!new File(basePath).exists()) new File(basePath).mkdirs();
        if (null == simu.getCreateTime()) simu.setCreateTime(new Timestamp(new Date().getTime()));
 
        String csvPath = basePath + File.separator + simu.getId() + ".csv";
        int unit = StringUtils.isEmpty(data.getIntensityUnit()) || "mm/h".equals(data.getIntensityUnit()) ? 60 : 5;
        createRainfallCsv(csvPath, data.getMode(), data.getTotal(), data.getIntensity() / unit, data.getDuration());
 
        List<Double> list = getValues(csvPath);
        if (!CollUtil.isEmpty(list)) {
            data.setRainfalls(new ArrayList<>());
            setRainfalls(simu, data, list);
        }
 
        simu.setData(JSON.toJSONString(data));
    }
 
    private List<Double> getValues(String csvPath) throws Exception {
        if (!new File(csvPath).exists()) return Collections.emptyList();
 
        List<String> list = Files.readAllLines(Paths.get(csvPath));
        list.remove(0);
 
        return list.stream()
                .map(s -> new BigDecimal(s).setScale(6, RoundingMode.HALF_DOWN).doubleValue())
                .collect(Collectors.toList());
    }
 
    private void setRainfalls(Simu simu, SimuData data, List<Double> vals) throws Exception {
        String basePath = config.getInPath() + File.separator + "Rainfalls";
        String dat = basePath + File.separator + simu.getId() + ".dat";
 
        Calendar cal = Calendar.getInstance();
        cal.setTime(data.getStartTime());
 
        List<String> list = new ArrayList<>();
        list.add(config.getRainfallTitle());
 
        double centerX = ComHelper.getMinVal((data.getMinx() + data.getMaxx()) / 2, DIGIT);
        double centerY = ComHelper.getMinVal((data.getMiny() + data.getMaxy()) / 2, DIGIT);
        String prefix = config.getRainfallSite() + " " + centerX + " " + centerY + " ";
 
        Double total = 0.0;
        for (int i = 0, c = vals.size(); i < c; i++) {
            total += vals.get(i);
            if (i % 15 == 0) {
                data.getRainfalls().add(new Rainfall(cal.getTime(), vals.get(i), total));
            }
            list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(cal.getTime()), vals.get(i)));
 
            cal.add(Calendar.MINUTE, 1);
        }
//        list.add(0, "1 " + (list.size() - 1));
 
        Files.write(Paths.get(dat), list, StandardCharsets.UTF_8);
    }
 
    public List<String> simulationResults(String serviceName) {
        String outPath = config.getOutPath();
        File serviceNameDir = new File(outPath, serviceName);
        List<String> res = new ArrayList<>();
        File[] files = serviceNameDir.listFiles();
        for (File file : files) {
            String name = file.getName();
            if (name.startsWith("layer")) {
                res.add(name);
            }
        }
        return res;
    }
}