wuww
2025-05-04 9db8d773af4f886cf0652b0ce303631a90692a50
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
package com.se.nsl.service;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.se.nsl.config.PropertiesConfig;
import com.se.nsl.domain.po.DataPo;
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.StringHelper;
import com.se.nsl.helper.WebHelper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.ogr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.Timestamp;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
@Slf4j
@Service
@SuppressWarnings("ALL")
public class ResolveService {
    @Resource
    SimuService simuService;
 
    @Resource
    PropertiesConfig config;
    @Autowired
    private UwService uwService;
 
    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.setEpsg(4548);
        data.setEnvelope(envelope);
 
        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 {
            DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class);
 
            update(simu, 1, "开始");
 
            initArgs(data);
            String rs = callUwSolver(data);
            //gedbService.copeVectors(token, data, db);
 
            //update(simu, 3, null);
            //gedbService.copeDem(token, data);
 
            //update(simu, 4, null);
            //uwService.createRainFile(data);
 
            update(simu, 10, "完成");
        } 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);
    }
 
    /**
     * 初始化参数
     */
    private void initArgs(DataPo data) throws IOException {
        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());
 
        // 临时复制高程tif,以后需要自行切割
        File uwBat = new File(config.getUwSolverBat());
        String sourceTif = uwBat.getParent() + File.separator + "data" + File.separator + "Hillzone.tif";
        String targetTif = inPath + File.separator + config.getDemFile();
        Files.copy(Paths.get(sourceTif), Paths.get(targetTif), StandardCopyOption.REPLACE_EXISTING);
    }
 
    private void createDir(String path) {
        File f = new File(path);
        if (f.exists() && f.isDirectory()) {
            FileUtil.del(f);
        }
        f.mkdirs();
    }
 
    /**
     * 调用UWSolver
     */
    private String callUwSolver(DataPo data) throws Exception {
        File uwBat = new File(config.getUwSolverBat());
 
        ConfigVo vo = new ConfigVo(data.getDuration(), config.getSaveFrames());
        //String configFile = uwBat.getParent() + File.separator + data.getInPath() + ".json";
        String configFile = config.getInPath() + File.separator + data.getInPath() + File.separator + data.getInPath() + ".json";
        ComHelper.writeJson(configFile, JSON.toJSONString(vo));
 
        String cmd = String.format("%s %s", config.getUwSolverBat(), configFile);
 
        return callBat(cmd);
    }
 
    private String callZarr2tif(DataPo data) throws Exception {
        String cmd = "";
 
        return callBat(cmd);
    }
 
    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;
        }
    }
}