RasterController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.fd.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.fd.constant.Command;
  5. import com.fd.constant.MsgCode;
  6. import com.fd.constant.TypeCode;
  7. import com.fd.dto.ConfigJsonDto;
  8. import com.fd.dto.MyQueue;
  9. import com.fd.dto.PageDto;
  10. import com.fd.entity.FileEntity;
  11. import com.fd.entity.OutputFileEntity;
  12. import com.fd.server.CmdServer;
  13. import com.fd.server.RasterServer;
  14. import com.fd.server.impl.CmdServerImpl;
  15. import com.fd.server.impl.RasterServerImpl;
  16. import com.fd.thread.AsyncTask;
  17. import com.fd.util.FileUtils;
  18. import com.fd.util.R;
  19. import com.fd.util.RegexUtils;
  20. import com.fd.util.SpringContext;
  21. import io.swagger.annotations.ApiOperation;
  22. import lombok.extern.log4j.Log4j2;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.scheduling.annotation.Async;
  27. import org.springframework.web.bind.annotation.*;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import java.io.File;
  30. import java.util.Arrays;
  31. import java.util.Date;
  32. import java.util.List;
  33. import java.util.concurrent.BlockingQueue;
  34. import java.util.concurrent.LinkedBlockingQueue;
  35. import java.util.concurrent.TimeUnit;
  36. /**
  37. * Created by Owen on 2019/11/12 0012 9:40
  38. *
  39. * 栅格数据
  40. */
  41. @Log4j2
  42. @RequestMapping("api/raster")
  43. @RestController
  44. public class RasterController {
  45. @Value("${input.file.path.raster}")
  46. private String INPUT_FILE_PATH;
  47. @Value("${output.file.path.raster}")
  48. private String OUTPUT_FILE_PATH;
  49. @Autowired
  50. private AsyncTask asyncTask;
  51. @Autowired
  52. private RasterServer rasterServer;
  53. private static BlockingQueue<MyQueue> sliceQueue = new LinkedBlockingQueue<MyQueue>(2);
  54. private static BlockingQueue<MyQueue> coordQueue = new LinkedBlockingQueue<MyQueue>(2);
  55. @ApiOperation("上传数据,校验文件名")
  56. @GetMapping("check/{fileName}/")
  57. private R checkFileName(@PathVariable("fileName") String fileName) {
  58. log.info("run checkFileName: {}",fileName);
  59. // 文件是否包含中文字符
  60. if (RegexUtils.regexChinese(fileName)) {
  61. log.info(MsgCode.E51005);
  62. return new R(51005, MsgCode.E51005);
  63. }
  64. String s = StringUtils.substringAfterLast(fileName, ".");
  65. if (!"tif".equals(s)) {
  66. log.info(MsgCode.E50008);
  67. return new R(50008,MsgCode.E50008);
  68. }
  69. List<FileEntity> list = rasterServer.findByFileName(fileName);
  70. if (list.size() > 0) {
  71. log.info( MsgCode.E51006);
  72. return new R(51006, MsgCode.E51006);
  73. }
  74. return new R(200, MsgCode.SUCCESS);
  75. }
  76. @ApiOperation("上传栅格数据,只能上传tif文件")
  77. @PostMapping(value = "upload", consumes = { "multipart/form-data" })
  78. private R upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "coord",required = false) String[] coord){
  79. log.info("run upload");
  80. log.info("coord: {}", Arrays.toString(coord));
  81. String strCoord = Arrays.toString(coord);
  82. // 文件名全名
  83. String fileName = file.getOriginalFilename();
  84. // 文件是否包含中文字符
  85. if (RegexUtils.regexChinese(fileName)) {
  86. return new R(51005, MsgCode.E51005);
  87. }
  88. String s = StringUtils.substringAfterLast(fileName, ".");
  89. if (!"tif".equals(s)) {
  90. return new R(50008,MsgCode.E50008);
  91. }
  92. List<FileEntity> list = rasterServer.findByFileName(fileName);
  93. if (list.size() > 0) {
  94. return new R(51006, MsgCode.E51006);
  95. }
  96. R r = rasterServer.uploadBigFile(file, strCoord);
  97. OutputFileEntity entity = (OutputFileEntity) r.getData();
  98. // 判断坐标
  99. entity = JudgeCoord(entity, rasterServer);
  100. return new R(200, entity);
  101. }
  102. /**
  103. * 上传后判断坐标,显示原始坐标
  104. * @return
  105. */
  106. private OutputFileEntity JudgeCoord(OutputFileEntity entity, RasterServer rasterServer){
  107. String cmd = Command.RASTER_JUDGE_COORD;
  108. cmd = cmd.replace("@inputFile", entity.getUploadPath());
  109. log.info("cmd: {}", cmd);
  110. Integer isJudge = rasterServer.cmdJudgeCoord(cmd);
  111. if (1000 == isJudge){
  112. log.info("need to transform");
  113. // 严格坐标转换
  114. entity.setCoordType(TypeCode.COORD_XIAN_1980);
  115. } else if (0 == isJudge){
  116. log.info("not to transform");
  117. entity.setCoordType(TypeCode.COORD_WGS84);
  118. } else {
  119. log.info("error exeCmd");
  120. entity.setStatus(7);
  121. }
  122. entity.setUpdateTime(new Date());
  123. rasterServer.save(entity);
  124. return entity;
  125. }
  126. @ApiOperation("获取栅格数据列表")
  127. @PostMapping(value = "list")
  128. private R list(@RequestBody PageDto param){
  129. return rasterServer.findByType(TypeCode.FILE_TYPE_RASTER, param);
  130. }
  131. @ApiOperation("删除文件")
  132. @GetMapping("delete/{fileId}/")
  133. private R deleteFile(@PathVariable("fileId") Long fileId) {
  134. log.info("run deleteFile: {}", fileId);
  135. OutputFileEntity entity = rasterServer.findById(fileId);
  136. entity.setResStatus(1);
  137. rasterServer.save(entity);
  138. FileEntity fileEntity = rasterServer.findByInputFileId(entity.getUploadId());
  139. fileEntity.setResStatus(1);
  140. rasterServer.saveInputFile(fileEntity);
  141. asyncTask.rasterDelete(fileId, rasterServer);
  142. log.info("delete raster id: {}", fileId);
  143. return new R(200, MsgCode.SUCCESS);
  144. }
  145. @ApiOperation("栅格数据判断坐标")
  146. @GetMapping("command/judge/coord/{fileId}/")
  147. private R cmdJudgeCoord(@PathVariable("fileId") Long fileId) {
  148. log.info("run cmdJudgeCoord: {}", fileId);
  149. OutputFileEntity entity = rasterServer.findById(fileId);
  150. String cmd = Command.RASTER_JUDGE_COORD;
  151. cmd = cmd.replace("@inputFile", entity.getUploadPath());
  152. log.info("cmd: {}", cmd);
  153. // 把数据放入队列中
  154. MyQueue data = new MyQueue();
  155. data.setOutputFile(entity);
  156. data.setStr(cmd);
  157. boolean offer = false;
  158. try {
  159. offer = coordQueue.offer(data, 1, TimeUnit.SECONDS);
  160. log.info("入队成功");
  161. } catch (Exception e) {
  162. log.error("error producer queue raster cmdJudgeCoord: {}", e);
  163. e.printStackTrace();
  164. }
  165. if (offer) {
  166. entity.setStatus(9);
  167. entity.setUpdateTime(new Date());
  168. entity = rasterServer.save(entity);
  169. log.info("coord entity: {}",entity);
  170. log.info("coord producer update time: {}", entity.getUpdateTime());
  171. asyncTask.rasterJudgeCoordConsumerThread(coordQueue, rasterServer, OUTPUT_FILE_PATH);
  172. return new R(200, entity);
  173. }
  174. return new R(52000, MsgCode.E52000);
  175. }
  176. @ApiOperation("栅格数据切片命令")
  177. @GetMapping("command/osgeo/{fileId}/{layerMin}/{layerMax}/")
  178. private R cmdSlice(@PathVariable("fileId") Long fileId, @PathVariable("layerMin") String layerMin, @PathVariable("layerMax") String layerMax) {
  179. log.info("run cmdSlice: {}", fileId);
  180. if (!RegexUtils.regexInt(layerMin)){
  181. return new R(50010, MsgCode.E50010) ;
  182. }
  183. if (!RegexUtils.regexInt(layerMax)){
  184. return new R(50010, MsgCode.E50010) ;
  185. }
  186. OutputFileEntity entity = rasterServer.findById(fileId);
  187. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  188. String outFilePath = OUTPUT_FILE_PATH + "slice" + File.separator + fileName;
  189. FileUtils.createDir(outFilePath);
  190. String cmd = Command.RASTER_SLICE_OSGEO;
  191. cmd = cmd.replace("@inputFile", entity.getCoordStrictPath());
  192. cmd = cmd.replace("@outputFile", outFilePath);
  193. cmd = cmd.replace("@layerMin", layerMin);
  194. cmd = cmd.replace("@layerMax", layerMax);
  195. log.info("cmd: {}", cmd);
  196. // 把数据放入队列中
  197. MyQueue data = new MyQueue();
  198. data.setOutputFile(entity);
  199. data.setStr(cmd);
  200. boolean offer = false;
  201. try {
  202. offer = sliceQueue.offer(data, 1, TimeUnit.SECONDS);
  203. log.info("raster slice 入队成功");
  204. } catch (Exception e) {
  205. log.error("error producer queue raster cmdSlice: {}", e);
  206. e.printStackTrace();
  207. }
  208. if (offer) {
  209. // 设个默认进度给前端显示
  210. entity.setProgress(1);
  211. entity.setStatus(6);
  212. entity.setLayerMin(Integer.valueOf(layerMin));
  213. entity.setLayerMax(Integer.valueOf(layerMax));
  214. entity.setUpdateTime(new Date());
  215. entity.setSlicePath(outFilePath);
  216. entity = rasterServer.save(entity);
  217. asyncTask.rasterSliceThread(sliceQueue, rasterServer);
  218. return new R(200, entity) ;
  219. }
  220. return new R(52000, MsgCode.E52000);
  221. }
  222. @ApiOperation("栅格数据进度查询")
  223. @GetMapping("progress/{fileId}/")
  224. private R getProgress(@PathVariable("fileId") Long fileId) {
  225. OutputFileEntity entity = rasterServer.findById(fileId);
  226. return new R(200, entity);
  227. }
  228. @ApiOperation("移动数据到服务器上")
  229. @PostMapping("move/{fileId}/")
  230. private R moveFile(@PathVariable("fileId") Long fileId, @RequestBody ConfigJsonDto param) {
  231. log.info("run moveFile: {}", fileId);
  232. return rasterServer.moveFileToServer(fileId, param);
  233. }
  234. }