I'm new to QGIS (using version 3.4). I'd like to make our raster files ready to use. There are 5k rasters in one folder with several subfolders. What's the best way to display them all in QGIS?

I thought that best would be to use a virtual raster. Because of the many subfolders and because I need to repeat the process of creating the virtual raster, I wanted to write a Python script. That's how far I've got trying to add only one file to the vrt:

from osgeo import gdal

input_File = r"D:\Folder\Raster.ecw"
output_File = r'C:\Folder\VRTFile.vrt'
gdal.BuildVRT (input_File, output_File)

This doesn't generate a traceback but also no file. What is wrong?

I also tried this:

import subprocess

with the same result.

(Also, how can I import all files from the folder? Can I use similar expressions as with arcpy?)

  • Welcome to GIS SE. As a new user, please take the Tour, which emphasizes the importance of asking One question per Question. Please focus on one issue at a time..
    – Vince
    Jan 22 '20 at 11:49
  • the problem has already been discussed here
    – BORUSSEN11
    Jan 22 '20 at 13:56
  • @BORUSSEN11 I've already seen this post before. That's how I came to one of the possible solutions. But it didn't work (as metioned in my question). Since the problem has been discussed already some time ago, I decided to open a new question. I hope, that's ok?
    – Qmountain
    Jan 22 '20 at 16:12

As I know now, the main problem solving my question was, that I didn't know where to run the commands. The commands mentioned in this post can be run in the OSGeo4W Shell. The commands I wrote in my question are wrong and don't work.

Anyhow, I also found an other solution using Python:

from qgis import processing
infiles = list()
outfile = r'C:/Users/VRTFinal.vrt'
txtfile = r"C:/Users/TXT.txt" # text file with all the file paths

infiles = [line.rstrip('\n') for line in open(txtfile)]
processing.run("gdal:buildvirtualraster", {'INPUT':infiles,'RESOLUTION':0,'SEPARATE':False,'PROJ_DIFFERENCE':False,'ADD_ALPHA':False,'ASSIGN_CRS':None,'RESAMPLING':0,'SRC_NODATA':'','OUTPUT':outfile})

This may be not the most elegant way to do it but it worked. One advantage: It also worked with .ecw files whereas it didn't with the OSGeo4W Shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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