zrj
2024-11-05 b8f2c19b869986efa519f1846b63b11378147861
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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,"操作成功","操作成功");
    }
}