Vector points, lines and areas usually have attribute data that are stored in DBMS. The attributes are linked to each vector object using a category number (attribute ID, usually the "cat" integer column). The category numbers are stored both in the vector geometry and the attribute table.
GRASS GIS supports various RDBMS (Relational database management system) and embedded databases. SQL (Structured Query Language) queries are directly passed to the underlying database system. The set of supported SQL commands depends on the RDMBS and database driver selected.
Note: The list of available database drivers can vary in various binary distributions of GRASS GIS:
sqlite | Data storage in SQLite database files (default DB backend) | http://sqlite.org/ |
dbf | Data storage in DBF files | http://shapelib.maptools.org/dbf_api.html |
pg | Data storage in PostgreSQL RDBMS | http://postgresql.org/ |
mysql | Data storage in MySQL RDBMS | http://mysql.org/ |
odbc | Data storage via UnixODBC (PostgreSQL, Oracle, etc.) | http://www.unixodbc.org/ |
ogr | Data storage in OGR files | http://gdal.org/ogr/ |
[A-Za-z][A-Za-z0-9_]*
d.vect map where="individual='juvenile' and area='beach'"
d.vect trapping_sites_points fcol=black icon=basic/diamond col=white size=13 \ where="valley <> 'LAMAR' OR (valley = 'LAMAR' AND description = 'extensive trapping')"
Select all attributes from table where str1 column values are not 'No Name':
echo "SELECT * FROM archsites WHERE str1 <> 'No Name'" | db.select
Example of subquery expressions from a list (not supported for DBF driver):
v.db.select mysites where="id IN ('P04', 'P05')"
# field contains string: # for DBF driver: v.extract rivers out=rivers_noce where="DES LIKE 'NOCE'" # for SQLite driver: v.extract rivers out=rivers_noce where="DES LIKE '%NOCE%'" # match exactly number of characters (here: 2), does not work for DBF driver: v.db.select mysites where="id LIKE 'P__'" #define wildcard: v.db.select mysites where="id LIKE 'P%'"
v.db.addcolumn map=roads col="nulltest int" v.db.update map=roads col=nulltest value=1 where="cat > 2" d.vect roads where="nulltest is null" v.db.update map=roads col=nulltest value=2 where="cat <= 2"
v.db.addcolumn map=roads col="exprtest double precision" v.db.update map=roads col=exprtest value=cat/nulltest v.db.update map=roads col=exprtest value=cat/nulltest+cat where=cat=1
Examples of complex expressions in updates (using db.* modules):
echo "UPDATE roads SET exprtest=null" echo "UPDATE roads SET exprtest=cat/2" | db.execute echo "UPDATE roads SET exprtest=cat/2+cat/3" | db.execute echo "UPDATE roads SET exprtest=NULL WHERE cat>2" | db.execute echo "UPDATE roads SET exprtest=cat/3*(cat+1) WHERE exprtest IS NULL" | db.execute"
Instead of creating and updating new columns with an expression, you can use the expression directly in a command:
d.vect roads where="(cat/3*(cat+1))>8" d.vect roads where="cat>exprtest"
North Carolina data set: convert string column to double precision:
# first copy map into current mapset g.copy vect=geodetic_pts,mygeodetic_pts v.db.addcolumn mygeodetic_pts col="zval double precision" # the 'z_value' col contains 'N/A' strings, not to be converted v.db.update mygeodetic_pts col=zval \ qcol="CAST(z_value AS double precision)" \ where="z_value <> 'N/A'"
v.db.update vectormap column=column3 qcolumn="column1 || column2"
Database management in GRASS GIS, Help pages for database modules
Last changed: $Date$