123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- package com.fd.controller;
- import com.fd.constant.Command;
- import com.fd.constant.MsgCode;
- import com.fd.constant.TypeCode;
- 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.FileServer;
- import com.fd.server.VectorServer;
- import com.fd.util.FileUtils;
- import com.fd.util.R;
- 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.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.PostConstruct;
- import java.io.File;
- import java.util.Date;
- 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/vector")
- @RestController
- public class VectorController {
- @Value("${input.file.path}")
- private String INPUT_FILE_PATH;
- @Value("${output.file.path}")
- private String OUTPUT_FILE_PATH;
- @Autowired
- private FileServer fileServer;
- @Autowired
- private VectorServer vectorServer;
- @Autowired
- private CmdServer cmdServer;
- BlockingQueue<MyQueue> coordQueue = new LinkedBlockingQueue<MyQueue>(5);
- /**
- * 初始化队列
- *
- * @return
- */
- @PostConstruct
- private void init() {
- // 判断坐标消费队列
- new Thread(new JudgeCoordConsumerThread(coordQueue)).start();
- }
- // @ApiOperation("上传矢量数据,coord:坐标, directoryName:目录名称")
- // @PostMapping(value = "upload/{directoryName}/", consumes = { "multipart/form-data" })
- // private R upload(@RequestParam("file") MultipartFile file, @PathVariable("directoryName") String directoryName,
- // @RequestParam(value = "coord",required = false) String coord){
- // log.info("run upload");
- // return fileServer.uploadFile(file, directoryName, TypeCode.FILE_TYPE_VECTOR);
- // }
- // @ApiOperation("上传矢量数据,只能上传zip文件,里面的文件名必须跟压缩包一致, coord:坐标(可以为空), 坐标格式:0,0,0,0,0,0,0")
- // @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");
- //
- // // 文件名全名
- // String fileName = file.getOriginalFilename();
- // String s = StringUtils.substringAfterLast(fileName, ".");
- //
- // if (!"zip".equals(s)) {
- // return new R(50007,MsgCode.E50007);
- // }
- //
- // return vectorServer.uploadFile(file, coord);
- // }
- @ApiOperation("上传矢量数据, coord:坐标(可以为空), 坐标格式:0,0,0,0,0,0,0")
- @PostMapping(value = "upload/{directoryName}/", consumes = { "multipart/form-data" })
- private R upload(@RequestParam("file") MultipartFile file,
- @PathVariable("directoryName") String directoryName,
- @RequestParam(value = "coord",required = false) String coord){
- log.info("run uploadVector");
- return vectorServer.uploadDirectoryFile(file, directoryName, coord);
- }
- // @ApiOperation("解压zip文件")
- // @GetMapping("unzip/{fileId}/")
- // private R fileUnzip(@PathVariable("fileId") Long fileId) {
- // log.info("run fileUnzip: {}", fileId);
- // FileEntity entity = fileServer.findById(fileId);
- //
- // boolean unzip = FileUtils.unzip(entity.getFileUrl(), INPUT_FILE_PATH);
- //
- // if (!unzip) {
- // log.info("zip error: {}", MsgCode.E51001);
- // return new R(51001, MsgCode.E51001);
- // }
- //
- //
- //
- // String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
- //
- // // 判断文件内容是否跟目录名称一样
- // String shpPath = INPUT_FILE_PATH + fileName + File.separator + fileName + ".shp";
- // File file = new File(shpPath);
- // if (!file.exists()) {
- // log.info("zip error: {}", MsgCode.E51002);
- // return new R(51002, MsgCode.E51002);
- // }
- //
- // 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_VECTOR);
- // fileEntity.setCoord(entity.getCoord());
- // fileEntity.setStatus(2);
- //
- // fileEntity = fileServer.save(fileEntity);
- //
- // // xxx.shp到数据库
- //// String sName = StringUtils.substringAfter(fileName, "_");
- //// sName = sName + ".shp";
- // fileEntity = new FileEntity();
- //// fileEntity.setFileName(sName);
- //// fileEntity.setFileUrl(INPUT_FILE_PATH + fileName + File.separator + sName);
- //
- // fileEntity.setFileName(fileName + ".shp");
- // fileEntity.setFileUrl(shpPath);
- //
- // fileEntity.setCreateTime(new Date());
- // fileEntity.setUpdateTime(new Date());
- // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
- // fileEntity.setCoord(entity.getCoord());
- // fileEntity = fileServer.save(fileEntity);
- //
- // return new R(200, fileEntity);
- // }
- @ApiOperation("获取矢量数据列表")
- @PostMapping(value = "list")
- private R list(@RequestBody PageDto param){
- log.info("run list");
- // return fileServer.findByType(TypeCode.FILE_TYPE_VECTOR, TypeCode.FILE_TYPE_VECTOR_SHP, param);
- return vectorServer.findByType(TypeCode.FILE_TYPE_VECTOR, param);
- }
- /**
- * 删除文件
- */
- @ApiOperation("删除文件")
- @GetMapping("delete/{fileId}/")
- private R deleteFile(@PathVariable("fileId") Long fileId) {
- log.info("run deleteFile: {}", fileId);
- 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);
- OutputFileEntity entity = vectorServer.findById(fileId);
- String coord = entity.getCoord();
- log.info("coord: {}", coord);
- // 判断是否需要坐标转换
- String cmd = Command.VECTOR_JUDGE_COORD;
- cmd = cmd.replace("@inputFile", entity.getUploadPath());
- log.info("cmd: {}", cmd);
- // 把数据放入队列中
- MyQueue data = new MyQueue();
- data.setOutputFile(entity);
- data.setStr(cmd);
- try {
- coordQueue.offer(data, 1, TimeUnit.SECONDS);
- log.info("入队成功");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // Integer isJudge = cmdServer.exeCmdJudgeCoord(cmd);
- //
- //// FileEntity fileEntity = null;
- // // 转换坐标 普通坐标转换
- // if (isJudge == 1000){
- // // 普通坐标转换
- // log.info("need to general transform");
- // if (coord == null) {
- // // 没有坐标参数,执行普通坐标转换(ogrinfo)
- // entity = generalCoordTransform(entity, Command.VECTOR_TRANSFORM_GENERAL);
- //
- // } else {
- // // 有坐标参数,执行严格坐标转换(CGCS2000转wgs80)
- // entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS80);
- // }
- //
- // } else if (isJudge == 1001) {
- // // 严格坐标转换 (西安80转wgs84),需要参数
- // log.info("need to strict transform");
- // if (coord == null) {
- // log.info("error: {}", MsgCode.E50009);
- // return new R(50009, MsgCode.E50009);
- // }
- // entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS84);
- //
- // } else if (0 == isJudge){ // 不转换坐标
- // log.info("not to transform");
- // entity = entity;
- // } else {
- // log.info("error exeCmd");
- // return new R(50005, MsgCode.E50005);
- // }
- return new R(200, entity);
- }
- public class JudgeCoordConsumerThread implements Runnable{
- private BlockingQueue<MyQueue> queue;
- private boolean isRun = true;
- public JudgeCoordConsumerThread(BlockingQueue<MyQueue> queue){
- this.queue = queue;
- }
- @Override
- public void run() {
- while (isRun) {
- try {
- MyQueue data = queue.poll(2, TimeUnit.SECONDS);
- if (data != null) {
- log.info("消费者,拿到队列中的数据data:" + data.toString());
- Integer isJudge = cmdServer.exeCmdJudgeCoord(data.getStr());
- OutputFileEntity entity = data.getOutputFile();
- // 转换坐标 普通坐标转换
- if (isJudge == 1000){
- // 普通坐标转换
- log.info("need to general transform");
- if (entity.getCoord() == null) {
- // 没有坐标参数,执行普通坐标转换(ogrinfo)
- entity = generalCoordTransform(entity, Command.VECTOR_TRANSFORM_GENERAL);
- } else {
- // 有坐标参数,执行严格坐标转换(CGCS2000转wgs80)
- entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS80);
- }
- } else if (isJudge == 1001) {
- // 严格坐标转换 (西安80转wgs84),需要参数
- log.info("need to strict transform");
- if (entity.getCoord() == null) {
- log.info("error: {}", MsgCode.E50009);
- entity.setStatus(7);
- vectorServer.save(entity);
- }
- entity = strictCoordTransform(entity, Command.VECTOR_TRANSFORM_STRICT_WGS84);
- } else if (0 == isJudge){ // 不转换坐标
- log.info("not to transform");
- entity = entity;
- } else {
- log.info("error exeCmd");
- entity.setStatus(7);
- vectorServer.save(entity);
- }
- }
- // Thread.sleep(4000);
- } catch (InterruptedException e) {
- isRun = false;
- e.printStackTrace();
- }
- }
- }
- }
- @ApiOperation("矢量数据转geojson")
- @GetMapping("command/geojson/{fileId}/")
- private R cmdGeojson(@PathVariable("fileId") Long fileId) {
- log.info("run cmdGeojson: {}", fileId);
- OutputFileEntity entity = vectorServer.findById(fileId);
- String fileName = StringUtils.substringBefore(entity.getFileName(), ".");
- // // 截取目录名称
- // String fileUrl = entity.getUploadPath();
- // String outputFile = StringUtils.substringBefore(fileUrl, ".");
- // outputFile = outputFile + ".json";
- String outPath = OUTPUT_FILE_PATH + "geojson";
- FileUtils.createDir(outPath);
- outPath = outPath +File.separator + fileName + ".json";
- String cmd = Command.VECTOR_TO_GEOJSON;
- if (entity.getCoordStrictPath() != null) {
- // 严格坐标转换
- cmd = cmd.replace("@inputFile", entity.getCoordStrictPath());
- } else if (entity.getCoordStrictPath() == null && entity.getCoordGeneralPath() == null ) {
- // 不需要坐标转换
- cmd = cmd.replace("@inputFile", entity.getUploadPath());
- } else {
- // 普通坐标转换
- cmd = cmd.replace("@inputFile", entity.getCoordGeneralPath());
- }
- cmd = cmd.replace("@outputFile", outPath);
- log.info("cmd: {}", cmd);
- Integer integer = cmdServer.exeCmdInt(cmd);
- if (integer != 0) {
- return new R(50005, MsgCode.E50005);
- }
- // FileEntity fileEntity = new FileEntity();
- // fileEntity.setFileName(fileName + ".json");
- // fileEntity.setFileUrl(outputFile);
- // fileEntity.setCreateTime(new Date());
- // fileEntity.setUpdateTime(new Date());
- // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
- // fileEntity.setStatus(4);
- entity.setGeojsonPath(outPath);
- entity.setUpdateTime(new Date());
- entity.setStatus(4);
- entity = vectorServer.save(entity);
- return new R(200, entity);
- }
- @ApiOperation("矢量数据切片")
- @GetMapping("command/slice/{fileId}/")
- private R cmdSlice (@PathVariable("fileId") Long fileId) {
- log.info("run cmdSlice: {}", fileId);
- // FileEntity entity = fileServer.findById(fileId);
- OutputFileEntity entity = vectorServer.findById(fileId);
- String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
- String outPath = OUTPUT_FILE_PATH + "mbtiles";
- FileUtils.createDir(outPath);
- outPath = outPath +File.separator + fileName + ".mbtiles";
- String cmd = Command.VECTOR_SLICE_TIPPECANOE;
- cmd = cmd.replace("@inputFile", entity.getGeojsonPath());
- cmd = cmd.replace("@outputFile", outPath);
- log.info("cmd: {}", cmd);
- Integer integer = cmdServer.exeCmdInt(cmd);
- if (integer != 0) {
- return new R(50005, MsgCode.E50005);
- }
- // FileEntity fileEntity = new FileEntity();
- // fileEntity.setFileName(fileName + ".mbtiles");
- // fileEntity.setFileUrl(outputFile);
- // fileEntity.setCreateTime(new Date());
- // fileEntity.setUpdateTime(new Date());
- // fileEntity.setType(TypeCode.FILE_TYPE_VECTOR);
- // fileEntity.setStatus(5);
- //
- // fileEntity = fileServer.save(fileEntity);
- entity.setSlicePath(outPath);
- entity.setUpdateTime(new Date());
- entity.setStatus(5);
- entity = vectorServer.save(entity);
- return new R(200, entity);
- }
- @ApiOperation("移动数据到服务器上")
- @GetMapping("move/{fileId}/")
- private R moveFile(@PathVariable("fileId") Long fileId) {
- log.info("run moveFile: {}", fileId);
- return vectorServer.moveFileToServer(fileId);
- }
- // 普通坐标转换
- private OutputFileEntity generalCoordTransform(OutputFileEntity entity, String cmd){
- String directory = createDirectory(entity);
- directory = directory + File.separator + entity.getFileName();
- cmd = cmd.replace("@inputFile", entity.getUploadPath());
- cmd = cmd.replace("@outputFile", directory);
- log.info("cmd: {}", cmd);
- return runCmd(cmd, entity, directory, null);
- }
- // 严格坐标转换
- private OutputFileEntity strictCoordTransform(OutputFileEntity entity, String cmd){
- String directory = createDirectory(entity);
- directory = directory + File.separator + entity.getFileName();
- cmd = cmd.replace("@coord", entity.getCoord());
- cmd = cmd.replace("@inputFile", entity.getUploadPath());
- cmd = cmd.replace("@outputFile", directory);
- log.info("cmd: {}", cmd);
- return runCmd(cmd, entity, null, directory);
- }
- // 创建目录
- private String createDirectory(OutputFileEntity entity){
- String fileName = StringUtils.substringBeforeLast(entity.getFileName(), ".");
- String directory = INPUT_FILE_PATH + "transform" + File.separator + fileName;
- FileUtils.createDir(directory);
- return directory;
- }
- // 执行命令,创建对象
- private OutputFileEntity runCmd(String cmd, OutputFileEntity entity, String generalPath, String strictPath){
- Integer integer = cmdServer.exeCmdInt(cmd);
- if (integer != 0) {
- return null;
- }
- entity.setFileName(entity.getFileName());
- entity.setCoordGeneralPath(generalPath);
- entity.setCoordStrictPath(strictPath);
- entity.setUpdateTime(new Date());
- entity.setStatus(3);
- entity = vectorServer.save(entity);
- return entity;
- }
- }
|