张洋洋
2025-02-18 5c87778c13e3e33bb30edfcc344e5e69754bc9bf
src/main/java/com/se/simu/controller/SimuController.java
@@ -27,6 +27,7 @@
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@@ -244,10 +245,6 @@
                        Double minY = jsonObject.getDouble("minX");
                        Double maxY = jsonObject.getDouble("maxX");
                        // 3 判断范围值是否为空
                        // vo.setMinx(jsonObject.getDouble("minX"));
                        // vo.setMaxx(jsonObject.getDouble("maxX"));
                        // vo.setMiny(jsonObject.getDouble("minY"));
                        // vo.setMaxy(jsonObject.getDouble("maxY"));
                        vo.setMinx(jsonObject.getDouble("minY"));
                        vo.setMaxx(jsonObject.getDouble("maxY"));
                        vo.setMiny(jsonObject.getDouble("minX"));
@@ -340,6 +337,15 @@
                    return fail("防汛范围不能为空", false);
                }
            }
            if (StringUtils.isEmpty(vo.getSemUrl())) {
                return fail("sem文件生成路径不能为空!", false);
            }
            List<GridDto> dtos = new ArrayList<>();
            dtos.add(new GridDto(vo.getMiny(), vo.getMinx()));
            dtos.add(new GridDto(vo.getMaxy(), vo.getMinx()));
            dtos.add(new GridDto(vo.getMaxy(), vo.getMaxx()));
            dtos.add(new GridDto(vo.getMiny(), vo.getMaxx()));
            createSem(tableName, dtos, vo);
            // 开始模拟计算
            boolean flag = simuFilesService.createByfiles(vo);
            return success(flag, flag ? "成功" : "失败");
