1

I would like to create a new attribute based on another attribute using a formula in GRASS GIS.

I would like to do multiple 'if' functions embedded example:

if attribute x = 1.2: 1e-6
if attribute x = 2: 5e-6
if attribute x = 3: 1e-5

there would be as many embedded if functions as different values in my vector table. I can do this in MS Access or Excel, but I would like to do this directly in GRASS GIS.

2

If your GRASS database backend is sqlite (the default) then the parallel to if...then... is CASE...WHEN...THEN.... So the CASE statement would look something like (assuming you have a column attrib):

CASE attrib
WHEN 1.2 THEN 1e-6
WHEN 2 THEN 5e-6
END

and the full v.db.update in GRASS would be (assuming the vector is named "my_vect" and the new column is "new_attrib":

v.db.addcolumn map=my_vect column="new_attrib DOUBLE PRECISION"
db.execute sql="UPDATE my_vect SET new_attrib='CASE attrib WHEN 1.2 THEN 1e-6 WHEN 2 THEN 5e-6 END'"

But if there are many values in the attrib column, this will get very cumbersome. You might consider extracting all the values, preparing a text file of the new attrib values, then running the update in a loop:

v.db.select -c map=my_vect column="attrib" file=attribs.txt
v.db.addcolumn map=my_vect column="new_attrib DOUBLE PRECISION"
# Now, edit the attribs.txt file so that each line contains a pair of old_value new_value
# Then run v.db.update in a loop
while read old new; do
v.db.update map=my_vect column=new_attrib where="attrib=$old" value=$new
done < attribs.txt

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.