0

I'm relatively new in GRASS GIS and I want to add a column to a shapefile.

I use GRASS GIS (version 7.8.6 on Ubuntu 20.04 .4 LTS), not the GRASS extension in QGIS.

I'm using the shapefile of the biogeographical realms. In this file the realms are identified by a string in the column REALM. I would like to assign a number to the corresponding string in a new column named REALM_ID.

What I want is something like that:

REALM REALM_ID
PA 1
NT 2
PA 1
NA 3

1 Answer 1

0

It's quite easy to add a column. The GRASS module is v.db.addcolumn. The tricky part in your question is creating numbers for each unique value of REALM. Here's how I would do it: (Let's say you have a GRASS vector called "bio_realms" and you're working in a Linux environment)

#First get a list of the unique values in the existing REALM column
realm_list=`db.select "SELECT DISTINCT REALM FROM bio_realms;"`
# Create the new column
v.db.addcolumn bio_realms column="realm_id INTEGER" 
# Now loop through all realms and update the new column,
# but keep a counter for the realm_id value
i=0
for r in $realm_list; do i=$((i+1)); v.db.update bio_realms column=realm_id value=$i where="REALM == '${r}'"; done

That should do it. If you're on some other operating system, you'll have to adapt the variables and looping appropriately.

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.