package com.fd.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.fd.constant.Command; import com.fd.constant.MsgCode; import com.fd.constant.TypeCode; import com.fd.dto.ConfigJsonDto; import com.fd.dto.MyQueue; import com.fd.dto.PageDto; import com.fd.entity.FileEntity; import com.fd.entity.OutputFileEntity; import com.fd.server.CmdServer; import com.fd.server.RasterServer; import com.fd.server.impl.CmdServerImpl; import com.fd.server.impl.RasterServerImpl; import com.fd.thread.AsyncTask; import com.fd.util.FileUtils; import com.fd.util.R; import com.fd.util.RegexUtils; import com.fd.util.SpringContext; 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.scheduling.annotation.Async; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; /** * Created by Owen on 2019/11/12 0012 9:40 * * 栅格数据 */ @Log4j2 @RequestMapping("api/raster") @RestController public class RasterController { @Value("${input.file.path.raster}") private String INPUT_FILE_PATH; @Value("${output.file.path.raster}") private String OUTPUT_FILE_PATH; @Autowired private AsyncTask asyncTask; @Autowired private RasterServer rasterServer; private static BlockingQueue sliceQueue = new LinkedBlockingQueue(2); private static BlockingQueue coordQueue = new LinkedBlockingQueue(2); @ApiOperation("上传数据,校验文件名") @GetMapping("check/{fileName}/") private R checkFileName(@PathVariable("fileName") String fileName) { log.info("run checkFileName: {}",fileName); // 文件是否包含中文字符 if (RegexUtils.regexChinese(fileName)) { log.info(MsgCode.E51005); return new R(51005, MsgCode.E51005); } String s = StringUtils.substringAfterLast(fileName, "."); if (!"tif".equals(s)) { log.info(MsgCode.E50008); return new R(50008,MsgCode.E50008); } List list = rasterServer.findByFileName(fileName); if (list.size() > 0) { log.info( MsgCode.E51006); return new R(51006, MsgCode.E51006); } return new R(200, MsgCode.SUCCESS); } @ApiOperation("上传栅格数据,只能上传tif文件") @PostMapping(value = "upload", consumes = { "multipart/form-data" }) private R upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "coord",required = false) String[] coord){ log.info("run upload"); log.info("coord: {}", Arrays.toString(coord)); String strCoord = Arrays.toString(coord); // 文件名全名 String fileName = file.getOriginalFilename(); // 文件是否包含中文字符 if (RegexUtils.regexChinese(fileName)) { return new R(51005, MsgCode.E51005); } String s = StringUtils.substringAfterLast(fileName, "."); if (!"tif".equals(s)) { return new R(50008,MsgCode.E50008); } List list = rasterServer.findByFileName(fileName); if (list.size() > 0) { return new R(51006, MsgCode.E51006); } R r = rasterServer.uploadBigFile(file, strCoord); OutputFileEntity entity = (OutputFileEntity) r.getData(); // 判断坐标 entity = JudgeCoord(entity, rasterServer); return new R(200, entity); } /** * 上传后判断坐标,显示原始坐标 * @return */ private OutputFileEntity JudgeCoord(OutputFileEntity entity, RasterServer rasterServer){ String cmd = Command.RASTER_JUDGE_COORD; cmd = cmd.replace("@inputFile", entity.getUploadPath()); log.info("cmd: {}", cmd); Integer isJudge = rasterServer.cmdJudgeCoord(cmd); if (1000 == isJudge){ log.info("need to transform"); // 严格坐标转换 entity.setCoordType(TypeCode.COORD_XIAN_1980); } else if (0 == isJudge){ log.info("not to transform"); entity.setCoordType(TypeCode.COORD_WGS84); } else { log.info("error exeCmd"); entity.setStatus(7); } entity.setUpdateTime(new Date()); rasterServer.save(entity); return entity; } @ApiOperation("获取栅格数据列表") @PostMapping(value = "list") private R list(@RequestBody PageDto param){ return rasterServer.findByType(TypeCode.FILE_TYPE_RASTER, param); } @ApiOperation("删除文件") @GetMapping("delete/{fileId}/") private R deleteFile(@PathVariable("fileId") Long fileId) { log.info("run deleteFile: {}", fileId); OutputFileEntity entity = rasterServer.findById(fileId); entity.setResStatus(1); rasterServer.save(entity); FileEntity fileEntity = rasterServer.findByInputFileId(entity.getUploadId()); fileEntity.setResStatus(1); rasterServer.saveInputFile(fileEntity); asyncTask.rasterDelete(fileId, rasterServer); log.info("delete raster id: {}", fileId); return new R(200, MsgCode.SUCCESS); } @ApiOperation("栅格数据判断坐标") @GetMapping("command/judge/coord/{fileId}/") private R cmdJudgeCoord(@PathVariable("fileId") Long fileId) { log.info("run cmdJudgeCoord: {}", fileId); OutputFileEntity entity = rasterServer.findById(fileId); String cmd = Command.RASTER_JUDGE_COORD; cmd = cmd.replace("@inputFile", entity.getUploadPath()); log.info("cmd: {}", cmd); // 把数据放入队列中 MyQueue data = new MyQueue(); data.setOutputFile(entity); data.setStr(cmd); boolean offer = false; try { offer = coordQueue.offer(data, 1, TimeUnit.SECONDS); log.info("入队成功"); } catch (Exception e) { log.error("error producer queue raster cmdJudgeCoord: {}", e); e.printStackTrace(); } if (offer) { entity.setStatus(9); entity.setUpdateTime(new Date()); entity = rasterServer.save(entity); log.info("coord entity: {}",entity); log.info("coord producer update time: {}", entity.getUpdateTime()); asyncTask.rasterJudgeCoordConsumerThread(coordQueue, rasterServer, OUTPUT_FILE_PATH); return new R(200, entity); } return new R(52000, MsgCode.E52000); } @ApiOperation("栅格数据切片命令") @GetMapping("command/osgeo/{fileId}/{layerMin}/{layerMax}/") private R cmdSlice(@PathVariable("fileId") Long fileId, @PathVariable("layerMin") String layerMin, @PathVariable("layerMax") String layerMax) { log.info("run cmdSlice: {}", fileId); if (!RegexUtils.regexInt(layerMin)){ return new R(50010, MsgCode.E50010) ; } if (!RegexUtils.regexInt(layerMax)){ return new R(50010, MsgCode.E50010) ; } OutputFileEntity entity = rasterServer.findById(fileId); String fileName = StringUtils.substringBeforeLast(entity.getFileName(), "."); String outFilePath = OUTPUT_FILE_PATH + "slice" + File.separator + fileName; FileUtils.createDir(outFilePath); String cmd = Command.RASTER_SLICE_OSGEO; cmd = cmd.replace("@inputFile", entity.getCoordStrictPath()); cmd = cmd.replace("@outputFile", outFilePath); cmd = cmd.replace("@layerMin", layerMin); cmd = cmd.replace("@layerMax", layerMax); log.info("cmd: {}", cmd); // 把数据放入队列中 MyQueue data = new MyQueue(); data.setOutputFile(entity); data.setStr(cmd); boolean offer = false; try { offer = sliceQueue.offer(data, 1, TimeUnit.SECONDS); log.info("raster slice 入队成功"); } catch (Exception e) { log.error("error producer queue raster cmdSlice: {}", e); e.printStackTrace(); } if (offer) { // 设个默认进度给前端显示 entity.setProgress(1); entity.setStatus(6); entity.setLayerMin(Integer.valueOf(layerMin)); entity.setLayerMax(Integer.valueOf(layerMax)); entity.setUpdateTime(new Date()); entity.setSlicePath(outFilePath); entity = rasterServer.save(entity); asyncTask.rasterSliceThread(sliceQueue, rasterServer); return new R(200, entity) ; } return new R(52000, MsgCode.E52000); } @ApiOperation("栅格数据进度查询") @GetMapping("progress/{fileId}/") private R getProgress(@PathVariable("fileId") Long fileId) { OutputFileEntity entity = rasterServer.findById(fileId); return new R(200, entity); } @ApiOperation("移动数据到服务器上") @PostMapping("move/{fileId}/") private R moveFile(@PathVariable("fileId") Long fileId, @RequestBody ConfigJsonDto param) { log.info("run moveFile: {}", fileId); return rasterServer.moveFileToServer(fileId, param); } }