foss4g how-to foss4g-how-to qgis 2.18.10 grass 7.2.1
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:
We’ll try to get the solar radiation and biomass potential values at the sample points.
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:
- Select the Layer containing sample points (sample-points).
- 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).
- Create a new Output point vector layer.
- Tick Add created layer to the TOC.
On the Fields tab:
- 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.
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:
- Select the point layer we want to sample with (sample-points).
- 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
- Select the vector layer we want to query (biomass-potential).
- 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.
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:
- Select the point layer we want to sample with (sample-points).
- Select the existing raster map to be queried (solar-radiation).
On the Optional tab:
- Select or set the name of the attribute column you want to update.
- 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.
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!
foss4g foss4gph qgis gis presentation
foss foss4g opensource freeasinfreedom freesoftware
foss4g foss4gph thesis uaap mapping the geography of the uaap spatial analytics basketball basketball analytics
foss4g foss4gph qgis gis presentation
If you find my website or any of the materials I share useful, you can consider donating to the cause below.
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)