VectorServerImpl.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. package com.fd.server.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fd.constant.Command;
  6. import com.fd.constant.MsgCode;
  7. import com.fd.constant.TypeCode;
  8. import com.fd.dto.ConfigJsonDto;
  9. import com.fd.dto.PageDto;
  10. import com.fd.dto.StyleDto;
  11. import com.fd.entity.FileEntity;
  12. import com.fd.entity.OutputFileEntity;
  13. import com.fd.entity.StyleEntity;
  14. import com.fd.repository.FileRepository;
  15. import com.fd.repository.OutputFileRepository;
  16. import com.fd.repository.StyleRepository;
  17. import com.fd.server.CmdServer;
  18. import com.fd.server.VectorServer;
  19. import com.fd.util.FileUtils;
  20. import com.fd.util.R;
  21. import com.fd.util.RegexUtils;
  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.data.domain.Page;
  27. import org.springframework.data.domain.PageRequest;
  28. import org.springframework.data.domain.Sort;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.web.multipart.MultipartFile;
  31. import java.io.BufferedReader;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.io.InputStreamReader;
  35. import java.math.BigDecimal;
  36. import java.util.*;
  37. /**
  38. * Created by Owen on 2019/11/21 0021 15:29
  39. */
  40. @Log4j2
  41. @Service
  42. //@Transactional
  43. public class VectorServerImpl implements VectorServer {
  44. @Value("${input.file.path.vector}")
  45. private String INPUT_FILE_PATH;
  46. @Value("${output.file.path.vector}")
  47. private String OUTPUT_FILE_PATH;
  48. @Value("${copy.file.path.vector}")
  49. private String MOVE_FILE_TO_SERVER;
  50. // config.json 地址
  51. @Value("${config.path}")
  52. private String CONFIG_JSON_PATH;
  53. // config.json teileset 的相对路径
  54. @Value("${config.tileset}")
  55. private String CONFIG_TILESET;
  56. @Autowired
  57. private FileRepository fileRepository;
  58. @Autowired
  59. private OutputFileRepository outputFileRepository;
  60. @Autowired
  61. private CmdServer cmdServer;
  62. @Autowired
  63. private StyleRepository styleRepository;
  64. // @Override
  65. // public R moveFileToServer(Long fileId, ConfigJsonDto param) {
  66. // Optional<OutputFileEntity> o = outputFileRepository.findById(fileId);
  67. // if (!o.isPresent()) {
  68. // log.info("id:{} 不存在", fileId);
  69. // return new R(50002, MsgCode.E50002);
  70. // }
  71. // OutputFileEntity entity = o.get();
  72. // FileUtils.createDir(MOVE_FILE_TO_SERVER);
  73. //
  74. // try {
  75. // org.apache.commons.io.FileUtils.copyDirectoryToDirectory(new File(entity.getSlicePath()), new File(MOVE_FILE_TO_SERVER));
  76. // // 修改前端的config.json 文件
  77. // writeJsonFile(param, entity.getDirectory(), entity);
  78. //
  79. // //修改linux 文件权限
  80. // String cmd = Command.CMD_CHOMD_755;
  81. // String slice = MOVE_FILE_TO_SERVER + entity.getDirectory();
  82. // cmd = cmd.replace("@path", slice);
  83. // log.info("cmd: {}", cmd);
  84. // cmdServer.exeCmdInt(cmd);
  85. //
  86. //
  87. // entity.setStatus(8);
  88. // entity.setServicePath(slice);
  89. // entity.setUpdateTime(new Date());
  90. // outputFileRepository.save(entity);
  91. // return new R(200, MsgCode.SUCCESS);
  92. // } catch (IOException e) {
  93. // e.printStackTrace();
  94. // // 服务发布失败
  95. // entity.setStatus(10);
  96. // entity.setUpdateTime(new Date());
  97. // outputFileRepository.save(entity);
  98. // return new R(51004, MsgCode.E51004, e);
  99. // }
  100. // }
  101. @Override
  102. public R moveFileToServer(Long fileId, ConfigJsonDto param) {
  103. Optional<OutputFileEntity> o = outputFileRepository.findById(fileId);
  104. if (!o.isPresent()) {
  105. log.info("id:{} 不存在", fileId);
  106. return new R(50002, MsgCode.E50002);
  107. }
  108. OutputFileEntity entity = o.get();
  109. // FileUtils.createDir(MOVE_FILE_TO_SERVER);
  110. try {
  111. // org.apache.commons.io.FileUtils.copyDirectoryToDirectory(new File(entity.getSlicePath()), new File(MOVE_FILE_TO_SERVER));
  112. // 修改前端的config.json 文件
  113. writeJsonFile(param, entity.getDirectory(), entity);
  114. //修改linux 文件权限
  115. // String cmd = Command.CMD_CHOMD_755;
  116. // String slice = MOVE_FILE_TO_SERVER + entity.getDirectory();
  117. // cmd = cmd.replace("@path", slice);
  118. // log.info("cmd: {}", cmd);
  119. // cmdServer.exeCmdInt(cmd);
  120. entity.setStatus(8);
  121. entity.setServicePath(entity.getSlicePath());
  122. entity.setUpdateTime(new Date());
  123. outputFileRepository.save(entity);
  124. return new R(200, MsgCode.SUCCESS);
  125. } catch (Exception e) {
  126. e.printStackTrace();
  127. // 服务发布失败
  128. entity.setStatus(10);
  129. entity.setUpdateTime(new Date());
  130. outputFileRepository.save(entity);
  131. return new R(51004, MsgCode.E51004, e);
  132. }
  133. }
  134. @Override
  135. public R deleteById(Long fileId) {
  136. // 删除服务器文件
  137. Optional<OutputFileEntity> e = outputFileRepository.findById(fileId);
  138. if (!e.isPresent()) {
  139. return new R(50002, MsgCode.E50002);
  140. }
  141. OutputFileEntity entity = e.get();
  142. // 删除配置文件config.json制定行
  143. StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(fileId);
  144. if (styleEntity != null) {
  145. deleteRowConfigJson(styleEntity);
  146. }
  147. // 删除数据库记录
  148. outputFileRepository.deleteById(fileId);
  149. fileRepository.deleteById(entity.getUploadId());
  150. styleRepository.deleteByOutputFileId(fileId);
  151. // 删除文件
  152. if (entity.getUploadPath() != null) {
  153. String path = entity.getUploadPath();
  154. deleteFolder(path, entity);
  155. }
  156. if (entity.getUnZipPath() != null) {
  157. FileUtils.delFolder(entity.getUnZipPath());
  158. }
  159. if (entity.getCoordGeneralPath() != null) {
  160. String path = entity.getCoordGeneralPath();
  161. deleteFolder(path, entity);
  162. }
  163. if (entity.getCoordStrictPath() != null) {
  164. String path = entity.getCoordStrictPath();
  165. deleteFolder(path, entity);
  166. }
  167. // 文件类型
  168. if (entity.getGeojsonPath() != null) {
  169. log.info("test file geojson");
  170. String path = entity.getGeojsonPath();
  171. deleteFolder(path, entity);
  172. }
  173. if (entity.getSlicePath() != null) {
  174. FileUtils.delFolder(entity.getSlicePath());
  175. }
  176. // 目录类型
  177. // if (entity.getServicePath() != null) {
  178. // FileUtils.delFolder(entity.getServicePath());
  179. // }
  180. return new R(200, MsgCode.SUCCESS);
  181. }
  182. // 以目录形式删除
  183. private void deleteFolder(String path, OutputFileEntity entity) {
  184. String directory = entity.getDirectory();
  185. int length = directory.length();
  186. path = path.substring(0, path.lastIndexOf(directory) + length);
  187. log.warn("delete vector file: {}", path);
  188. FileUtils.delFolder(path);
  189. }
  190. // @Override
  191. // public R uploadDirectoryFile(MultipartFile file, String directoryName, String coord) {
  192. //
  193. // log.warn("run uploadBigFile");
  194. // long start = System.currentTimeMillis();
  195. // if (file.isEmpty() || file.getSize() <= 0) {
  196. // log.info("文件为空");
  197. // return new R(50001, MsgCode.E50001);
  198. // }
  199. //
  200. // // 文件名全名
  201. // String fullFileName = file.getOriginalFilename();
  202. //
  203. // // 创建目录路径
  204. // String filePath = INPUT_FILE_PATH + directoryName + File.separator;
  205. // FileUtils.createDir(filePath);
  206. //
  207. //
  208. // // 文件保存路径
  209. // filePath = filePath + fullFileName;
  210. //
  211. // // 写文件到本地
  212. //
  213. // try {
  214. // FileUtils.bigFileWrite(file.getInputStream(), filePath);
  215. // } catch (IOException e) {
  216. // e.printStackTrace();
  217. // }
  218. // log.info("filePath: {}", filePath);
  219. //
  220. // // 保存信息到db
  221. // FileEntity entity = new FileEntity();
  222. // entity.setFileName(fullFileName);
  223. // entity.setDirectory(directoryName);
  224. // entity.setFileUrl(filePath);
  225. // entity.setCreateTime(new Date());
  226. // entity.setUpdateTime(new Date());
  227. // entity.setType(TypeCode.FILE_TYPE_VECTOR);
  228. // entity.setCoord(coord);
  229. // entity.setStatus(2);
  230. // fileRepository.save(entity);
  231. //
  232. // OutputFileEntity outputFile = null;
  233. // String s = StringUtils.substringAfterLast(fullFileName, ".");
  234. // if ("shp".equals(s)) {
  235. //
  236. // outputFile = new OutputFileEntity();
  237. // outputFile.setUploadId(entity.getId());
  238. // outputFile.setUploadPath(entity.getFileUrl());
  239. // outputFile.setFileName(entity.getFileName());
  240. // outputFile.setStatus(2);
  241. // outputFile.setType(TypeCode.FILE_TYPE_VECTOR);
  242. // outputFile.setCoord(entity.getCoord());
  243. // outputFile.setCreateTime(new Date());
  244. // outputFile.setUpdateTime(new Date());
  245. //
  246. // outputFile = outputFileRepository.save(outputFile);
  247. // }
  248. //
  249. //
  250. // long end = System.currentTimeMillis();
  251. // log.info("end uploadBigFile, total time: {} s", (end - start) / 1000);
  252. // return new R(200, outputFile);
  253. // }
  254. @Override
  255. public R uploadDirectoryFileMul(MultipartFile[] files, String directoryName, String coord) {
  256. log.warn("run uploadDirectoryFileMul");
  257. // 判断目录重名
  258. // List<FileEntity> en = fileRepository.findByDirectory(directoryName);
  259. List<FileEntity> en = fileRepository.findByDirectoryAndResStatus(directoryName, 0);
  260. if (en.size() > 0) {
  261. return new R(51007, MsgCode.E51007);
  262. }
  263. // 创建目录路径
  264. String filePath = INPUT_FILE_PATH + directoryName + File.separator;
  265. FileUtils.createDir(filePath);
  266. FileEntity entity = new FileEntity();
  267. StringBuilder sb = new StringBuilder();
  268. // 检查文件名、格式
  269. int suffixCheckInt = 0;
  270. HashSet<Object> setPrefix = new HashSet<>();
  271. // 记录shp 信息
  272. boolean shpFlag = false;
  273. String shpName = "";
  274. if (files != null && files.length > 0) {
  275. for (MultipartFile f : files) {
  276. String filename = f.getOriginalFilename();
  277. // 文件是否包含中文字符
  278. if (RegexUtils.regexChinese(filename)) {
  279. return new R(51005, MsgCode.E51005);
  280. }
  281. log.info("fileName : {}", filename);
  282. String s = StringUtils.substringAfterLast(filename, ".");
  283. // 判断后缀名,需要包含这以下四个
  284. String [] suffixCheck = {"dbf","shp","shx","prj"};
  285. if (Arrays.asList(suffixCheck).contains(s)) {
  286. suffixCheckInt += 1;
  287. // 判断前缀名字是否一致
  288. String prefix = StringUtils.substringBeforeLast(filename, ".");
  289. setPrefix.add(prefix);
  290. }
  291. if ("shp".equals(s)) {
  292. shpFlag = true;
  293. shpName = filename;
  294. }
  295. try {
  296. FileUtils.bigFileWrite(f.getInputStream(), filePath + filename);
  297. } catch (IOException e) {
  298. e.printStackTrace();
  299. }
  300. sb.append(filename).append(",");
  301. }
  302. }
  303. // 上传文件名是否一致
  304. if (setPrefix.size() > 1) {
  305. log.info("文件名前缀不一致");
  306. return new R(51009, MsgCode.E51009);
  307. }
  308. // 上传文件格式有误
  309. if (suffixCheckInt != 4) {
  310. log.info("文件后缀名有误");
  311. return new R(51008, MsgCode.E51008);
  312. }
  313. OutputFileEntity outputFile = new OutputFileEntity();
  314. if (shpFlag) {
  315. entity.setDirectory(directoryName);
  316. entity.setFileName(sb.toString());
  317. entity.setFileUrl(filePath + shpName);
  318. entity.setCreateTime(new Date());
  319. entity.setUpdateTime(new Date());
  320. entity.setType(TypeCode.FILE_TYPE_VECTOR);
  321. entity.setCoord(coord);
  322. entity.setResStatus(0);
  323. entity = fileRepository.save(entity);
  324. outputFile.setUploadId(entity.getId());
  325. outputFile.setUploadPath(entity.getFileUrl());
  326. outputFile.setDirectory(entity.getDirectory());
  327. outputFile.setFileName(shpName);
  328. outputFile.setStatus(2);
  329. outputFile.setType(TypeCode.FILE_TYPE_VECTOR);
  330. outputFile.setCoord(entity.getCoord());
  331. outputFile.setCreateTime(new Date());
  332. outputFile.setUpdateTime(new Date());
  333. outputFile.setResStatus(0);
  334. outputFile = outputFileRepository.save(outputFile);
  335. }
  336. return new R(200, outputFile);
  337. }
  338. // @Override
  339. // public R findByType(String type, PageDto pageDto) {
  340. // Page<OutputFileEntity> page = outputFileRepository.findByType(type, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
  341. // return new R(200, page);
  342. // }
  343. @Override
  344. public R findByType(String type, PageDto pageDto) {
  345. Page<OutputFileEntity> page = outputFileRepository.findByTypeAndResStatus(type, 0, PageRequest.of(pageDto.getPageNum(), pageDto.getPageSize(), Sort.by("createTime").descending()));
  346. return new R(200, page);
  347. }
  348. @Override
  349. public List<FileEntity> findByDirectory(String directory) {
  350. // return fileRepository.findByDirectory(directory);
  351. return fileRepository.findByDirectoryAndResStatus(directory, 0);
  352. }
  353. @Override
  354. public FileEntity findByUploadId(Long uploadId) {
  355. Optional<FileEntity> o = fileRepository.findById(uploadId);
  356. if (o.isPresent()) {
  357. return o.get();
  358. }
  359. return null;
  360. }
  361. /**
  362. * 修改样式
  363. *
  364. * @param entity
  365. * @return
  366. */
  367. @Override
  368. public R editStyle(StyleDto entity) {
  369. StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getOutputFileId());
  370. Optional<OutputFileEntity> o = outputFileRepository.findById(entity.getOutputFileId());
  371. OutputFileEntity outputFileEntity = null;
  372. if (o.isPresent()) {
  373. outputFileEntity = o.get();
  374. outputFileEntity.setStatus(11);
  375. outputFileEntity.setUpdateTime(new Date());
  376. outputFileRepository.save(outputFileEntity);
  377. }
  378. styleEntity.setUpdateTime(new Date());
  379. styleEntity.setContent(entity.getContent());
  380. StyleEntity save = styleRepository.save(styleEntity);
  381. /**
  382. * 发布完成才允许这一步操作,因为第一次发布时,还没有样式在config里面
  383. *
  384. * 发布时数据,已经把样式提交过去config了
  385. */
  386. editStyleConfigJson(save);
  387. return new R(200, save.getOutputFileId());
  388. }
  389. @Override
  390. public StyleEntity saveStyle(StyleEntity entity) {
  391. return styleRepository.save(entity);
  392. }
  393. @Override
  394. public R getStyle(Long id) {
  395. return new R(200, styleRepository.findByOutputFileIdTop(id));
  396. }
  397. @Override
  398. public Map cmdJudgeCoord(String commandStr) {
  399. // 命令运行结果 1:失败, 0:成功
  400. Integer isCmd = null;
  401. StringBuffer sb = new StringBuffer();
  402. StringBuffer errorStr = new StringBuffer();
  403. Map map = null;
  404. try {
  405. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  406. Process ps = Runtime.getRuntime().exec(cmd);
  407. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  408. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  409. // error : 坑, 控制台信息是从errorBuf这里出来的
  410. String errorLine;
  411. while ((errorLine = errorBuf.readLine()) != null) {
  412. errorStr.append(errorLine).append("\n");
  413. }
  414. if (StringUtils.isNotEmpty(errorStr)) {
  415. log.info("error result: {}", errorStr.toString());
  416. }
  417. // success ,没有获取到信息
  418. String line;
  419. while ((line = br.readLine()) != null) {
  420. //执行结果加上回车
  421. sb.append(line).append("\n");
  422. // 获取经纬度
  423. if (line.contains("Extent: (")) {
  424. log.info("extent: {}", line);
  425. map = getExtent(line);
  426. }
  427. }
  428. log.info("result: {}", sb.toString());
  429. // 结束命令行
  430. isCmd = ps.waitFor();
  431. // 关闭流
  432. br.close();
  433. errorBuf.close();
  434. } catch (Exception e) {
  435. e.printStackTrace();
  436. }
  437. if (isCmd == 0) {
  438. log.info("end exeCmd : {}", isCmd);
  439. // 判断坐标
  440. if (sb.toString().contains("GEOGCS[\"China Geodetic Coordinate System 2000\"")) {
  441. // 需要普通坐标转换
  442. isCmd = 1000;
  443. map.put("code", isCmd);
  444. log.info("需要坐标转换code:{}, GEOGCS[\"China Geodetic Coordinate System 2000\"", isCmd);
  445. }
  446. if (sb.toString().contains("GEOGCS[\"Xian 1980\"")) {
  447. // 需要严格坐标转换
  448. isCmd = 1001;
  449. map.put("code", isCmd);
  450. log.info("需要严格坐标转换code:{}, GEOGCS[\"Xian 1980\"", isCmd);
  451. }
  452. if (sb.toString().contains("GEOGCS[\"WGS 84\"")) {
  453. // 不需要坐标转换
  454. isCmd = 0;
  455. map.put("code", isCmd);
  456. log.info("不需要坐标转换code:{}, GEOGCS[\"WGS 84\"", isCmd);
  457. }
  458. } else {
  459. map.put("code", isCmd);
  460. log.info("error exeCmd wsitFore: {}", isCmd);
  461. }
  462. return map;
  463. }
  464. /**
  465. * 这个进度输出是从getErrorStream 输出
  466. * @param commandStr
  467. * @param entity
  468. * @return
  469. */
  470. @Override
  471. public Integer cmdSlice(String commandStr, OutputFileEntity entity) {
  472. log.info("run vector cmdSlice");
  473. // 命令运行结果 1:失败, 0:成功
  474. Integer isCmd = null;
  475. try {
  476. String[] cmd = new String[]{"/bin/sh", "-c", commandStr};
  477. Process ps = Runtime.getRuntime().exec(cmd);
  478. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  479. BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream()));
  480. StringBuffer sb = new StringBuffer();
  481. StringBuffer errorStr = new StringBuffer();
  482. // error : 坑, 控制台信息是从errorBuf这里出来的
  483. String errorLine;
  484. while ((errorLine = errorBuf.readLine()) != null) {
  485. errorStr.append(errorLine).append("\n");
  486. log.info("error: {}", errorLine);
  487. // 获取进度
  488. if (errorLine.contains("%")) {
  489. String s = StringUtils.substringBefore(errorLine, "%");
  490. // 去除空白字符串
  491. s = StringUtils.deleteWhitespace(s);
  492. // 只有是数字的
  493. if (RegexUtils.regexInt(s)) {
  494. Integer round = (int) Math.round(Double.valueOf(s));
  495. // log.info("round: {}", round);
  496. if (round % 5 == 0 ) {
  497. // log.info("round1: {}", round);
  498. entity.setUpdateTime(new Date());
  499. entity.setProgress(round - 1);
  500. if (s.equals("99.9")) {
  501. entity.setProgress(100);
  502. }
  503. outputFileRepository.save(entity);
  504. }
  505. }
  506. }
  507. }
  508. if (StringUtils.isNotEmpty(errorStr)){
  509. log.info("error result: {}", errorStr.toString());
  510. }
  511. // success ,没有获取到信息
  512. String line;
  513. while ((line = br.readLine()) != null) {
  514. //执行结果加上回车
  515. sb.append(line).append("\n");
  516. log.info("data: {}", line);
  517. }
  518. log.info("result: {}", sb.toString());
  519. // 结束命令行
  520. isCmd = ps.waitFor();
  521. // 关闭流
  522. br.close();
  523. errorBuf.close();
  524. } catch (Exception e) {
  525. e.printStackTrace();
  526. }
  527. if (isCmd == 0) {
  528. log.info("end exeCmd : {}", isCmd);
  529. } else {
  530. log.info("wsitFore cmd run error : {}", isCmd);
  531. }
  532. return isCmd;
  533. }
  534. @Override
  535. public FileEntity findByInputFileId(Long fileId) {
  536. Optional<FileEntity> o = fileRepository.findById(fileId);
  537. if (o.isPresent()){
  538. FileEntity fileEntity = o.get();
  539. return fileEntity;
  540. }
  541. return null;
  542. }
  543. @Override
  544. public FileEntity saveInputFile(FileEntity fileEntity) {
  545. FileEntity save = fileRepository.save(fileEntity);
  546. return save;
  547. }
  548. /**
  549. * 获取经纬度平均值
  550. *
  551. * @param str 传入参数
  552. * @return map
  553. */
  554. private Map getExtent(String str) {
  555. str = str.replace("Extent: (", "");
  556. str = str.replaceAll("\\(", "");
  557. str = str.replaceAll("\\)", "");
  558. str = str.replaceAll(" -", ",");
  559. // 去除所有空白字符
  560. str = StringUtils.deleteWhitespace(str);
  561. String[] strArray = str.split(",");
  562. List<String> list = Arrays.asList(strArray);
  563. log.info("extent list: {}", Arrays.asList(strArray));
  564. BigDecimal b0 = new BigDecimal(list.get(0));
  565. BigDecimal b1 = new BigDecimal(list.get(1));
  566. BigDecimal b2 = new BigDecimal(list.get(2));
  567. BigDecimal b3 = new BigDecimal(list.get(3));
  568. // 经度
  569. BigDecimal longitude = b0.add(b2);
  570. longitude = longitude.divide(new BigDecimal(2), 6, BigDecimal.ROUND_HALF_UP);
  571. // 纬度
  572. BigDecimal latitude = b1.add(b3);
  573. latitude = latitude.divide(new BigDecimal(2), 6, BigDecimal.ROUND_HALF_UP);
  574. HashMap<Object, Object> map = new HashMap<>();
  575. map.put("longitude", longitude);
  576. map.put("latitude", latitude);
  577. return map;
  578. }
  579. @Override
  580. public OutputFileEntity findById(Long fileId) {
  581. Optional<OutputFileEntity> o = outputFileRepository.findById(fileId);
  582. if (o.isPresent()) {
  583. return o.get();
  584. }
  585. return null;
  586. }
  587. @Override
  588. public OutputFileEntity save(OutputFileEntity fileSchedule) {
  589. return outputFileRepository.save(fileSchedule);
  590. }
  591. /**
  592. * 发布服务
  593. * 修改config.json
  594. */
  595. // private void writeJsonFile(ConfigJsonDto param, String lastName, OutputFileEntity entity) {
  596. //
  597. // StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getId());
  598. //
  599. // String s = FileUtils.readFile(CONFIG_JSON_PATH);
  600. //
  601. // JSONObject original = JSON.parseObject(s);
  602. //
  603. // log.info("original: {}", s);
  604. //
  605. // JSONArray layers = JSON.parseArray(original.getString("layers"));
  606. //
  607. // JSONObject subJson = new JSONObject();
  608. // long cu = System.currentTimeMillis();
  609. // String name = "vector_" + cu;
  610. // // 需要唯一
  611. // subJson.put("name", name);
  612. //
  613. // subJson.put("text", param.getText());
  614. // // vector 就用这个类型geodata
  615. // subJson.put("type", "geodata");
  616. // subJson.put("checked", false);
  617. // subJson.put("show", true);
  618. // subJson.put("url", "../data/" + lastName);
  619. //// subJson.put("style", JSON.parse(styleEntity.getContent()));
  620. // layers.add(subJson);
  621. //
  622. // original.put("layers", layers);
  623. //
  624. // log.info("original update: {}", original.toJSONString());
  625. // try {
  626. // FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
  627. //
  628. // // 将图层信息保存到db
  629. // // 前端需要layer信息
  630. // styleEntity.setLayer(subJson.toJSONString());
  631. // styleEntity.setUpdateTime(new Date());
  632. // styleEntity.setName(name);
  633. //// log.info("check test 2");
  634. // styleRepository.save(styleEntity);
  635. //// log.info("check test 3");
  636. //
  637. // } catch (IOException e) {
  638. // e.printStackTrace();
  639. // }
  640. // }
  641. private void writeJsonFile(ConfigJsonDto param, String lastName, OutputFileEntity entity) {
  642. StyleEntity styleEntity = styleRepository.findByOutputFileIdTop(entity.getId());
  643. String s = FileUtils.readFile(CONFIG_JSON_PATH);
  644. JSONObject original = JSON.parseObject(s);
  645. log.info("original: {}", s);
  646. JSONArray layers = JSON.parseArray(original.getString("layers"));
  647. JSONObject subJson = new JSONObject();
  648. long cu = System.currentTimeMillis();
  649. String name = "vector_" + cu;
  650. // 需要唯一
  651. subJson.put("name", name);
  652. subJson.put("text", param.getText());
  653. // vector 就用这个类型geodata
  654. subJson.put("type", "geodata");
  655. subJson.put("checked", false);
  656. subJson.put("show", true);
  657. // subJson.put("url", "../data/" + lastName);
  658. String slicePath = entity.getSlicePath();
  659. slicePath = slicePath.replace("/root/gis/cesium", "");
  660. subJson.put("url", slicePath);
  661. layers.add(subJson);
  662. original.put("layers", layers);
  663. log.info("original update: {}", original.toJSONString());
  664. try {
  665. FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
  666. // 将图层信息保存到db
  667. // 前端需要layer信息
  668. styleEntity.setLayer(subJson.toJSONString());
  669. styleEntity.setUpdateTime(new Date());
  670. styleEntity.setName(name);
  671. styleRepository.save(styleEntity);
  672. } catch (IOException e) {
  673. e.printStackTrace();
  674. }
  675. }
  676. /**
  677. * 删除指定行的config.json 数据
  678. */
  679. private void deleteRowConfigJson(StyleEntity entity) {
  680. String s = FileUtils.readFile(CONFIG_JSON_PATH);
  681. JSONObject original = JSON.parseObject(s);
  682. log.info("original: {}", s);
  683. JSONArray layers = JSON.parseArray(original.getString("layers"));
  684. for (int i = 0; i < layers.size(); i++) {
  685. JSONObject o = (JSONObject) layers.get(i);
  686. if (o.getString("name").equals(entity.getName())) {
  687. // 删除对象
  688. layers.remove(i);
  689. }
  690. }
  691. // 更新json
  692. original.put("layers", layers);
  693. log.info("original update: {}", original.toJSONString());
  694. // 更新config.json
  695. try {
  696. FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
  697. } catch (IOException e) {
  698. e.printStackTrace();
  699. }
  700. }
  701. /**
  702. * 编辑样式,指定行的config.json 数据
  703. * 添加样式
  704. */
  705. private void editStyleConfigJson(StyleEntity entity) {
  706. String s = FileUtils.readFile(CONFIG_JSON_PATH);
  707. JSONObject original = JSON.parseObject(s);
  708. log.info("original: {}", s);
  709. JSONArray layers = JSON.parseArray(original.getString("layers"));
  710. for (int i = 0; i < layers.size(); i++) {
  711. JSONObject o = (JSONObject) layers.get(i);
  712. if (o.getString("name").equals(entity.getName())) {
  713. log.warn("old sytle: {}", o.toJSONString());
  714. //修改样式
  715. o.put("style", JSON.parse(entity.getContent()));
  716. }
  717. }
  718. log.warn("update sytle: {}", layers.toJSONString());
  719. // 更新json
  720. original.put("layers", layers);
  721. log.info("original update: {}", original.toJSONString());
  722. // 更新config.json
  723. try {
  724. FileUtils.fileWriter(JSON.toJSONString(original), CONFIG_JSON_PATH);
  725. } catch (IOException e) {
  726. e.printStackTrace();
  727. }
  728. }
  729. public static void main(String[] args) throws IOException {
  730. String inPath = "F:\\test\\clip\\aaa\\wwww";
  731. String outPath = "F:\\test\\copy\\aaa\\123.com";
  732. System.out.println(inPath.substring(0, inPath.lastIndexOf("aaa") + 3));
  733. System.out.println(outPath.substring(0, outPath.lastIndexOf("aaa") + 3));
  734. }
  735. }