Java使用gdal解析tif文件获取外边框BOX信息

maven依赖,jar可自行下载

<dependency>
    <groupId>org.gdal</groupId>
    <artifactId>gdal</artifactId>
    <version>3.6.3</version>
</dependency>

在启动类或者解析之前执行以下代码

        // 注册所有的驱动
        ogr.RegisterAll();
        // 为了支持中文路径,请添加下面这句代码
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
        // 为了使属性表字段支持中文,请添加下面这句
        gdal.SetConfigOption("SHAPE_ENCODING","");
public void processTif() throws IOException {

        // 打开 dataset,path是tif文件路径
        Dataset dataset = gdal.Open(path);

        // 检查 dataset 是否成功打开
        if (dataset == null) {
            System.err.println("Failed to open the GeoTIFF file.");
            System.exit(1);
        }

        // 获取外边框信息
        double[] geotransform = dataset.GetGeoTransform();
        double topLeftX = geotransform[0];
        double topLeftY = geotransform[3];
        double pixelWidth = geotransform[1];
        double pixelHeight = geotransform[5];
        int width = dataset.GetRasterXSize();
        int height = dataset.GetRasterYSize();
        double bottomRightX = topLeftX + width * pixelWidth;
        double bottomRightY = topLeftY + height * pixelHeight;

        // 输出外边框信息
        System.out.println("Bottom Left: (" + bottomRightX + ", " + bottomRightY + ")");
        System.out.println("Top Right: (" + topLeftX + ", " + topLeftY + ")");

        // Close the dataset
        dataset.delete();
    }

你可能感兴趣的:(gdal,Java,gdal,java)