1

I'm trying to batch import files into the GRASS (v. 7.4.0) plugin in QGIS 2.18.18. I'm sure this is pretty basic, but I'm not sure quite how to do this. This exact question was asked before here, but it was for an earlier version of QGIS. Does anybody know how to to do this for 2.18.18?

EDIT: The rasters are different folders, all contained in the same parent folder (with all the same name no less...). I could add all of them in QGIS by dragging groups of folders into the window, is there anything like this for GRASS?

1

This is an easy task if you're willing to use the command line. The GRASS plugin offers the "GRASS shell" and of course you can use GRASS directly.

Using the Linux bash shell, the loop to import i.e. tif rasters from a certain directory would be:

BASEDIR="your/directory/of/tiffs"
cd $BASEDIR
# Import rasters
for f in *.tif; do
        r=`basename $f .tif`
        r.in.gdal input=$f output="${r}"
done

This can also be changed to run at a windows command prompt.

Also, take care to setup the GRASS Location in advance to match the coordinate system of the input files.

Addition for multiple input directories:

# Outer loop to go thru all directories
for d in /your/first/dir /your/second/dir /your/third/dir; do
cd ${d}
# Import rasters
for f in *.tif; do
   r=`basename $f .tif`
   r.in.gdal input=$f output="${r}"
done
done
  • Is there a command to do this if the files are in different folders? – Shane Apr 17 '18 at 5:17
  • You'll have to "tell" GRASS somehow where the rasters are located. You can do a loop thru the folder names, or anything that allows the script to read each. See my addition above to the original answer. – Micha Apr 18 '18 at 15:12

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.