소스 검색

完成栅格数据切片进度条

wuweihao 5 년 전
부모
커밋
6fe27e0dd3

+ 1 - 1
src/main/java/com/fd/constant/Command.java

@@ -59,7 +59,7 @@ public class Command {
      *
      *
      */
-    public static String RASTER_OSGEO  = "docker run --rm -v /root/gis/cesium/input:/root/gis/cesium/input " +
+    public static String RASTER_SLICE_OSGEO  = "docker run --rm -v /root/gis/cesium/input:/root/gis/cesium/input " +
             "osgeo/gdal python3 /usr/bin/gdal2tiles.py --zoom=9-19 --processes=6 @filePath /root/gis/cesium/input/@fileName";
 
 

+ 2 - 1
src/main/java/com/fd/controller/FdModelController.java

@@ -48,7 +48,8 @@ public class FdModelController {
 
         // 文件名全名
         String fileName = file.getOriginalFilename();
-        String s = StringUtils.substringAfter(fileName, ".");
+        String s = StringUtils.substringAfterLast(fileName, ".");
+
         if (!"zip".equals(s)) {
             return new R(50007,MsgCode.E50007);
         }

+ 82 - 23
src/main/java/com/fd/controller/RasterController.java

@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.Date;
 
 /**
@@ -46,7 +47,7 @@ public class RasterController {
 
         // 文件名全名
         String fileName = file.getOriginalFilename();
-        String s = StringUtils.substringAfter(fileName, ".");
+        String s = StringUtils.substringAfterLast(fileName, ".");
         if (!"tif".equals(s)) {
             return new R(50008,MsgCode.E50008);
         }
@@ -175,17 +176,16 @@ public class RasterController {
         String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
         // fileName_tiles
         fileName = fileName + "_tiles";
-        String cmd = Command.RASTER_OSGEO;
+        String cmd = Command.RASTER_SLICE_OSGEO;
         cmd = cmd.replace("@filePath", entity.getFileUrl());
         cmd = cmd.replace("@fileName",fileName);
         log.info("cmd: {}", cmd);
 
-        Integer integer = cmdServer.exeCmdInt(cmd);
-        if (integer != 0) {
-            log.info("error command transform");
-            return new R(50005, MsgCode.E50005);
-        }
-
+//        Integer integer = cmdServer.exeCmdRasterSlice(cmd);
+//        if (integer != 0) {
+//            log.info("error command exeCmdRasterSlice");
+//            return new R(50005, MsgCode.E50005);
+//        }
 
         FileEntity fileEntity = new FileEntity();
         fileEntity.setFileName(fileName);
@@ -195,9 +195,43 @@ public class RasterController {
         fileEntity.setType(TypeCode.FILE_TYPE_RASTER_TIF);
         fileEntity = fileServer.save(fileEntity);
 
+        // 多线程运行切片
+        new Thread(new RasterSliceThread(cmd, fileEntity)).start();
+
         return new R(200, fileEntity) ;
     }
 
+
+    public class RasterSliceThread implements Runnable{
+
+        private String cmd;
+
+        private FileEntity entity;
+
+        private RasterSliceThread(String cmd, FileEntity entity){
+            this.cmd = cmd;
+            this.entity = entity;
+        }
+
+        @Override
+        public void run() {
+            log.warn("run RasterSliceThread");
+            Integer integer = cmdServer.exeCmdRasterSlice(cmd, entity);
+            if (integer != 0) {
+                log.info("error command exeCmdRasterSlice");
+//                return new R(50005, MsgCode.E50005);
+                // 如果命令运行失败,删除刚才创建的实体类
+                fileServer.deleteById(entity.getId());
+                return;
+            }
+//            // 需要告诉前端,当状态为1是,才是可以的
+//            entity.setStatus(1);
+//            fileServer.save(entity);
+
+            log.warn("end RasterSliceThread");
+        }
+    }
+
     // 严格坐标转换
     private FileEntity cmdTansformGdalwarpStrict(Long fileId){
         log.info("run cmdTansformGdalwarpStrict: {}", fileId);
@@ -233,21 +267,46 @@ public class RasterController {
     }
 
 
+    //        String a = "112222.zip";
+//        String b = "11/2222/aa";
+//        String c = "11/2222/aa.zip";
+//        String e = "/root/gis/cesium/input/test/clip.shp";
+//        System.out.println(StringUtils.stripStart(a, "."));
+//        System.out.println("After: " + StringUtils.substringAfter(a, "."));
+//        System.out.println("before: " + StringUtils.substringBefore(a, "."));
+//        System.out.println("c: " + StringUtils.substringBefore(c, "/"));
+//        System.out.println("c: " + StringUtils.substringBeforeLast(c, "/"));
+//        System.out.println(StringUtils.substringBefore(b, "/"));
+//        System.out.println(StringUtils.substringBeforeLast(b, "/"));
+//        System.out.println(StringUtils.substring(b, b.lastIndexOf("/") + 1, b.length()));
+//        System.out.println("e: " + StringUtils.substringBeforeLast(e, "/"));
+//        System.out.println("e1: " + StringUtils.substring(e, e.indexOf("input/") + 6, e.lastIndexOf("/")));
+//        System.out.println("===========================================");
+//        System.out.println();
+//
+//        System.out.println("a: " + StringUtils.substringAfterLast(a, "."));
+
     public static void main(String[] args) {
-        String a = "112222.zip";
-        String b = "11/2222/aa";
-        String c = "11/2222/aa.zip";
-        String e = "/root/gis/cesium/input/test/clip.shp";
-        System.out.println(StringUtils.stripStart(a,"."));
-        System.out.println("After: "+StringUtils.substringAfter(a,"."));
-        System.out.println("before: "+StringUtils.substringBefore(a,"."));
-        System.out.println("c: "+ StringUtils.substringBefore(c,"/"));
-        System.out.println("c: "+ StringUtils.substringBeforeLast(c,"/"));
-        System.out.println(StringUtils.substringBefore(b,"/"));
-        System.out.println(StringUtils.substringBeforeLast(b,"/"));
-        System.out.println(StringUtils.substring(b, b.lastIndexOf("/")+1, b.length()));
-        System.out.println("e: " + StringUtils.substringBeforeLast(e,"/"));
-        System.out.println("e1: " + StringUtils.substring(e,e.indexOf("input/") +6,e.lastIndexOf("/")));
-//        String path = StringUtils.substringBefore(e,"/");
+        String a = "0...10...20...30...40...50...60...70...80...90...100.....10..11";
+        String b = "...10";
+        String c = "...10...20";
+        String d = "...10...100";
+        String re = "\\.\\.\\.";
+        String r1 = "...";
+//        String s = a.replaceAll(re, "@");
+        String rr = "\\.\\.\\.[0-9]{2,3}";
+        System.out.println("b: " + b.matches(rr));
+        System.out.println("c: " + c.matches(rr));
+
+        System.out.println("c: " + StringUtils.substring(c, -2));
+        System.out.println("d: " + StringUtils.substring(d, -2));
+
+        System.out.println("a: " + StringUtils.contains(a, "..."));
+
+        System.out.println("d: " + StringUtils.substring(d, -6));
+        System.out.println("c: " + StringUtils.substring(c, -6));
+
+        System.out.println("a: " + a.matches(r1));
+
     }
 }

+ 3 - 4
src/main/java/com/fd/entity/FileEntity.java

@@ -29,10 +29,9 @@ public class FileEntity extends BaseEntity implements Serializable {
 //    @JsonIgnore
 //    @Column
 //    private String description; // 描述
-//
-//    @JsonIgnore
-//    @Column
-//    private String version; // 版本
+
+    @Column
+    private Integer progress; // 进度
 
 
     @Column

+ 3 - 0
src/main/java/com/fd/server/CmdServer.java

@@ -1,6 +1,7 @@
 package com.fd.server;
 
 
+import com.fd.entity.FileEntity;
 import com.fd.util.R;
 
 /**
@@ -23,4 +24,6 @@ public interface CmdServer {
     Integer exeCmdJudgeCoord(String cmd);
 
     Integer exeCmdRasterJudgeCoord(String cmd);
+
+    Integer exeCmdRasterSlice(String cmd, FileEntity entity);
 }

+ 107 - 3
src/main/java/com/fd/server/impl/CmdServerImpl.java

@@ -6,6 +6,7 @@ import com.fd.repository.FileRepository;
 import com.fd.server.CmdServer;
 import com.fd.util.R;
 import lombok.extern.log4j.Log4j2;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -77,7 +78,10 @@ public class CmdServerImpl implements CmdServer {
             while ((errorLine = errorBuf.readLine()) != null) {
                 errorStr.append(errorLine).append("\n");
             }
-            log.info("error result: {}", errorStr.toString());
+//            log.info("error result: {}", errorStr.toString());
+            if (StringUtils.isNotEmpty(errorStr)){
+                log.info("error result: {}", errorStr.toString());
+            }
 
             // success ,没有获取到信息
             String line;
@@ -214,7 +218,11 @@ public class CmdServerImpl implements CmdServer {
             while ((errorLine = errorBuf.readLine()) != null) {
                 errorStr.append(errorLine).append("\n");
             }
-            log.info("error result: {}", errorStr.toString());
+//            log.info("error result: {}", errorStr.toString());
+            if (StringUtils.isNotEmpty(errorStr)){
+                log.info("error result: {}", errorStr.toString());
+            }
+
 
             // success ,没有获取到信息
             String line;
@@ -280,7 +288,9 @@ public class CmdServerImpl implements CmdServer {
             while ((errorLine = errorBuf.readLine()) != null) {
                 errorStr.append(errorLine).append("\n");
             }
-            log.info("error result: {}", errorStr.toString());
+            if (StringUtils.isNotEmpty(errorStr)){
+                log.info("error result: {}", errorStr.toString());
+            }
 
             // success ,没有获取到信息
             String line;
@@ -321,5 +331,99 @@ public class CmdServerImpl implements CmdServer {
 
     }
 
+    /**
+     * 栅格数据切片
+     *
+     * 需要处理进度条
+     * @param
+     * @param entity
+     * @return
+     */
+    @Override
+    public Integer exeCmdRasterSlice(String commandStr, FileEntity entity) {
+        log.warn("run exeCmdRasterSlice");
+        Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
+        try {
+            String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
+            Process ps = Runtime.getRuntime().exec(cmd);
+
+            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
+            BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
+
+            StringBuffer sb = new StringBuffer();
+            // 进度
+            int pre = 0;
+            int num;
+            String str;
+            while ((num = br.read()) != -1) {
+                str = String.valueOf((char) num);
+                
+                // 判断数字
+                if (numRegex(str)) {
+                    if (!str.equals("0")) {
+                        // 非0开头的
+                        sb.append(",").append(str);
+                    } else {
+                        // 以0开头的
+                        sb.append(str);
+                    }
+                    // 截取进度数字
+                    str = StringUtils.substringAfterLast(sb.toString(), ",");
+
+                    // 100,1000都会当一次
+                    if (str.length() == 2) {
+                        pre = pre + 5;
+                        System.out.println("pre: " + pre);
+                        log.warn("pre: {}", pre);
+                        // 能被2整除的存一下db
+                        if (pre % 2 == 0) {
+                            entity.setProgress(pre);
+                            entity.setUpdateTime(new Date());
+                            fileRepository.save(entity);
+                        }
+                    }
+                }
+            }
+
+            log.info("cmd console: {}", sb.toString());
+
+            StringBuffer errorStr = new StringBuffer();
+            String errorLine;
+            while ((errorLine = errorBuf.readLine()) != null) {
+                errorStr.append(errorLine).append("\n");
+            }
+            if (StringUtils.isNotEmpty(errorStr)){
+                log.info("error result: {}", errorStr.toString());
+            }
+
+            // 结束命令行
+            isCmd = ps.waitFor();
+
+            // 关闭流
+            br.close();
+            errorBuf.close();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        if (isCmd == 0) {
+            log.info("end exeCmd : {}", isCmd);
+        } else {
+            log.info("wsitFore cmd run error : {}", isCmd);
+        }
+        log.warn("end exeCmdRasterSlice");
+        return isCmd;
+    }
+
+    // 匹配数字
+    private boolean numRegex(String str) {
+        String reg = "\\d";
+        if (StringUtils.isEmpty(str)) {
+            return false;
+        }
+        return str.matches(reg);
+    }
+
 
 }

+ 4 - 2
src/main/java/com/fd/server/impl/FileServerImpl.java

@@ -43,6 +43,8 @@ public class FileServerImpl implements FileServer {
 
     @Override
     public R uploadBigFile(MultipartFile file, String type) {
+        log.warn("run uploadBigFile");
+        long start = System.currentTimeMillis();
         if (file.isEmpty() || file.getSize() <= 0) {
             log.info("文件为空");
             return new R(50001, MsgCode.E50001);
@@ -78,8 +80,8 @@ public class FileServerImpl implements FileServer {
         entity.setUpdateTime(new Date());
         entity.setType(type);
         fileRepository.save(entity);
-
-        log.info("end uploadBigFile");
+        long end = System.currentTimeMillis();
+        log.info("end uploadBigFile, total time: {} s", (end - start)/1000);
         return new R(200, entity);
     }