I have an image of size 1GB (.tif), with the width and height 94000x71680. I would like to chunk this image into 20000X20000 tiles so that I can process them.
How can I do this?
Answer
I propose two solutions: the first one using QGIS, the second one using Python (GDAL).
Solution using QGIS
In QGIS you may create a VRT mosaic.
Please follow this procedure (see the image below):
- Load the raster in the Layers Panel;
- Right-click on it and choose
Save As...
; - Check the
Create VRT
option; - Choose the folder where your outputs will be saved;
- Set the extent (if you want to work on the whole raster, don't modify anything);
- Choose if using the current resolution (I suggest to leave it as default);
- Set the max number of columns and rows (in your case, it should be 20000 columns and 2000 rows);
- Press the
OK
button.
For example, the using of the parameters in the above dialog on this sample raster (the parameters I set are chosen randomly):
will generate 100 tiles in the path specified at step 4:
Loading them in QGIS, they look like this:
As @bugmenot123 correctly said in the comments, the result looks weird just because the style of each image fits itself to the distribution of values per image (but the data are perfectly fine).
Solution using Python (GDAL)
Another way to obtain the same result is the using of GDAL (gdal_translate).
With reference to the same example described above, you may use this script:
import os, gdal
in_path = 'C:/Users/Marco/Desktop/'
input_filename = 'dtm_5.tif'
out_path = 'C:/Users/Marco/Desktop/output_folder/'
output_filename = 'tile_'
tile_size_x = 50
tile_size_y = 70
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)
You obviously need to adapt the values to your specific case.
No comments:
Post a Comment