VectorController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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.MyQueue;
  6. import com.fd.dto.PageDto;
  7. import com.fd.entity.FileEntity;
  8. import com.fd.entity.OutputFileEntity;
  9. import com.fd.server.CmdServer;
  10. import com.fd.server.FileServer;
  11. import com.fd.server.VectorServer;
  12. import com.fd.util.FileUtils;
  13. import com.fd.util.R;
  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 javax.annotation.PostConstruct;
  22. import java.io.File;
  23. import java.util.Date;
  24. import java.util.concurrent.BlockingQueue;
  25. import java.util.concurrent.LinkedBlockingQueue;
  26. import java.util.concurrent.TimeUnit;
  27. /**
  28. * Created by Owen on 2019/11/12 0012 9:40
  29. *
  30. * 矢量数据
  31. */
  32. @Log4j2
  33. @RequestMapping("api/vector")
  34. @RestController
  35. public class VectorController {
  36. @Value("${input.file.path}")
  37. private String INPUT_FILE_PATH;
  38. @Value("${output.file.path}")
  39. private String OUTPUT_FILE_PATH;
  40. @Autowired
  41. private FileServer fileServer;
  42. @Autowired
  43. private VectorServer vectorServer;
  44. @Autowired
  45. private CmdServer cmdServer;
  46. BlockingQueue<MyQueue> coordQueue = new LinkedBlockingQueue<MyQueue>(5);
  47. /**
  48. * 初始化队列
  49. *
  50. * @return
  51. */
  52. @PostConstruct
  53. private void init() {
  54. // 判断坐标消费队列
  55. new Thread(new JudgeCoordConsumerThread(coordQueue)).start();
  56. }
  57. // @ApiOperation("上传矢量数据,coord:坐标, directoryName:目录名称")
  58. // @PostMapping(value = "upload/{directoryName}/", consumes = { "multipart/form-data" })
  59. // private R upload(@RequestParam("file") MultipartFile file, @PathVariable("directoryName") String directoryName,
  60. // @RequestParam(value = "coord",required = false) String coord){
  61. // log.info("run upload");
  62. // return fileServer.uploadFile(file, directoryName, TypeCode.FILE_TYPE_VECTOR);
  63. // }
  64. // @ApiOperation("上传矢量数据,只能上传zip文件,里面的文件名必须跟压缩包一致, coord:坐标(可以为空), 坐标格式:0,0,0,0,0,0,0")
  65. // @PostMapping(value = "upload", consumes = { "multipart/form-data" })
  66. // private R upload(@RequestParam("file") MultipartFile file,
  67. // @RequestParam(value = "coord",required = false) String coord){
  68. // log.info("run upload");
  69. //
  70. // // 文件名全名
  71. // String fileName = file.getOriginalFilename();
  72. // String s = StringUtils.substringAfterLast(fileName, ".");
  73. //
  74. // if (!"zip".equals(s)) {
  75. // return new R(50007,MsgCode.E50007);
  76. // }
  77. //
  78. // return vectorServer.uploadFile(file, coord);
  79. // }
  80. @ApiOperation("上传矢量数据, coord:坐标(可以为空), 坐标格式:0,0,0,0,0,0,0")
  81. @PostMapping(value = "upload/{directoryName}/", consumes = { "multipart/form-data" })
  82. private R upload(@RequestParam("file") MultipartFile file,
  83. @PathVariable("directoryName") String directoryName,
  84. @RequestParam(value = "coord",required = false) String coord){
  85. log.info("run uploadVector");
  86. return vectorServer.uploadDirectoryFile(file, directoryName, coord);
  87. }
  88. // @ApiOperation("解压zip文件")
  89. // @GetMapping("unzip/{fileId}/")
  90. // private R fileUnzip(@PathVariable("fileId") Long fileId) {
  91. // log.info("run fileUnzip: {}", fileId);
  92. // FileEntity entity = fileServer.findById(fileId);
  93. //
  94. // boolean unzip = FileUtils.unzip(entity.getFileUrl(), INPUT_FILE_PATH);
  95. //
  96. // if (!unzip) {
  97. // log.info("zip error: {}", MsgCode.E51001);
  98. // return new R(51001, MsgCode.E51001);
  99. // }
  100. //
  101. //
  102. //
  103. // String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
  104. //
  105. // // 判断文件内容是否跟目录名称一样
  106. // String shpPath = INPUT_FILE_PATH + fileName + File.separator + fileName + ".shp";
  107. // File file = new File(shpPath);
  108. // if (!file.exists()) {
  109. // log.info("zip error: {}", MsgCode.E51002);
  110. // return new R(51002, MsgCode.E51002);
  111. // }
  112. //
  113. // FileEntity fileEntity = new FileEntity();
  114. //
  115. // // 添加文件夹到数据库
  116. // fileEntity.setFileName(fileName);
  117. // fileEntity.setFileUrl(INPUT_FILE_PATH + fileName);
  118. // fileEntity.setCreateTime(new Date());
  119. // fileEntity.setUpdateTime(new Date());
  120. // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
  121. // fileEntity.setCoord(entity.getCoord());
  122. // fileEntity.setStatus(2);
  123. //
  124. // fileEntity = fileServer.save(fileEntity);
  125. //
  126. // // xxx.shp到数据库
  127. //// String sName = StringUtils.substringAfter(fileName, "_");
  128. //// sName = sName + ".shp";
  129. // fileEntity = new FileEntity();
  130. //// fileEntity.setFileName(sName);
  131. //// fileEntity.setFileUrl(INPUT_FILE_PATH + fileName + File.separator + sName);
  132. //
  133. // fileEntity.setFileName(fileName + ".shp");
  134. // fileEntity.setFileUrl(shpPath);
  135. //
  136. // fileEntity.setCreateTime(new Date());
  137. // fileEntity.setUpdateTime(new Date());
  138. // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
  139. // fileEntity.setCoord(entity.getCoord());
  140. // fileEntity = fileServer.save(fileEntity);
  141. //
  142. // return new R(200, fileEntity);
  143. // }
  144. @ApiOperation("获取矢量数据列表")
  145. @PostMapping(value = "list")
  146. private R list(@RequestBody PageDto param){
  147. log.info("run list");
  148. // return fileServer.findByType(TypeCode.FILE_TYPE_VECTOR, TypeCode.FILE_TYPE_VECTOR_SHP, param);
  149. return vectorServer.findByType(TypeCode.FILE_TYPE_VECTOR, param);
  150. }
  151. /**
  152. * 删除文件
  153. */
  154. @ApiOperation("删除文件")
  155. @GetMapping("delete/{fileId}/")
  156. private R deleteFile(@PathVariable("fileId") Long fileId) {
  157. log.info("run deleteFile: {}", fileId);
  158. return fileServer.deleteById(fileId);
  159. }
  160. @ApiOperation("矢量数据判断坐标")
  161. @GetMapping("command/judge/coord/{fileId}/")
  162. private R cmdJudgeCoord(@PathVariable("fileId") Long fileId) {
  163. log.info("run cmdJudgeCoord: {}", fileId);
  164. // FileEntity entity = fileServer.findById(fileId);
  165. OutputFileEntity entity = vectorServer.findById(fileId);
  166. String coord = entity.getCoord();
  167. log.info("coord: {}", coord);
  168. // 判断是否需要坐标转换
  169. String cmd = Command.VECTOR_JUDGE_COORD;
  170. cmd = cmd.replace("@inputFile", entity.getUploadPath());
  171. log.info("cmd: {}", cmd);
  172. // 把数据放入队列中
  173. MyQueue data = new MyQueue();
  174. data.setOutputFile(entity);
  175. data.setStr(cmd);
  176. try {
  177. coordQueue.offer(data, 1, TimeUnit.SECONDS);
  178. log.info("入队成功");
  179. } catch (InterruptedException e) {
  180. e.printStackTrace();
  181. }
  182. // Integer isJudge = cmdServer.exeCmdJudgeCoord(cmd);
  183. //
  184. //// FileEntity fileEntity = null;
  185. // // 转换坐标 普通坐标转换
  186. // if (isJudge == 1000){
  187. // // 普通坐标转换
  188. // log.info("need to general transform");
  189. // if (coord == null) {
  190. // // 没有坐标参数,执行普通坐标转换(ogrinfo)
  191. // entity = generalCoordTransform(entity, Command.VECTOR_TRANSFORM_GENERAL);
  192. //
  193. // } else {
  194. // // 有坐标参数,执行严格坐标转换(CGCS2000转wgs80)
  195. // entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS80);
  196. // }
  197. //
  198. // } else if (isJudge == 1001) {
  199. // // 严格坐标转换 (西安80转wgs84),需要参数
  200. // log.info("need to strict transform");
  201. // if (coord == null) {
  202. // log.info("error: {}", MsgCode.E50009);
  203. // return new R(50009, MsgCode.E50009);
  204. // }
  205. // entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS84);
  206. //
  207. // } else if (0 == isJudge){ // 不转换坐标
  208. // log.info("not to transform");
  209. // entity = entity;
  210. // } else {
  211. // log.info("error exeCmd");
  212. // return new R(50005, MsgCode.E50005);
  213. // }
  214. return new R(200, entity);
  215. }
  216. public class JudgeCoordConsumerThread implements Runnable{
  217. private BlockingQueue<MyQueue> queue;
  218. private boolean isRun = true;
  219. public JudgeCoordConsumerThread(BlockingQueue<MyQueue> queue){
  220. this.queue = queue;
  221. }
  222. @Override
  223. public void run() {
  224. while (isRun) {
  225. try {
  226. MyQueue data = queue.poll(2, TimeUnit.SECONDS);
  227. if (data != null) {
  228. log.info("消费者,拿到队列中的数据data:" + data.toString());
  229. Integer isJudge = cmdServer.exeCmdJudgeCoord(data.getStr());
  230. OutputFileEntity entity = data.getOutputFile();
  231. // 转换坐标 普通坐标转换
  232. if (isJudge == 1000){
  233. // 普通坐标转换
  234. log.info("need to general transform");
  235. if (entity.getCoord() == null) {
  236. // 没有坐标参数,执行普通坐标转换(ogrinfo)
  237. entity = generalCoordTransform(entity, Command.VECTOR_TRANSFORM_GENERAL);
  238. } else {
  239. // 有坐标参数,执行严格坐标转换(CGCS2000转wgs80)
  240. entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS80);
  241. }
  242. } else if (isJudge == 1001) {
  243. // 严格坐标转换 (西安80转wgs84),需要参数
  244. log.info("need to strict transform");
  245. if (entity.getCoord() == null) {
  246. log.info("error: {}", MsgCode.E50009);
  247. entity.setStatus(7);
  248. vectorServer.save(entity);
  249. }
  250. entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS84);
  251. } else if (0 == isJudge){ // 不转换坐标
  252. log.info("not to transform");
  253. entity = entity;
  254. } else {
  255. log.info("error exeCmd");
  256. entity.setStatus(7);
  257. vectorServer.save(entity);
  258. }
  259. }
  260. // Thread.sleep(4000);
  261. } catch (InterruptedException e) {
  262. isRun = false;
  263. e.printStackTrace();
  264. }
  265. }
  266. }
  267. }
  268. @ApiOperation("矢量数据转geojson")
  269. @GetMapping("command/geojson/{fileId}/")
  270. private R cmdGeojson(@PathVariable("fileId") Long fileId) {
  271. log.info("run cmdGeojson: {}", fileId);
  272. OutputFileEntity entity = vectorServer.findById(fileId);
  273. String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
  274. // // 截取目录名称
  275. // String fileUrl = entity.getUploadPath();
  276. // String outputFile = StringUtils.substringBefore(fileUrl, ".");
  277. // outputFile = outputFile + ".json";
  278. String outPath = OUTPUT_FILE_PATH + "geojson";
  279. FileUtils.createDir(outPath);
  280. outPath = outPath +File.separator + fileName + ".json";
  281. String cmd = Command.VECTOR_TO_GEOJSON;
  282. if (entity.getCoordStrictPath() != null) {
  283. // 严格坐标转换
  284. cmd = cmd.replace("@inputFile", entity.getCoordStrictPath());
  285. } else if (entity.getCoordStrictPath() == null && entity.getCoordGeneralPath() == null ) {
  286. // 不需要坐标转换
  287. cmd = cmd.replace("@inputFile", entity.getUploadPath());
  288. } else {
  289. // 普通坐标转换
  290. cmd = cmd.replace("@inputFile", entity.getCoordGeneralPath());
  291. }
  292. cmd = cmd.replace("@outputFile", outPath);
  293. log.info("cmd: {}", cmd);
  294. Integer integer = cmdServer.exeCmdInt(cmd);
  295. if (integer != 0) {
  296. return new R(50005, MsgCode.E50005);
  297. }
  298. // FileEntity fileEntity = new FileEntity();
  299. // fileEntity.setFileName(fileName + ".json");
  300. // fileEntity.setFileUrl(outputFile);
  301. // fileEntity.setCreateTime(new Date());
  302. // fileEntity.setUpdateTime(new Date());
  303. // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
  304. // fileEntity.setStatus(4);
  305. entity.setGeojsonPath(outPath);
  306. entity.setUpdateTime(new Date());
  307. entity.setStatus(4);
  308. entity = vectorServer.save(entity);
  309. return new R(200, entity);
  310. }
  311. @ApiOperation("矢量数据切片")
  312. @GetMapping("command/slice/{fileId}/")
  313. private R cmdSlice (@PathVariable("fileId") Long fileId) {
  314. log.info("run cmdSlice: {}", fileId);
  315. // FileEntity entity = fileServer.findById(fileId);
  316. OutputFileEntity entity = vectorServer.findById(fileId);
  317. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  318. String outPath = OUTPUT_FILE_PATH + "mbtiles";
  319. FileUtils.createDir(outPath);
  320. outPath = outPath +File.separator + fileName + ".mbtiles";
  321. String cmd = Command.VECTOR_SLICE_TIPPECANOE;
  322. cmd = cmd.replace("@inputFile", entity.getGeojsonPath());
  323. cmd = cmd.replace("@outputFile", outPath);
  324. log.info("cmd: {}", cmd);
  325. Integer integer = cmdServer.exeCmdInt(cmd);
  326. if (integer != 0) {
  327. return new R(50005, MsgCode.E50005);
  328. }
  329. // FileEntity fileEntity = new FileEntity();
  330. // fileEntity.setFileName(fileName + ".mbtiles");
  331. // fileEntity.setFileUrl(outputFile);
  332. // fileEntity.setCreateTime(new Date());
  333. // fileEntity.setUpdateTime(new Date());
  334. // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
  335. // fileEntity.setStatus(5);
  336. //
  337. // fileEntity = fileServer.save(fileEntity);
  338. entity.setSlicePath(outPath);
  339. entity.setUpdateTime(new Date());
  340. entity.setStatus(5);
  341. entity = vectorServer.save(entity);
  342. return new R(200, entity);
  343. }
  344. @ApiOperation("移动数据到服务器上")
  345. @GetMapping("move/{fileId}/")
  346. private R moveFile(@PathVariable("fileId") Long fileId) {
  347. log.info("run moveFile: {}", fileId);
  348. return vectorServer.moveFileToServer(fileId);
  349. }
  350. // 普通坐标转换
  351. private OutputFileEntity generalCoordTransform(OutputFileEntity entity, String cmd){
  352. String directory = createDirectory(entity);
  353. directory = directory + File.separator + entity.getFileName();
  354. cmd = cmd.replace("@inputFile", entity.getUploadPath());
  355. cmd = cmd.replace("@outputFile", directory);
  356. log.info("cmd: {}", cmd);
  357. return runCmd(cmd, entity, directory, null);
  358. }
  359. // 严格坐标转换
  360. private OutputFileEntity strictCoordTransform(OutputFileEntity entity, String cmd){
  361. String directory = createDirectory(entity);
  362. directory = directory + File.separator + entity.getFileName();
  363. cmd = cmd.replace("@coord", entity.getCoord());
  364. cmd = cmd.replace("@inputFile", entity.getUploadPath());
  365. cmd = cmd.replace("@outputFile", directory);
  366. log.info("cmd: {}", cmd);
  367. return runCmd(cmd, entity, null, directory);
  368. }
  369. // 创建目录
  370. private String createDirectory(OutputFileEntity entity){
  371. String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
  372. String directory = INPUT_FILE_PATH + "transform" + File.separator + fileName;
  373. FileUtils.createDir(directory);
  374. return directory;
  375. }
  376. // 执行命令,创建对象
  377. private OutputFileEntity runCmd(String cmd, OutputFileEntity entity, String generalPath, String strictPath){
  378. Integer integer = cmdServer.exeCmdInt(cmd);
  379. if (integer != 0) {
  380. return null;
  381. }
  382. entity.setFileName(entity.getFileName());
  383. entity.setCoordGeneralPath(generalPath);
  384. entity.setCoordStrictPath(strictPath);
  385. entity.setUpdateTime(new Date());
  386. entity.setStatus(3);
  387. entity = vectorServer.save(entity);
  388. return entity;
  389. }
  390. }