RasterController.java 9.3 KB

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