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 implements IGeomInfoService { /** * 自定义分页 * @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); } } catch (Exception e) { e.printStackTrace(); } // 返回 return R.data(200,"操作成功","操作成功"); } }