[GIS] Opening a BigTIFF file in Python

bigtiffgdalgeotiff-tiffpythonraster

I am trying to open and extract data from a 90MB TIFF file using Python. The code I'm using is the following:

from osgeo import gdal, osr, ogr

def get_value_at_point(rasterfile, pos):
    gdal.UseExceptions()
    gdata = gdal.Open(rasterfile)
    gt = gdata.GetGeoTransform()
    data = gdata.ReadAsArray().astype(np.float)
    gdata = None

    x = int((pos[0] - gt[0])/gt[1])
    y = int((pos[1] - gt[3])/gt[5])
    return data[x, y]

However, I'm getting the following error message:

RuntimeError: This is a BigTIFF file.  BigTIFF is not supported by this version of GDAL and libtiff.

Any hints on how to overcome this? I'm using Anaconda and my GDAL version is 2.3.3 and the libtiff version is 4.0.10

Best Answer

A couple of things here:

Firstly don't set gdata to None until you are COMPLETELY done with that raster, data will return a broken dataset if you do. Please see for a more detailed explanation: https://trac.osgeo.org/gdal/wiki/PythonGotchas

Next thing uninstall gdal using pip then reinstall it using the .whl file from here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal Let me know if you need an explanation how to do this.

Also 90MB is -tiny- it shouldn't need to be a bigtiff till it goes over 4GB when compressed.

Also just to make everything a bit faster/memory efficient:

from osgeo import gdal, osr, ogr

def get_value_at_point(rasterfile, pos):
    gdal.UseExceptions()
    gdata = gdal.Open(rasterfile)
    gt = gdata.GetGeoTransform()
    x = int((pos[0] - gt[0])/gt[1])
    y = int((pos[1] - gt[3])/gt[5])

    data = gdata.ReadAsArray(x,y).astype(np.float) #This will only load the cell you want
    return data