dcb
2025-06-24 8c7daad24082b796f1f49b4e2b3f18d2d3c058bf
src/main/java/com/se/nsl/service/ResultService.java
@@ -14,7 +14,7 @@
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -38,9 +38,9 @@
    @Resource
    PropertiesConfig config;
    public final static double MAX_X_OFFSET = 0;
    public static final double MAX_X_OFFSET = 0;
    private 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 process(DataPo data) throws Exception {
        String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator;
@@ -57,14 +57,14 @@
        process(dto, layer);
    }
    private void process(ResultDto dto, LayerDto layer) throws Exception {
    public void process(ResultDto dto, LayerDto layer) throws Exception {
        try {
            copeTerrain(dto, layer);
            copeBuilding(dto, layer);
            copeBuilding(dto);
            List<BuildingDepthVo> buildings = copeWater(dto, layer);
            copeFlow(dto, layer);
            copeLayerJson(dto, layer);
            copeRainFallJson(dto, layer);
            copeRainFallJson(dto);
            copeBuildingDepthJson(dto, buildings);
        } finally {
            File dir = new File(dto.getTemp());
@@ -74,10 +74,10 @@
        }
    }
    private void copeTerrain(ResultDto dto, LayerDto layer) {
    public void copeTerrain(ResultDto dto, LayerDto layer) {
        Dataset ds = null;
        try {
            ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly);
            ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            setTerrainInfo(ds, layer);
@@ -88,7 +88,7 @@
        }
    }
    private void setTerrainInfo(Dataset ds, LayerDto layer) {
    public void setTerrainInfo(Dataset ds, LayerDto layer) {
        Geometry minPoint = GdalHelper.getMinPoint(ds);
        Geometry maxPoint = GdalHelper.getMaxPoint(ds);
        double minx = ComHelper.getMinVal(minPoint.GetX(0), 10000000);
@@ -104,41 +104,43 @@
        layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, mm[0], mm[1]));
    }
    private void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) {
    public void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) {
        String terrainPath = dto.getOutPath() + File.separator + "terrain";
        File f = new File(terrainPath);
        if (!f.exists() || !f.isDirectory()) f.mkdirs();
        for (int[] sizes : layer.getTerrain().getSize()) {
            String tif = dto.getTemp() + File.separator + "terrain_" + sizes[0] + "_" + sizes[1] + ".tif";
            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
            ComHelper.cutAndResample(ds, tif, sizes[0], sizes[1], layer, false);
            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]);
        }
    }
    private 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, gdalconst.GA_ReadOnly);
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            Band band = ds.GetRasterBand(1);
            float[] buffer = new float[width * height];
            //band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
            band.ReadRaster(0, 0, width, height, buffer);
            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 {
@@ -148,7 +150,6 @@
                    }
                    Color color = new Color(r, g, b, 127);
                    //graphic.drawImage(image, x, y, 1, 1, color, null);
                    image.setRGB(x, y, color.getRGB());
                }
            }
@@ -158,7 +159,7 @@
        }
    }
    private void copeBuilding(ResultDto dto, LayerDto layer) {
    public void copeBuilding(ResultDto dto) {
        Driver driver = null;
        DataSource dataSource = null;
        Layer shpLayer = null;
@@ -187,33 +188,33 @@
        }
    }
    private void setWaterInfo(ResultDto dto, LayerDto layer) {
    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);
    }
    private List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) {
    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 processBuilding(dto, files, layer);
    }
    private List<String> getFiles(String path, String suffix) {
    public List<String> getFiles(String path, String suffix) {
        List<String> files = new ArrayList<>();
        ComHelper.getFiles(files, new File(path), suffix);
        files.sort((a, b) -> a.compareToIgnoreCase(b));
        files.sort(String::compareToIgnoreCase);
        return files;
    }
    private void setWaterData(LayerDto layer, List<String> files) {
    public void setWaterData(LayerDto layer, List<String> files) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MILLISECOND, 0);
@@ -240,11 +241,11 @@
        layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1));
    }
    private void setWaterHeight(LayerDto layer, List<String> files) {
    public void setWaterHeight(LayerDto layer, List<String> files) {
        files.parallelStream().forEach(file -> {
            Dataset ds = null;
            try {
                ds = gdal.Open(file, gdalconst.GA_ReadOnly);
                ds = gdal.Open(file, gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                double[] mm = new double[2];
@@ -255,34 +256,30 @@
                if (null != ds) ds.delete();
            }
        });
        //layer.getExtension().setMinHeight(getMinVal(layer.getExtension().getMinHeight() - 1, 1000));
        //layer.getExtension().setMaxHeight(getMaxVal(layer.getExtension().getMaxHeight() + 1, 1000));
        //layer.getWaters().setMinHeight(getMinVal(layer.getWaters().getMinHeight() - 1, 1000));
        //layer.getWaters().setMaxHeight(getMaxVal(layer.getWaters().getMaxHeight() + 1, 1000));
        layer.getExtension().setMaxHeight(layer.getExtension().getMaxHeight() + layer.getWaters().getMaxHeight());
        layer.getExtension().setMaxHeight(ComHelper.getMaxVal(layer.getExtension().getMaxHeight(), 1000000));
        layer.getExtension().setMinHeight(ComHelper.getMaxVal(layer.getExtension().getMinHeight(), 1000000));
        layer.getExtension().setDiffer();
    }
    private void processWaters(ResultDto dto, List<String> files, LayerDto layer) {
    public void processWaters(ResultDto dto, List<String> files, LayerDto layer) {
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
                createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i));
                if (config.getCopyTif()) copyWaterTif(dto, ds, layer.getWaters().getData().get(i));
                createVectors(dto, ds, layer, layer.getWaters().getData().get(i));
                createVectors(dto, ds, layer.getWaters().getData().get(i));
            } finally {
                if (null != ds) ds.delete();
            }
        }
    }
    private void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
    public void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
        String waterPath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks;
        File dir = new File(waterPath);
        if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
