ywf 3 settimane fa
parent
commit
439cbb5cfe

+ 56 - 14
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ThematicImpl.java

@@ -45,6 +45,8 @@ public class ThematicImpl implements ThematicService {
 
     @Value("${python.cadastralmap}")
     private String cadastralmapPy;
+    @Value("${python.qgisRunner:D:\\python\\run_qgis_python.bat}")
+    private String qgisPythonRunner;
     @Value("${python.customCadastralmap}")
     private String customCadastralmapPy;
     @Value("${python.parcelmap}")
@@ -237,24 +239,26 @@ public class ThematicImpl implements ThematicService {
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "开始制图任务。。。", "info");
                 // 更新状态为运行中
                 thematicMapper.updateRwzt(uuid, "1");
-                //TODO command ProcessBuilder
-                ProcessBuilder pb = new ProcessBuilder(pythonHome, finalPyPath, blc, tempPath, tfh);
+                ProcessBuilder pb = buildQgisProcess(finalPyPath, blc, tempPath, tfh);
                 pb.redirectErrorStream(true);
                 Process process = null;
                 process = pb.start();
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "制图任务启动。。。", "info");
-                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
                 String line;
+                StringBuilder processOutput = new StringBuilder();
                 ArrayList<String> datas = new ArrayList<>();
                 while ((line = reader.readLine()) != null) {
                     if (StringUtils.isNotEmpty(line)) {
+                        processOutput.append(line).append("\n");
                         if (line.contains("||")) {
                             datas.add(line.split("ERROR")[0]);
                         }
                     }
                 }
+                int exitCode = process.waitFor();
 
-                if (datas.size() > 0) {
+                if (exitCode == 0 && datas.size() > 0) {
                     // 更新状态为成功
                     thematicMapper.updateRwzt2(uuid, "2");
                     for (String cur : datas) {
@@ -269,10 +273,16 @@ public class ThematicImpl implements ThematicService {
                 } else {
                     // 更新状态为失败
                     thematicMapper.updateRwzt2(uuid, "3");
-                    fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "制图失败", "info");
+                    String errorMsg = processOutput.length() > 0 ? processOutput.toString() : "Python未返回制图结果";
+                    if (errorMsg.length() > 900) {
+                        errorMsg = errorMsg.substring(0, 900);
+                    }
+                    fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "制图失败:" + errorMsg, "info");
                 }
-            } catch (IOException e) {
+            } catch (Exception e) {
                 e.printStackTrace();
+                thematicMapper.updateRwzt2(uuid, "3");
+                fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "制图失败:" + e.getMessage(), "info");
             }
         });
         return R.ok("制图任务创建成功!id:" + uuid);
@@ -295,28 +305,30 @@ public class ThematicImpl implements ThematicService {
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "开始制图任务。。。", "info");
                 // 更新状态为运行中
                 thematicMapper.updateRwzt(uuid, "1");
-                //TODO command ProcessBuilder
                 ProcessBuilder pb = null;
                 if (StringUtils.isBlank(zdh)) { //全量制作,慎用
-                    pb = new ProcessBuilder(pythonHome, finalPyPath, tempPath, ztdw, ztry);
+                    pb = buildQgisProcess(finalPyPath, tempPath, ztdw, ztry);
                 } else {
-                    pb = new ProcessBuilder(pythonHome, finalPyPath, tempPath, ztdw, ztry, zdh);
+                    pb = buildQgisProcess(finalPyPath, tempPath, ztdw, ztry, zdh);
                 }
                 pb.redirectErrorStream(true);
                 Process process = null;
                 process = pb.start();
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "制图任务启动。。。", "info");
-                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
                 String line;
+                StringBuilder processOutput = new StringBuilder();
                 ArrayList<String> datas = new ArrayList<>();
                 while ((line = reader.readLine()) != null) {
                     if (StringUtils.isNotEmpty(line)) {
+                        processOutput.append(line).append("\n");
                         if (line.contains("||")) {
                             datas.add(line.split("ERROR")[0]);
                         }
                     }
                 }
-                if (datas.size() > 0) {
+                int exitCode = process.waitFor();
+                if (exitCode == 0 && datas.size() > 0) {
                     // 更新状态为成功
                     thematicMapper.updateRwzt2(uuid, "2");
                     for (String cur : datas) {
@@ -331,10 +343,16 @@ public class ThematicImpl implements ThematicService {
                 } else {
                     // 更新状态为失败
                     thematicMapper.updateRwzt2(uuid, "3");
-                    fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "制图失败", "info");
+                    String errorMsg = processOutput.length() > 0 ? processOutput.toString() : "Python未返回制图结果";
+                    if (errorMsg.length() > 900) {
+                        errorMsg = errorMsg.substring(0, 900);
+                    }
+                    fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "制图失败:" + errorMsg, "info");
                 }
-            } catch (IOException e) {
+            } catch (Exception e) {
                 e.printStackTrace();
+                thematicMapper.updateRwzt2(uuid, "3");
+                fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "制图失败:" + e.getMessage(), "info");
             }
         });
         return R.ok("制图任务创建成功!id:" + uuid);
@@ -390,7 +408,7 @@ public class ThematicImpl implements ThematicService {
                 Files.createDirectories(outputDir);
                 Path rangeFile = outputDir.resolve("custom_range.ewkt");
                 Files.writeString(rangeFile, finalEwkt, StandardCharsets.UTF_8);
-                ProcessBuilder pb = new ProcessBuilder(pythonHome, finalPyPath, tempPath, "@" + rangeFile, finalTitle, finalZtdw, finalZtry);
+                ProcessBuilder pb = buildCustomQgisProcess(finalPyPath, tempPath, "@" + rangeFile, finalTitle, finalZtdw, finalZtry);
                 pb.redirectErrorStream(true);
                 Process process = null;
                 process = pb.start();
@@ -439,6 +457,30 @@ public class ThematicImpl implements ThematicService {
         return R.ok("制图任务创建成功!id:" + uuid);
     }
 
+    private ProcessBuilder buildCustomQgisProcess(String scriptPath, String tempPath, String rangeFileArg, String title, String unit, String person) {
+        return buildQgisProcess(scriptPath, tempPath, rangeFileArg, title, unit, person);
+    }
+
+    private ProcessBuilder buildQgisProcess(String scriptPath, String... args) {
+        if (StringUtils.isNotBlank(qgisPythonRunner) && Files.exists(Path.of(qgisPythonRunner))) {
+            List<String> command = new ArrayList<>();
+            command.add("cmd.exe");
+            command.add("/c");
+            command.add(qgisPythonRunner);
+            command.add(scriptPath);
+            command.addAll(Arrays.asList(args));
+            return new ProcessBuilder(command);
+        }
+        if (StringUtils.isNotBlank(pythonHome) && Files.exists(Path.of(pythonHome))) {
+            List<String> command = new ArrayList<>();
+            command.add(pythonHome);
+            command.add(scriptPath);
+            command.addAll(Arrays.asList(args));
+            return new ProcessBuilder(command);
+        }
+        throw new ServiceException("QGIS 启动器不存在,python.home=" + pythonHome + ",python.qgisRunner=" + qgisPythonRunner);
+    }
+
 
     @Override
     public R<Object> productlist(String name, String taskid, Integer page, Integer limit) {