zrj
2024-11-05 b8f2c19b869986efa519f1846b63b11378147861
增加空间数据和基础接口及shp导入导出
5 files modified
10 files added
700 ■■■■■ changed files
src/main/java/org/springblade/common/constant/FileConstant.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/utils/FileUtil.java 47 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/utils/ShapeFileUtil.java 45 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/utils/ZipUtil.java 142 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/controller/DownZipController.java 60 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/controller/GeomInfoController.java 120 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/entity/GeomInfoEntity.java 87 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/mapper/GeomInfoMapper.java 28 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/mapper/GeomInfoMapper.xml 16 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/service/IGeomInfoService.java 32 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java 77 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/yw/vo/GeomInfoVO.java 18 ●●●●● patch | view | raw | blame | history
src/main/resources/application-dev.yml 3 ●●●●● patch | view | raw | blame | history
src/main/resources/application-prod.yml 4 ●●●● patch | view | raw | blame | history
src/main/resources/application-test.yml 4 ●●●● patch | view | raw | blame | history
src/main/java/org/springblade/common/constant/FileConstant.java
New file
@@ -0,0 +1,17 @@
package org.springblade.common.constant;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class FileConstant {
    /**
     * zip 临时保存路径
     */
    @Value("${zip.temp.savePath}")
    private String zipTempSavePath;
}
src/main/java/org/springblade/common/utils/FileUtil.java
New file
@@ -0,0 +1,47 @@
package org.springblade.common.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
public class FileUtil {
    /**
     * MultipartFile 转 file
     * @param multipartFile
     * @return
     */
    public static File toFile(MultipartFile multipartFile) {
        // 如果multipartFile为空,则直接返回null
        if (multipartFile == null) {
            return null;
        }
        // 创建一个临时的文件
        File file = null;
        try {
            file = File.createTempFile("temp", multipartFile.getOriginalFilename());
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
    /**
     * 获取文件后缀
     * @param file
     * @return
     */
    public static String getFileExtension(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null) {
            int dotIndex = originalFilename.lastIndexOf(".");
            if (dotIndex >= 0) {
                extension = originalFilename.substring(dotIndex + 1);
            }
        }
        return extension;
    }
}
src/main/java/org/springblade/common/utils/ShapeFileUtil.java
@@ -15,6 +15,7 @@
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
@@ -39,7 +40,10 @@
     * @param geomType           geometry类型
     * @param saveFolder         导出位置
     */
    private static void exportShp(List<Map<String, Object>> dataPropertiesList, String fileName, String geomType,String saveFolder) {
    public static void exportShp(List<Map<String, Object>> dataPropertiesList,
                                 String fileName,
                                 String geomType,
                                 String saveFolder) {
        //创建保存shp文件夹
        File dir = new File(saveFolder);
        if (!dir.exists()) {
@@ -194,13 +198,17 @@
                featureJSON.writeFeature(feature, writer);
                String temp = writer.toString();
                JSONObject json = JSONObject.parseObject(temp);
                Object value = feature.getDefaultGeometryProperty().getValue();
                try {
                    // 空间坐标
                    shpDTO.put("geometry",json.getJSONObject("geometry").getString("coordinates"));
                    shpDTO.put("geometry",value);
                    // 空间类型
                    if (json.getJSONObject("geometry").get("type") != null) {
                        shpDTO.put("geoType",json.getJSONObject("geometry").getString("type"));
                        shpDTO.put("type",json.getJSONObject("geometry").getString("type"));
                    }
                    // 名称
                    if (json.getJSONObject("properties").get("name")!= null) {
                        shpDTO.put("name",json.getJSONObject("properties").getString("name"));
                    }
                    dtoList.add(shpDTO);
                } catch (NumberFormatException e) {
@@ -220,22 +228,27 @@
    public static void main(String[] args) throws IOException, ParseException {
//        File file = new File("F:\\test4jdata\\shp\\import\\zrq.zip");
        File file = new File("F:\\test4jdata\\shp\\export\\test.zip");
        File file = new File("F:\\test4jdata\\shp\\zip\\test.zip");
        List<Map<String, Object>> list = shpToGeoJson(file);
        System.out.println("list = " + list);
        // 导出shp 文件
        List<Map<String,Object>> propertyList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("ID", i);
            map.put("名称", "test" + i);
            map.put("描述", "测试shp导出" + i);
            map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.549318250000056, 114.09104754000009 22.549328150000065))");
            propertyList.add(map);
        }
        String exportPath = "F:\\test4jdata\\shp\\export\\";
        exportShp(propertyList, "test", "MultiLineString",exportPath);
//        List<Map<String,Object>> propertyList = new ArrayList<>();
//        for (int i = 0; i < 3; i++) {
//            Map<String,Object> map = new HashMap<>();
//            map.put("ID", i);
//            map.put("名称", "test" + i);
//            map.put("描述", "测试shp导出" + i);
//            map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.549318250000056, 114.09104754000009 22.549328150000065))");
//            propertyList.add(map);
//        }
//        String exportPath = "F:\\test4jdata\\shp\\export\\";
//        exportShp(propertyList, "test", "MultiLineString",exportPath);
//        // 打包成 zip 包,然后导出
//        String zipPath = "F:\\test4jdata\\shp\\zip\\test.zip";
//        boolean folder = ZipUtil.zipFolder(exportPath, zipPath);
        // 打包成功后导出
    }
    public static double[] getLongitudeLatitude(Geometry wktPoint) throws ParseException {
src/main/java/org/springblade/common/utils/ZipUtil.java
@@ -1,8 +1,13 @@
package org.springblade.common.utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -81,7 +86,12 @@
        return list;
    }
    // 定义一个公共的静态方法zipFolder,用于压缩文件夹
    /**
     * 压缩文件夹
     * @param sourceFolderPath 压缩文件路径
     * @param zipFilePath 压缩后包的路径
     * @return
     */
    public static boolean zipFolder(String sourceFolderPath, String zipFilePath) {
        File sourceFile = new File(sourceFolderPath);
        try (
@@ -96,8 +106,48 @@
        return false;
    }
    // 将文件夹整体压缩
    private static void zipFile(File fileToZip, String fileName, ZipOutputStream zos) throws IOException {
    /**
     * 压缩文件夹
     * @param sourceFolderPath 压缩文件路径
     * @param zos zip输出流
     * @return
     */
    public static boolean zipFolder(String sourceFolderPath,ZipOutputStream zos) {
        File sourceFile = new File(sourceFolderPath);
        try {
            zipFile(sourceFile, sourceFile.getName(), zos);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 删除生成的文件
            deleteDirectory(sourceFile);
        }
        return false;
    }
    /**
     * 删除文件夹下文件
     * @param directoryToBeDeleted
     * @return
     */
    public static boolean deleteDirectory(File directoryToBeDeleted) {
        File[] allContents = directoryToBeDeleted.listFiles();
        if (allContents != null) {
            for (File file : allContents) {
                deleteDirectory(file);
            }
        }
        return directoryToBeDeleted.delete();
    }
    /**
     * 将文件夹整体压缩
     * @param fileToZip
     * @param fileName 文件名称
     * @param zos
     * @throws IOException
     */
    public static void zipFile(File fileToZip, String fileName, ZipOutputStream zos) throws IOException {
        if (fileToZip.isHidden()) {
            return;
        }
@@ -126,7 +176,91 @@
        fis.close();
    }
    public static void main(String[] args) {
    /**
     * 设置导出zip的响应格式
     *
     * @param request
     * @param response
     * @param fileZip  zip的名字
     * @param filePath zip的路径
     * @throws UnsupportedEncodingException
     */
    public static void downLoadFile(HttpServletRequest request, HttpServletResponse response, String fileZip, String filePath) throws UnsupportedEncodingException {
        //进行浏览器下载
        final String userAgent = request.getHeader("USER-AGENT");
        //判断浏览器代理并分别设置响应给浏览器的编码格式
        String finalFileName = null;
        if (StringUtils.contains(userAgent, "MSIE") || StringUtils.contains(userAgent, "Trident")) {
            // IE浏览器
            finalFileName = URLEncoder.encode(fileZip, "UTF8");
            System.out.println("IE浏览器");
        } else if (StringUtils.contains(userAgent, "Mozilla")) {
            // google,火狐浏览器
            finalFileName = new String(fileZip.getBytes(), "ISO8859-1");
        } else {
            // 其他浏览器
            finalFileName = URLEncoder.encode(fileZip, "UTF8");
        }
        // 告知浏览器下载文件,而不是直接打开,浏览器默认为打开
        response.setContentType("application/x-download");
        // 下载文件的名称
        response.setHeader("Content-Disposition", "attachment;filename=\"" + finalFileName + "\"");
        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DataOutputStream temps = new DataOutputStream(
            servletOutputStream);
        // 浏览器下载文件的路径
        DataInputStream in = null;
        try {
            in = new DataInputStream(
                new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] b = new byte[2048];
        // 之后用来删除临时压缩文件
        File reportZip = new File(filePath);
        try {
            while ((in.read(b)) != -1) {
                temps.write(b);
            }
            temps.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (temps != null) {
                try {
                    temps.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reportZip != null) {
                // 删除服务器本地产生的临时压缩文件!
                reportZip.delete();
            }
            try {
                servletOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        // 调用方法进行测试
    }
}
src/main/java/org/springblade/modules/yw/controller/DownZipController.java
New file
@@ -0,0 +1,60 @@
package org.springblade.modules.yw.controller;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.FileConstant;
import org.springblade.common.utils.ShapeFileUtil;
import org.springblade.common.utils.ZipUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@AllArgsConstructor
@RequestMapping("/yw/downZip")
@RestController
public class DownZipController {
    private final FileConstant fileConstant;
    /**
     * 导出指定文件夹下文件(压缩成zip)
     * @param response
     * @throws IOException
     */
    @GetMapping("/download-folder-as-zip")
    public void downloadFolderAsZip(HttpServletResponse response) throws IOException {
        // 要导出的文件夹路径
        String sourceFolderPath = fileConstant.getZipTempSavePath();
        // 先生成文件
        List<Map<String,Object>> propertyList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("ID", i);
            map.put("名称", "test" + i);
            map.put("描述", "测试shp导出" + i);
            map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.549318250000056, 114.09104754000009 22.549328150000065))");
            propertyList.add(map);
        }
        // 导出shp 文件
        ShapeFileUtil.exportShp(propertyList, "test", "MultiLineString",sourceFolderPath);
        // ZIP文件名
        String zipFileName = "exported_folder.zip";
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
        try (
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            ZipOutputStream zipOut = new ZipOutputStream(bos);
        ) {
            ZipUtil.zipFolder(sourceFolderPath, zipOut);
        }
    }
}
src/main/java/org/springblade/modules/yw/controller/GeomInfoController.java
New file
@@ -0,0 +1,120 @@
package org.springblade.modules.yw.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.yw.entity.GeomInfoEntity;
import org.springblade.modules.yw.service.IGeomInfoService;
import org.springblade.modules.yw.vo.GeomInfoVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import java.util.Map;
/**
 * 空间信息表 控制器
 *
 * @author BladeX
 * @since 2024-11-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("yw/geomInfo")
@Api(value = "空间信息表", tags = "空间信息表接口")
public class GeomInfoController {
    private final IGeomInfoService geomInfoService;
    /**
     * 空间信息表 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入geomInfo")
    public R detail(GeomInfoEntity geomInfo) {
        GeomInfoEntity detail = geomInfoService.getOne(Condition.getQueryWrapper(geomInfo));
        return R.data(detail);
    }
    /**
     * 空间信息表 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入geomInfo")
    public R<IPage> list(@ApiIgnore @RequestParam Map<String, Object> geomInfo, Query query) {
        IPage<GeomInfoEntity> pages = geomInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(geomInfo, GeomInfoEntity.class));
        return R.data(pages);
    }
    /**
     * 空间信息表 自定义分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入geomInfo")
    public R<IPage<GeomInfoVO>> page(GeomInfoVO geomInfo, Query query) {
        IPage<GeomInfoVO> pages = geomInfoService.selectGeomInfoPage(Condition.getPage(query), geomInfo);
        return R.data(pages);
    }
    /**
     * 空间信息表 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入geomInfo")
    public R save(@Valid @RequestBody GeomInfoEntity geomInfo) {
        return R.status(geomInfoService.save(geomInfo));
    }
    /**
     * 空间信息表 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入geomInfo")
    public R update(@Valid @RequestBody GeomInfoEntity geomInfo) {
        return R.status(geomInfoService.updateById(geomInfo));
    }
    /**
     * 空间信息表 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入geomInfo")
    public R submit(@Valid @RequestBody GeomInfoEntity geomInfo) {
        return R.status(geomInfoService.saveOrUpdate(geomInfo));
    }
    /**
     * 空间信息表 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(geomInfoService.removeByIds(Func.toLongList(ids)));
    }
    /**
     * 导入 shp zip 包文件解析空间信息
     * @param file
     * @return
     */
    @PostMapping("/importShpZip")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "导入 shp zip 包文件解析空间信息", notes = "传入文件")
    public R importShpZip(MultipartFile file) {
        return geomInfoService.importShpZip(file);
    }
}
src/main/java/org/springblade/modules/yw/entity/GeomInfoEntity.java
New file
@@ -0,0 +1,87 @@
package org.springblade.modules.yw.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
 * 空间信息表 实体类
 *
 * @author BladeX
 * @since 2024-11-05
 */
@Data
@TableName("yw_geom_info")
@ApiModel(value = "GeomInfo对象", description = "空间信息表")
public class GeomInfoEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 主键id
     */
    @ApiModelProperty(value = "主键ID", example = "")
    @TableId(value = "id",type = IdType.ASSIGN_ID)
    private Long id;
    /**
     * 名称
     */
    @ApiModelProperty(value = "名称")
    private String name;
    /**
     * 空间类型
     */
    @ApiModelProperty(value = "空间类型")
    private String type;
    /**
     * 空间信息
     */
    @ApiModelProperty(value = "空间信息")
    private String geom;
    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人", example = "")
    @TableField(value = "create_user", fill = FieldFill.INSERT)
    private Long createUser;
    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间", example = "")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty(value = "更新人", example = "")
    @TableField("update_user")
    private Long updateUser;
    /**
     * 更新人时间
     */
    @ApiModelProperty(value = "更新人时间", example = "")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(value = "update_time", fill = FieldFill.INSERT)
    private Date updateTime;
    /**
     * 是否删除 0:否  1:是
     */
    @ApiModelProperty(value = "是否删除 0:否  1:是", example = "")
    @TableField("is_deleted")
    @TableLogic
    private Integer isDeleted;
}
src/main/java/org/springblade/modules/yw/mapper/GeomInfoMapper.java
New file
@@ -0,0 +1,28 @@
package org.springblade.modules.yw.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.springblade.modules.yw.entity.GeomInfoEntity;
import org.springblade.modules.yw.vo.GeomInfoVO;
import java.util.List;
/**
 * 空间信息表 Mapper 接口
 *
 * @author BladeX
 * @since 2024-11-05
 */
public interface GeomInfoMapper extends BaseMapper<GeomInfoEntity> {
    /**
     * 自定义分页
     *
     * @param page
     * @param geomInfo
     * @return
     */
    List<GeomInfoVO> selectGeomInfoPage(IPage page, GeomInfoVO geomInfo);
    int saveGeoInfo(@Param("geomInfoEntity") GeomInfoEntity geomInfoEntity);
}
src/main/java/org/springblade/modules/yw/mapper/GeomInfoMapper.xml
New file
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.modules.yw.mapper.GeomInfoMapper">
    <!--自定义分页查询-->
    <select id="selectGeomInfoPage" resultType="org.springblade.modules.yw.vo.GeomInfoVO">
        select * from yw_geom_info where is_deleted = 0
    </select>
    <!--保存空间信息-->
    <insert id="saveGeoInfo">
        insert into yw_geom_info (name,type,geom)
        values (#{geomInfoEntity.name},#{geomInfoEntity.type},ST_GeomFromText(${geomInfoEntity.geom}))
    </insert>
</mapper>
src/main/java/org/springblade/modules/yw/service/IGeomInfoService.java
New file
@@ -0,0 +1,32 @@
package org.springblade.modules.yw.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springblade.core.tool.api.R;
import org.springblade.modules.yw.entity.GeomInfoEntity;
import org.springblade.modules.yw.vo.GeomInfoVO;
import org.springframework.web.multipart.MultipartFile;
/**
 * 空间信息表 服务类
 *
 * @author zhongrj
 * @since 2024-11-05
 */
public interface IGeomInfoService extends IService<GeomInfoEntity> {
    /**
     * 自定义分页
     *
     * @param page
     * @param geomInfo
     * @return
     */
    IPage<GeomInfoVO> selectGeomInfoPage(IPage<GeomInfoVO> page, GeomInfoVO geomInfo);
    /**
     * 导入 shp zip 包文件解析空间信息
     * @param file
     * @return
     */
    R importShpZip(MultipartFile file);
}
src/main/java/org/springblade/modules/yw/service/impl/GeomInfoServiceImpl.java
New file
@@ -0,0 +1,77 @@
package org.springblade.modules.yw.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.WKBReader;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.springblade.common.utils.FileUtil;
import org.springblade.common.utils.ShapeFileUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.yw.entity.GeomInfoEntity;
import org.springblade.modules.yw.mapper.GeomInfoMapper;
import org.springblade.modules.yw.service.IGeomInfoService;
import org.springblade.modules.yw.vo.GeomInfoVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
 * 空间信息表 服务实现类
 *
 * @author BladeX
 * @since 2024-11-05
 */
@Service
public class GeomInfoServiceImpl extends ServiceImpl<GeomInfoMapper, GeomInfoEntity> implements IGeomInfoService {
    /**
     * 自定义分页
     * @param page
     * @param geomInfo
     * @return
     */
    @Override
    public IPage<GeomInfoVO> selectGeomInfoPage(IPage<GeomInfoVO> page, GeomInfoVO geomInfo) {
        return page.setRecords(baseMapper.selectGeomInfoPage(page, geomInfo));
    }
    /**
     * 导入 shp zip 包文件解析空间信息
     * @param multipartFile
     * @return
     */
    @Override
    public R importShpZip(MultipartFile multipartFile) {
        // 判断文件是否为 zip 文件
        if (!FileUtil.getFileExtension(multipartFile).equals("zip")){
            return R.data(400,"文件格式不对,必须是 zip 文件","文件格式不对,必须是 zip 文件");
        }
        // 转 file
        File file = FileUtil.toFile(multipartFile);
        // 获取文件信息
        try {
            List<Map<String, Object>> list = ShapeFileUtil.shpToGeoJson(file);
            // 写入数据库,暂时考虑只有一组数据的情况
            if (list.size()>0){
                Map<String, Object> map = list.get(0);
                GeomInfoEntity geomInfoEntity = Objects.requireNonNull(BeanUtil.copy(map, GeomInfoEntity.class));
                String geometry = map.get("geometry").toString();
                geomInfoEntity.setGeom("'" + geometry +"'");
                // 保存
                baseMapper.saveGeoInfo(geomInfoEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回
        return R.data(200,"操作成功","操作成功");
    }
}
src/main/java/org/springblade/modules/yw/vo/GeomInfoVO.java
New file
@@ -0,0 +1,18 @@
package org.springblade.modules.yw.vo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.modules.yw.entity.GeomInfoEntity;
/**
 * 空间信息表 视图实体类
 *
 * @author BladeX
 * @since 2024-11-05
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class GeomInfoVO extends GeomInfoEntity {
    private static final long serialVersionUID = 1L;
}
src/main/resources/application-dev.yml
@@ -56,3 +56,6 @@
    upload-domain: http://localhost:8999
    remote-path: /usr/share/nginx/html
zip:
  temp:
    savePath: F:\\test4jdata\\shp\\export\\
src/main/resources/application-prod.yml
@@ -38,3 +38,7 @@
    remote-mode: true
    upload-domain: http://localhost:8999
    remote-path: /usr/share/nginx/html
zip:
  temp:
    savePath: /home/temp
src/main/resources/application-test.yml
@@ -38,3 +38,7 @@
    remote-mode: true
    upload-domain: http://localhost:8999
    remote-path: /usr/share/nginx/html
zip:
  temp:
    savePath: /home/temp