dcb
2025-06-10 3b3c0d589b727769277af0fdde81129bcded377f
模拟返回结果增加最大最小水深数据
已修改3个文件
128 ■■■■■ 文件已修改
src/main/java/com/se/nsl/domain/dto/WaterDto.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/service/ResolveService.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/service/TestService.java 98 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/domain/dto/WaterDto.java
@@ -10,10 +10,10 @@
    @JSONField(serialize = false)
    private List<String> files;
    @JSONField(serialize = false)
//    @JSONField(serialize = false)
    private Double minHeight;
    @JSONField(serialize = false)
//    @JSONField(serialize = false)
    private Double maxHeight;
    private List<Long> data;
src/main/java/com/se/nsl/service/ResolveService.java
@@ -556,17 +556,6 @@
        return newLayerJsonName;
    }
//    private static void readDemData(File serviceNameDir, LayerDto layerDto) {
//        File dem = new File(serviceNameDir, "DEM.tif");
//        Dataset ds = gdal.Open(dem.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
//        Band band = ds.GetRasterBand(1);
//        int width = ds.getRasterXSize();
//        int height = ds.getRasterYSize();
//        float[] buffer = new float[width * height];
//        band.ReadRaster(0, 0, width, height, buffer);
//        layerDto.getTerrain().getVals().put(width + "_" + height, buffer);
//    }
    private static File generateNewZarr2TifJson(File serviceNameDir, long currentTime) throws IOException {
        File srcZarr2TifJson = new File(serviceNameDir, "zarr2tif.json");
        ObjectMapper mapper = new ObjectMapper();
@@ -574,7 +563,8 @@
        //修改zarr2tif对象中的字段
        String stamp = TimeFormatUtil.formatTime(currentTime, "yyyy-MM-dd HH:mm:ss");
        zarr2Tif.setStart_timestamp(stamp);
        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
//        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
        String newZarrPath = serviceNameDir + File.separator + "result.zarr";
        zarr2Tif.setZarr_file(newZarrPath);
        zarr2Tif.setGeotiff_dir(serviceNameDir + File.separator + "depth_" + currentTime);
        File newZarr2TifJson = new File(serviceNameDir, "zarr2tif_" + currentTime + ".json");
@@ -584,16 +574,16 @@
    private File generateNewZarrConfigFile(File serviceNameDir, String serviceName, long currentTime, File newDatFile) throws IOException {
        File configFile = new File(serviceNameDir, serviceName + ".json");
        String str = Files.readAllLines(configFile.toPath()).stream().collect(Collectors.joining());
        ConfigVo configVo = JSON.parseObject(str, ConfigVo.class);
        configVo.setDuration(300); //固定为5min
        ObjectMapper mapper = new ObjectMapper();
        ConfigVo configVo = mapper.readValue(configFile, ConfigVo.class);
        configVo.setDuration(configVo.getDuration() + 300); //固定为5min
        configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file
        ResultVo result = configVo.getResult();
        result.setSave_interval(60); //60s生成一帧
        result.setSave_frames(5); //保留5帧
        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
        result.setSave_frames(result.getSave_frames() + 5); //保留5帧,在原来的基础上增加5帧
//        String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr";
        String newZarrPath = serviceNameDir + File.separator + "result.zarr";
        result.setSave_name(newZarrPath);
        ObjectMapper mapper = new ObjectMapper();
        File newConfigFile = new File(serviceNameDir, currentTime + ".json");
        mapper.writeValue(newConfigFile, configVo);
        return newConfigFile;
src/main/java/com/se/nsl/service/TestService.java
@@ -7,14 +7,12 @@
import com.se.nsl.domain.po.SimuData;
import com.se.nsl.domain.vo.BuildingDepthVo;
import com.se.nsl.helper.ComHelper;
import com.se.nsl.helper.GdalHelper;
import com.se.nsl.utils.TimeFormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -23,10 +21,6 @@
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.List;
@@ -37,11 +31,11 @@
    @Resource
    PropertiesConfig config;
    public final static double MIN_VAL = 0.00001;
    public static final double MIN_VAL = 0.00001;
    public final static double MAX_X_OFFSET = 0;
    public static final double MAX_X_OFFSET = 0;
    public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public void test(SimuData data) throws IOException {
        String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator;
@@ -117,7 +111,8 @@
    }
    private void setSizes(Dataset ds, LayerDto layer) {
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
        int xSize = ds.getRasterXSize();
        int ySize = ds.getRasterYSize();
        double rate = xSize < ySize ? xSize * 1.0 / ySize : ySize * 1.0 / xSize;
        List<int[]> list = layer.getTerrain().getSize();
@@ -131,8 +126,8 @@
    }
    public void setTerrainInfo(Dataset ds, LayerDto layer) {
        Geometry minPoint = GdalHelper.getMinPoint(ds);
        Geometry maxPoint = GdalHelper.getMaxPoint(ds);
//        Geometry minPoint = GdalHelper.getMinPoint(ds);
//        Geometry maxPoint = GdalHelper.getMaxPoint(ds);
//        double minx = ComHelper.getMinVal(minPoint.GetX(0), 10000000);
//        double miny = ComHelper.getMinVal(minPoint.GetY(0), 10000000);
//        double maxx = ComHelper.getMaxVal(maxPoint.GetX(0) + MAX_X_OFFSET, 10000000);
@@ -161,11 +156,11 @@
            if (!new File(tif).exists()) continue;
            String png = terrainPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
            terrain2Png(layer, tif, png, sizes[0], sizes[1]);
        }
    }
    public void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
    public void terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
@@ -178,13 +173,16 @@
            layer.getTerrain().getVals().put(width + "_" + height, buffer);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight(), minHeight = layer.getExtension().getMinHeight();
            double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight();
            double minHeight = layer.getExtension().getMinHeight();
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int offset = x + y * width;
                    if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
                    int r = 0, g, b;
                    int r = 0;
                    int g;
                    int b;
                    if (buffer[offset] - layer.getExtension().getMaxHeight() > 0) {
                        g = b = 255;
                    } else {
@@ -207,7 +205,7 @@
    public void setWaterInfo(ResultDto dto, LayerDto layer) {
        List<String> files = getFiles(dto.getWaterPath(), ".tif");
        layer.getWaters().setFiles(files);
        if (null == files || files.size() == 0) return;
        if (null == files || files.isEmpty()) return;
        setWaterData(layer, files);
        setWaterHeight(layer, files);
@@ -215,11 +213,11 @@
    public List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) {
        List<String> files = layer.getWaters().getFiles();
        if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null;
        if (files.isEmpty() || files.size() != layer.getWaters().getData().size()) return Collections.emptyList();
        processWaters(dto, files, layer);
        return null;
        return Collections.emptyList();
    }
    public List<String> getFiles(String path, String suffix) {
@@ -240,35 +238,9 @@
        layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1));
    }
    /*public void setWaterData(LayerDto layer, List<String> files) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MILLISECOND, 0);
        for (String file : files) {
            String fileName = ComHelper.getNameWithExt(file);
            int year = Integer.parseInt(fileName.substring(0, 4));
            int month = Integer.parseInt(fileName.substring(4, 6));
            int day = Integer.parseInt(fileName.substring(6, 8));
            int hour = Integer.parseInt(fileName.substring(8, 10));
            int minute = Integer.parseInt(fileName.substring(10, 12));
            int second = Integer.parseInt(fileName.substring(12, 14));
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);
            layer.getWaters().getData().add(calendar.getTime().getTime());
        }
        layer.getDuration().setStart(layer.getWaters().getData().get(0));
        layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1));
    }*/
    public void setWaterHeight(LayerDto layer, List<String> files) {
        int c = files.size(), step = files.size() / 10;
        int c = files.size();
        int step = files.size() / 10;
        if (step < 1) step = 1;
        for (int i = 0; i < c; i += step) {
            Dataset ds = null;
@@ -337,24 +309,25 @@
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            //double differ = layer.getWaters().getMaxHeight() - layer.getWaters().getMinHeight(), minHeight = layer.getWaters().getMinHeight();
            double differ = layer.getExtension().getDiffer(), maxHeight = layer.getExtension().getMaxHeight(), minHeight = layer.getExtension().getMinHeight();
            double differ = layer.getExtension().getDiffer();
            double maxHeight = layer.getExtension().getMaxHeight();
            double minHeight = layer.getExtension().getMinHeight();
            float[] ts = layer.getTerrain().getVals().get(width + "_" + height);
            double minDepth = Double.MAX_VALUE;
            double maxDepth = Double.MIN_VALUE;
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int offset = x + y * width;
                    //if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
                    if (Float.isNaN(buffer[offset]) || buffer[offset] < MIN_VAL || Float.isNaN(ts[offset])) continue;
                    float depth = buffer[offset];
                    if (Float.isNaN(depth) || depth < MIN_VAL || Float.isNaN(ts[offset])) continue;
                    //double X = transform[0] + x * transform[1] + y * transform[2];
                    //double Y = transform[3] + x * transform[4] + y * transform[5];
                    //BuildingDto building = intersects(dto, X, Y);
                    //if (null != building) continue;
                    int r = 0, g, b;
                    if (buffer[offset] + ts[offset] > maxHeight) {
                    int r = 0;
                    int g;
                    int b;
                    if (depth + ts[offset] > maxHeight) {
                        g = b = 255;
                    } else {
                        int val = (int) ((buffer[offset] + ts[offset] - minHeight) / differ * 65535);
                        int val = (int) ((depth + ts[offset] - minHeight) / differ * 65535);
                        //int val = (int) (buffer[offset] / differ * 65535);
                        g = val / 256;
                        b = val % 256;
@@ -363,8 +336,17 @@
                    b = ComHelper.getSafeValue(b);
                    Color color = new Color(r, g, b, 127);
                    image.setRGB(x, y, color.getRGB());
                    if (minDepth > depth) {
                        minDepth = depth;
                    }
                    if (maxDepth < depth) {
                        maxDepth = depth;
                }
            }
            }
            layer.getWaters().setMinHeight(minDepth);
            layer.getWaters().setMaxHeight(maxDepth);
            ComHelper.savePng(image, png);
        } finally {
            if (null != ds) ds.delete();