dcb
2025-05-28 115146311d2521fcb3b57c562df0f2a8e9972311
根据不同的区域类型选择不同的地形
已添加1个文件
已修改4个文件
139 ■■■■ 文件已修改
src/main/java/com/se/nsl/config/PropertiesConfig.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/service/ResolveService.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/service/SimuService.java 68 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/utils/AreaType.java 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-dev.yml 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/nsl/config/PropertiesConfig.java
@@ -98,6 +98,8 @@
    private Integer epsg;
    private String keyDitch;
    public String getVer() {
        return ver;
    }
@@ -453,4 +455,12 @@
    public void setLanduseFile(String landuseFile) {
        this.landuseFile = landuseFile;
    }
    public String getKeyDitch() {
        return keyDitch;
    }
    public void setKeyDitch(String keyDitch) {
        this.keyDitch = keyDitch;
    }
}
src/main/java/com/se/nsl/service/ResolveService.java
@@ -14,6 +14,7 @@
import com.se.nsl.helper.GdalHelper;
import com.se.nsl.helper.StringHelper;
import com.se.nsl.helper.WebHelper;
import com.se.nsl.utils.AreaType;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
@@ -154,6 +155,29 @@
        createDir(inPath + File.separator + "velocity");
        createDir(config.getOutPath() + File.separator + data.getOutPath());
        Short areaType = simu.getAreaType();
        AreaType at = AreaType.of(areaType);
        String terrainTif = config.getSourceDem();
        String landuseTif = config.getSourceLanduse();
        if (at == AreaType.KEY_DITCH) {
            File keyDitchDir = new File(config.getKeyDitch());
            String areaName = simu.getAreaName();
            Optional<File> first = Arrays.stream(keyDitchDir.listFiles()).filter(f -> f.getName().equals(areaName)).findFirst();
            if (first.isPresent()) {
                File targetTifDir = first.get();
                File[] files = targetTifDir.listFiles();
                for (File file : files) {
                    String name = file.getName();
                    if (name.toLowerCase().contains("dem")) {
                        terrainTif = file.getAbsolutePath();
                    }
                    if (name.toLowerCase().contains("landuse")) {
                        landuseTif = file.getAbsolutePath();
                    }
                }
            }
        }
        Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
        if (geom.GetGeometryType() == ogr.wkbMultiPolygon) geom = geom.GetGeometryRef(0);
        SpatialReference dstSR = GdalHelper.createSpatialReference(config.getEpsg());
@@ -163,12 +187,14 @@
        String wkt = geom.ExportToWkt();
        String terrainFile = inPath + File.separator + config.getTerrainFile();
        Dataset dsDem = gdal.Open(config.getSourceDem(), gdalconstConstants.GA_ReadOnly);
        Dataset dsDem = gdal.Open(terrainTif, gdalconstConstants.GA_ReadOnly);
        ComHelper.Resample(dsDem, null, terrainFile, null, wkt, null, null);
        dsDem.delete();
        String landuseFile = inPath + File.separator + config.getLanduseFile();
        Dataset dsLanduse = gdal.Open(config.getSourceLanduse(), gdalconstConstants.GA_ReadOnly);
        Dataset dsLanduse = gdal.Open(landuseTif, gdalconstConstants.GA_ReadOnly);
        ComHelper.Resample(dsLanduse, null, landuseFile, null, wkt, null, null);
        dsLanduse.delete();
    }
src/main/java/com/se/nsl/service/SimuService.java
@@ -134,7 +134,7 @@
//        System.out.println(String.format("转换前的坐标:x:%s,y:%s", lon, lat));
//        System.out.println(String.format("转换后的坐标:x:%s,y:%s", xy[0], xy[1]));
        //read from zarr
//        return null;
//        return queryByZarr(xy, time, serviceName);
        //read from tif file
        return queryByTif(xy, time, serviceName);
@@ -146,28 +146,29 @@
        double y = xy[1];
        String prefix = formatTime(time);
        File inPath = new File(config.getInPath());
        File tifDir = new File(inPath, serviceName + File.separator + "depth");
        int index = 0;
        File[] files = tifDir.listFiles();
        for (File file : files) {
            String name = file.getName();
            if (!name.endsWith(TIF_EXTSION)) continue;
            if (name.equals(prefix + TIF_EXTSION)) {
                break;
            }
            index++;
        }
        //TODO需要计算行列号
        int col = 0;
        int row = 0;
