1
0
Просмотр исходного кода

Merge branch 'dev-excel0521' into dev-to-user-1

DESKTOP-2K9OVK9\siwei 3 недель назад
Родитель
Сommit
a41dcc8092

+ 2 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/mapper/CadastreFileMapper.java

@@ -108,6 +108,8 @@ public interface CadastreFileMapper {
 
 
     Map<String,Object> selectTableDataByBsm(@Param("tableName") String tableName,@Param("bsm") String bsm);
     Map<String,Object> selectTableDataByBsm(@Param("tableName") String tableName,@Param("bsm") String bsm);
 
 
+    String selectTableUnionEwkt(@Param("tableName") String tableName, @Param("validFlag") String validFlag);
+
 
 
     List<Map<String,Object>> selectTableRowExcel(@Param("tableName") String tableName, @Param("geomFlag") String geomFlag, @Param("bsmList") List<Object> bsmList, @Param("ywhList") List<String> ywhList);
     List<Map<String,Object>> selectTableRowExcel(@Param("tableName") String tableName, @Param("geomFlag") String geomFlag, @Param("bsmList") List<Object> bsmList, @Param("ywhList") List<String> ywhList);
 
 

+ 98 - 63
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ThematicImpl.java

@@ -22,6 +22,9 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.*;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
@@ -42,6 +45,10 @@ public class ThematicImpl implements ThematicService {
 
 
     @Value("${python.cadastralmap}")
     @Value("${python.cadastralmap}")
     private String cadastralmapPy;
     private String cadastralmapPy;
+    @Value("${python.qgisRunner:D:\\python\\run_qgis_python.bat}")
+    private String qgisPythonRunner;
+    @Value("${python.customCadastralmap}")
+    private String customCadastralmapPy;
     @Value("${python.parcelmap}")
     @Value("${python.parcelmap}")
     private String parcelmapPy;
     private String parcelmapPy;
 
 
@@ -232,24 +239,26 @@ public class ThematicImpl implements ThematicService {
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "开始制图任务。。。", "info");
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "开始制图任务。。。", "info");
                 // 更新状态为运行中
                 // 更新状态为运行中
                 thematicMapper.updateRwzt(uuid, "1");
                 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);
                 pb.redirectErrorStream(true);
                 Process process = null;
                 Process process = null;
                 process = pb.start();
                 process = pb.start();
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "制图任务启动。。。", "info");
                 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;
                 String line;
+                StringBuilder processOutput = new StringBuilder();
                 ArrayList<String> datas = new ArrayList<>();
                 ArrayList<String> datas = new ArrayList<>();
                 while ((line = reader.readLine()) != null) {
                 while ((line = reader.readLine()) != null) {
                     if (StringUtils.isNotEmpty(line)) {
                     if (StringUtils.isNotEmpty(line)) {
+                        processOutput.append(line).append("\n");
                         if (line.contains("||")) {
                         if (line.contains("||")) {
                             datas.add(line.split("ERROR")[0]);
                             datas.add(line.split("ERROR")[0]);
                         }
                         }
                     }
                     }
                 }
                 }
+                int exitCode = process.waitFor();
 
 
-                if (datas.size() > 0) {
+                if (exitCode == 0 && datas.size() > 0) {
                     // 更新状态为成功
                     // 更新状态为成功
                     thematicMapper.updateRwzt2(uuid, "2");
                     thematicMapper.updateRwzt2(uuid, "2");
                     for (String cur : datas) {
                     for (String cur : datas) {
@@ -264,10 +273,16 @@ public class ThematicImpl implements ThematicService {
                 } else {
                 } else {
                     // 更新状态为失败
                     // 更新状态为失败
                     thematicMapper.updateRwzt2(uuid, "3");
                     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();
                 e.printStackTrace();
+                thematicMapper.updateRwzt2(uuid, "3");
+                fzssFxrwrzHandleService.insertFxrwrz(uuid, "地籍制图", "制图失败:" + e.getMessage(), "info");
             }
             }
         });
         });
         return R.ok("制图任务创建成功!id:" + uuid);
         return R.ok("制图任务创建成功!id:" + uuid);
