张洋洋
2025-01-23 440888a8ae5f0a6df4eba6710f096327d4d78c9c
src/main/java/com/se/simu/controller/SimuController.java
@@ -3,27 +3,28 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.io.Resources;
import com.se.simu.config.PropertiesConfig;
import com.se.simu.domain.dto.GeDb;
import com.se.simu.domain.dto.GeLayer;
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.CreateFilesSimuVo;
import com.se.simu.domain.vo.CreateSimuVo;
import com.se.simu.domain.vo.R;
import com.se.simu.domain.vo.SimuVo;
import com.se.simu.domain.vo.*;
import com.se.simu.service.*;
import com.se.simu.utils.CsvToSQLiteUtils;
import com.se.simu.utils.ShpToolUtils;
import com.se.simu.utils.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
@Api(tags = "仿真管理")
@Slf4j
@@ -48,6 +49,9 @@
    @Value("${simu-app.filePath}")
    private String uploadedFolder;
    @Value("${config.outPath}")
    private String outPath;
    @Resource
@@ -253,7 +257,7 @@
                return fail("解析范围文件失败!选择范围不能为空,请重新选择文件!", false);
            }
        }
        JSONArray jsonArray = new JSONArray();
        String stationFile = vo.getStationFile();
        log.info("站点文件shp地址:{}", stationFile);
        try {
@@ -262,7 +266,7 @@
                return fail("站点文件shp地址不能为空", false);
            }
            // 1 读取shp文件,获取站点坐标值
            JSONArray jsonArray = ShpToolUtils.readShpGetLocal(stationFile);
            jsonArray = ShpToolUtils.readShpGetLocal(stationFile);
            System.out.println("jsonArray = " + jsonArray);
        } catch (Exception e) {
            log.error("解析站点文件shp失败");
@@ -270,15 +274,16 @@
        String stationRainFile = vo.getStationRainFile();
        log.info("站点雨量CSV文件地址:{}", floodFile);
        JSONArray array = new JSONArray();
        // 创建表名 时间戳
        String tableName = "station_rain_" + System.currentTimeMillis();
        try {
            // 判断地址不为空
            if (StringUtils.isEmpty(stationRainFile)) {
                return fail("站点雨量CSV文件地址不能为空", false);
            }
            // 创建表名 时间戳
            String tableName = "station_rain_" + System.currentTimeMillis();
            // 1 读取CSV 文件
            CsvToSQLiteUtils.readCsvSaveLocal(stationRainFile, tableName);
            array = CsvToSQLiteUtils.readCsvSaveLocal(stationRainFile, tableName);
            // 获取仿真时间 duration
            Integer duration = CsvToSQLiteUtils.getDuration(tableName);
            log.info("仿真时间 duration = {}", duration);
@@ -336,5 +341,190 @@
        }
    }
    @ApiOperation(value = "tarr文件组装")
    @GetMapping(value = "/saveZarr", produces = "application/json; charset=UTF-8")
    public R<String> saveZarr(@RequestParam("name") String tableName) throws Exception {
        List<String> list = CsvToSQLiteUtils.getNameList(tableName);
        String path = outPath + "\\";
        String rainfall = "rainfall\\";
        String basePath = path + tableName + "\\" + rainfall;
        JSONObject jsonObject = getModule("rainfallmodule.json");
        for (String src : list
        ) {
            File directories = new File(basePath);
            if (!directories.exists()) {
                directories.mkdirs();
                System.out.println("Directories created successfully.");
            } else {
                System.out.println("Directories already exist.");
            }
            List<StationRainVo> stationRainVos = CsvToSQLiteUtils.getList(tableName, src);
            ZarrUtils.saveZarrRainfall(basePath + src, stationRainVos);
            ZarrUtils.saveZarrTime(basePath + src, stationRainVos);
            ZipUtils.toZarr(basePath + src, basePath + src + ".zip");
            System.out.println(src + "的zarr数据生成=====================");
            //json拼装
            String uuid = "UUID_" + UUID.randomUUID().toString();
            //拼装zarr
            JSONObject dynamizer = new JSONObject();
            dynamizer.put("url", rainfall.replace("\\", "/") + src + ".zarr");
            dynamizer.put("gmlId", uuid);
            jsonObject.getJSONArray("Dynamizers").add(dynamizer);
            //拼装坐标
            JSONArray vertice = new JSONArray();
            vertice.add(stationRainVos.get(0).getLongitude());
            vertice.add(stationRainVos.get(0).getLatitude());
            vertice.add(0.0);
            jsonObject.getJSONArray("vertices").add(vertice);
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+Rainfall");
            JSONObject attribute = new JSONObject();
            attribute.put("name", src);
            cityObject.put("attributes", attribute);
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiPoint");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            boundarie.add(jsonObject.getJSONArray("vertices").size()-1);
            metry.put("boundaries", boundarie);
            geometry.add(metry);
            cityObject.put("geometry", geometry);
            jsonObject.getJSONObject("CityObjects").put(uuid, cityObject);
        }
        File jsonFile = new File(path + tableName + "\\降雨量.json");
        if (jsonFile.exists()) {
            jsonFile.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(path + tableName + "\\降雨量.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        SemUtils.cityJsonToSem(path + tableName + "\\降雨量.json",path + tableName + "\\station_rain.sem");
        return success(path + tableName + "\\station_rain.sem");
    }
    @ApiOperation(value = "管点转cityjson")
    @GetMapping(value = "/pointToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> pointToCityJson(@RequestParam("shpPath") String shpPath) throws Exception {
        String path = outPath + "\\point\\";
        List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath);
        JSONObject jsonObject = getModule("pointmodule.json");
        for (Map<String, Object> map : list
        ) {
            //拼装坐标
            JSONArray vertice = new JSONArray();
            vertice.add(map.get("lon"));
            vertice.add(map.get("lat"));
            vertice.add(0.0);
            jsonObject.getJSONArray("vertices").add(vertice);
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+PipePoint");
            JSONObject attribute = new JSONObject();
            attribute.put("name", map.get("fsw"));
            cityObject.put("attributes", attribute);
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiPoint");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            boundarie.add(jsonObject.getJSONArray("vertices").size()-1);
            metry.put("boundaries", boundarie);
            geometry.add(metry);
            cityObject.put("geometry", geometry);
            jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject);
        }
        long times = System.currentTimeMillis();
        String pointPath = path + times + "\\管点.json";
        File dirFile = new File(path + times);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        SemUtils.cityJsonToSem(pointPath,path + times + "\\point.sem");
        return success(path + times + "\\point.sem");
    }
    @ApiOperation(value = "管线转cityjson")
    @GetMapping(value = "/lineToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> lineToCityJson(@RequestParam("shpPath") String shpPath) throws Exception {
        String path = outPath + "\\line\\";
        List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath);
        JSONObject jsonObject = getModule("linemodule.json");
        for (Map<String, Object> map : list
        ) {
            //拼装基础信息
            JSONObject cityObject = new JSONObject();
            cityObject.put("type", "+PipeLine");
            JSONObject attribute = new JSONObject();
            attribute.put("name", map.get("msfs"));
            JSONArray geometry = new JSONArray();
            JSONObject metry = new JSONObject();
            metry.put("type", "MultiLineString");
            metry.put("lod", 0);
            JSONArray boundarie = new JSONArray();
            JSONArray array = JSONObject.parseObject(map.get("the_geom").toString()).getJSONArray("coordinates");
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = JSONObject.parseObject(array.get(i).toString());
                jsonObject.getJSONArray("vertices").add(ProjectionToGeographicUtil.getPoint(Double.valueOf(object.get("x").toString()),Double.valueOf(object.get("y").toString())));
                boundarie.add(jsonObject.getJSONArray("vertices").size()-1);
            }
            JSONArray jsonArray=new JSONArray();
            jsonArray.add(boundarie);
            metry.put("boundaries", jsonArray);
            JSONArray metryArray=new JSONArray();
            metryArray.add(metry);
            cityObject.put("geometry",metryArray);
            cityObject.put("attributes",attribute);
            cityObject.put("attributes", attribute);
            geometry.add(metry);
            jsonObject.getJSONObject("CityObjects").put("UUID_" + UUID.randomUUID().toString(), cityObject);
        }
        long times = System.currentTimeMillis();
        String pointPath = path + times + "\\管线.json";
        File dirFile = new File(path + times);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        SemUtils.cityJsonToSem(pointPath, path + times + "\\line.sem");
        return success(path + times + "\\line.sem");
    }
    @ApiOperation(value = "地形转cityjson")
    @GetMapping(value = "/terrainToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> terrainToCityJson(@RequestParam("tifPath") String tifPath) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\terrain\\"+times+"\\";
        File dirFile = new File(path+"appearance");
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String pngPath=path+"appearance\\terrain.png";
        TiffToRGBUtil.tifToPng(tifPath,pngPath);
        JSONObject jsonObject = getModule("terrainmodule.json");
        jsonObject.put("vertices",TiffCoordinateExtractorUtil.getCoordinate(tifPath));
        FileWriter fileWriter = new FileWriter(path + "terrain.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        SemUtils.cityJsonToSem(path + "terrain.json", path + "terrain.sem");
        return success(path + "terrain.sem");
    }
    public JSONObject getModule(String moduleName) {
        JSONObject jsonObject = new JSONObject();
        try {
            URL resource = Resources.getResource(moduleName);
            String fileContent = Resources.toString(resource, StandardCharsets.UTF_8);
            jsonObject = JSONObject.parseObject(fileContent);
            System.out.println(fileContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}