wiki:CatalogueForQIS

When you have a set of raster that are a mosaic. If really useful a catalogue to use all as a single raster. Qgis is capable to use the GDAL VirtualCatalog.
To create a catalogue (on windows) usable from qgis 1.9, you can create an ASCII file with a list of all the raster with their paths, e.g.:

D:\dati\folder1\folder2\folder_11\raster_1.tif
D:\dati\folder1\folder2\folder_12\raster_2.tif
D:\dati\folder1\folder2\folder_13\raster_3.tif
D:\dati\folder1\folder2\folder_13\raster_4.tif
D:\dati\folder1\folder2\folder_13\raster_5.tif
...

(save the file as e.g. "my_list.txt")

Please, notice that every raster could be on distinct path.
To create the catalog use this call.

gdalbuildvrt.exe -resolution average -allow_projection_difference -addalpha -hidenodata -input_file_list my_list.txt -overwrite my_catalog.vrt [[BR]]

After the catalog is created, it can be used from qgs. But the gdalbuildvrt doesn't compute the statistics of the rasters in the catalogue; unfortunately qgis, if detect that the stats are not available in the catalogue, tries to compute it for each raster (ouch!). If the rasters are huge and on a shared remote folder, this step can take very long.
So it is really a good idea to add the stats to the catalogue before use it on QGIS :)
QGIS needs only of min/max, nothing else. So, to speed up this step you could use without any problem an approximated stats computing.
To add this approx-stats to the catalogue you can use this simple and smart python code:

[Thanks to Giuseppe Sucameli & Faunalia for this smart code]

#!/usr/bin/env python

from osgeo import gdal
from gdalconst import *

def main(path, approx):
	ds = gdal.Open(path, GA_ReadOnly)
	if ds is None:
		return 1

	for i in range(ds.RasterCount):
		band = ds.GetRasterBand(i+1)
		band.ComputeStatistics(approx)

	ds = None
	return 0

if __name__ == "__main__":
	import sys

	approx = False
	path = None

	for i in range(1, len(sys.argv)):
		if sys.argv[i].lower() == "-approx":
			approx = True
		else:
			path = sys.argv[i]
			break

	if path is None:
		print "Usage:", sys.argv[0], "[-approx] <datasource>"
		sys.exit(0)

	ret = main(path, approx)
	sys.exit(ret)

Save it as "computestat.py" and use it with this sintax:

computestats.py -approx my_catalog.vrt

et voila! Now QGIS starts fast and smart using the raster catalogue.

Happy cataloguing!
Andrea Peri


Last modified 12 years ago Last modified on Apr 2, 2012, 2:08:35 AM
Note: See TracWiki for help on using the wiki.