Browse Source

完成栅格数据坐标判断

wuweihao 5 years ago
parent
commit
4e8397de9c

+ 9 - 0
src/main/java/com/fd/constant/Command.java

@@ -63,6 +63,15 @@ public class Command {
             "osgeo/gdal python3 /usr/bin/gdal2tiles.py --zoom=9-19 --processes=6 @filePath /root/gis/cesium/input/@fileName";
 
 
+
+    /**
+     * 栅格数据判断坐标
+     * 输入文件:@inputFile
+     *
+     * gdalsrsinfo -o proj4 /root/gis/cesium/input/owen_2.tif
+     */
+    public static String RASTER_JUDGE_COORD = "gdalsrsinfo -o proj4 @inputFile";
+
     /**
      * 普通坐标转换
      * 输入文件: /root/gis/cesium/input/@fileName.tif

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

@@ -21,6 +21,8 @@ public class MsgCode {
 
     public static final String E50007 = "只支持.zip 文件";
 
+    public static final String E50008 = "只支持.tip 文件";
+
 
 
 

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

@@ -41,7 +41,7 @@ public class FdModelController {
 
 
 
-    @ApiOperation("上传3D模型数据")
+    @ApiOperation("上传3D模型数据,只能上传zip文件")
     @PostMapping(value = "upload", consumes = { "multipart/form-data" })
     private R upload(@RequestParam("file") MultipartFile file){
         log.info("run upload");

+ 116 - 49
src/main/java/com/fd/controller/RasterController.java

@@ -39,10 +39,18 @@ public class RasterController {
     private CmdServer cmdServer;
 
 
-    @ApiOperation("上传栅格数据")
+    @ApiOperation("上传栅格数据,只能上传tif文件")
     @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 (!"tif".equals(s)) {
+            return new R(50008,MsgCode.E50008);
+        }
+
         return fileServer.uploadBigFile(file, TypeCode.FILE_TYPE_RASTER_TIF);
     }
 
@@ -64,18 +72,111 @@ public class RasterController {
         return fileServer.deleteById(fileId);
     }
 
+    @ApiOperation("栅格数据判断坐标")
+    @GetMapping("command/judge/coord/{fileId}/")
+    private R cmdJudgeCoord(@PathVariable("fileId") Long fileId) {
+        log.info("run cmdJudgeCoord: {}", fileId);
+        FileEntity entity = fileServer.findById(fileId);
+
+        String cmd = Command.RASTER_JUDGE_COORD;
+        cmd = cmd.replace("@inputFile", entity.getFileUrl());
+        log.info("cmd: {}", cmd);
+
+        Integer isJudge = cmdServer.exeCmdRasterJudgeCoord(cmd);
+
+        if (1000 == isJudge){
+            log.info("need to transform");
+            // 严格坐标转换
+            entity = cmdTansformGdalwarpStrict(fileId);
+        } else if (0 == isJudge){
+            log.info("not to transform");
+        } else {
+            log.info("error exeCmd");
+            return new R(50005, MsgCode.E50005);
+        }
+
+
+        return new R(200, entity);
+    }
+
 
 
+//    @ApiOperation("栅格数据普通坐标转换")
+//    @GetMapping("command/transform/gdalwarp/{fileId}/")
+//    private R cmdTansformGdalwarp(@PathVariable("fileId") Long fileId) {
+//        log.info("run cmdTansformGdalwarp: {}", fileId);
+//        FileEntity entity = fileServer.findById(fileId);
+//
+//        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
+//
+//        String cmd = Command.RASTER_TRANSFORM_GDALWARP;
+//        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(entity.getFileName());
+//        // /root/gis/cesium/input/transform/@fileName.tif
+//        fileEntity.setFileUrl(INPUT_FILE_PATH + "transform" + File.separator + entity.getFileName());
+//        fileEntity.setCreateTime(new Date());
+//        fileEntity.setUpdateTime(new Date());
+//        fileEntity.setType(TypeCode.FILE_TYPE_RASTER_TIF);
+//        fileEntity = fileServer.save(fileEntity);
+//
+//        return new R(200, fileEntity) ;
+//    }
+
+
+//    @ApiOperation("栅格数据严格坐标转换")
+//    @GetMapping("command/transform/strict/gdalwarp/{fileId}/")
+//    private R cmdTansformGdalwarpStrict(@PathVariable("fileId") Long fileId) {
+//        log.info("run cmdTansformGdalwarpStrict: {}", fileId);
+//        FileEntity entity = fileServer.findById(fileId);
+//
+//        String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
+//
+//        String step_1 = Command.RASTER_TRANSFORM_GDALWARP_STRICT_1;
+//        String step_2 = Command.RASTER_TRANSFORM_GDALWARP_STRICT_2;
+//        step_1 = step_1.replace("@fileName",fileName);
+//        step_2 = step_2.replace("@fileName",fileName);
+//        log.info("cmd1: {}", step_1);
+//        log.info("cmd2: {}", step_2);
+//
+//        Integer integer = cmdServer.exeCmd(step_1, step_2);
+//
+//        if (integer != 0) {
+//            log.info("error command transform");
+//            return new R(50005, MsgCode.E50005);
+//        }
+//
+//        FileEntity fileEntity = new FileEntity();
+//        fileEntity.setFileName(entity.getFileName());
+//        // /root/gis/cesium/input/transform_strict/@fileName.tif
+//        fileEntity.setFileUrl(INPUT_FILE_PATH + "transform_strict" + File.separator + entity.getFileName());
+//        fileEntity.setCreateTime(new Date());
+//        fileEntity.setUpdateTime(new Date());
+//        fileEntity.setType(TypeCode.FILE_TYPE_RASTER_TIF);
+//        fileEntity = fileServer.save(fileEntity);
+//
+//        return new R(200, fileEntity) ;
+//    }
 
-    @ApiOperation("栅格数据普通坐标转换")
-    @GetMapping("command/transform/gdalwarp/{fileId}/")
-    private R cmdTansformGdalwarp(@PathVariable("fileId") Long fileId) {
-        log.info("run cmdTansformGdalwarp: {}", 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_TRANSFORM_GDALWARP;
+        // 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);
 
@@ -85,10 +186,10 @@ public class RasterController {
             return new R(50005, MsgCode.E50005);
         }
 
+
         FileEntity fileEntity = new FileEntity();
-        fileEntity.setFileName(entity.getFileName());
-        // /root/gis/cesium/input/transform/@fileName.tif
-        fileEntity.setFileUrl(INPUT_FILE_PATH + "transform" + File.separator + entity.getFileName());
+        fileEntity.setFileName(fileName);
+        fileEntity.setFileUrl(INPUT_FILE_PATH + fileName);
         fileEntity.setCreateTime(new Date());
         fileEntity.setUpdateTime(new Date());
         fileEntity.setType(TypeCode.FILE_TYPE_RASTER_TIF);
@@ -97,10 +198,8 @@ public class RasterController {
         return new R(200, fileEntity) ;
     }
 
-
-    @ApiOperation("栅格数据严格坐标转换")
-    @GetMapping("command/transform/strict/gdalwarp/{fileId}/")
-    private R cmdTansformGdalwarpStrict(@PathVariable("fileId") Long fileId) {
+    // 严格坐标转换
+    private FileEntity cmdTansformGdalwarpStrict(Long fileId){
         log.info("run cmdTansformGdalwarpStrict: {}", fileId);
         FileEntity entity = fileServer.findById(fileId);
 
@@ -117,7 +216,8 @@ public class RasterController {
 
         if (integer != 0) {
             log.info("error command transform");
-            return new R(50005, MsgCode.E50005);
+            return null;
+//            return new R(50005, MsgCode.E50005);
         }
 
         FileEntity fileEntity = new FileEntity();
@@ -129,42 +229,9 @@ public class RasterController {
         fileEntity.setType(TypeCode.FILE_TYPE_RASTER_TIF);
         fileEntity = fileServer.save(fileEntity);
 
-        return new R(200, fileEntity) ;
+        return 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.zip";

+ 6 - 3
src/main/java/com/fd/controller/VectorController.java

@@ -103,11 +103,11 @@ public class VectorController {
         cmd = cmd.replace("@inputFile", entity.getFileUrl());
         log.info("cmd: {}", cmd);
 
-        boolean isJudge = cmdServer.exeCmdJudgeCoord(cmd);
+        Integer isJudge = cmdServer.exeCmdJudgeCoord(cmd);
 
         FileEntity fileEntity = null;
         // 转换坐标 普通坐标转换
-        if (isJudge){
+        if (isJudge == 1000){
             log.info("need to transform");
 
             String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
@@ -135,9 +135,12 @@ public class VectorController {
             fileEntity.setUpdateTime(new Date());
             fileEntity.setType(TypeCode.FILE_TYPE_VECTOR_SHP);
             fileEntity = fileServer.save(fileEntity);
-        } else { // 不转换坐标
+        } else if (0 == isJudge){ // 不转换坐标
             log.info("not to transform");
             fileEntity = entity;
+        } else {
+            log.info("error exeCmd");
+            return new R(50005, MsgCode.E50005);
         }
 
 

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

@@ -20,5 +20,7 @@ public interface CmdServer {
 
     Integer exeCmdInt(String cmd);
 
-    boolean exeCmdJudgeCoord(String cmd);
+    Integer exeCmdJudgeCoord(String cmd);
+
+    Integer exeCmdRasterJudgeCoord(String cmd);
 }

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

@@ -192,12 +192,12 @@ public class CmdServerImpl implements CmdServer {
     }
 
     /**
-     * 判断坐标
+     * 矢量数据判断坐标
      * @param
      * @return
      */
     @Override
-    public boolean exeCmdJudgeCoord(String commandStr) {
+    public Integer exeCmdJudgeCoord(String commandStr) {
         Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
 
         StringBuffer sb = new StringBuffer();
@@ -236,21 +236,89 @@ public class CmdServerImpl implements CmdServer {
             e.printStackTrace();
         }
 
-        boolean isJudge = false;
+
         if (isCmd == 0) {
             log.info("end exeCmd : {}", isCmd);
             // 判断坐标
             if (sb.toString().contains("GEOGCS[\"China Geodetic Coordinate System 2000\"")) {
                 // 需要坐标转换
-                isJudge = true;  // 2:
+                isCmd = 1000;  // 2:
             }
             if (sb.toString().contains("GEOGCS[\"WGS 84\"")) {
                 // 不需要坐标转换
-                isJudge = false;
+                isCmd = 0;
+            }
+        } else {
+            log.info("error exeCmd wsitFore: {}", isCmd);
+        }
+
+        return isCmd;
+    }
+
+    /**
+     * 栅格数据坐标判断
+     *
+     * return :
+     *  1000:需要转换
+     *  1001:不需要转换
+     */
+    @Override
+    public Integer exeCmdRasterJudgeCoord(String commandStr) {
+        Integer isCmd = null; // 命令运行结果 1:失败, 0:成功
+
+        StringBuffer sb = new StringBuffer();
+        StringBuffer errorStr = new StringBuffer();
+        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()));
+
+            // error : 坑, 控制台信息是从errorBuf这里出来的
+            String errorLine;
+            while ((errorLine = errorBuf.readLine()) != null) {
+                errorStr.append(errorLine).append("\n");
+            }
+            log.info("error result: {}", errorStr.toString());
+
+            // success ,没有获取到信息
+            String line;
+            while ((line = br.readLine()) != null) {
+//                log.info("=====  br.readLine: ======== {}", br.readLine());
+                //执行结果加上回车
+                sb.append(line).append("\n");
+            }
+            log.info("result: {}", sb.toString());
+
+            // 结束命令行
+            isCmd = ps.waitFor();
+
+            // 关闭流
+            br.close();
+            errorBuf.close();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+
+        if (isCmd == 0) {
+            log.info("end exeCmd : {}", isCmd);
+            // 判断坐标
+            if (sb.toString().contains("+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=38500000")) {
+                // 需要坐标转换
+                isCmd = 1000;  // 1000: 需要转换
+            }
+            if (sb.toString().contains("+proj=longlat +datum=WGS84")) {
+                // 不需要坐标转换
+                isCmd = 0;
             }
+        } else {
+            log.info("error cmd wsitFore: {}", isCmd);
         }
+        return isCmd;
 
-        return isJudge;
     }