//        File tifDir = new File(inPath, serviceName + File.separator + "depth");
//        int index = 0;
//        File[] files = tifDir.listFiles();
//        for (File file : files) {
//            String name = file.getName();
//            if (!name.endsWith(TIF_EXTSION)) continue;
//            if (name.equals(prefix + TIF_EXTSION)) {
//                break;
//            }
//            index++;
//        }
        File dem = new File(inPath, serviceName + File.separator + "DEM.tif");
        ColumnRow cr = getColumnRow(dem.getAbsoluteFile(), x, y);
        if (cr == null) return null;
        File zarr = new File(inPath, serviceName + File.separator + "result.zarr");
        try {
            ZarrGroup group = ZarrGroup.open(zarr.toPath());
            ZarrArray depthArray = group.openArray("depth");
            Object object = depthArray.read();
            System.out.println(object);
            int[] shape = new int[] {60, 637, 351};
//            int[] offset = new int[]{210, 384};
            float[] depth = (float[]) depthArray.read(shape);
            System.out.println("depth:" + depth.length);
        } catch (IOException | InvalidRangeException e) {
            throw new RuntimeException(e);
        }
@@ -187,6 +188,18 @@
            return null;
        }
        ColumnRow cr = getColumnRow(tifFile, x, y);
        if (cr == null) return null;
        System.out.println("col:" + cr.col + " ,row:" + cr.row);
        float depth = readPixelValue(cr.dataset, cr.col, cr.row, 1);
        float velocity = calcVelocity(cr.dataset, cr.col, cr.row);
        SimuResult result = new SimuResult();
        result.setDepth(depth);
        result.setVelocity(velocity);
        return  result;
    }
    private static ColumnRow getColumnRow(File tifFile, double x, double y) {
        Dataset dataset = gdal.Open(tifFile.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
        // èŽ·å–åœ°ç†å˜æ¢å‚æ•°ï¼ˆ6元素数组)
        // [0]: å·¦ä¸Šè§’X坐标, [1]: åƒå…ƒå®½åº¦, [2]: X方向旋转,
@@ -201,12 +214,19 @@
            log.warn("行列号不在tif范围内");
            return null;
        }
        float depth = readPixelValue(dataset, col, row, 1);
        float velocity = calcVelocity(dataset, col, row);
        SimuResult result = new SimuResult();
        result.setDepth(depth);
        result.setVelocity(velocity);
        return  result;
        return new ColumnRow(dataset, col, row);
    }
    private static class ColumnRow {
        public final Dataset dataset;
        public final int col;
        public final int row;
        public ColumnRow(Dataset dataset, int col, int row) {
            this.dataset = dataset;
            this.col = col;
            this.row = row;
        }
    }
    private float calcVelocity(Dataset dataset, int col, int row) {
src/main/java/com/se/nsl/utils/AreaType.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,28 @@
package com.se.nsl.utils;
/**
 * åŒºåŸŸç±»åž‹
 */
public enum AreaType {
    CUSTOM(0),
    ADMINISTRATIVE_AREA(1),
    KEY_AREA(2),
    KEY_DITCH(3);
    private int val;
    AreaType(int val) {
        this.val = val;
    }
    public static AreaType of(int val) {
        AreaType[] values = values();
        for (AreaType type : values) {
            if (type.val == val) {
                return type;
            }
        }
        return AreaType.ADMINISTRATIVE_AREA;
    }
}
src/main/resources/application-dev.yml
@@ -169,3 +169,6 @@
  sourceLanduse: D:\other\simu\CudaUWSolver-2.0\Beijing-Data-10m\Beijing-4548-Landuse-10m-Nonodata.tif
  copyTif: false
  tifPath: D:\other\simu\uwsolver\5ca43c87cd8e48c5a9c5399a5da46dbc\tongzhou_raster_4548_1m_clip_river_fill.tif
  #tif中的高程文件名要含有dem,土地利用要含有landuse,站点要含有station,不限制大小写,文字尽量用英文字母表达
  keyDitch: D:\other\simu\CudaUWSolver-2.0\KeyDitch