VectorServerImpl.java 25 KB

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