ArchGDAL.read error: GDALError (CE_Failure, code 4):not recognized as a supported file format

Hi,
I am currently stuck with a strange issue in GDAL and do not understand how to solve it. I have a method inside an IO module:

read_image_file(filename::AbstractPath) = AG.read(string(filename))

This method is being called from another method:

function write_cog(output_path::SystemPath, dataset_path::AbstractPath)
    dataset = read_image_file(dataset_path)
    AG.copy(dataset; filename = string(output_path), driver = AG.getdriver("COG"))
end

and here it fails giving me the error

Exception has occurred: GDAL.GDALError
GDALError (CE_Failure, code 4):
	`.../temp.tif' not recognized as a supported file format.

When I run the same code through the debugger, it works well. Not sure how to solve this issue. Any help is much appreciated.

I find this solution in Stack Overflow:

but don’t see gdal.AllRegister() equivalent in ArchGDAL, could someone here point me in the right direction.

GDAL.jl has a gdalallregister function, but that is automatically called during module initialization, so that’s not the problem here. It looks more like your TIFF file might not be a GeoTIFF? GDAL cannot read TIFF files without geospatial metadata. You could check by trying if the file linked in this example can be loaded: Raster Data · ArchGDAL.jl.

I can confirm that the temp.tif file is created and I can read it fine using ArchGDAL.read from the REPL

@visr thanks, I was able to resolve the issue, the problem was coming from this function I had:

function _save_array(
    input_dtm_path::AbstractPath,
    array2D::AbstractMatrix{T},
    temp_dir::AbstractPath,
) where {T<:Real}
    # Read the DTM geotiff and extract the geotransform and projection
    dataset = read_image_file(input_dtm_path)
    projection = AG.getproj(dataset)
    geotransform = AG.getgeotransform(dataset)
    (m, n) = size(array2D)

    # Create a temporary dataset and write array2D to it
    # gds_out = AG.create(
    #     string(temp_dir),
    #     driver = AG.getdriver("GTiff"),
    #     width = m,
    #     height = n,
    #     nbands = 1,
    #     options = [
    #         "COMPRESS=DEFLATE",
    #         "COPY_SRC_OVERVIEWS=YES",
    #         "OVERVIEW_COMPRESS=DEFLATE",
    #     ],
    #     dtype = T,
    # )
    # AG.setgeotransform!(gds_out, geotransform)
    # AG.setproj!(gds_out, projection)
    # AG.write!(gds_out, array2D[:, :], 1)
    opts = ["COMPRESS=DEFLATE", "COPY_SRC_OVERVIEWS=YES", "OVERVIEW_COMPRESS=DEFLATE"]
    AG.create(
        string(temp_dir),
        driver = AG.getdriver("GTiff"),
        width = m,
        height = n,
        nbands = 1,
        dtype = T,
        options = opts,
    ) do ds
        band = AG.getband(ds, 1)
        AG.write!(band, array2D[:, :])
        AG.setgeotransform!(ds, geotransform)
        AG.setproj!(ds, projection)
        AG.destroy(
            AG.copy(
                dataset,
                filename = string(temp_dir),
                driver = AG.getdriver("GTiff"),
                options = opts,
            ),
        )

    end
    return nothing

end

I made updates to it as per your advice on GitHub

2 Likes