Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colab - Split big tiff file using GDAL

I'm trying to split a huge tiff file into tiles using gdal on Colab.

My google drive is mounted and I can read and write from / into it.

The code is taken from this answer:

com_string = "gdal_translate -of GTIFF -srcwin" + ...
os.system(com_string)

The cell completes but no new files show up on the drive. Any ideas or another way to achieve the splitting of the file?

like image 565
eli Avatar asked Dec 18 '25 10:12

eli


1 Answers

This answer gives a suggestion:

You would need to come up with the pixel/line locations or corner coordinates and then loop over the values with gdal_translate.

import os, sys
from osgeo import gdal

dset = gdal.Open(sys.argv[1])

width = dset.RasterXSize
height = dset.RasterYSize

print width, 'x', height

tilesize = 5000

for i in range(0, width, tilesize):
    for j in range(0, height, tilesize):
        w = min(i+tilesize, width) - i
        h = min(j+tilesize, height) - j
        gdaltranString = "gdal_translate -of GTIFF -srcwin "+str(i)+", "+str(j)+", "+str(w)+", " \
            +str(h)+" " + sys.argv[1] + " " + sys.argv[2] + "_"+str(i)+"_"+str(j)+".tif"
        os.system(gdaltranString)

This, of course, relies on your gdal installation working properly. If the above doesn't work (you get no files still) try first running it in a location that isn't a mounted Google Drive. If that works, you know the problem is with your mounting. If not, next I would check to make sure you are getting the input image you are expecting with something like plt.imshow(your_source_image). If you see the image, then move on. If not, your source image is either missing or the path is wrong.

If it still doesn't work, I'd suspect it is a problem with your gdal installation. In that case I would first try a very simple function and make sure it gives the result you expect. You can also try running something else on Collab and make sure that works.

like image 199
Matthew Salvatore Viglione Avatar answered Dec 21 '25 03:12

Matthew Salvatore Viglione



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!