@@ -290,28 +305,30 @@ public class ThematicImpl implements ThematicService {
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "开始制图任务。。。", "info");
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "开始制图任务。。。", "info");
                 // 更新状态为运行中
                 // 更新状态为运行中
                 thematicMapper.updateRwzt(uuid, "1");
                 thematicMapper.updateRwzt(uuid, "1");
-                //TODO command ProcessBuilder
                 ProcessBuilder pb = null;
                 ProcessBuilder pb = null;
                 if (StringUtils.isBlank(zdh)) { //全量制作,慎用
                 if (StringUtils.isBlank(zdh)) { //全量制作,慎用
-                    pb = new ProcessBuilder(pythonHome, finalPyPath, tempPath, ztdw, ztry);
+                    pb = buildQgisProcess(finalPyPath, tempPath, ztdw, ztry);
                 } else {
                 } else {
-                    pb = new ProcessBuilder(pythonHome, finalPyPath, tempPath, ztdw, ztry, zdh);
+                    pb = buildQgisProcess(finalPyPath, tempPath, ztdw, ztry, zdh);
                 }
                 }
                 pb.redirectErrorStream(true);
                 pb.redirectErrorStream(true);
                 Process process = null;
                 Process process = null;
                 process = pb.start();
                 process = pb.start();
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "制图任务启动。。。", "info");
                 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;
                 String line;
+                StringBuilder processOutput = new StringBuilder();
                 ArrayList<String> datas = new ArrayList<>();
                 ArrayList<String> datas = new ArrayList<>();
                 while ((line = reader.readLine()) != null) {
                 while ((line = reader.readLine()) != null) {
                     if (StringUtils.isNotEmpty(line)) {
                     if (StringUtils.isNotEmpty(line)) {
+                        processOutput.append(line).append("\n");
                         if (line.contains("||")) {
                         if (line.contains("||")) {
                             datas.add(line.split("ERROR")[0]);
                             datas.add(line.split("ERROR")[0]);
                         }
                         }
                     }
                     }
                 }
                 }
-                if (datas.size() > 0) {
+                int exitCode = process.waitFor();
+                if (exitCode == 0 && datas.size() > 0) {
                     // 更新状态为成功
                     // 更新状态为成功
                     thematicMapper.updateRwzt2(uuid, "2");
                     thematicMapper.updateRwzt2(uuid, "2");
                     for (String cur : datas) {
                     for (String cur : datas) {
@@ -326,103 +343,91 @@ public class ThematicImpl implements ThematicService {
                 } else {
                 } else {
                     // 更新状态为失败
                     // 更新状态为失败
                     thematicMapper.updateRwzt2(uuid, "3");
                     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();
                 e.printStackTrace();
+                thematicMapper.updateRwzt2(uuid, "3");
+                fzssFxrwrzHandleService.insertFxrwrz(uuid, "宗地制图", "制图失败:" + e.getMessage(), "info");
             }
             }
         });
         });
         return R.ok("制图任务创建成功!id:" + uuid);
         return R.ok("制图任务创建成功!id:" + uuid);
     }
     }
 
 
 
 
