zhongrj
2025-02-10 93264081cc9cbdf829d885d898cb06f9af2ad910
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package org.springblade.modules.yw.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
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.FirmInfo;
import org.springblade.modules.yw.entity.GeomInfoEntity;
import org.springblade.modules.yw.mapper.GeomInfoMapper;
import org.springblade.modules.yw.service.IFirmInfoService;
import org.springblade.modules.yw.service.IGeomInfoService;
import org.springblade.modules.yw.vo.GeomInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.util.*;
 
/**
 * 空间信息表 服务实现类
 *
 * @author BladeX
 * @since 2024-11-05
 */
@Service
public class GeomInfoServiceImpl extends ServiceImpl<GeomInfoMapper, GeomInfoEntity> implements IGeomInfoService {
 
    @Autowired
    private IFirmInfoService firmInfoService;
 
    /**
     * 自定义分页
     * @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);
                // 返回
                return R.data(200,geometry,"操作成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回
        return R.data(200,"操作失败","操作失败!读取数据错误!");
    }
 
    /**
     * 导出 空间信息shp zip 包
     * @param ids
     * @return
     */
    @Override
    public List<GeomInfoVO> getGeomInfoList(String ids) {
        // 查询空间数据
        return baseMapper.getGeomInfoList(ids);
    }
 
 
    /**
     * 导入 shp zip 包文件解析空间信息
     * @param multipartFile
     * @return
     */
    @Override
    public R importSpaceShpZip(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){
                for (Map<String, Object> map : list) {
                    GeomInfoEntity geomInfoEntity = Objects.requireNonNull(BeanUtil.copy(map, GeomInfoEntity.class));
                    String geometry = map.get("geometry").toString();
                    geomInfoEntity.setGeom("'" + geometry +"'");
                    String firmName = null;
                    if (null!=map.get("firmName")){
                        firmName = map.get("firmName").toString();
                    }
                    // 设置所属单位
                    setFirmId(geomInfoEntity,firmName);
                    // 判断是否已经存在
                    Long id = isSave(geomInfoEntity);
                    if (id!=null){
                        // 覆盖更新
//                        updateById();
                        continue;
                    }
                    // 保存
                    baseMapper.saveGeoInfo(geomInfoEntity);
                }
                // 返回
                return R.data(200,list,"操作成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回
        return R.data(200,"操作失败","操作失败!读取数据错误!");
    }
 
    /**
     * 设置所属企业id
     * @param geomInfoEntity
     * @param firmName
     */
    private void setFirmId(GeomInfoEntity geomInfoEntity,String firmName) {
        if (!Strings.isBlank(firmName)) {
            QueryWrapper<FirmInfo> wrapper = new QueryWrapper<>();
            wrapper.like("name", '%' +firmName + '%');
            FirmInfo one = firmInfoService.getOne(wrapper);
            if (null!=one)
                geomInfoEntity.setFirmId(one.getId());
        }
    }
 
    /**
     * 判断是否已经保存
     * @param geomInfoEntity
     * @return
     */
    private Long isSave(GeomInfoEntity geomInfoEntity) {
        QueryWrapper<GeomInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",geomInfoEntity);
        GeomInfoEntity one = getOne(wrapper);
        if (null!=one){
            return one.getId();
        }
        return null;
    }
 
    /**
     * 导入 geojson 文件解析空间信息
     * @param multipartFile
     * @return
     */
    @Override
    public R importGeoJson(MultipartFile multipartFile) {
        // 转 file
        File file = FileUtil.toFile(multipartFile);
 
        FeatureJSON featureJSON = new FeatureJSON();
        try {
            FeatureCollection featureCollection = featureJSON.readFeatureCollection(file);
            FeatureIterator iterator = featureCollection.features();
            // 遍历feature转为json对象
            while (iterator.hasNext()) {
                SimpleFeature feature = (SimpleFeature) iterator.next();
                StringWriter writer = new StringWriter();
                featureJSON.writeFeature(feature, writer);
                String temp = writer.toString();
                JSONObject json = JSONObject.parseObject(temp);
                Object value = feature.getDefaultGeometryProperty().getValue();
                System.out.println("value = " + value);
                try {
                    GeomInfoEntity geomInfoEntity = new GeomInfoEntity();
                    // 空间坐标
                    geomInfoEntity.setGeom("'" + value + "'");
                    // 空间类型
                    if (json.getJSONObject("geometry").get("type") != null) {
                        geomInfoEntity.setType(json.getJSONObject("geometry").getString("type"));
                    }
                    // 保存
                    baseMapper.saveGeoInfo(geomInfoEntity);
                } catch (NumberFormatException e) {
                    log.error("NumberFormatException: {}", e);
                } catch (Exception e) {
                    log.error("Exception: {}", e);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 返回
        return R.data(200,"","操作成功!");
    }
}