123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- 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<RmYhdl, IRmYhdlService> {
- @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<RmYhdl> queryWrapper = QueryGenerator.initQueryWrapper(rmYhdl, req.getParameterMap());
- Page<RmYhdl> page = new Page<RmYhdl>(pageNo, pageSize);
- IPage<RmYhdl> 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<String> 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<Map<String, Object>> 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<String, Object> 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 "";
- }
- }
|