276f7bed16c3c9adb16760f985363380644d869d.svn-base 16 KB

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