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 implements IGeomInfoService { @Autowired private IFirmInfoService firmInfoService; /** * 自定义分页 * @param page * @param geomInfo * @return */ @Override public IPage selectGeomInfoPage(IPage 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> list = ShapeFileUtil.shpToGeoJson(file); // 写入数据库,暂时考虑只有一组数据的情况 if (list.size()>0){ Map 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 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> list = ShapeFileUtil.shpToGeoJson(file); // 写入数据库,暂时考虑只有一组数据的情况 if (list.size()>0){ for (Map 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 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 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,"","操作成功!"); } }