@@ -290,28 +287,29 @@
        for (int[] sizes : layer.getTerrain().getSize()) {
            String fileName = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
            String tif = dto.getTemp() + File.separator + fileName + ".tif";
            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
            ComHelper.cutAndResample(ds, tif, sizes[0], sizes[1], layer, false);
            if (!new File(tif).exists()) continue;
            String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            water2Png(dto, layer, tif, png, sizes[0], sizes[1]);
            water2Png(layer, tif, png, sizes[0], sizes[1]);
        }
    }
    private void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) {
    public void water2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            Band band = ds.GetRasterBand(1);
            float[] buffer = new float[width * height];
            band.ReadRaster(0, 0, width, height, buffer);
            double[] transform = ds.GetGeoTransform();
            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);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
@@ -324,7 +322,9 @@
                    //BuildingDto building = intersects(dto, X, Y);
                    //if (null != building) continue;
                    int r = 0, g, b;
                    int r = 0;
                    int g;
                    int b;
                    if (buffer[offset] + ts[offset] > maxHeight) {
                        g = b = 255;
                    } else {
@@ -343,12 +343,12 @@
        }
    }
    private List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) {
    public List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) {
        List<BuildingDepthVo> list = new CopyOnWriteArrayList<>();
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) continue;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
@@ -361,18 +361,20 @@
        return list;
    }
    private void copeBuildingDepth(ResultDto dto, Dataset ds, long ticks, List<BuildingDepthVo> list) {
    public void copeBuildingDepth(ResultDto dto, Dataset ds, long ticks, List<BuildingDepthVo> list) {
        double[] transform = ds.GetGeoTransform();
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
        double minX = transform[0];
        double pixelWidth = transform[1];
        double maxY = transform[3];
        double pixelHeight = Math.abs(transform[5]);
        for (BuildingDto building : dto.getBuildingList()) {
            Double val = getValue(ds, building, xSize, ySize, minX, maxY, pixelWidth, pixelHeight);
            Double val = getValue(ds, building, minX, maxY, pixelWidth, pixelHeight);
            list.add(new BuildingDepthVo(building.getId(), ticks, val));
        }
    }
    private Double getValue(Dataset ds, BuildingDto building, int xSize, int ySize, double minX, double maxY, double pixelWidth, double pixelHeight) {
    public Double getValue(Dataset ds, BuildingDto building, double minX, double maxY, double pixelWidth, double pixelHeight) {
        double[] env = new double[4];
        building.getGeom().GetEnvelope(env);
@@ -396,20 +398,20 @@
        return ComHelper.isValid(val) ? val : null;
    }
    private void copyWaterTif(ResultDto dto, Dataset ds, long ticks) {
    public void copyWaterTif(ResultDto dto, Dataset ds, long ticks) {
        String source = ds.GetDescription();
        String target = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.tif";
        FileUtil.copyFile(source, target);
    }
    private void createVectors(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) {
    public void createVectors(ResultDto dto, Dataset ds, Long ticks) {
        String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "polygonize.geojson";
        ShpHelper.polygonize2Geojson(ds, filePath);
        List<Geometry> geometries = getGeometries(filePath);
        if (CollectionUtils.isEmpty(geometries)) return;
        List<PondingPo> list = copePonding(dto, ds, layer, geometries);
        List<PondingPo> list = copePonding(ds, geometries);
        if (CollectionUtils.isEmpty(list)) return;
        try {
@@ -420,18 +422,18 @@
        }
    }
    private List<Geometry> getGeometries(String filePath) {
        if (!FileUtil.exist(filePath)) return null;
    public List<Geometry> getGeometries(String filePath) {
        if (!FileUtil.exist(filePath)) return Collections.emptyList();
        Driver driver = null;
        DataSource dataSource = null;
        org.gdal.ogr.Layer layer = null;
        try {
            driver = ogr.GetDriverByName("GeoJSON");
            if (null == driver) return null;
            if (null == driver) return Collections.emptyList();
            DataSource ds = driver.Open(filePath);
            if (null == ds) return null;
            if (null == ds) return Collections.emptyList();
            layer = ds.GetLayer(0);
            List<Geometry> list = new ArrayList<>();
@@ -445,32 +447,25 @@
            return list;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
            return Collections.emptyList();
        } finally {
            GdalHelper.delete(layer, dataSource, driver);
        }
    }
    private List<PondingPo> copePonding(ResultDto dto, Dataset ds, LayerDto layer, List<Geometry> geometries) {
    public List<PondingPo> copePonding(Dataset ds, List<Geometry> geometries) {
        double[] transform = ds.GetGeoTransform();
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
        List<PondingPo> list = new ArrayList<>();
        for (Geometry geometry : geometries) {
            //List<PointDto> points = getValues(ds, geometry, transform, xSize, ySize);
            //if (CollectionUtils.isEmpty(points)) continue;
            //PointDto point = Collections.max(points);
            PointDto point = getValues(ds, geometry, transform, xSize, ySize);
            PointDto point = getValues(ds, geometry, transform);
            if (null == point) continue;
            list.add(new PondingPo(geometry, point));
        }
        return list;
    }
    private PointDto getValues(Dataset ds, Geometry g, double[] transform, int xSize, int ySize) {
    public PointDto getValues(Dataset ds, Geometry g, double[] transform) {
        double[] env = new double[4];
        g.GetEnvelope(env);
@@ -494,14 +489,14 @@
        return new PointDto(g.Centroid().GetX(), g.Centroid().GetY(), ComHelper.isValid(val) ? val : 0.0);
    }
    private void copeFlow(ResultDto dto, LayerDto layer) {
    public void copeFlow(ResultDto dto, LayerDto layer) {
        List<String> files = getFiles(dto.getFlowPath(), ".tif");
        if (null == files || files.size() != layer.getWaters().getData().size()) return;
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
@@ -512,7 +507,7 @@
        }
    }
    private void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
    public void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
        String flowPath = dto.getOutPath() + File.separator + "flows" + File.separator + ticks;
        File dir = new File(flowPath);
        if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
@@ -520,21 +515,22 @@
        for (int[] sizes : layer.getTerrain().getSize()) {
            String name = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
            String tif = dto.getTemp() + File.separator + name + ".tif";
            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
            ComHelper.cutAndResample(ds, tif, sizes[0], sizes[1], layer, false);
            if (!new File(tif).exists()) continue;
            String png = flowPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            vxyTif2Png(layer, tif, png, sizes[0], sizes[1]);
            vxyTif2Png(tif, png, sizes[0], sizes[1]);
        }
    }
    private void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
    public void vxyTif2Png(String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
            float[] vxBuffer = new float[width * height];
            float[] vyBuffer = new float[width * height];
            ds.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer);
            ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vyBuffer);
@@ -544,7 +540,7 @@
        }
    }
    private void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
    public void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
@@ -568,43 +564,37 @@
        ComHelper.savePng(image, png);
    }
    private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
    public void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
        layer.getWaters().setFiles(null);
        layer.getTerrain().setEpsg(null);
        layer.getExtension().setDiffer(null);
        String json = JSON.toJSONString(layer);
        // String json = JSONUtil.toJsonPrettyStr(layer);
        String filePath = dto.getOutPath() + File.separator + "layer.json";
        ComHelper.writeJson(filePath, json);
    }
    public void copeRainFallJson(ResultDto dto, LayerDto layer) throws IOException, ParseException {
    public void copeRainFallJson(ResultDto dto) throws IOException, ParseException {
        String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat";
        String filePath = dto.getOutPath() + File.separator + "rainfall.json";
        Map<String, Double> map = new LinkedHashMap<>();
        FileReader fr = new FileReader(rainGageFilePath);
        BufferedReader br = new BufferedReader(fr);
        try (FileReader fr = new FileReader(rainGageFilePath);
             BufferedReader br = new BufferedReader(fr)) {
        String line = br.readLine();
        while ((line = br.readLine()) != null) {
            String[] rainFall = line.split(" ");
            if (rainFall.length < 7) continue;
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] rainFall = line.split(" ");
                if (rainFall.length < 7) continue;
            String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5];
            BigDecimal num = new BigDecimal(rainFall[6]);
            map.put("" + SDF.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue());
                String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5];
                BigDecimal num = new BigDecimal(rainFall[6]);
                map.put("" + SDF.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue());
            }
        }
        br.close();
        fr.close();
        String json = JSON.toJSONString(map);
        ComHelper.writeJson(filePath, json);
    }
    private void copeBuildingDepthJson(ResultDto dto, List<BuildingDepthVo> list) throws IOException {
    public void copeBuildingDepthJson(ResultDto dto, List<BuildingDepthVo> list) throws IOException {
        if (CollectionUtils.isEmpty(list)) return;
        String json = JSON.toJSONString(list);