25

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?

2
  • Anup, github.com/mapbox/rasterio would give you numpy array(s) of your input data. You could split that using numpy o scipy methods and save each part to a new file easily. Would such solution qualify? If so, you need to say how the "uneven bits" (71680 is not cleanly divisible by 20000) should be treated. Commented Dec 16, 2016 at 11:46
  • In QGIS, if you have the Processing plugin installed and active, you get additional menu items on the Raster menu, one of which is Extraction. This has "Clip Raster by Extent" on it, which is a screen that allows you to draw a box around an area and save as a GeoTiff. This allowed me to drop unwanted areas on huge tiffs, and processing time dropped significantly. Commented Feb 2, 2022 at 9:07

4 Answers 4

34

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):

  1. Load the raster in the Layers Panel;
  2. Right-click on it and choose Save As...;
  3. Check the Create VRT option;
  4. Choose the folder where your outputs will be saved;
  5. Set the extent (if you want to work on the whole raster, don't modify anything);
  6. Choose if using the current resolution (I suggest to leave it as default);
  7. Set the max number of columns and rows (in your case, it should be 20000 columns and 2000 rows);
  8. Press the OK button.

enter image description here

For example, the using of the parameters in the above dialog on this sample raster (the parameters I set are chosen randomly):

enter image description here

will generate 100 tiles in the path specified at step 4:

enter image description here

Loading them in QGIS, they look like this:

enter image description here

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.

10
  • 3
    Just a quick comment that the example of how QGIS renders them looks weird just because the style fits itself to the distribution of values per image. The data is perfectly fine :) Commented Dec 16, 2016 at 10:06
  • Thanks, first of all for your answer really helpful, But i want achieve same result using python? Can you help me on that also Because it's more manual rather then programmatic so. Commented Dec 16, 2016 at 10:16
  • @bugmenot123 Thanks, I edit the answer with your useful comment.
    – mgri
    Commented Dec 16, 2016 at 11:30
  • @Anup Panwar, if I find a solution with PyQGIS, I'll edit my answer! =)
    – mgri
    Commented Dec 16, 2016 at 11:30
  • @HowToInQGIS Thanks, for else if you want a way in python that also will help :) Commented Dec 16, 2016 at 11:40
6

Don't split

You gdalbuildvrt, you can create virtual tiles that will only use a few bytes on your disk. Then you can use most softwares that will take your vrt's as input to perform your processing.

Alternatively, I would rather look for a tool that can work with a 1Gb image than split and merge an image. For instance, OTB has most of the capabilities for standard (and sometimes advanced) image processing with large images. You can wrap OTB for Python or run it from QGIS or from command lines.

6

Another solution using GDAL is the gdal_retile.py tool:

mkdir image_tiles
gdal_retile.py -v -r bilinear -levels 1 -ps 20000 20000 -co "TILED=YES" -co "COMPRESS=JPEG" -targetDir image_tiles big_input_image.tif

For more Information see: http://www.gdal.org/gdal_retile.html

1
  • Please note that this solution does not create separate files, but just adds internal reorganization of the raster data to allow quick access to various zoom levels and/or partial display of the raster.
    – ygesher
    Commented Dec 1, 2022 at 16:32
2

Using the Split Raster tool, you can split a raster into tiles based on a few methods of division including the size of each tile or the number of tiles.

Also see Splitting Raster in ArcGIS

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.