package org.jeecg.modules.demo.hzz.axgh.yhdl.controller; import java.io.File; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import antlr.StringUtils; import com.alibaba.fastjson.JSONObject; import io.netty.util.internal.StringUtil; import org.geotools.data.DataStore; import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.util.CommonUtils; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.demo.hzz.axgh.yhdl.entity.RmYhdl; import org.jeecg.modules.demo.hzz.axgh.yhdl.service.IRmYhdlService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import org.jeecg.modules.demo.onemap.utils.Geotools; import org.jeecg.modules.demo.onemap.utils.PGDatastore; import org.jeecg.modules.demo.onemap.utils.Utility; import org.jeecg.modules.demo.onemap.utils.ZipAndRarTools; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.def.NormalExcelConstants; import org.jeecgframework.poi.excel.entity.ExportParams; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; import org.jeecg.common.system.base.controller.JeecgController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.jeecg.common.aspect.annotation.AutoLog; /** * @Description: rm_yhdl * @Author: jeecg-boot * @Date: 2022-01-18 * @Version: V1.0 */ @Api(tags = "rm_yhdl") @RestController @RequestMapping("/hzz.axgh.yhdl/rmYhdl") @Slf4j public class RmYhdlController extends JeecgController { @Autowired private IRmYhdlService rmYhdlService; private Utility utility = new Utility(); @Value(value = "${jeecg.path.upload}") private String uploadpath; /** * 分页列表查询 * * @param rmYhdl * @param pageNo * @param pageSize * @param req * @return */ @AutoLog(value = "rm_yhdl-分页列表查询") @ApiOperation(value = "rm_yhdl-分页列表查询", notes = "rm_yhdl-分页列表查询") @GetMapping(value = "/list") public Result queryPageList(RmYhdl rmYhdl, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(rmYhdl, req.getParameterMap()); Page page = new Page(pageNo, pageSize); IPage pageList = rmYhdlService.page(page, queryWrapper); return Result.OK(pageList); } /** * 添加 * * @param rmYhdl * @return */ @AutoLog(value = "rm_yhdl-添加") @ApiOperation(value = "rm_yhdl-添加", notes = "rm_yhdl-添加") @PostMapping(value = "/add") public Result add(@RequestBody RmYhdl rmYhdl) throws Exception { rmYhdlService.save(rmYhdl); if (StringUtil.isNullOrEmpty(rmYhdl.getGeopath())) { return Result.OK("添加成功!矢量数据未上传!"); } String msg = handleGeometry(rmYhdl.getId(), rmYhdl.getGeopath()); return Result.OK("添加成功!" + msg); } private String handleGeometry(String id, String geoPath) { //解压上传的zip包 String warnMsg = "Shp解析成功!"; if(StringUtil.isNullOrEmpty(id)||StringUtil.isNullOrEmpty(geoPath)){ warnMsg = "参数不全,无法处理!"; return warnMsg; } String upFilePath = uploadpath + "/" + geoPath; String upFileName = upFilePath.substring(upFilePath.lastIndexOf("/") + 1); String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.')); String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt; try { if(geoPath.endsWith(".zip")){ ZipAndRarTools.unZip(upFilePath, unZipPath); } else if (geoPath.endsWith(".rar")){ ZipAndRarTools.unrar(upFilePath, unZipPath); }else{ return "矢量数据格式不正确!"; } //获取解压后的shp文件及坐标系文件的路径 String shpfilepath = ""; String prjFilePath = ""; File file = new File(unZipPath); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { File shpFile = files[i]; String fileName = shpFile.getName(); if (fileName.endsWith(".shp")) { shpfilepath = unZipPath + "/" + fileName; continue; } if (fileName.endsWith(".prj")) { prjFilePath = unZipPath + "/" + fileName; continue; } } //创建数据库连接 PGDatastore pgDatastore = new PGDatastore(); DataStore datastore = pgDatastore.getDefeaultDatastore(); Geotools geotools = new Geotools(datastore); //根据坐标系文件,判断上传的矢量是否需要转换坐标 int prjType = 4490; if (StringUtil.isNullOrEmpty(prjFilePath)) { warnMsg = "上传的SHP文件没有坐标信息(*.prj),默认按照2000大地坐标处理!"; } else { String prjWkt = geotools.getShpPrjWkt(prjFilePath); if (!prjWkt.contains("CGCS2000")) { warnMsg = "上传的SHP文件坐标系不是2000国家大地坐标系,无法处理!"; return warnMsg; } if (prjWkt.startsWith("PROJCS")) { if (prjWkt.contains("Zone_39")) { prjType = 4527; } else if (prjWkt.contains("CM_117E")) { prjType = 4548; } else { prjType = 4490; } } } //将shp文件解析为wkt,然后根据坐标系情况,转为4490,然后更新geom String shpWkt = geotools.shp2wkt(shpfilepath); if (!StringUtil.isNullOrEmpty(shpWkt)) { if (shpWkt.startsWith("MULTILINESTRING")) { shpWkt = shpWkt.replace("MULTILINESTRING", "LineString"); shpWkt = shpWkt.replace("((", "("); shpWkt = shpWkt.replace("))", ")"); } switch (prjType) { case 4490: { rmYhdlService.updYhdl3(shpWkt, id); break; } case 4527: { rmYhdlService.updYhdl2(shpWkt, id); break; } case 4548: { rmYhdlService.updYhdl(shpWkt, id); break; } default: break; } } else { warnMsg = "上传的SHP文件没有解析到空间图形信息!"; return warnMsg; } //将生成的geom转为geojson,保存到geoinfo字段中 rmYhdlService.updGeoinfo(id); } catch (IOException e) { e.printStackTrace(); warnMsg = e.getMessage(); return warnMsg; } catch (Exception e) { e.printStackTrace(); warnMsg = e.getMessage(); return warnMsg; } return warnMsg; } /** * 编辑 * * @param rmYhdl * @return */ @AutoLog(value = "rm_yhdl-编辑") @ApiOperation(value = "rm_yhdl-编辑", notes = "rm_yhdl-编辑") @PostMapping(value = "/edit") public Result edit(@RequestBody RmYhdl rmYhdl) throws Exception { String id = rmYhdl.getId(); RmYhdl oldRmYhdl = rmYhdlService.getById(id); String oldGeoPath = oldRmYhdl.getGeopath(); String newGeoPath = rmYhdl.getGeopath(); if (!oldGeoPath.equals(newGeoPath)) { //1、存在旧的文件,删除原来上传的shp文件 if(!StringUtil.isNullOrEmpty(oldGeoPath)){ delShpPath(oldGeoPath); }else{ rmYhdl.setGeoinfo(""); rmYhdl.setGeom(null); } //2、更新geom和geoinfo if(StringUtil.isNullOrEmpty(newGeoPath)){ rmYhdl.setGeoinfo(""); rmYhdl.setGeom(null); } rmYhdlService.updateById(rmYhdl); handleGeometry(id, newGeoPath); } else { rmYhdlService.updateById(rmYhdl); } return Result.OK("编辑成功!"); } /** * 根据shp上传的文件名,删除压缩文件、解压后的文件夹及文件 */ private void delShpPath(String shpPath) { String upFilePath = uploadpath + "/" + shpPath; if (!StringUtil.isNullOrEmpty(upFilePath)) { File zipFile = new File(upFilePath); zipFile.delete(); } String upFileName = upFilePath.substring(upFilePath.lastIndexOf("/") + 1); String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.')); String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt; File oldGeoFolder = new File(unZipPath); Utility.deleteDir(oldGeoFolder); } /** * 通过id删除 * * @param id * @return */ @AutoLog(value = "rm_yhdl-通过id删除") @ApiOperation(value = "rm_yhdl-通过id删除", notes = "rm_yhdl-通过id删除") @PostMapping(value = "/delete") public Result delete(@RequestParam(name = "id", required = true) String id) throws Exception { RmYhdl rmYhdl = rmYhdlService.getById(id); if (rmYhdl == null) { return Result.error("未找到对应数据,删除失败!"); } String geoPath = rmYhdl.getGeopath(); if (!StringUtil.isNullOrEmpty(geoPath)) { delShpPath(geoPath); } rmYhdlService.removeById(id); return Result.OK("删除成功!"); } /** * 批量删除 * * @param ids * @return */ @AutoLog(value = "rm_yhdl-批量删除") @ApiOperation(value = "rm_yhdl-批量删除", notes = "rm_yhdl-批量删除") @PostMapping(value = "/deleteBatch") public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) throws Exception { // this.rmYhdlService.removeByIds(Arrays.asList(ids.split(","))); List yhdlList = Arrays.asList(ids.split(",")); for (int i = 0; i < yhdlList.size(); i++) { delete(yhdlList.get(i)); } return Result.OK("批量删除成功!"); } /** * 通过id查询 * * @param id * @return */ @AutoLog(value = "rm_yhdl-通过id查询") @ApiOperation(value = "rm_yhdl-通过id查询", notes = "rm_yhdl-通过id查询") @GetMapping(value = "/queryById") public Result queryById(@RequestParam(name = "id", required = true) String id) { RmYhdl rmYhdl = rmYhdlService.getById(id); if (rmYhdl == null) { return Result.error("未找到对应数据"); } return Result.OK(rmYhdl); } /** * 导出excel * * @param request * @param rmYhdl */ @RequestMapping(value = "/exportXls") public ModelAndView exportXls(HttpServletRequest request, RmYhdl rmYhdl) { return super.exportXls(request, rmYhdl, RmYhdl.class, "rm_yhdl"); } /** * 通过excel导入数据 * * @param request * @param response * @return */ @RequestMapping(value = "/importExcel", method = RequestMethod.POST) public Result importExcel(HttpServletRequest request, HttpServletResponse response) { return super.importExcel(request, response, RmYhdl.class); } /** * 通过shapefile导入数据 * * @param request * @param response * @return */ @RequestMapping(value = "/importShapefile", method = RequestMethod.POST) public Result importShapefile(HttpServletRequest request, HttpServletResponse response) { Result result = new Result<>(); String warnMsg = null; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("shp");// 获取上传文件对象(“shp”是前端设置的文件对象名) String fileSavePath = uploadLocal(file, "hzz"); // if (!StringUtil.isNullOrEmpty(fileSavePath)) { try { //1、解压上传的zip文件 String upFileName = fileSavePath.substring(fileSavePath.lastIndexOf("/") + 1); String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.')); String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt; ZipAndRarTools.unZip(fileSavePath, unZipPath); //2、获取shp文件及坐标系文件的路径 String shpfilepath = ""; String prjFilePath = ""; File shpdir = new File(unZipPath); File[] files = shpdir.listFiles(); for (int i = 0; i < files.length; i++) { File shpFile = files[i]; String fileName = shpFile.getName(); if (fileName.endsWith(".shp")) { shpfilepath = unZipPath + "/" + fileName; continue; } if (fileName.endsWith(".prj")) { prjFilePath = unZipPath + "/" + fileName; continue; } } //创建数据库连接 PGDatastore pgDatastore = new PGDatastore(); DataStore datastore = pgDatastore.getDefeaultDatastore(); Geotools geotools = new Geotools(datastore); //根据坐标系文件,判断上传的矢量是否需要转换坐标 int prjType = 4490; if (StringUtil.isNullOrEmpty(prjFilePath)) { warnMsg = "上传的SHP文件没有坐标信息(*.prj),默认按照2000大地坐标处理!"; } else { String prjWkt = geotools.getShpPrjWkt(prjFilePath); if (!prjWkt.contains("CGCS2000")) { warnMsg = "上传的SHP文件坐标系不是2000国家大地坐标系,无法处理!"; result.setMessage(warnMsg); result.setSuccess(false); return result; } if (prjWkt.startsWith("PROJCS")) { if (prjWkt.contains("Zone_39")) { prjType = 4527; } else if (prjWkt.contains("CM_117E")) { prjType = 4548; } else { prjType = 4490; } } } //3、解析shp文件,读取属性和the_geom //4、wkt转存为geom //5、geom转geojson并赋值给geoinfo List> featureList = geotools.readShpFile(shpfilepath); if (featureList.size() == 0) { result.setMessage("上传的文件中没有解析到有效要素!"); result.setSuccess(true); return result; } for (int i = 0; i < featureList.size(); i++) { RmYhdl rmYhdl = new RmYhdl(); String wktStr = null; Map feature = featureList.get(i); for (String key : feature.keySet()) { if ("the_geom".equals(key.toLowerCase())) { wktStr = feature.get(key).toString(); } if ("hlmc".equals(key.toLowerCase())) { rmYhdl.setHlmc(feature.get(key).toString()); } if ("hlbm".equals(key.toLowerCase())) { rmYhdl.setHlbm(feature.get(key).toString()); } if ("ab".equals(key.toLowerCase())) { rmYhdl.setAb(feature.get(key).toString()); } if ("dlmc".equals(key.toLowerCase())) { rmYhdl.setDlmc(feature.get(key).toString()); } if ("dljb".equals(key.toLowerCase())) { rmYhdl.setDljb(feature.get(key).toString()); } if ("dlcd".equals(key.toLowerCase())) { rmYhdl.setDlcd(Double.parseDouble(feature.get(key).toString())); } if ("dlkd".equals(key.toLowerCase())) { rmYhdl.setDlkd(Double.parseDouble(feature.get(key).toString())); } } rmYhdlService.save(rmYhdl); String id = rmYhdl.getId(); if (!StringUtil.isNullOrEmpty(wktStr)) { if (wktStr.startsWith("MULTILINESTRING")) { wktStr = wktStr.replace("MULTILINESTRING", "LineString"); wktStr = wktStr.replace("((", "("); wktStr = wktStr.replace("))", ")"); } switch (prjType) { case 4490: { rmYhdlService.updYhdl3(wktStr, id); break; } case 4527: { rmYhdlService.updYhdl2(wktStr, id); break; } case 4548: { rmYhdlService.updYhdl(wktStr, id); break; } default: break; } } rmYhdlService.updGeoinfo(id); } result.setMessage("文件上传成功!"); result.setSuccess(true); } catch (Exception e) { result.setMessage("上传文件出现异常:" + e.getMessage()); result.setSuccess(false); return result; } } return result; } /** * 本地文件上传 * * @param mf 文件 * @param bizPath 自定义路径 * @return */ private String uploadLocal(MultipartFile mf, String bizPath) { try { String ctxPath = uploadpath; String fileName = null; File file = new File(ctxPath + File.separator + bizPath + File.separator); if (!file.exists()) { file.mkdirs();// 创建文件根目录 } String orgName = mf.getOriginalFilename();// 获取文件名 orgName = CommonUtils.getFileName(orgName); if (orgName.indexOf(".") != -1) { fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf(".")); } else { fileName = orgName + "_" + System.currentTimeMillis(); } String savePath = file.getPath() + File.separator + fileName; File savefile = new File(savePath); FileCopyUtils.copy(mf.getBytes(), savefile); if(savePath.contains("\\")){ savePath = savePath.replace("\\", "/"); } return savePath; } catch (IOException e) { log.error(e.getMessage(), e); } return ""; } }