I have been using this answer on GIS SE to split 3-band (RGB) imagery into 256×256 3-band image tiles:
import os, gdal
in_path = '/path/to/indata/'
input_filename = 'dtm_5.tif'
out_path = '/path/to/output_folder/'
output_filename = 'tile_'
tile_size_x = 256
tile_size_y = 256
ds = gdal.Open(in_path + input_filename)
band = ds.GetRasterBand(1)
xsize = band.XSize
ysize = band.YSize
for i in range(0, xsize, tile_size_x):
for j in range(0, ysize, tile_size_y):
com_string = "gdal_translate -of GTIFF -srcwin " + str(i)+ ", " + str(j) + ", " + str(tile_size_x) + ", " + str(tile_size_y) + " " + str(in_path) + str(input_filename) + " " + str(out_path) + str(output_filename) + str(i) + "_" + str(j) + ".tif"
os.system(com_string)
Is there a comparable Rasterio or numpy approach?
Best Answer
Below is a simple example (
rasterio
1.0.0 or later, won't work in 0.3.6). There might be better/simpler ways (and there is an easier way if your raster is internally tiled and the tile block sizes match your desired output tile size).The
rasterio
docs have some examples of concurrent processing if you want to go down that road.