0

I want to create a new attribute field contains data coming from a raster in a vector file. I tried the GRASS v.drape tool but the analysis did not create any new field in shapefile. Also, I tried the drape tool in QGIS and the result was the same.

0

THere are two tools to query a raster and upload values to a vector attribute table.

  1. If your vector is a points layer, then v.what.rast does that. You need to create the target attribute column in advance, with v.db.addcolumn.
  2. If your vector contains lines or polygons, and you want to calculate "zonal statistics", then use v.rast.stats. This module creates several new attribute columns with average, maximum, minimum, standard deviation, etc. These columns are named with a "prefix" that you specify.

If you post some further details about your needs, your OS, etc, then a more detailed answer will be possible.

TIP: Don't forget to set your computational region to match the vector extent at the start. See g.region to understand how.

Here's the procedure. I have a roads line vector and a dem raster. First I show the existing columns in the roads attrib table before v.rast.stats:

micha@TP480:~$ v.info -c roads
Data element 'vector/roads' was found in more mapsets (also found in
<PERMANENT>)
Using <roads@Har_Hanegev>...
Displaying column types/names for database connection of layer <1>:
INTEGER|cat
INTEGER|gid
INTEGER|id
INTEGER|length
CHARACTER|name
CHARACTER|type
CHARACTER|use
CHARACTER|yishuv

Set the region

micha@TP480:~$ g.region -p vect=roads2

Now run v.rast.stats

micha@TP480:~$ v.rast.stats roads2 rast=dem_4m column_prefix=elev
WARNING: No areas selected from vector map <roads2>
WARNING: Not all vector categories converted to raster. Converted 147 of
         312.
Processing input data (147 categories)...
 100%
Updating the database ...

and here are the new attrib columns:

micha@TP480:~$ v.info -c roads2
Displaying column types/names for database connection of layer <1>:
INTEGER|cat
INTEGER|gid
INTEGER|id
INTEGER|length
CHARACTER|name
CHARACTER|type
CHARACTER|use
CHARACTER|yishuv
DOUBLE PRECISION|elev_number
DOUBLE PRECISION|elev_null_cells
DOUBLE PRECISION|elev_minimum
DOUBLE PRECISION|elev_maximum
DOUBLE PRECISION|elev_range
DOUBLE PRECISION|elev_average
DOUBLE PRECISION|elev_stddev
DOUBLE PRECISION|elev_variance
DOUBLE PRECISION|elev_coeff_var
DOUBLE PRECISION|elev_sum
DOUBLE PRECISION|elev_first_quartile
DOUBLE PRECISION|elev_median
DOUBLE PRECISION|elev_third_quartile
DOUBLE PRECISION|elev_percentile_90

a small sample of the results:

micha@TP480:~$ v.db.select roads2 columns=elev_minimum,elev_maximum,elev_average,elev_stddev | head
elev_minimum|elev_maximum|elev_average|elev_stddev
-378.044708251953|-110.445526123047|-306.623860067546|81.2172675415943
-372.899230957031|-338.318267822266|-355.503255666097|8.52284278526592
-217.204177856445|-216.291137695312|-216.782756260463|0.324664221025042
-159.378814697266|-152.474395751953|-156.435197845877|1.78710129652147
-159.060150146484|-122.469970703125|-146.012489771125|11.1211311595026
| improve this answer | |
  • Thank you for your answer. I am using a line vector type. v.rast.stats works with just polygons and creates points. I also tried "add raster values to features" tool of SAGA but it works with using interpolation. I try to find a tool like "Add Surface Information" tool of ArcMap. Also, I am using GRASS 7.6.0. – jazzygis May 17 '19 at 10:46
  • No, v.rast.stats works on line vectors as well. It calculates for each line feature several univariate statistics: average, minimum, maximum, stddev, etc. Did you expect something else? – Micha May 17 '19 at 16:37
  • I am trying to obtain these statistics in input line vector attribute table. – jazzygis May 17 '19 at 20:14
  • I added to my answer an example of a full workflow to get raster values into a line vector attribute table. After your read the manual pages for each of the GRASS modules, the procedure should become clear. – Micha May 18 '19 at 13:01
  • It is worked. Thank you so much for your support. – jazzygis May 18 '19 at 16:00

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.