2980adba557bf10fd22d74154b46f0e985262ae1.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. package org.jeecg.modules.demo.hzz.axgh.yhdl.controller;
  2. import java.io.File;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.stream.Collectors;
  7. import java.io.IOException;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.URLDecoder;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import antlr.StringUtils;
  13. import com.alibaba.fastjson.JSONObject;
  14. import io.netty.util.internal.StringUtil;
  15. import org.geotools.data.DataStore;
  16. import org.jeecg.common.api.vo.Result;
  17. import org.jeecg.common.system.query.QueryGenerator;
  18. import org.jeecg.common.util.CommonUtils;
  19. import org.jeecg.common.util.oConvertUtils;
  20. import org.jeecg.modules.demo.hzz.axgh.yhdl.entity.RmYhdl;
  21. import org.jeecg.modules.demo.hzz.axgh.yhdl.service.IRmYhdlService;
  22. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  23. import com.baomidou.mybatisplus.core.metadata.IPage;
  24. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.jeecg.modules.demo.onemap.utils.Geotools;
  27. import org.jeecg.modules.demo.onemap.utils.PGDatastore;
  28. import org.jeecg.modules.demo.onemap.utils.Utility;
  29. import org.jeecg.modules.demo.onemap.utils.ZipAndRarTools;
  30. import org.jeecgframework.poi.excel.ExcelImportUtil;
  31. import org.jeecgframework.poi.excel.def.NormalExcelConstants;
  32. import org.jeecgframework.poi.excel.entity.ExportParams;
  33. import org.jeecgframework.poi.excel.entity.ImportParams;
  34. import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
  35. import org.jeecg.common.system.base.controller.JeecgController;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.beans.factory.annotation.Value;
  38. import org.springframework.util.FileCopyUtils;
  39. import org.springframework.web.bind.annotation.*;
  40. import org.springframework.web.multipart.MultipartFile;
  41. import org.springframework.web.multipart.MultipartHttpServletRequest;
  42. import org.springframework.web.servlet.ModelAndView;
  43. import com.alibaba.fastjson.JSON;
  44. import io.swagger.annotations.Api;
  45. import io.swagger.annotations.ApiOperation;
  46. import org.jeecg.common.aspect.annotation.AutoLog;
  47. /**
  48. * @Description: rm_yhdl
  49. * @Author: jeecg-boot
  50. * @Date: 2022-01-18
  51. * @Version: V1.0
  52. */
  53. @Api(tags = "rm_yhdl")
  54. @RestController
  55. @RequestMapping("/hzz.axgh.yhdl/rmYhdl")
  56. @Slf4j
  57. public class RmYhdlController extends JeecgController<RmYhdl, IRmYhdlService> {
  58. @Autowired
  59. private IRmYhdlService rmYhdlService;
  60. private Utility utility = new Utility();
  61. @Value(value = "${jeecg.path.upload}")
  62. private String uploadpath;
  63. /**
  64. * 分页列表查询
  65. *
  66. * @param rmYhdl
  67. * @param pageNo
  68. * @param pageSize
  69. * @param req
  70. * @return
  71. */
  72. @AutoLog(value = "rm_yhdl-分页列表查询")
  73. @ApiOperation(value = "rm_yhdl-分页列表查询", notes = "rm_yhdl-分页列表查询")
  74. @GetMapping(value = "/list")
  75. public Result<?> queryPageList(RmYhdl rmYhdl,
  76. @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
  77. @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  78. HttpServletRequest req) {
  79. QueryWrapper<RmYhdl> queryWrapper = QueryGenerator.initQueryWrapper(rmYhdl, req.getParameterMap());
  80. Page<RmYhdl> page = new Page<RmYhdl>(pageNo, pageSize);
  81. IPage<RmYhdl> pageList = rmYhdlService.page(page, queryWrapper);
  82. return Result.OK(pageList);
  83. }
  84. /**
  85. * 添加
  86. *
  87. * @param rmYhdl
  88. * @return
  89. */
  90. @AutoLog(value = "rm_yhdl-添加")
  91. @ApiOperation(value = "rm_yhdl-添加", notes = "rm_yhdl-添加")
  92. @PostMapping(value = "/add")
  93. public Result<?> add(@RequestBody RmYhdl rmYhdl) throws Exception {
  94. rmYhdlService.save(rmYhdl);
  95. if (StringUtil.isNullOrEmpty(rmYhdl.getGeopath())) {
  96. return Result.OK("添加成功!矢量数据未上传!");
  97. }
  98. String msg = handleGeometry(rmYhdl.getId(), rmYhdl.getGeopath());
  99. return Result.OK("添加成功!" + msg);
  100. }
  101. private String handleGeometry(String id, String geoPath) {
  102. //解压上传的zip包
  103. String warnMsg = "Shp解析成功!";
  104. if(StringUtil.isNullOrEmpty(id)||StringUtil.isNullOrEmpty(geoPath)){
  105. warnMsg = "参数不全,无法处理!";
  106. return warnMsg;
  107. }
  108. String upFilePath = uploadpath + "/" + geoPath;
  109. String upFileName = upFilePath.substring(upFilePath.lastIndexOf("/") + 1);
  110. String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.'));
  111. String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt;
  112. try {
  113. if(geoPath.endsWith(".zip")){
  114. ZipAndRarTools.unZip(upFilePath, unZipPath);
  115. } else if (geoPath.endsWith(".rar")){
  116. ZipAndRarTools.unrar(upFilePath, unZipPath);
  117. }else{
  118. return "矢量数据格式不正确!";
  119. }
  120. //获取解压后的shp文件及坐标系文件的路径
  121. String shpfilepath = "";
  122. String prjFilePath = "";
  123. File file = new File(unZipPath);
  124. File[] files = file.listFiles();
  125. for (int i = 0; i < files.length; i++) {
  126. File shpFile = files[i];
  127. String fileName = shpFile.getName();
  128. if (fileName.endsWith(".shp")) {
  129. shpfilepath = unZipPath + "/" + fileName;
  130. continue;
  131. }
  132. if (fileName.endsWith(".prj")) {
  133. prjFilePath = unZipPath + "/" + fileName;
  134. continue;
  135. }
  136. }
  137. //创建数据库连接
  138. PGDatastore pgDatastore = new PGDatastore();
  139. DataStore datastore = pgDatastore.getDefeaultDatastore();
  140. Geotools geotools = new Geotools(datastore);
  141. //根据坐标系文件,判断上传的矢量是否需要转换坐标
  142. int prjType = 4490;
  143. if (StringUtil.isNullOrEmpty(prjFilePath)) {
  144. warnMsg = "上传的SHP文件没有坐标信息(*.prj),默认按照2000大地坐标处理!";
  145. } else {
  146. String prjWkt = geotools.getShpPrjWkt(prjFilePath);
  147. if (!prjWkt.contains("CGCS2000")) {
  148. warnMsg = "上传的SHP文件坐标系不是2000国家大地坐标系,无法处理!";
  149. return warnMsg;
  150. }
  151. if (prjWkt.startsWith("PROJCS")) {
  152. if (prjWkt.contains("Zone_39")) {
  153. prjType = 4527;
  154. } else if (prjWkt.contains("CM_117E")) {
  155. prjType = 4548;
  156. } else {
  157. prjType = 4490;
  158. }
  159. }
  160. }
  161. //将shp文件解析为wkt,然后根据坐标系情况,转为4490,然后更新geom
  162. String shpWkt = geotools.shp2wkt(shpfilepath);
  163. if (!StringUtil.isNullOrEmpty(shpWkt)) {
  164. if (shpWkt.startsWith("MULTILINESTRING")) {
  165. shpWkt = shpWkt.replace("MULTILINESTRING", "LineString");
  166. shpWkt = shpWkt.replace("((", "(");
  167. shpWkt = shpWkt.replace("))", ")");
  168. }
  169. switch (prjType) {
  170. case 4490: {
  171. rmYhdlService.updYhdl3(shpWkt, id);
  172. break;
  173. }
  174. case 4527: {
  175. rmYhdlService.updYhdl2(shpWkt, id);
  176. break;
  177. }
  178. case 4548: {
  179. rmYhdlService.updYhdl(shpWkt, id);
  180. break;
  181. }
  182. default:
  183. break;
  184. }
  185. } else {
  186. warnMsg = "上传的SHP文件没有解析到空间图形信息!";
  187. return warnMsg;
  188. }
  189. //将生成的geom转为geojson,保存到geoinfo字段中
  190. rmYhdlService.updGeoinfo(id);
  191. } catch (IOException e) {
  192. e.printStackTrace();
  193. warnMsg = e.getMessage();
  194. return warnMsg;
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. warnMsg = e.getMessage();
  198. return warnMsg;
  199. }
  200. return warnMsg;
  201. }
  202. /**
  203. * 编辑
  204. *
  205. * @param rmYhdl
  206. * @return
  207. */
  208. @AutoLog(value = "rm_yhdl-编辑")
  209. @ApiOperation(value = "rm_yhdl-编辑", notes = "rm_yhdl-编辑")
  210. @PostMapping(value = "/edit")
  211. public Result<?> edit(@RequestBody RmYhdl rmYhdl) throws Exception {
  212. String id = rmYhdl.getId();
  213. RmYhdl oldRmYhdl = rmYhdlService.getById(id);
  214. String oldGeoPath = oldRmYhdl.getGeopath();
  215. String newGeoPath = rmYhdl.getGeopath();
  216. if (!oldGeoPath.equals(newGeoPath)) {
  217. //1、存在旧的文件,删除原来上传的shp文件
  218. if(!StringUtil.isNullOrEmpty(oldGeoPath)){
  219. delShpPath(oldGeoPath);
  220. }else{
  221. rmYhdl.setGeoinfo("");
  222. rmYhdl.setGeom(null);
  223. }
  224. //2、更新geom和geoinfo
  225. if(StringUtil.isNullOrEmpty(newGeoPath)){
  226. rmYhdl.setGeoinfo("");
  227. rmYhdl.setGeom(null);
  228. }
  229. rmYhdlService.updateById(rmYhdl);
  230. handleGeometry(id, newGeoPath);
  231. } else {
  232. rmYhdlService.updateById(rmYhdl);
  233. }
  234. return Result.OK("编辑成功!");
  235. }
  236. /**
  237. * 根据shp上传的文件名,删除压缩文件、解压后的文件夹及文件
  238. */
  239. private void delShpPath(String shpPath) {
  240. String upFilePath = uploadpath + "/" + shpPath;
  241. if (!StringUtil.isNullOrEmpty(upFilePath)) {
  242. File zipFile = new File(upFilePath);
  243. zipFile.delete();
  244. }
  245. String upFileName = upFilePath.substring(upFilePath.lastIndexOf("/") + 1);
  246. String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.'));
  247. String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt;
  248. File oldGeoFolder = new File(unZipPath);
  249. Utility.deleteDir(oldGeoFolder);
  250. }
  251. /**
  252. * 通过id删除
  253. *
  254. * @param id
  255. * @return
  256. */
  257. @AutoLog(value = "rm_yhdl-通过id删除")
  258. @ApiOperation(value = "rm_yhdl-通过id删除", notes = "rm_yhdl-通过id删除")
  259. @PostMapping(value = "/delete")
  260. public Result<?> delete(@RequestParam(name = "id", required = true) String id) throws Exception {
  261. RmYhdl rmYhdl = rmYhdlService.getById(id);
  262. if (rmYhdl == null) {
  263. return Result.error("未找到对应数据,删除失败!");
  264. }
  265. String geoPath = rmYhdl.getGeopath();
  266. if (!StringUtil.isNullOrEmpty(geoPath)) {
  267. delShpPath(geoPath);
  268. }
  269. rmYhdlService.removeById(id);
  270. return Result.OK("删除成功!");
  271. }
  272. /**
  273. * 批量删除
  274. *
  275. * @param ids
  276. * @return
  277. */
  278. @AutoLog(value = "rm_yhdl-批量删除")
  279. @ApiOperation(value = "rm_yhdl-批量删除", notes = "rm_yhdl-批量删除")
  280. @PostMapping(value = "/deleteBatch")
  281. public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) throws Exception {
  282. // this.rmYhdlService.removeByIds(Arrays.asList(ids.split(",")));
  283. List<String> yhdlList = Arrays.asList(ids.split(","));
  284. for (int i = 0; i < yhdlList.size(); i++) {
  285. delete(yhdlList.get(i));
  286. }
  287. return Result.OK("批量删除成功!");
  288. }
  289. /**
  290. * 通过id查询
  291. *
  292. * @param id
  293. * @return
  294. */
  295. @AutoLog(value = "rm_yhdl-通过id查询")
  296. @ApiOperation(value = "rm_yhdl-通过id查询", notes = "rm_yhdl-通过id查询")
  297. @GetMapping(value = "/queryById")
  298. public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
  299. RmYhdl rmYhdl = rmYhdlService.getById(id);
  300. if (rmYhdl == null) {
  301. return Result.error("未找到对应数据");
  302. }
  303. return Result.OK(rmYhdl);
  304. }
  305. /**
  306. * 导出excel
  307. *
  308. * @param request
  309. * @param rmYhdl
  310. */
  311. @RequestMapping(value = "/exportXls")
  312. public ModelAndView exportXls(HttpServletRequest request, RmYhdl rmYhdl) {
  313. return super.exportXls(request, rmYhdl, RmYhdl.class, "rm_yhdl");
  314. }
  315. /**
  316. * 通过excel导入数据
  317. *
  318. * @param request
  319. * @param response
  320. * @return
  321. */
  322. @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  323. public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
  324. return super.importExcel(request, response, RmYhdl.class);
  325. }
  326. /**
  327. * 通过shapefile导入数据
  328. *
  329. * @param request
  330. * @param response
  331. * @return
  332. */
  333. @RequestMapping(value = "/importShapefile", method = RequestMethod.POST)
  334. public Result<?> importShapefile(HttpServletRequest request, HttpServletResponse response) {
  335. Result<?> result = new Result<>();
  336. String warnMsg = null;
  337. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  338. MultipartFile file = multipartRequest.getFile("shp");// 获取上传文件对象(“shp”是前端设置的文件对象名)
  339. String fileSavePath = uploadLocal(file, "hzz"); //
  340. if (!StringUtil.isNullOrEmpty(fileSavePath)) {
  341. try {
  342. //1、解压上传的zip文件
  343. String upFileName = fileSavePath.substring(fileSavePath.lastIndexOf("/") + 1);
  344. String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.'));
  345. String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt;
  346. ZipAndRarTools.unZip(fileSavePath, unZipPath);
  347. //2、获取shp文件及坐标系文件的路径
  348. String shpfilepath = "";
  349. String prjFilePath = "";
  350. File shpdir = new File(unZipPath);
  351. File[] files = shpdir.listFiles();
  352. for (int i = 0; i < files.length; i++) {
  353. File shpFile = files[i];
  354. String fileName = shpFile.getName();
  355. if (fileName.endsWith(".shp")) {
  356. shpfilepath = unZipPath + "/" + fileName;
  357. continue;
  358. }
  359. if (fileName.endsWith(".prj")) {
  360. prjFilePath = unZipPath + "/" + fileName;
  361. continue;
  362. }
  363. }
  364. //创建数据库连接
  365. PGDatastore pgDatastore = new PGDatastore();
  366. DataStore datastore = pgDatastore.getDefeaultDatastore();
  367. Geotools geotools = new Geotools(datastore);
  368. //根据坐标系文件,判断上传的矢量是否需要转换坐标
  369. int prjType = 4490;
  370. if (StringUtil.isNullOrEmpty(prjFilePath)) {
  371. warnMsg = "上传的SHP文件没有坐标信息(*.prj),默认按照2000大地坐标处理!";
  372. } else {
  373. String prjWkt = geotools.getShpPrjWkt(prjFilePath);
  374. if (!prjWkt.contains("CGCS2000")) {
  375. warnMsg = "上传的SHP文件坐标系不是2000国家大地坐标系,无法处理!";
  376. result.setMessage(warnMsg);
  377. result.setSuccess(false);
  378. return result;
  379. }
  380. if (prjWkt.startsWith("PROJCS")) {
  381. if (prjWkt.contains("Zone_39")) {
  382. prjType = 4527;
  383. } else if (prjWkt.contains("CM_117E")) {
  384. prjType = 4548;
  385. } else {
  386. prjType = 4490;
  387. }
  388. }
  389. }
  390. //3、解析shp文件,读取属性和the_geom
  391. //4、wkt转存为geom
  392. //5、geom转geojson并赋值给geoinfo
  393. List<Map<String, Object>> featureList = geotools.readShpFile(shpfilepath);
  394. if (featureList.size() == 0) {
  395. result.setMessage("上传的文件中没有解析到有效要素!");
  396. result.setSuccess(true);
  397. return result;
  398. }
  399. for (int i = 0; i < featureList.size(); i++) {
  400. RmYhdl rmYhdl = new RmYhdl();
  401. String wktStr = null;
  402. Map<String, Object> feature = featureList.get(i);
  403. for (String key : feature.keySet()) {
  404. if ("the_geom".equals(key.toLowerCase())) {
  405. wktStr = feature.get(key).toString();
  406. }
  407. if ("hlmc".equals(key.toLowerCase())) {
  408. rmYhdl.setHlmc(feature.get(key).toString());
  409. }
  410. if ("hlbm".equals(key.toLowerCase())) {
  411. rmYhdl.setHlbm(feature.get(key).toString());
  412. }
  413. if ("ab".equals(key.toLowerCase())) {
  414. rmYhdl.setAb(feature.get(key).toString());
  415. }
  416. if ("dlmc".equals(key.toLowerCase())) {
  417. rmYhdl.setDlmc(feature.get(key).toString());
  418. }
  419. if ("dljb".equals(key.toLowerCase())) {
  420. rmYhdl.setDljb(feature.get(key).toString());
  421. }
  422. if ("dlcd".equals(key.toLowerCase())) {
  423. rmYhdl.setDlcd(Double.parseDouble(feature.get(key).toString()));
  424. }
  425. if ("dlkd".equals(key.toLowerCase())) {
  426. rmYhdl.setDlkd(Double.parseDouble(feature.get(key).toString()));
  427. }
  428. }
  429. rmYhdlService.save(rmYhdl);
  430. String id = rmYhdl.getId();
  431. if (!StringUtil.isNullOrEmpty(wktStr)) {
  432. if (wktStr.startsWith("MULTILINESTRING")) {
  433. wktStr = wktStr.replace("MULTILINESTRING", "LineString");
  434. wktStr = wktStr.replace("((", "(");
  435. wktStr = wktStr.replace("))", ")");
  436. }
  437. switch (prjType) {
  438. case 4490: {
  439. rmYhdlService.updYhdl3(wktStr, id);
  440. break;
  441. }
  442. case 4527: {
  443. rmYhdlService.updYhdl2(wktStr, id);
  444. break;
  445. }
  446. case 4548: {
  447. rmYhdlService.updYhdl(wktStr, id);
  448. break;
  449. }
  450. default:
  451. break;
  452. }
  453. }
  454. rmYhdlService.updGeoinfo(id);
  455. }
  456. result.setMessage("文件上传成功!");
  457. result.setSuccess(true);
  458. } catch (Exception e) {
  459. result.setMessage("上传文件出现异常:" + e.getMessage());
  460. result.setSuccess(false);
  461. return result;
  462. }
  463. }
  464. return result;
  465. }
  466. /**
  467. * 本地文件上传
  468. *
  469. * @param mf 文件
  470. * @param bizPath 自定义路径
  471. * @return
  472. */
  473. private String uploadLocal(MultipartFile mf, String bizPath) {
  474. try {
  475. String ctxPath = uploadpath;
  476. String fileName = null;
  477. File file = new File(ctxPath + File.separator + bizPath + File.separator);
  478. if (!file.exists()) {
  479. file.mkdirs();// 创建文件根目录
  480. }
  481. String orgName = mf.getOriginalFilename();// 获取文件名
  482. orgName = CommonUtils.getFileName(orgName);
  483. if (orgName.indexOf(".") != -1) {
  484. fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
  485. } else {
  486. fileName = orgName + "_" + System.currentTimeMillis();
  487. }
  488. String savePath = file.getPath() + File.separator + fileName;
  489. File savefile = new File(savePath);
  490. FileCopyUtils.copy(mf.getBytes(), savefile);
  491. if(savePath.contains("\\")){
  492. savePath = savePath.replace("\\", "/");
  493. }
  494. return savePath;
  495. } catch (IOException e) {
  496. log.error(e.getMessage(), e);
  497. }
  498. return "";
  499. }
  500. }