Extract raster values to vector points using GRASS GIS

One of my more frequently visited posts is about how to extract data at point locations in QGIS (see here). In GRASS GIS this can be done with the ‘Sample raster maps at point location’ function. This uploads raster values of one or more raster layers at positions of vector points to user-defined columns in the attribute table.

Below a short explanation how to do this using the menu or command line. In the example I have a point vector layer ‘samples’ and the raster layers ‘precipitation’ and ‘temperature’.

1a) The data of the raster(s) will be written to user-defined columns. Any data in the columns will be overwritten. Often you will want to have the data of the raster layers written to new columns rather than updating existing ones. That means you’ll need to create one column per raster layer:

Open the screen using the menu: Database → Manage Databases → Add columns
Or use the command line: v.db.addcol
Figure 1. Click on image for larger version

Select the point layer to upload the raster data and give the name and type of the columns you want to create. In the example above, I created two columns: PREC and TEMP, both with data type DOUBLE PRECISION. Next, click Run and you are done.

1b) Alternatively, you can open the point layer in the Layer Manager and open the attribute table (Figure 2).

Figure 2. Click on image for larger version

In the Attribute Table Manager screen open the second tab ‘Manage table’ and add the columns in the ‘Column name’ field (Figure 3).

Figure 3. Click on image for larger version

1c) Or, the easiest way, do it all at once on the command line:

v.db.addcol map=points@rwanda_work columns="PREC DOUBLE PRECISION, TEMP DOUBLE PRECISION"

2) To upload the raster values to the attribute table you can use the menu or command line:

Vector → Update point attributes from raster → Sample raster maps at point location
or type on the command line: v.what.rast

This will open the v.what.rast screen where you select the point layer, the name of the raster file to be queried and the column name to upload the raster values to (Figure 4)

Figure 4. Click on image for larger version

Repeat this for all raster layers. Alternatively, use the command line:

v.what.rast vect=points rast=precipitation col=PREC
v.what.rast vect=points rast=temperature col=TEMP

Easy enough, ain’t it :-). Update: if you want to do something similar, but for lines instead of points, see the comments below.


9 thoughts on “Extract raster values to vector points using GRASS GIS

  1. Pingback: Sampling raster values at point locations in QGIS « Ecostudies

  2. Great post pvanb!

    I’m looking to do something similar, but using vector lines rather than points. I can’t find an elegant way do to it. Any thoughts? I want (for example) the maximum slope (from a raster) assigned to a road (vector line).

    1. pvanb

      Not sure, but you might want to check v.segment or v.split in GRASS GIS. Both functions allow the creation of points along a line. I just had a quick look, but it seems you can define any distance between points. It requires one more step then when sampling at point locations, but especially if you use it in a script, it should be fairly straightforwards to implement.

      There is a plugin in QGIS (profile from line) that does the same, it creates a point file with points along the line(s) at an user-defined distance (using the mapping unit). You can use these points in QGIS to sample your rasters or import the point layer in GRASS using the GRASS GIS toolbox.

      The alternative is to convert your line vector layer to a raster, with the same resolution as the raster you want to sample (or you can use a higher resolution, but then you’ll need to resample your raster layer you want to sample too). You can then use a statistical overlay (see raster menu). Make sure to set the region settings to the required resolution first.

  3. Michael Curran

    I would like 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 am attempting this now, but would be interested to hear suggestions on how you would process this type of task.

  4. Michael Curran

    Update: My script worked a charm and looks like this:
    # to create a loop function for worldclim variables with similar naming, use the “for” statement. Construct one loop per variable type (bio, tmin, tmax, prec)

    # bioclimatic layers, bio1-19
    for i in $(seq 1 19)
    v.db.addcol map=borneo_geo_pristine columns=”bio$i double precision”
    v.what.rast vect=borneo_geo_pristine rast=bio$i col=bio$i

    #repeated with each variable type

    1. pvanb

      I see our comments crossed in cyberspace :-). Thanks for posting your example. Similar to the R script I posted. One difference is that I first create the columns, but that isn’t that much of a difference I think.

  5. Pingback: Extract values from multiple rasters | Ecostudies

  6. Pingback: Sample raster values at point location in QGIS – yet another way – Ecostudies

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s