| | |
| | | package com.se.nsl.service; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.metadata.OrderItem; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.bc.zarr.ZarrArray; |
| | | import com.bc.zarr.ZarrGroup; |
| | | import com.se.nsl.config.PropertiesConfig; |
| | | import com.se.nsl.domain.dto.GeDb; |
| | | import com.se.nsl.domain.po.DataPo; |
| | | import com.se.nsl.domain.po.SimuPo; |
| | | import com.se.nsl.domain.vo.CreateSimuVo; |
| | | import com.se.nsl.domain.po.Simu; |
| | | import com.se.nsl.domain.vo.SimuResult; |
| | | import com.se.nsl.domain.vo.SimuVo; |
| | | import com.se.nsl.helper.StringHelper; |
| | | import com.se.nsl.helper.WebHelper; |
| | | import com.se.nsl.mapper.SimuMapper; |
| | | import lombok.SneakyThrows; |
| | | import com.se.nsl.utils.CoordinateTransformer; |
| | | import com.se.nsl.utils.SolverTifUtil; |
| | | import com.se.nsl.utils.TimeFormatUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import ucar.ma2.InvalidRangeException; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.File; |
| | | import java.sql.Timestamp; |
| | | import java.util.Date; |
| | | import java.io.IOException; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class SimuService { |
| | | public static final String TIF_EXTSION = ".tif"; |
| | | public static final String YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss"; |
| | | @Resource |
| | | SimuMapper simuMapper; |
| | | |
| | | @Resource |
| | | PropertiesConfig config; |
| | | |
| | | @Resource |
| | | GedbService gedbService; |
| | | /** |
| | | * 分页查询推演模拟 |
| | | * |
| | | * @param pageNum 页码 |
| | | * @param pageSize 每页数量 |
| | | * @return 分页后的推演模拟 |
| | | */ |
| | | public IPage<Simu> selectPage(SimuVo vo, int pageNum, int pageSize) { |
| | | QueryWrapper<Simu> wrapper = getPageWrapper(vo, pageNum, pageSize); |
| | | |
| | | @Resource |
| | | UwService uwService; |
| | | |
| | | @Resource |
| | | ResultService resultService; |
| | | |
| | | public IPage<SimuPo> get(SimuVo vo) { |
| | | QueryWrapper<SimuPo> wrapper = getPageWrapper(vo); |
| | | |
| | | Page<SimuPo> page = new Page<>(vo.getPageIndex(), vo.getPageSize()); |
| | | Page<Simu> page = new Page<>(pageNum, pageSize); |
| | | page.addOrder(OrderItem.desc("id")); |
| | | |
| | | IPage<SimuPo> paged = simuMapper.selectPage(page, wrapper); |
| | | |
| | | return paged; |
| | | return simuMapper.selectPage(page, wrapper); |
| | | } |
| | | |
| | | private QueryWrapper<SimuPo> getPageWrapper(SimuVo vo) { |
| | | QueryWrapper<SimuPo> wrapper = new QueryWrapper<>(); |
| | | private QueryWrapper<Simu> getPageWrapper(SimuVo vo, int pageNum, int pageSize) { |
| | | QueryWrapper<Simu> wrapper = new QueryWrapper<>(); |
| | | if (null != vo.getId()) { |
| | | wrapper.eq("id", vo.getId()); |
| | | } |
| | | if (null != vo.getPid()) { |
| | | wrapper.eq("pid", vo.getPid()); |
| | | } |
| | | if (null != vo.getNum()) { |
| | | wrapper.eq("num", vo.getNum()); |
| | | } |
| | | if (!StringHelper.isEmpty(vo.getName())) { |
| | | wrapper.like("lower(name)", vo.getName().trim().toLowerCase()); |
| | | } |
| | | if (!CollUtil.isEmpty(vo.getStatus())) { |
| | | wrapper.in("status", vo.getStatus()); |
| | | if (!StringHelper.isEmpty(vo.getServiceName())) { |
| | | wrapper.like("service_name", vo.getServiceName().trim()); |
| | | } |
| | | if (null != vo.getType()) { |
| | | wrapper.eq("type", vo.getType()); |
| | | } |
| | | if (null != vo.getAreaType()) { |
| | | wrapper.eq("area_type", vo.getAreaType()); |
| | | } |
| | | if (null != vo.getStatus()) { |
| | | wrapper.eq("status", vo.getStatus()); |
| | | } |
| | | |
| | | return wrapper; |
| | | } |
| | | |
| | | public int del(List<Integer> ids) { |
| | | List<SimuPo> list = simuMapper.selectBatchIds(ids); |
| | | if (null != list && list.size() > 0) { |
| | | for (SimuPo po : list) { |
| | | try { |
| | | if (StringHelper.isEmpty(po.getData())) continue; |
| | | |
| | | DataPo dp = JSONUtil.toBean(po.getData(), DataPo.class); |
| | | if (null == dp) continue; |
| | | |
| | | delDir(config.getInPath() + File.separator + dp.getInPath()); |
| | | delDir(config.getOutPath() + File.separator + dp.getOutPath()); |
| | | FileUtil.del(config.getInPath() + File.separator + dp.getInPath() + ".json"); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据ID批量删除推演模拟 |
| | | * |
| | | * @param ids 要删除的区域ID列表 |
| | | * @return 删除成功的记录数 |
| | | */ |
| | | public int deleteByIds(List<Integer> ids) { |
| | | return simuMapper.deleteBatchIds(ids); |
| | | } |
| | | |
| | | private boolean delDir(String dir) { |
| | | File file = new File(dir); |
| | | if (!file.exists() || !file.isDirectory()) { |
| | | return false; |
| | | } |
| | | |
| | | return FileUtil.del(dir); |
| | | /** |
| | | * 新增推演模拟 |
| | | * |
| | | * @param Simu 推演模拟对象 |
| | | * @return 新增成功的记录数 |
| | | */ |
| | | public int insert(Simu simu) { |
| | | return simuMapper.insert(simu); |
| | | } |
| | | |
| | | public Integer getMaxId() { |
| | | return simuMapper.selectMaxId(); |
| | | public int inserts(List<Simu> list) { |
| | | return simuMapper.inserts(list); |
| | | } |
| | | |
| | | public SimuPo getSimuByPid(Integer pid) { |
| | | QueryWrapper<SimuPo> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("id", pid); |
| | | wrapper.last("limit 1"); |
| | | |
| | | return simuMapper.selectOne(wrapper); |
| | | } |
| | | public SimuPo getSimuById(Integer id) { |
| | | /** |
| | | * 根据ID查询 |
| | | * |
| | | * @param id ID |
| | | * @return Simu |
| | | */ |
| | | public Simu selectById(Integer id) { |
| | | return simuMapper.selectById(id); |
| | | } |
| | | |
| | | public SimuPo getSimuByServiceName(String serviceName) { |
| | | if (StringHelper.isEmpty(serviceName)) { |
| | | /** |
| | | * 修改推演模拟 |
| | | * |
| | | * @param Simu 推演模拟对象 |
| | | * @return 修改成功的记录数 |
| | | */ |
| | | public int updateById(Simu simu) { |
| | | return simuMapper.updates(Arrays.asList(simu)); |
| | | } |
| | | |
| | | public SimuResult queryByPosition(double lon, double lat, long time, String serviceName) { |
| | | //transform coordiante from 4326 to 4548 |
| | | double[] xy = CoordinateTransformer.transform(4326, 4548, lon, lat); |
| | | // System.out.println(String.format("转换前的坐标:x:%s,y:%s", lon, lat)); |
| | | // System.out.println(String.format("转换后的坐标:x:%s,y:%s", xy[0], xy[1])); |
| | | //read from zarr |
| | | // return queryByZarr(xy, time, serviceName); |
| | | |
| | | //read from tif file |
| | | return queryByTif(xy, time, serviceName); |
| | | |
| | | } |
| | | |
| | | public List<SimuResult> queryByPosition(double lon, double lat, String serviceName) { |
| | | //transform coordiante from 4326 to 4548 |
| | | double[] xy = CoordinateTransformer.transform(4326, config.getEpsg(), lon, lat); |
| | | // System.out.println(String.format("转换前的坐标:x:%s,y:%s", lon, lat)); |
| | | // System.out.println(String.format("转换后的坐标:x:%s,y:%s", xy[0], xy[1])); |
| | | //read from zarr |
| | | // return queryByZarr(xy, time, serviceName); |
| | | |
| | | //read from tif file |
| | | return queryByTif(xy, serviceName); |
| | | |
| | | } |
| | | |
| | | private SimuResult queryByZarr(double[] xy, long time, String serviceName) { |
| | | double x = xy[0]; |
| | | double y = xy[1]; |
| | | String prefix = formatTime(time); |
| | | File inPath = new File(config.getInPath()); |
| | | // File tifDir = new File(inPath, serviceName + File.separator + "depth"); |
| | | // int index = 0; |
| | | // File[] files = tifDir.listFiles(); |
| | | // for (File file : files) { |
| | | // String name = file.getName(); |
| | | // if (!name.endsWith(TIF_EXTSION)) continue; |
| | | // if (name.equals(prefix + TIF_EXTSION)) { |
| | | // break; |
| | | // } |
| | | // index++; |
| | | // } |
| | | File dem = new File(inPath, serviceName + File.separator + "DEM.tif"); |
| | | //TODO |
| | | |
| | | File zarr = new File(inPath, serviceName + File.separator + "result.zarr"); |
| | | try { |
| | | ZarrGroup group = ZarrGroup.open(zarr.toPath()); |
| | | ZarrArray depthArray = group.openArray("depth"); |
| | | int[] shape = new int[] {60, 637, 351}; |
| | | // int[] offset = new int[]{210, 384}; |
| | | float[] depth = (float[]) depthArray.read(shape); |
| | | System.out.println("depth:" + depth.length); |
| | | } catch (IOException | InvalidRangeException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private SimuResult queryByTif(double[] xy, long time, String serviceName) { |
| | | File inPath = new File(config.getInPath()); |
| | | String prefix = formatTime(time); |
| | | String child = serviceName + File.separator + "depth" + File.separator + prefix + TIF_EXTSION; |
| | | File tifFile = new File(inPath, child); |
| | | if (!tifFile.exists()) { |
| | | return null; |
| | | } |
| | | |
| | | QueryWrapper<SimuPo> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("service_name", serviceName); |
| | | wrapper.last("limit 1"); |
| | | |
| | | return simuMapper.selectOne(wrapper); |
| | | return SolverTifUtil.getSimuResult(tifFile, xy); |
| | | } |
| | | |
| | | public boolean create(CreateSimuVo vo) { |
| | | Date now = new Date(); |
| | | String date = StringHelper.YMDHMS2_FORMAT.format(now); |
| | | if (StringHelper.isEmpty(vo.getName())) { |
| | | vo.setName(date); |
| | | private List<SimuResult> queryByTif(double[] xy, String serviceName) { |
| | | List<SimuResult> res = new ArrayList<>(); |
| | | File inPath = new File(config.getInPath()); |
| | | Path depthPath = Paths.get(inPath.getAbsolutePath(), serviceName, "depth"); |
| | | File depthDir = depthPath.toFile(); |
| | | File[] files = depthDir.listFiles(); |
| | | for (File tifFile : files) { |
| | | String name = tifFile.getName(); |
| | | if (!name.endsWith(TIF_EXTSION)) continue; |
| | | SimuResult result = SolverTifUtil.getSimuResult(tifFile, xy); |
| | | if (result == null) continue; |
| | | res.add(result); |
| | | } |
| | | |
| | | DataPo data = BeanUtil.copyProperties(vo, DataPo.class); |
| | | data.setPath(date, date); |
| | | initPath(data); |
| | | |
| | | SimuPo simu = new SimuPo(vo.getNum(), vo.getPid(), vo.getName(), JSONUtil.toJsonStr(data), 0, vo.getBak()); |
| | | simu.setServiceName(date); |
| | | simu.setCreateTime(new Timestamp(now.getTime())); |
| | | |
| | | int rows = simuMapper.insert(simu); |
| | | if (rows > 0) { |
| | | asyncCall(simu); |
| | | } |
| | | |
| | | return rows > 0; |
| | | return res; |
| | | } |
| | | |
| | | private void initPath(DataPo data) { |
| | | createDir(config.getInPath() + File.separator + data.getInPath()); |
| | | createDir(config.getOutPath() + File.separator + data.getOutPath()); |
| | | private String formatTime(long time) { |
| | | return TimeFormatUtil.formatTime(time, YYYY_MM_DD_HH_MM_SS); |
| | | } |
| | | |
| | | private void createDir(String path) { |
| | | File f = new File(path); |
| | | if (f.exists() && f.isDirectory()) { |
| | | FileUtil.del(f); |
| | | } |
| | | f.mkdirs(); |
| | | } |
| | | |
| | | private void asyncCall(SimuPo simu) { |
| | | ExecutorService executor = Executors.newSingleThreadExecutor(); |
| | | executor.execute(new Runnable() { |
| | | @Override |
| | | @SneakyThrows |
| | | public void run() { |
| | | cope(simu); |
| | | } |
| | | }); |
| | | executor.shutdown(); |
| | | } |
| | | |
| | | private void cope(SimuPo simu) { |
| | | try { |
| | | DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class); |
| | | |
| | | update(simu, 1, null); |
| | | String token = gedbService.getToken(); |
| | | GeDb db = gedbService.connectGedb(token, data); |
| | | simu.setData(JSONUtil.toJsonStr(data)); |
| | | |
| | | update(simu, 2, null); |
| | | gedbService.copeVectors(token, data, db); |
| | | |
| | | update(simu, 3, null); |
| | | gedbService.copeDem(token, data); |
| | | |
| | | update(simu, 4, null); |
| | | uwService.createRainFile(data); |
| | | |
| | | update(simu, 5, null); |
| | | uwService.createConfig(data); |
| | | |
| | | update(simu, 6, null); |
| | | uwService.callExe(data); |
| | | |
| | | update(simu, 7, null); |
| | | //uwService.copeWaterFiles(); |
| | | |
| | | update(simu, 8, null); |
| | | uwService.copeDrainFiles(data); |
| | | |
| | | update(simu, 9, null); |
| | | resultService.process(data); |
| | | |
| | | update(simu, 10, "完成"); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | update(simu, -simu.getStatus(), ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private void update(SimuPo simu, int status, String rs) { |
| | | simu.setStatus(status); |
| | | if (null != rs) simu.setResult(rs); |
| | | simu.setUpdateTime(WebHelper.getCurrentTimestamp()); |
| | | |
| | | simuMapper.updateById(simu); |
| | | } |
| | | } |