Extract values from multiple rasters

I got a question how to create a loop function using the “for” command to process Worldclim layers, all with similar names (e.g. bio1-19, tmin1-12, tmax1-12, prec1-12). The loop would need to repeat for each iteration:

  • adding a column with the layer name (e.g. bio1)
  • extracting the data for that layer
  • repeat for next layer until iteration limit is reached

I guess this should be rather straightforward to implement as a bash script. But I am not very familiar with bash scripting, whereas this would (also) be very easy to implement in R (run from within GRASS GIS). So see below how this can be done in R. It is just a quick hack, I am sure there are faster or better ways, but it should work:

# Create a vector with all variable names first. In this example the variables included are the 19 bioclimatic variables (bio) and the monthly mean minimum temperatures (tmin)

vari1 vari2 vari
# You can create a new column each time you go through the loop, but I would create all the columns in once (columns are created in the attribute table of the vector layer points)

system(paste("v.db.addcol map=point columns='", paste(vari, "INTEGER", collapse=","),"'", sep=""))

# Then, use a loop to add the values to the respective columns of the vector map 'points'

for(i in 1:31){
system(paste("v.what.rast vect=points rast=",  vari[i], " col=", vari[i], sep=""))

If you rather want to use bash script, see my script to calculate bioclim data layers for an example how to use for loops. From that it shouldn’t be too difficult to create your own bash script doing something similar to the above. And if you do, maybe you can post it here in the comments?

Update: see in the comments section of this post for a bash script by Michael Curran

Interesting links: Check out this post for a batch script using awk and GRASS and this post that shows how to extract point data from raster files using R (and GRASS).


One thought on “Extract values from multiple rasters

  1. To complement, I wrote a function in Python.

    Since the points and bioclimatic variables are inside the GRASS GIS database, simply copy and paste those commands into cmd, change the names of the points and bioclimatic.

    # initiate python

    # import libraries
    import os
    import grass.script as grass


    # define function for extract multi points
    def extract_multi_points(input, raster):
    # input = points
    # raster = raster

    # name of colums
    colunm = str(raster)

    # add column
    grass.run_command(“g.message”, message = “–Add column–“)
    grass.run_command(“v.db.addcolumn”, map = input, columns = colunm + ” double precision”, quiet = True)

    # extract values
    grass.run_command(“g.message”, message = “–Extracting values–“)
    grass.run_command(“v.what.rast”, map = input, raster = raster, column = colunm, quiet = True)


    # list raster
    bio = grass.list_grouped(“rast”, pattern = “*bio*”)[“PERMANENT”]
    print ra

    # extract
    for i in bio:
    extract_multi_points(input = “points”, raster = i)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s