Przeglądaj źródła

完成了矢量数据模块
添加了解压zip方法

wuweihao 5 lat temu
rodzic
commit
83d2e54012

+ 26 - 2
src/main/java/com/fd/constant/Command.java

@@ -8,6 +8,10 @@ public class Command {
 
     // ./3dtile -f osgb -i /root/gis/cesium/input/xxx -o /root/gis/cesium/out/xxx  xxx:文件名
 //    public static String MODEL_OSGB = "./3dtile -f osgb -i /root/gis/cesium/input/@fileName -o /root/gis/cesium/output/@fileName";
+    /**
+     * 输入目录:/root/gis/cesium/input/@fileName fileName:是目录名
+     * 输出目录:/root/gis/cesium/output/@fileName fileName:是目录名
+     */
     public static String MODEL_OSGB = "/opt/3dtiles_v1/target/release/3dtile -f osgb -i /root/gis/cesium/input/@fileName -o /root/gis/cesium/output/@fileName";
 
     /**
@@ -18,18 +22,38 @@ public class Command {
      */
     public static String VECTOR_TRANSFORM_OGR2OGR  = "docker run --rm -v /root/gis/cesium/input:/root/gis/cesium/input osgeo/gdal  ogr2ogr -t_srs EPSG:4326  /root/gis/cesium/input/transform/@directory/@fileName.shp  /root/gis/cesium/input/@directory/@fileName.shp";
 
+    /**
+     * 矢量数据切片 step1
+     * shp转geojson
+     * 输入文件:@inputFile
+     * 输出文件:@outputFile
+     *
+     */
+    public static String VECTOR_TO_GEOJSON  = "docker run --rm -v /root/gis/cesium/input:/root/gis/cesium/input osgeo/gdal " +
+            "ogr2ogr -f GeoJSON @outputFile @inputFile";
+
+
+    /**
+     * 矢量数据切片 step2
+     * 输入文件:@inputFile
+     * 输出文件:@outputFile
+     *
+     * tippecanoe -zg -o output.mbtiles --force --drop-densest-as-needed --extend-zooms-if-still-dropping input.json
+     */
+    public static String VECTOR_CUT_TIPPECANOE = "tippecanoe -zg -o @outputFile --force --drop-densest-as-needed --extend-zooms-if-still-dropping @inputFile";
+
 
     /**
      * 栅格数据切片命令
      *
      * 使用了docket 运行插件,因为数据挂载文件,就没有把生成的文件放到output 目录了
      * 输入文件: /root/gis/cesium/input/@fileName.tif  @filePath
-     * 输出文件:/root/gis/cesium/input/@fileName_tiles
+     * 输出文件:/root/gis/cesium/input/@fileName
      *
      *
      */
     public static String RASTER_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_tiles";
+            "osgeo/gdal python3 /usr/bin/gdal2tiles.py --zoom=9-19 --processes=6 @filePath /root/gis/cesium/input/@fileName";
 
 
     /**

+ 4 - 0
src/main/java/com/fd/constant/MsgCode.java

@@ -17,6 +17,10 @@ public class MsgCode {
 
     public static final String E50005 = "命令执行失败";
 
+    public static final String E50006 = "文件解压失败";
+
+    public static final String E50007 = "只支持.zip 文件";
+
 
 
 

+ 71 - 43
src/main/java/com/fd/controller/FdModelController.java

@@ -1,19 +1,25 @@
 package com.fd.controller;
 
 import com.fd.constant.Command;
+import com.fd.constant.MsgCode;
 import com.fd.constant.TypeCode;
 import com.fd.dto.PageDto;
 import com.fd.entity.FileEntity;
 import com.fd.server.CmdServer;
 import com.fd.server.FileServer;
+import com.fd.util.FileUtils;
 import com.fd.util.R;
 import io.swagger.annotations.ApiOperation;
 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.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
+import java.util.Date;
+
 /**
  * Created by Owen on 2019/11/12 0012 9:40
  *
@@ -24,6 +30,9 @@ import org.springframework.web.multipart.MultipartFile;
 @RestController
 public class FdModelController {
 
+    @Value("${input.file.path}")
+    private String INPUT_FILE_PATH;
+
     @Autowired
     private FileServer fileServer;
 
@@ -31,62 +40,47 @@ public class FdModelController {
     private CmdServer cmdServer;
 
 
-    @ApiOperation("倾斜摄影数据切片")
-    @GetMapping("command/osgb/{fileId}/")
-    private R cmdOsgb(@PathVariable("fileId") Long fileId) {
-        log.info("run cmdOsgb: {}", fileId);
-        FileEntity entity = fileServer.findById(fileId);
 
-//        String fileName = entity.getFileName();
-//        fileName = StringUtils.substring(fileName, fileName.lastIndexOf("/")+1, fileName.length());
-        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
-        String cmd = Command.MODEL_OSGB;
-        cmd = cmd.replace("@fileName",fileName);
-        log.info("cmd: {}", cmd);
+    @ApiOperation("上传3D模型数据")
+    @PostMapping(value = "upload", consumes = { "multipart/form-data" })
+    private R upload(@RequestParam("file") MultipartFile file){
+        log.info("run upload");
+
+        // 文件名全名
+        String fileName = file.getOriginalFilename();
+        String s = StringUtils.substringAfter(fileName, ".");
+        if (!"zip".equals(s)) {
+            return new R(50007,MsgCode.E50007);
+        }
 
-        return cmdServer.exeCmdOsgb(cmd, fileName);
+        return fileServer.uploadBigFile(file, TypeCode.FILE_TYPE_MODEL_OSGB);
     }
 
+
     @ApiOperation("解压zip文件")
-    @GetMapping("command/unzip/{fileId}/")
-    private R cmdUnzip(@PathVariable("fileId") Long fileId) {
-        log.info("run cmdUnzip: {}", fileId);
+    @GetMapping("unzip/{fileId}/")
+    private R fileUnzip(@PathVariable("fileId") Long fileId) {
+        log.info("run fileUnzip: {}", fileId);
         FileEntity entity = fileServer.findById(fileId);
 
-        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
-        String path = StringUtils.substringBeforeLast(entity.getFileUrl(),"/");
+        boolean unzip = FileUtils.unzip(entity.getFileUrl(), INPUT_FILE_PATH);
 
-        String cmd = Command.CMD_ZIP;
-        cmd = cmd.replace("@fileName",fileName);
-        cmd = cmd.replace("@path", path);
-        log.info("cmd: {}", cmd);
-
-        return cmdServer.exeCmdUnzip(cmd);
-    }
-
-    @ApiOperation("移动切片数据到服务器")
-    @GetMapping("command/move/{fileId}/")
-    private R cmdMv(@PathVariable("fileId") Long fileId) {
-        log.info("run cmdUnzip: {}", fileId);
-        FileEntity entity = fileServer.findById(fileId);
+        if (!unzip) {
+            return new R(50006, MsgCode.E50006);
+        }
 
         String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
-        String path = StringUtils.substringBeforeLast(entity.getFileUrl(),"/");
 
-        String cmd = Command.CMD_MV;
-        cmd = cmd.replace("@fileName",fileName);
-        cmd = cmd.replace("@path", path);
-        log.info("cmd: {}", cmd);
+        FileEntity fileEntity = new FileEntity();
 
-        return cmdServer.exeCmd(cmd);
-    }
+        fileEntity.setFileName(fileName);
+        fileEntity.setFileUrl(INPUT_FILE_PATH + File.separator + fileName);
+        fileEntity.setCreateTime(new Date());
+        fileEntity.setUpdateTime(new Date());
+        fileEntity.setType(TypeCode.FILE_TYPE_MODEL_OSGB);
+        fileEntity = fileServer.save(fileEntity);
 
-
-    @ApiOperation("上传3D模型数据")
-    @PostMapping(value = "upload", consumes = { "multipart/form-data" })
-    private R upload(@RequestParam("file") MultipartFile file){
-        log.info("run upload");
-        return fileServer.uploadBigFile(file, TypeCode.FILE_TYPE_MODEL_OSGB);
+        return new R(200, fileEntity);
     }
 
     @ApiOperation("获取3D模型数据列表")
@@ -107,5 +101,39 @@ public class FdModelController {
     }
 
 
+    @ApiOperation("倾斜摄影数据切片")
+    @GetMapping("command/osgb/{fileId}/")
+    private R cmdOsgb(@PathVariable("fileId") Long fileId) {
+        log.info("run cmdOsgb: {}", fileId);
+        FileEntity entity = fileServer.findById(fileId);
+        // 传入的是目录
+        String cmd = Command.MODEL_OSGB;
+        cmd = cmd.replace("@fileName",entity.getFileName());
+        log.info("cmd: {}", cmd);
+
+        return cmdServer.exeCmdOsgb(cmd, entity.getFileName());
+    }
+
+
+
+
+    @ApiOperation("移动切片数据到服务器")
+    @GetMapping("command/move/{fileId}/")
+    private R cmdMove(@PathVariable("fileId") Long fileId) {
+        log.info("run cmdMove: {}", fileId);
+        FileEntity entity = fileServer.findById(fileId);
+
+        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
+        String path = StringUtils.substringBeforeLast(entity.getFileUrl(),"/");
+
+        String cmd = Command.CMD_MV;
+        cmd = cmd.replace("@fileName",fileName);
+        cmd = cmd.replace("@path", path);
+        log.info("cmd: {}", cmd);
+
+        return cmdServer.exeCmd(cmd);
+    }
+
+
 
 }

+ 35 - 17
src/main/java/com/fd/controller/RasterController.java

@@ -64,21 +64,7 @@ public class RasterController {
         return fileServer.deleteById(fileId);
     }
 
-    @ApiOperation("栅格数据切片命令")
-    @GetMapping("command/osgeo/{fileId}/")
-    private R cmdOsgeo(@PathVariable("fileId") Long fileId) {
-        log.info("run cmdOsgeo: {}", fileId);
-        FileEntity entity = fileServer.findById(fileId);
 
-        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
-
-        String cmd = Command.RASTER_OSGEO;
-        cmd = cmd.replace("@filePath", entity.getFileUrl());
-        cmd = cmd.replace("@fileName",fileName);
-        log.info("cmd: {}", cmd);
-
-        return cmdServer.exeCmd(cmd);
-    }
 
 
     @ApiOperation("栅格数据普通坐标转换")
@@ -146,16 +132,48 @@ public class RasterController {
         return new R(200, fileEntity) ;
     }
 
+    @ApiOperation("栅格数据切片命令")
+    @GetMapping("command/osgeo/{fileId}/")
+    private R cmdOsgeo(@PathVariable("fileId") Long fileId) {
+        log.info("run cmdOsgeo: {}", fileId);
+        FileEntity entity = fileServer.findById(fileId);
+
+        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
+        // fileName_tiles
+        fileName = fileName + "_tiles";
+        String cmd = Command.RASTER_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);
+        }
+
+
+        FileEntity fileEntity = new FileEntity();
+        fileEntity.setFileName(fileName);
+        fileEntity.setFileUrl(INPUT_FILE_PATH + fileName);
+        fileEntity.setCreateTime(new Date());
+        fileEntity.setUpdateTime(new Date());
+        fileEntity.setType(TypeCode.FILE_TYPE_RASTER_TIF);
+        fileEntity = fileServer.save(fileEntity);
+
+        return new R(200, fileEntity) ;
+    }
+
 
 
     public static void main(String[] args) {
-        String a = "112222.aa";
+        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(StringUtils.substringAfter(a,"."));
-        System.out.println(StringUtils.substringBefore(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,"/"));

+ 34 - 0
src/main/java/com/fd/controller/VectorController.java

@@ -101,4 +101,38 @@ public class VectorController {
 
         return new R(200, shpFile);
     }
+
+    @ApiOperation("矢量数据转geojson")
+    @GetMapping("command/geojson/{fileId}/")
+    private R cmdGeojson(@PathVariable("fileId") Long fileId) {
+        log.info("run cmdGeojson: {}", fileId);
+        FileEntity entity = fileServer.findById(fileId);
+
+        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
+        // 截取目录名称
+        String fileUrl = entity.getFileUrl();
+        String outputFile = StringUtils.substringBefore(fileUrl, ".");
+        outputFile = outputFile + ".json";
+
+        String cmd = Command.VECTOR_TO_GEOJSON;
+        cmd = cmd.replace("@inputFile", fileUrl);
+        cmd = cmd.replace("@outputFile", outputFile);
+        log.info("cmd: {}", cmd);
+
+
+        Integer integer = cmdServer.exeCmdInt(cmd);
+        if (integer != 0) {
+            return new R(50005, MsgCode.E50005);
+        }
+
+        FileEntity fileEntity = new FileEntity();
+        fileEntity.setFileName(fileName + ".json");
+        fileEntity.setFileUrl(outputFile);
+        fileEntity.setCreateTime(new Date());
+        fileEntity.setUpdateTime(new Date());
+        fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
+        fileEntity = fileServer.save(fileEntity);
+
+        return new R(200, fileEntity);
+    }
 }

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

@@ -140,15 +140,15 @@ public class CmdServerImpl implements CmdServer {
             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();
-            StringBuffer errorStr = new StringBuffer();
+//            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
+//            BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
+//
+//            StringBuffer sb = new StringBuffer();
+//            StringBuffer errorStr = new StringBuffer();
 
             // error : 坑, 控制台信息是从errorBuf这里出来的
-            String errorLine;
-            log.info("run 111111");
+//            String errorLine;
+//            log.info("run 111111");
 //            while ((errorLine = errorBuf.readLine()) != null) {
 //                log.info("run 2222222");
 //                errorStr.append(errorLine).append("\n");
@@ -165,14 +165,14 @@ public class CmdServerImpl implements CmdServer {
 //                sb.append(line).append("\n");
 //            }
 
-            log.info("result: {}", sb.toString());
+//            log.info("result: {}", sb.toString());
 
             // 结束命令行
             isCmd = ps.waitFor();
             log.info("run 5555555");
             // 关闭流
-            br.close();
-            errorBuf.close();
+//            br.close();
+//            errorBuf.close();
 
         } catch (Exception e) {
             e.printStackTrace();

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

@@ -165,10 +165,10 @@ public class FileServerImpl implements FileServer {
     public Integer getGeoData(HttpServletResponse response, String filePath) {
         // 判断文件是否存在
         filePath = OUTPUT_FILE_PATH + filePath;
-        log.info("filePath: {}", filePath);
+//        log.info("filePath: {}", filePath);
         File file = new File(filePath);
         if (!file.exists()) {
-            log.info("文件不存在: {}", filePath);
+//            log.info("文件不存在: {}", filePath);
             return null;
 
         }

+ 108 - 0
src/main/java/com/fd/util/FileUtils.java

@@ -10,6 +10,10 @@ import java.net.URLEncoder;
 import java.text.SimpleDateFormat;
 import java.util.Base64;
 import java.util.Date;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
 
 /**
  * Created by Owen on 2019/10/25 0025 14:20
@@ -252,6 +256,110 @@ public class FileUtils {
     }
 
 
+    /**
+     * 解压缩方法
+     *
+     *
+     * @param zipFileName 压缩文件名
+     * @param dstPath 解压目标路径
+     *
+     * @return
+     */
+    public static boolean unzip(String zipFileName, String dstPath) {
+        System.out.println("zip uncompressing...");
+
+        try {
+            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFileName));
+            ZipEntry       zipEntry       = null;
+            byte[]         buffer         = new byte[1024];//缓冲器
+            int            readLength     = 0;//每次读出来的长度
+
+            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
+                if (zipEntry.isDirectory()) {//若是zip条目目录,则需创建这个目录
+                    File dir = new File(dstPath + "/" + zipEntry.getName());
+
+                    if (!dir.exists()) {
+                        dir.mkdirs();
+                        System.out.println("mkdirs: " + dir.getCanonicalPath());
+
+                        continue;//跳出
+                    }
+                }
+
+                File file = createFile(dstPath, zipEntry.getName());//若是文件,则需创建该文件
+
+                System.out.println("file created: " + file.getCanonicalPath());
+
+                OutputStream outputStream = new FileOutputStream(file);
+
+                while ((readLength = zipInputStream.read(buffer, 0, 1024)) != -1) {
+                    outputStream.write(buffer, 0, readLength);
+                }
+
+                outputStream.close();
+                System.out.println("file uncompressed: " + file.getCanonicalPath());
+            }    // end while
+        } catch (FileNotFoundException e) {
+            System.out.println(e.getMessage());
+            e.printStackTrace();
+            System.out.println("unzip fail!");
+
+            return false;
+        } catch (IOException e) {
+            System.out.println(e.getMessage());
+            e.printStackTrace();
+            System.out.println("unzip fail!");
+
+            return false;
+        }
+
+        System.out.println("unzip success!");
+
+        return true;
+    }
+
+    /**
+     * 创建文件
+     * 根据压缩包内文件名和解压缩目的路径,创建解压缩目标文件,
+     * 生成中间目录
+     * @param dstPath 解压缩目的路径
+     * @param fileName 压缩包内文件名
+     *
+     * @return 解压缩目标文件
+     *
+     * @throws IOException
+     */
+    private static File createFile(String dstPath, String fileName) throws IOException {
+        String[] dirs = fileName.split("/");//将文件名的各级目录分解
+        File     file = new File(dstPath);
+
+        if (dirs.length > 1) {//文件有上级目录
+            for (int i = 0; i < dirs.length - 1; i++) {
+                file = new File(file, dirs[i]);//依次创建文件对象知道文件的上一级目录
+            }
+
+            if (!file.exists()) {
+                file.mkdirs();//文件对应目录若不存在,则创建
+                System.out.println("mkdirs: " + file.getCanonicalPath());
+            }
+
+            file = new File(file, dirs[dirs.length - 1]);//创建文件
+
+            return file;
+        } else {
+            if (!file.exists()) {
+                file.mkdirs();//若目标路径的目录不存在,则创建
+                System.out.println("mkdirs: " + file.getCanonicalPath());
+            }
+
+            file = new File(file, dirs[0]);//创建文件
+
+            return file;
+        }
+    }
+
+
+
     public static void main(String[] args) {
         String a = "aaaa"+ File.separator + "1111.bb";
         String b = "aaaa\\1111.bb";