dcb
2025-05-28 115146311d2521fcb3b57c562df0f2a8e9972311
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) {