FOSS4G How-to: Sample Raster or Vector Values at Points in QGIS and GRASS

26 Jul 2017 ᛫ 5 min read

Share:


Let’s say we want to know the values of rasters and vectors at specific points in a point layer (e.g. household locations). How would we go about doing this in QGIS or GRASS?

Well, it’s actuallly pretty easy. You just need to know the right tools to use. QGIS has the Point sampling tool plugin for sampling rasters and vectors while GRASS has the v.what.rast or Sample raster maps at point locations module for sampling rasters and the v.what.vect or Update point attributes from areas module for sampling vectors.

Now let’s take a look at these tools.

For this how-to, we’ll be using the following layers:

  1. sample-points – a point (vector) layer
  2. solar-radiation – a (raster) layer of Global Horizontal Irradiance (GHI) values
  3. biomass-potential – a polygon (vector) layer of available Biomass potential

We’ll try to get the solar radiation and biomass potential values at the sample points.

Point sampling tool in QGIS

The point sampling tool collects polygon attributes and raster values from multiple layers at specified sampling points. It is a QGIS plugin that you can download from the QGIS Python Plugins Repository. You can check it out here.

If you need help with installing plugins, you can check this post.

Once installed, the plugin can be accessed via Plugins -> Analyses -> Point sampling tool.

DIRECTIONS:

Make sure that the layers you want to sample values from are active (checked).

Open the Point sampling tool.

On the General tab:

  1. Select the Layer containing sample points (sample-points).
  2. Select the Fields you want to get values from. You can select multiple fields using CTRL + click. This will be the fields of the output point vector (id from sample-points, Bn_Coconut and Bv_Coconut from the biomass-potential, Band1 of solar-radiation).
  3. Create a new Output point vector layer.
  4. Tick Add created layer to the TOC.

On the Fields tab:

  1. You can change the name of the fields of the output point vector. Be sure to make the field names unique. Vector field names also have a max length of 10 characters.

Click OK.

If the tool is successful, there should be a new point layer in the Layer panel whose fields are the ones you selected.

Open the attribute table of the created point layer to check.

Some important things to note with the Point sampling tool.

  1. It does not copy the sample points but creates a new point shapefile. Thus, if you want to include the fields of the sample points, you need to select them.
  2. It does not overwrite an existing point layer. Doing so will result in an error. You need to create a new point shapefile as output for the tool.
  3. It is not compatible with multipoint sources except if each multipoint contains exactly one point.

v.what.vect in GRASS

The v.what.vect module transfers attributes from the query_map’s map attribute table into the attribute table of points present in the map map. The script is based on v.distance. You can read more about it here.

Before running the module, we first need to prepare our sampling point layer. The v.what.vect module does not create new fields on the sampling point layer so we need to add new fields that will hold the sampled values if there aren’t ones yet.

Adding new fields in the attribute table is easy. You can type v.db.addcolumn in the Command console or open the Attribute table of the point layer (right-click on layer -> Show attribute data) and go to the Manage tables tab.

In the Manage tables tab you can add a Bn_Coconut and a Bv_Coconut field of type double.

Afterwards, the v.what.vect module can be accessed by typing v.what.vect in the Command console or via Vector -> Update attributes -> Update point attributes from areas [v.what.vect].

DIRECTIONS:

On the Required tab:

  1. Select the point layer we want to sample with (sample-points).
  2. Select the attribute column (field) that we want to update (e.g. the Bn_Coconut field we added earlier). If the dropdown button doesn’t work, you can just type in the attribute column name
  3. Select the vector layer we want to query (biomass-potential).
  4. Select the attribute column (field) that we want to query (Bn_Coconut). If the dropdown button doesn’t work, you can just type in the attribute column name

Click Run.

Do the same for the Bv_Coconut field.

If you are successful, the Bn_Coconut and Bv_Coconut fields of the sample-points layer should now have values. Open the attribute table to check.

v.what.rast in GRASS

The v.what.rast module retrieves raster value from a given raster map for each point or centroid stored in a given vector map. It can update a column in the linked vector attribute table with the retrieved raster cell value or print it. The column type needs to be numeric (integer, float, double, …). If the column doesn’t exist in the vector attribute table than the module will create the new column of type corresponding with the input raster map. You can read more about it here.

It can be accessed by typing v.what.rast in the Command console or via Vector -> Update attributes -> Sample raster maps at point locations [v.what.rast].

DIRECTIONS:

On the Required tab:

  1. Select the point layer we want to sample with (sample-points).
  2. Select the existing raster map to be queried (solar-radiation).

On the Optional tab:

  1. Select or set the name of the attribute column you want to update.
  2. Unlike v.what.vect, the v.what.rast module will create a new attibute column (field) if the attribute column to update does not exist in the sampling point layer.

Click Run

If you are successful, the GHI attribute column of sample-points should now have values. Open the attribute table to check.

Final thoughts

Unlike the Point sampling tool that creates a new point shapefile, the GRASS modules used for sampling just updates the attribute table of your sampling point layer.

If you’ll notice, the results we got from using the Point sampling tool in QGIS and v.what.vect and v.what.rast in GRASS are just the same.

That’s it. That’s how you can sample rasters and vectors using a point shapefile in QGIS and GRASS.

Stay tuned for the other installments of FOSS4G How-to and if you have any queries or requests, don’t hesitate to contact me.

Cheers!


Share:


You may also like:

Mapping Icebergs in QGIS



Hillshade in QGIS



#MakeTheSwitch: Choose GeoPackage



Connecting QGIS to Remote Services (WFS, WMS, GeoNode, OSM): Hazard Mapping with Phil-LiDAR 1 and Project NOAH Hazard Maps on the Web