package org.jeecg.modules.demo.hzz.axgh.axlh.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 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.axlh.entity.RmAxlh; import org.jeecg.modules.demo.hzz.axgh.axlh.service.IRmAxlhService; 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.hzz.axgh.yhdl.entity.RmYhdl; import org.jeecg.modules.demo.hzz.axgh.yhdl.service.IRmYhdlService; 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_axlh * @Author: jeecg-boot * @Date: 2022-01-19 * @Version: V1.0 */ @Api(tags="rm_axlh") @RestController @RequestMapping("/hzz.axgh.axlh/rmAxlh") @Slf4j public class RmAxlhController extends JeecgController { @Autowired private IRmAxlhService rmAxlhService; private Utility utility = new Utility(); @Value(value = "${jeecg.path.upload}") private String uploadpath; /** * 分页列表查询 * * @param rmAxlh * @param pageNo * @param pageSize * @param req * @return */ @AutoLog(value = "rm_axlh-分页列表查询") @ApiOperation(value="rm_axlh-分页列表查询", notes="rm_axlh-分页列表查询") @GetMapping(value = "/list") public Result queryPageList(RmAxlh rmAxlh, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(rmAxlh, req.getParameterMap()); Page page = new Page(pageNo, pageSize); IPage pageList = rmAxlhService.page(page, queryWrapper); return Result.OK(pageList); } /** * 添加 * * @param rmAxlh * @return */ @AutoLog(value = "rm_axlh-添加") @ApiOperation(value="rm_axlh-添加", notes="rm_axlh-添加") @PostMapping(value = "/add") public Result add(@RequestBody RmAxlh rmAxlh) { rmAxlhService.save(rmAxlh); if (StringUtil.isNullOrEmpty(rmAxlh.getGeopath())) { return Result.OK("添加成功!矢量数据未上传!"); } String msg = handleGeometry(rmAxlh.getId(),rmAxlh.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 { ZipAndRarTools.unZip(upFilePath, unZipPath); //获取解压后的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 (Utility.isEmpty(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; } } } //将审批文件解析为wkt,然后根据坐标系情况,转为4490,然后更新geom String shpWkt = geotools.shp2wkt(shpfilepath); if (!Utility.isEmpty(shpWkt)) { if (shpWkt.startsWith("MULTILINESTRING")) { shpWkt = shpWkt.replace("MULTILINESTRING", "LineString"); shpWkt = shpWkt.replace("((", "("); shpWkt = shpWkt.replace("))", ")"); } switch (prjType) { case 4490: { rmAxlhService.updAxlh3(shpWkt, id); break; } case 4527: { rmAxlhService.updAxlh2(shpWkt, id); break; } case 4548: { rmAxlhService.updAxlh(shpWkt, id); break; } default: break; } } else { warnMsg = "上传的SHP文件没有解析到空间图形信息!"; return warnMsg; } //将生成的geom转为geojson,保存到geoinfo字段中 rmAxlhService.updGeoinfo(id); } catch (IOException e) { e.printStackTrace(); warnMsg = e.getMessage(); return warnMsg; } return warnMsg; } /** * 编辑 * * @param rmAxlh * @return */ @AutoLog(value = "rm_axlh-编辑") @ApiOperation(value="rm_axlh-编辑", notes="rm_axlh-编辑") @PostMapping(value = "/edit") public Result edit(@RequestBody RmAxlh rmAxlh) throws Exception { String id = rmAxlh.getId(); RmAxlh oldrmAxlh = rmAxlhService.getById(id); String oldGeoPath = oldrmAxlh.getGeopath(); String newGeoPath = rmAxlh.getGeopath(); if(!oldGeoPath.equals(newGeoPath)){ //1、存在旧的文件,删除原来上传的shp文件 if(!StringUtil.isNullOrEmpty(oldGeoPath)){ delShpPath(oldGeoPath); }else{ rmAxlh.setGeoinfo(""); rmAxlh.setGeom(null); } //2、更新geom和geoinfo if(StringUtil.isNullOrEmpty(newGeoPath)){ rmAxlh.setGeoinfo(""); rmAxlh.setGeom(null); } rmAxlhService.updateById(rmAxlh); handleGeometry(id,newGeoPath); }else{ rmAxlhService.updateById(rmAxlh); } 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_axlh-通过id删除") @ApiOperation(value="rm_axlh-通过id删除", notes="rm_axlh-通过id删除") @PostMapping(value = "/delete") public Result delete(@RequestParam(name="id",required=true) String id) { RmAxlh rmAxlh= rmAxlhService.getById(id); if (rmAxlh == null) { return Result.error("未找到对应数据,删除失败!"); } String geoPath = rmAxlh.getGeopath(); if(!StringUtil.isNullOrEmpty(geoPath)){ delShpPath(geoPath); } rmAxlhService.removeById(id); return Result.OK("删除成功!"); } /** * 批量删除 * * @param ids * @return */ @AutoLog(value = "rm_axlh-批量删除") @ApiOperation(value="rm_axlh-批量删除", notes="rm_axlh-批量删除") @PostMapping(value = "/deleteBatch") public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) { List strings = Arrays.asList(ids.split(",")); for(int i=0;i queryById(@RequestParam(name="id",required=true) String id) { RmAxlh rmAxlh = rmAxlhService.getById(id); if(rmAxlh==null) { return Result.error("未找到对应数据"); } return Result.OK(rmAxlh); } /** * 导出excel * * @param request * @param rmAxlh */ @RequestMapping(value = "/exportXls") public ModelAndView exportXls(HttpServletRequest request, RmAxlh rmAxlh) { return super.exportXls(request, rmAxlh, RmAxlh.class, "rm_axlh"); } /** * 通过excel导入数据 * * @param request * @param response * @return */ @RequestMapping(value = "/importExcel", method = RequestMethod.POST) public Result importExcel(HttpServletRequest request, HttpServletResponse response) { return super.importExcel(request, response, RmAxlh.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++) { RmAxlh rmAxlh = new RmAxlh(); 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())) { rmAxlh.setHlmc(feature.get(key).toString()); } if ("hlbm".equals(key.toLowerCase())) { rmAxlh.setHlbm(feature.get(key).toString()); } if ("ab".equals(key.toLowerCase())) { rmAxlh.setAb(feature.get(key).toString()); } if ("lhcd".equals(key.toLowerCase())) { rmAxlh.setLhcd(Double.parseDouble(feature.get(key).toString())); } if ("lhkd".equals(key.toLowerCase())) { rmAxlh.setLhkd(Double.parseDouble(feature.get(key).toString())); } } rmAxlhService.save(rmAxlh); String id = rmAxlh.getId(); if (!StringUtil.isNullOrEmpty(wktStr)) { if (wktStr.startsWith("MULTILINESTRING")) { wktStr = wktStr.replace("MULTILINESTRING", "LineString"); wktStr = wktStr.replace("((", "("); wktStr = wktStr.replace("))", ")"); } switch (prjType) { case 4490: { rmAxlhService.updAxlh3(wktStr, id); break; } case 4527: { rmAxlhService.updAxlh2(wktStr, id); break; } case 4548: { rmAxlhService.updAxlh(wktStr, id); break; } default: break; } } rmAxlhService.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 ""; } }