wiki:CatalogueForQIS

Version 2 (modified by brushtyler, 12 years ago) ( diff )

fix retcode and use a more pythonic syntax :)

When you have a set of raster that are a mosaic. If really useful a catalogue to use all ads un unique raster. Qgis is capable to use the VirtualCatalog of gdal.
To create a catalogue (on windows) usable from qgis 1.9, you can create a file ascii with a list of all the raster with their paths. Like this:

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 "my_list.txt for example)

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

After the catalog is create it could be used from qgs. But the gdalbuildvrt don't compute the statistics of the rasters in the catalogue and unfortunately qgis, if detect that the stats are not available in the catalog, try to compute they for each raster (ouch!). If the rasters are huge and on a shared remote folder. This step could be more time expensive.
So is really a good idea to add the stats to the catalogue before use it on qgis :)
Qgis really need only of min/max nothing else. Also to speed up this step you could use without any problem an approximated stats computing.
To add this approx-stats to the gdal 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 start fast and smart using the raster catalogue

Happy catalog-ing
Andrea Peri


Note: See TracWiki for help on using the wiki.