SundaySee
2025-05-15 e040d561d798536f666fffbe33f9b43ecbed5bba
src/main/java/com/se/nsl/helper/ComHelper.java
@@ -61,7 +61,7 @@
        //vector.add("-te_srs");
        //vector.add("EPSG:" + 4326);
        vector.add("-r");
        vector.add("bilinear");
        vector.add("bilinear"); // 双线性插值
        vector.add("-of");
        vector.add("GTiff");
        WarpOptions warpOptions = new WarpOptions(vector);
@@ -70,6 +70,73 @@
        destDs.delete();
    }
    public static void Resample2(Dataset ds, String dest, int width, int height, LayerDto layer) {
        Vector<String> vector = new Vector<>();
        vector.add("-s_srs");
        vector.add("EPSG:" + 4548);
        vector.add("-t_srs");
        vector.add("EPSG:" + 4326);
        vector.add("-ts");
        vector.add("" + width);
        vector.add("" + height);
        //vector.add("-te");
        //vector.add("" + layer.getExtension().getMinx());
        //vector.add("" + layer.getExtension().getMiny());
        //vector.add("" + layer.getExtension().getMaxx());
        //vector.add("" + layer.getExtension().getMaxy());
        //vector.add("-te_srs");
        //vector.add("EPSG:" + 4326);
        vector.add("-r");
        vector.add("average");
        vector.add("-of");
        vector.add("GTiff");
        WarpOptions warpOptions = new WarpOptions(vector);
        Dataset destDs = gdal.Warp(dest, new Dataset[]{ds}, warpOptions);
        updateLayerExtension(destDs, layer);
        destDs.delete();
    }
    private static void updateLayerExtension(Dataset ds, LayerDto layer) {
        double[] bbox = readTifBbox(ds);
        double minLon = bbox[0];
        double maxLon = bbox[1];
        double minLat = bbox[2];
        double maxLat = bbox[3];
        ExtensionDto extension = layer.getExtension();
        double minx = extension.getMinx();
        double miny = extension.getMiny();
        double maxx = extension.getMaxx();
        double maxy = extension.getMaxy();
        if (minx > minLon) extension.setMinx(minLon);
        if (miny > minLat) extension.setMiny(minLat);
        if (maxx < maxLon) extension.setMaxx(maxLon);
        if (maxy < maxLat) extension.setMaxy(maxLat);
    }
    private static double[] readTifBbox(Dataset ds) {
        // 1. 获取图像尺寸
        int width = ds.getRasterXSize();
        int height = ds.getRasterYSize();
        // 2. 获取GeoTransform参数
        double[] geoTransform = new double[6];
        ds.GetGeoTransform(geoTransform);
        // 3. 解析GeoTransform参数(经纬度坐标)
        double originLon = geoTransform[0];  // 左上角经度
        double originLat = geoTransform[3];  // 左上角纬度
        double pixelWidth = geoTransform[1];  // 经度方向分辨率(度/像素)
        double pixelHeight = geoTransform[5]; // 纬度方向分辨率(度/像素,通常为负)
        // 4. 计算四至范围(经纬度)
        double minLon = Math.min(originLon, originLon + width * pixelWidth);
        double maxLon = Math.max(originLon, originLon + width * pixelWidth);
        double minLat = Math.min(originLat, originLat + height * pixelHeight);
        double maxLat = Math.max(originLat, originLat + height * pixelHeight);
        return new double[] {minLon, maxLon, minLat, maxLat};
    }
    public static BuildingDto intersects(ResultDto dto, double x, double y) {
        Geometry p = new Geometry(ogr.wkbPoint);
        p.AddPoint_2D(x, y);