张洋洋
2025-02-24 9804628abf554c3658345fc8fc9472cfb179fd5f
src/main/java/com/se/simu/controller/SimuController.java
@@ -1,5 +1,7 @@
package com.se.simu.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -12,6 +14,10 @@
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.*;
import com.se.simu.enums.RadioEnums;
import com.se.simu.enums.SemErrorEnums;
import com.se.simu.helper.StringHelper;
import com.se.simu.helper.WebHelper;
import com.se.simu.mapper.SimuMapper;
import com.se.simu.service.*;
import com.se.simu.utils.*;
import io.swagger.annotations.*;
@@ -30,6 +36,7 @@
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -58,6 +65,9 @@
    @Resource
    PropertiesConfig config;
    @Resource
    SimuMapper simuMapper;
    @Value("${simu-app.filePath}")
    private String uploadedFolder;
@@ -395,13 +405,35 @@
            JSONObject link = getModule("layerQueryParams.json");
            String linkUrl = assemble(vo.getLinkName(), link.getString("layerid"), bbox, token);
            vo.setLinkName(linkUrl);
            asyncCall(tableName, dtos, vo, token);
            SimuPo simu = saveSimu(vo);
            //生成sem文件
            asyncCall(tableName, dtos, vo, token, simu);
            // 开始模拟计算
            boolean flag = simuFilesService.createByfiles(vo);
            return success(flag, flag ? "成功" : "失败");
            //boolean flag = simuFilesService.createByfiles(vo, token);
            return success(simu.getId() != null ? "成功" : "失败");
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    public SimuPo saveSimu(CreateFilesSimuVo vo) {
        Date now = new Date();
        String date = StringHelper.YMDHMS2_FORMAT.format(now);
        if (StringHelper.isEmpty(vo.getName())) {
            vo.setName(date);
        }
        DataPo data = BeanUtil.copyProperties(vo, DataPo.class);
        data.setPath(date, date);
        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()));
        simu.setSemUrl(vo.getSemUrl());
        simu.setPointUrl(vo.getPointName());
        simu.setLinkUrl(vo.getLinkName());
        simu.setSemUrl(inPath + "\\" + vo.getSemUrl());
        int rows = simuMapper.insert(simu);
        return simu;
    }
    @ApiOperation(value = "获取点路径")
@@ -426,44 +458,77 @@
        return success(null);
    }
    private void asyncCall(String tableName, List<GridDto> dtos, CreateFilesSimuVo vo, String token) {
    private void asyncCall(String tableName, List<GridDto> dtos, CreateFilesSimuVo vo, String token, SimuPo simu) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            @SneakyThrows
            public void run() {
                createSem(tableName, dtos, vo, token);
                createSem(tableName, dtos, vo, token, simu);
            }
        });
        executor.shutdown();
    }
    public void createSem(String tableName, List<GridDto> dtos, CreateFilesSimuVo vo, String token) throws Exception {
        vo.setSemUrl(inPath + "\\" + vo.getSemUrl());
        boolean ends = vo.getSemUrl().endsWith("\\");
        if (!ends) {
            vo.setSemUrl(vo.getSemUrl() + "\\");
    public void createSem(String tableName, List<GridDto> dtos, CreateFilesSimuVo vo, String token, SimuPo simu) throws Exception {
        int code=1;
        try {
            vo.setSemUrl(inPath + "\\" + vo.getSemUrl());
            boolean ends = vo.getSemUrl().endsWith("\\");
            if (!ends) {
                vo.setSemUrl(vo.getSemUrl() + "\\");
            }
            File file = new File(vo.getSemUrl());
            if (!file.exists()) {
                file.mkdirs();
            }
            //初始化
            update(simu, code, null);
            code+=1;
            //降雨文件生成
            saveZarr(tableName, vo.getSemUrl());
            update(simu, code, null);
            code+=1;
            //管点生成
            pointTosem(dtos, token, vo.getSemUrl());
            update(simu, code, null);
            code+=1;
            //管线生成
            lineToSem(dtos, token, vo.getSemUrl());
            update(simu, code, null);
            code+=1;
            //范围生成
            gridToCityJson(dtos, vo.getSemUrl());
            update(simu, code, null);
            code+=1;
            //河流生成
            riverToSem(vo.getSemUrl());
            update(simu, code, null);
            code+=1;
            //土地利用生成
            landuseToSem(vo.getSemUrl());
            update(simu, code, null);
            code+=1;
            //地形生成
            terrainToSem(dtos, vo.getSemUrl());
            update(simu, 10, null);
            code+=1;
            //9=调用求解/10=求解分析
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            update(simu, -simu.getStatus(), SemErrorEnums.of(code));
        }
        File file = new File(vo.getSemUrl());
        if (!file.exists()) {
            file.mkdirs();
        }
        //降雨文件生成
        saveZarr(tableName, vo.getSemUrl());
        //管点生成
        pointTosem(dtos, token, vo.getSemUrl());
        //管线生成
        lineToSem(dtos, token, vo.getSemUrl());
        //范围生成
        gridToCityJson(dtos, vo.getSemUrl());
        //河流生成
        riverToSem(vo.getSemUrl());
        //土地利用生成
        landuseToSem(vo.getSemUrl());
        //地形生成
        terrainToSem(dtos, vo.getSemUrl());
    }
    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);
    }
    public String assemble(String name, String layerid, String bbox, String token) throws Exception {
        JSONObject jsonObject = EntityLibraryUtils.createAssemble(name, token);
@@ -855,7 +920,13 @@
        String pngPath = path + "appearance\\terrain.png";
        TiffToRGBUtil.tifToPng(tifPath, pngPath);
        JSONObject jsonObject = getModule("terrainmodule.json");
        jsonObject.put("vertices", TiffCoordinateExtractorUtil.getCoordinate(tifPath));
        JSONArray array=new JSONArray();
        array.add(ProjectionToGeographicUtil.getPoint(dtos.get(1).getLon(),dtos.get(1).getLat()));
        array.add(ProjectionToGeographicUtil.getPoint(dtos.get(2).getLon(),dtos.get(2).getLat()));
        array.add(ProjectionToGeographicUtil.getPoint(dtos.get(3).getLon(),dtos.get(3).getLat()));
        array.add(ProjectionToGeographicUtil.getPoint(dtos.get(0).getLon(),dtos.get(0).getLat()));
        //jsonObject.put("vertices", TiffCoordinateExtractorUtil.getCoordinate(tifPath));
        jsonObject.put("vertices", array);
        FileWriter fileWriter = new FileWriter(path + "terrain.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();