VectorServerImpl.java 25 KB

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