@@ -348,14 +354,90 @@
        }
    }
    @ApiOperation(value = "tarr文件组装")
    @GetMapping(value = "/saveZarr", produces = "application/json; charset=UTF-8")
    public R<String> saveZarr(@RequestParam("name") String tableName) throws Exception {
    @ApiOperation(value = "获取点路径")
    @GetMapping("/getPointUrl")
    public R<String> getPointUrl(@RequestParam("id") Integer id) throws Exception{
        SimuPo po = simuService.getSimuById(id);
        String token = EntityLibraryUtils.login();
        if (po != null) {
            return success(po.getPointUrl().replace("{token}",token));
        }
        return success(null);
    }
    @ApiOperation(value = "获取线路径")
    @GetMapping("/getLineUrl")
    public R<String> getLineUrl(@RequestParam("id") Integer id) throws Exception{
        SimuPo po = simuService.getSimuById(id);
        String token = EntityLibraryUtils.login();
        if (po != null) {
            return success(po.getLinkUrl().replace("{token}",token));
        }
        return success(null);
    }
    public R<Boolean> createSem(String tableName, List<GridDto> dtos, CreateFilesSimuVo vo) throws Exception {
        String name = vo.getSemUrl();
        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();
        }
        String token = EntityLibraryUtils.login();
        //降雨文件生成
        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());
        String bbox = "";
        //生成点和线的实体库模型并保存路径
        vo.setPointName(name + "-point");
        vo.setLinkName(name + "-line");
        JSONArray min = ProjectionToGeographicUtil.get4548Point(dtos.get(0).getLon(), dtos.get(0).getLat());
        JSONArray max = ProjectionToGeographicUtil.get4548Point(dtos.get(2).getLon(), dtos.get(2).getLat());
        bbox = bbox + min.getDouble(0) + "," + min.getDouble(1) + "," + max.getDouble(0) + "," + max.getDouble(1);
        JSONObject point = getModule("layerQueryPointParams.json");
        String pointUrl = assemble(vo.getPointName(), point.getString("layerid"), bbox, token);
        vo.setPointName(pointUrl);
        JSONObject link = getModule("layerQueryParams.json");
        String linkUrl = assemble(vo.getLinkName(), link.getString("layerid"), bbox, token);
        vo.setLinkName(linkUrl);
        return success(true);
    }
    public String assemble(String name, String layerid, String bbox, String token) throws Exception {
        JSONObject jsonObject = EntityLibraryUtils.createAssemble(name, token);
        String packageid = jsonObject.getString("packageid");
        EntityLibraryUtils.addAssemble(layerid, token, packageid, bbox);
        JSONObject result = EntityLibraryUtils.submitAssemble(token, packageid);
        Random random = new Random();
        int randomNumber = random.nextInt(90000) + 10000;
        String url = "http://106.120.22.26:8024/geo-service/package/3d/normal/85257774fdb64e5f99f6778696cad02a/" + packageid + "/" + layerid + "//l0/tileset.json?r=" + randomNumber + "&token={token}";
        return url;
    }
    public R<String> saveZarr(String tableName, String semUrl) throws Exception {
        List<String> list = CsvToSQLiteUtils.getNameList(tableName);
        String path = outPath + "\\";
        String rainfall = "rainfall\\";
        String basePath = path + tableName + "\\" + rainfall;
        JSONObject jsonObject = getModule("rainfallmodule.json");
        String startTime = null;
        String endTime = null;
        for (String src : list
        ) {
            File directories = new File(basePath);
@@ -366,6 +448,12 @@
                System.out.println("Directories already exist.");
            }
            List<StationRainVo> stationRainVos = CsvToSQLiteUtils.getList(tableName, src);
            if (startTime == null) {
                startTime = stationRainVos.get(0).getDatetime();
            }
            if (endTime == null) {
                endTime = stationRainVos.get(stationRainVos.size() - 1).getDatetime();
            }
            ZarrUtils.saveZarrRainfall(basePath + src, stationRainVos);
            ZarrUtils.saveZarrTime(basePath + src, stationRainVos);
            ZipUtils.toZarr(basePath + src, basePath + src + ".zip");
@@ -407,21 +495,27 @@
        FileWriter fileWriter = new FileWriter(path + tableName + "\\降雨量.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + tableName + "\\降雨量.json", filePatn + "raingage.sem");
        return success(filePatn + "raingage.sem");
        SemUtils.cityJsonToSem(path + tableName + "\\降雨量.json", semUrl + "raingage.sem");
        getConfigJson(startTime, endTime, semUrl);
        return success(semUrl + "raingage.sem");
    }
    @ApiOperation(value = "管点转sem")
    @PostMapping(value = "/pointTosem", produces = "application/json; charset=UTF-8")
    public R<String> pointTosem(@RequestBody List<GridDto> dtos) throws Exception {
    public void getConfigJson(String startTime, String endTime, String semUrl) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date start = sdf.parse(startTime);
        Date end = sdf.parse(endTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        JSONObject jsonObject = getModule("config.json");
        jsonObject.put("start_datetime", simpleDateFormat.format(start));
        jsonObject.put("end_datetime", simpleDateFormat.format(end));
        FileWriter fileWriter = new FileWriter(semUrl + "\\config.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
    }
    public R<String> pointTosem(List<GridDto> dtos, String token, String semUrl) throws Exception {
        String path = outPath + "\\point\\";
        String publicKey = EntityLibraryUtils.getPublicKey();
        String token = EntityLibraryUtils.login();
        JSONArray jsonArrayList = EntityLibraryUtils.getPointInfo(dtos, token);
        JSONObject jsonObject = getModule("pointmodule.json");
        for (int s = 0; s < jsonArrayList.size(); s++) {
@@ -484,6 +578,49 @@
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        SemUtils.cityJsonToSem(pointPath, semUrl + "node.sem");
        return success(semUrl + "node.sem");
    }
    public R<String> pointToCityJson(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();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
@@ -492,61 +629,10 @@
        SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem");
        return success(filePatn + "node.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();
//        String filePatn = inPath + "\\tongzhou\\";
//        File file = new File(filePatn);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        SemUtils.cityJsonToSem(pointPath, filePatn + "node.sem");
//        return success(filePatn + "node.sem");
//    }
    @ApiOperation(value = "管线sem生成")
    @PostMapping(value = "/lineToSem", produces = "application/json; charset=UTF-8")
    public R<String> lineToSem(@RequestBody List<GridDto> dtos) throws Exception {
    public R<String> lineToSem(List<GridDto> dtos, String token, String semUrl) throws Exception {
        String path = outPath + "\\line\\";
        String publicKey = EntityLibraryUtils.getPublicKey();
        String token = EntityLibraryUtils.login();
        JSONArray jsonArrayList = EntityLibraryUtils.getLineInfo(dtos, token);
        if (jsonArrayList.size() == 0) {
            return fail("该区域不存在管线!");
@@ -635,18 +721,11 @@
        FileWriter fileWriter = new FileWriter(pointPath);
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(pointPath, filePatn + "link.sem");
        return success(filePatn + "link.sem");
        SemUtils.cityJsonToSem(pointPath, semUrl + "link.sem");
        return success(semUrl + "link.sem");
    }
    @ApiOperation(value = "管线转cityjson")
    @GetMapping(value = "/lineToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> lineToCityJson(@RequestParam("shpPath") String shpPath) throws Exception {
    public R<String> lineToCityJson(String shpPath) throws Exception {
        String path = outPath + "\\line\\";
        List<Map<String, Object>> list = ShpReadUtils.readPointShp(shpPath);
        JSONObject jsonObject = getModule("linemodule.json");
@@ -697,9 +776,48 @@
        return success(filePatn + "link.sem");
    }
    @ApiOperation(value = "地形转cityjson")
    @GetMapping(value = "/terrainToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> terrainToCityJson(@RequestParam("tifPath") String tifPath) throws Exception {
    public R<String> terrainToSem(List<GridDto> dtos, String semUrl) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\terrain\\" + times + "\\";
        File dirFile = new File(path + "appearance");
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        double minx = 180.0;
        double maxx = 0.0;
        double miny = 180.0;
        double maxy = 0.0;
        for (GridDto dto : dtos
        ) {
            if (dto.getLon() > maxx) {
                maxx = dto.getLon();
            }
            if (dto.getLon() < minx) {
                minx = dto.getLon();
            }
            if (dto.getLat() > maxy) {
                maxy = dto.getLat();
            }
            if (dto.getLat() < miny) {
                miny = dto.getLat();
            }
        }
        JSONArray min = ProjectionToGeographicUtil.get4548Point(minx, miny);
        JSONArray max = ProjectionToGeographicUtil.get4548Point(maxx, maxy);
        String tifPath = path + "terrain.tif";
        TiffClipper.cropTiffByLatLon(config.getTifPath(), tifPath, min.getDouble(0), min.getDouble(1), max.getDouble(0), max.getDouble(1));
        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", semUrl + "terrain.sem");
        return success(semUrl + "terrain.sem");
    }
    public R<String> terrainToCityJson(String tifPath) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\terrain\\" + times + "\\";
        File dirFile = new File(path + "appearance");
@@ -722,9 +840,7 @@
        return success(filePatn + "terrain.sem");
    }
    @ApiOperation(value = "grid转cityjson")
    @PostMapping(value = "/gridToCityJson", produces = "application/json; charset=UTF-8")
    public R<String> gridToCityJson(@RequestBody List<GridDto> dtos) throws Exception {
    public R<String> gridToCityJson(List<GridDto> dtos, String semUrl) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\grid\\" + times + "\\";
        File dirFile = new File(path);
@@ -745,18 +861,11 @@
        FileWriter fileWriter = new FileWriter(path + "grid.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "grid.json", filePatn + "grid.sem");
        return success(filePatn + "grid.sem");
        SemUtils.cityJsonToSem(path + "grid.json", semUrl + "grid.sem");
        return success(semUrl + "grid.sem");
    }
    @ApiOperation(value = "river转sem")
    @GetMapping(value = "/riverToSem", produces = "application/json; charset=UTF-8")
    public R<String> riverToSem() throws Exception {
    public R<String> riverToSem(String semUrl) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\river\\" + times + "\\";
        File dirFile = new File(path);
@@ -767,18 +876,11 @@
        FileWriter fileWriter = new FileWriter(path + "river.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "river.json", filePatn + "river.sem");
        return success(filePatn + "river.sem");
        SemUtils.cityJsonToSem(path + "river.json", semUrl + "river.sem");
        return success(semUrl + "river.sem");
    }
    @ApiOperation(value = "landuse转sem")
    @GetMapping(value = "/landuseToSem", produces = "application/json; charset=UTF-8")
    public R<String> landuseToSem() throws Exception {
    public R<String> landuseToSem(String semUrl) throws Exception {
        long times = System.currentTimeMillis();
        String path = outPath + "\\landuse\\" + times + "\\";
        File dirFile = new File(path);
@@ -789,13 +891,8 @@
        FileWriter fileWriter = new FileWriter(path + "landuse.json");
        fileWriter.write(jsonObject.toJSONString());
        fileWriter.close();
        String filePatn = inPath + "\\tongzhou\\";
        File file = new File(filePatn);
        if (!file.exists()) {
            file.mkdirs();
        }
        SemUtils.cityJsonToSem(path + "landuse.json", filePatn + "landuse.sem");
        return success(filePatn + "landuse.sem");
        SemUtils.cityJsonToSem(path + "landuse.json", semUrl + "landuse.sem");
        return success(semUrl + "landuse.sem");
    }
    public JSONObject getModule(String moduleName) {