-    //todo 自定义
     @Override
     @Override
     public R<Object> launchCustom(String name, String ztfw, String ztbt, String ztry, String ztdw, String fwlx, String bh) {
     public R<Object> launchCustom(String name, String ztfw, String ztbt, String ztry, String ztdw, String fwlx, String bh) {
         String uuid = StringUtils.getUUID();
         String uuid = StringUtils.getUUID();
         // 插入制图任务表  2---自定义制图
         // 插入制图任务表  2---自定义制图
-        thematicMapper.addRw(uuid, name, "2",  ztfw, ztry, ztdw);
+        thematicMapper.addRw(uuid, name, "2", ztfw, ztry, ztdw);
         fzssFxrwrzHandleService.insertFxrwrz(uuid, "自定义制图", "任务创建成功", "info");
         fzssFxrwrzHandleService.insertFxrwrz(uuid, "自定义制图", "任务创建成功", "info");
-        //这里根据类型,获取比例尺相关表的图幅号:
-        String tfhInfo = ""; //图幅号信息
-        String ewkt = "";
 
 
-        if (fwlx.equals("1")) { //1行政区;2地籍区;3地籍子区;4.单个图幅,5.自定义范围
-            tfhInfo = "3206.0-39385.5,3206.0-39386.0,3206.5-39384.5";
-        } else if (fwlx.equals("2")) {
-            // 根据地籍区的编号获取
+        String ewkt = "";
+        if ("1".equals(fwlx)) {
+            ewkt = cadastreFileMapper.selectTableUnionEwkt("gj_xzqh_shp", "0");
+            if (StringUtils.isBlank(ewkt) && StringUtils.isNotBlank(ztfw)) {
+                ewkt = ztfw;
+            }
+        } else if ("2".equals(fwlx)) {
             Map<String, Object> map = cadastreFileMapper.selectTableDataByBsm("djq", bh);
             Map<String, Object> map = cadastreFileMapper.selectTableDataByBsm("djq", bh);
             if (MapUtils.isNotEmpty(map)) {
             if (MapUtils.isNotEmpty(map)) {
                 ewkt = (String) map.get("geom2");
                 ewkt = (String) map.get("geom2");
             }
             }
-        } else if (fwlx.equals("3")) {
-            // 根据地籍区的编号获取
+        } else if ("3".equals(fwlx)) {
             Map<String, Object> map = cadastreFileMapper.selectTableDataByBsm("djzq", bh);
             Map<String, Object> map = cadastreFileMapper.selectTableDataByBsm("djzq", bh);
             if (MapUtils.isNotEmpty(map)) {
             if (MapUtils.isNotEmpty(map)) {
                 ewkt = (String) map.get("geom2");
                 ewkt = (String) map.get("geom2");
             }
             }
-        } else if (fwlx.equals("4")) { //单个图幅
-            tfhInfo = bh;
-        } else if (fwlx.equals("5")) { //自定义范围
-            if (StringUtils.isNotBlank(ztfw)) {
-                if (!ztfw.contains("SRID=")) {
-                    ztfw = "SRID=" + "4326" + ";" + ztfw;
-                }
-                ewkt = ztfw;
+        } else if (StringUtils.isNotBlank(ztfw)) {
+            if (!ztfw.contains("SRID=")) {
+                ztfw = "SRID=" + "4326" + ";" + ztfw;
             }
             }
+            ewkt = ztfw;
         }
         }
-
-        String blc = "500"; //todo  暂时固定500
-        if (StringUtils.isBlank(tfhInfo) && StringUtils.isNotBlank(blc) && StringUtils.isNotBlank(ewkt)) {
-            String tableName = "";
-            if ("500".equals(blc)) {
-                tableName = "tf_500";
-            } else if ("1000".equals(blc)) {
-                tableName = "tf_1000";
-            } else if ("2000".equals(blc)) {
-                tableName = "tf_2000";
-            }
-            List<Map<String, Object>> intersectsResult = cadastreFileMapper.intersectsTableWkt(tableName, "", ewkt);
-            if (CollectionUtils.isEmpty(intersectsResult)) {
-                throw new ServiceException("该区域不在可制图范围内");
-            }
-            tfhInfo = intersectsResult.stream()
-                    .map(s -> (String) s.get("tfh"))  // 直接转为String
-                    .filter(Objects::nonNull)
-                    .collect(Collectors.joining(","));
-        }
-        if (StringUtils.isBlank(tfhInfo)) {
-            throw new ServiceException("查询不到图幅号");
+        if (StringUtils.isBlank(ewkt)) {
+            throw new ServiceException("自定义制图范围为空,请选择或绘制制图范围");
         }
         }
 
 
-        // 判断需要执行的python脚本
         String tempPath = temppath + "专题图制作/自定义/" + name + uuid + "/";
         String tempPath = temppath + "专题图制作/自定义/" + name + uuid + "/";
-        String tfh = tfhInfo; //图幅号
-        String finalPyPath = cadastralmapPy;
+        String finalPyPath = customCadastralmapPy;
+        String finalEwkt = ewkt;
+        String finalTitle = StringUtils.isNotBlank(ztbt) ? ztbt : name;
+        String finalZtdw = StringUtils.defaultString(ztdw);
+        String finalZtry = StringUtils.defaultString(ztry);
         // 异步线程执行分析
         // 异步线程执行分析
         FixedThreadUtil.FIXED_THREAD_POOL.execute(() -> {
         FixedThreadUtil.FIXED_THREAD_POOL.execute(() -> {
             try {
             try {
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "自定义制图", "开始制图任务。。。", "info");
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "自定义制图", "开始制图任务。。。", "info");
                 // 更新状态为运行中
                 // 更新状态为运行中
                 thematicMapper.updateRwzt(uuid, "1");
                 thematicMapper.updateRwzt(uuid, "1");
-                //TODO command ProcessBuilder
-                ProcessBuilder pb = new ProcessBuilder(pythonHome, finalPyPath, blc, tempPath, tfh);
+                Path outputDir = Path.of(tempPath);
+                Files.createDirectories(outputDir);
+                Path rangeFile = outputDir.resolve("custom_range.ewkt");
+                Files.writeString(rangeFile, finalEwkt, StandardCharsets.UTF_8);
+                ProcessBuilder pb = buildCustomQgisProcess(finalPyPath, tempPath, "@" + rangeFile, finalTitle, finalZtdw, finalZtry);
                 pb.redirectErrorStream(true);
                 pb.redirectErrorStream(true);
                 Process process = null;
                 Process process = null;
                 process = pb.start();
                 process = pb.start();
                 fzssFxrwrzHandleService.insertFxrwrz(uuid, "自定义制图", "制图任务启动。。。", "info");
                 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;
                 String line;
+                StringBuilder processOutput = new StringBuilder();
                 ArrayList<String> datas = new ArrayList<>();
                 ArrayList<String> datas = new ArrayList<>();
                 while ((line = reader.readLine()) != null) {
                 while ((line = reader.readLine()) != null) {
                     if (StringUtils.isNotEmpty(line)) {
                     if (StringUtils.isNotEmpty(line)) {
+                        processOutput.append(line).append("\n");
                         if (line.contains("||")) {
                         if (line.contains("||")) {
                             datas.add(line.split("ERROR")[0]);
                             datas.add(line.split("ERROR")[0]);
                         }
                         }
                     }
                     }
                 }
                 }
+                int exitCode = process.waitFor();
 
 
-                if (datas.size() > 0) {
+                if (exitCode == 0 && datas.size() > 0) {
                     // 更新状态为成功
                     // 更新状态为成功
                     thematicMapper.updateRwzt2(uuid, "2");
                     thematicMapper.updateRwzt2(uuid, "2");
                     for (String cur : datas) {
                     for (String cur : datas) {
@@ -437,15 +442,45 @@ public class ThematicImpl implements ThematicService {
                 } else {
                 } else {
                     // 更新状态为失败
                     // 更新状态为失败
                     thematicMapper.updateRwzt2(uuid, "3");
                     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();
                 e.printStackTrace();
+                thematicMapper.updateRwzt2(uuid, "3");
+                fzssFxrwrzHandleService.insertFxrwrz(uuid, "自定义制图", "制图失败:" + e.getMessage(), "info");
             }
             }
         });
         });
         return R.ok("制图任务创建成功!id:" + uuid);
         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
     @Override
     public R<Object> productlist(String name, String taskid, Integer page, Integer limit) {
     public R<Object> productlist(String name, String taskid, Integer page, Integer limit) {

+ 9 - 0
siwei-modules/siwei-apply/src/main/resources/mapper/cadastre/CadastreFileMapper.xml

@@ -412,6 +412,15 @@
         LIMIT 1
         LIMIT 1
     </select>
     </select>
 
 
+    <select id="selectTableUnionEwkt" resultType="String">
+        SELECT public.st_asewkt(public.st_multi(public.st_union(geom))) geom2
+        FROM vector.${tableName}
+        WHERE 1=1
+        <if test="validFlag != null and validFlag != ''">
+            and valid_flag = #{validFlag}::int2
+        </if>
+    </select>
+
     <select id="checkQueryWhere" resultType="String">
     <select id="checkQueryWhere" resultType="String">
         EXPLAIN (FORMAT JSON)
         EXPLAIN (FORMAT JSON)
         SELECT 1 FROM vector.${tableName} WHERE ${queryWhere} LIMIT 1
         SELECT 1 FROM vector.${tableName} WHERE ${queryWhere} LIMIT 1