Note: This document is for an older version of GRASS GIS that will be discontinued soon. You should upgrade, and read the current manual page.
The Python expression is specified by the expression option.
Existing attribute values can be accessed in this expression using the column names.
For example, an expression place_name.split(",")[0]
would
uses Python string function split
on a value from column
place_name
assuming that column place_name is of SQL type
TEXT.
Attributes are accessible as variables using the column names as specified in the attribute table. By default, all attributes will be also accessible using the column name in all lower case. If this is not desired, -k flag can be used to keep only the original name and not provide the additional lower-cased version.
The types of variables in Python are int
and float
according if the attribute value can be represented by int
and
float
respectively. The str
type is used for all other values.
If other types (objects) are desired, they need to be constructed in explicitly.
The result of the expression needs to be something which can be converted
into string by the Python format function such as int
, float
,
or str
.
The Python math
package is loaded by default for convenience,
so expressions such as math.cos(column_name)
are possible without
further settings. Additional packages can be loaded using the option packages.
Multiple packages can be specified as a comma separated list, for example,
os,cmath,json
.
If the -s flag is specified, the imports of the packages specified by
option packages are additionally imported using a star import, i.e.,
import *
. This is considered a bad practice for general Python code,
but doing this might be helpful for constructing concise expressions.
The star import makes all functions (and other objects) from the package available
without the need to specify a package name. For example, packages set to
math
with -s allows us to write cos(column_name)
bringing the syntax closer to, e.g., raster algebra with r.mapcalc.
An arbitrary form of import statements, such as from math import cos
,
can be used with the Python file provided using the function option (see below).
name is null
.
The condition option is advantageous for more complex computations
where SQL does not provide enough functionality or in case consistency
with the Python syntax in the expression option is more desired than speed.
The code in the condition option has access to the same
variables, functions, and packages as the expression for computing the new value.
Syntactically, the where option is the SQL WHERE clause without the WHERE keyword,
while the condition option is Python if
statement
without the if
keyword and the trailing colon (:
).
Similarly to the SQL WHERE clause which selects the rows to be processed,
the condition option, when evaluated as True
for a given row,
selects that row to be processed.
If the condition evaluates as False
, the row is skipped (filtered out).
Both options can be used together. When none is specified, all rows (records) are updated.
For simple expressions, SQL-based v.db.update is much more advantageous.
The module uses only GRASS GIS interfaces to access the database, so it works for all
database backends used for attribute tables in GRASS GIS.
A future or alternative version may use, e.g., a more direct
create_function
function from Connection from the sqlite3 Python package.
If you are calling this module from Python, it is worth noting that you cannot pass directly functions defined or imported in your current Python file (Python module) nor access any of the variables. However, you can use string substitution to pass the variable values and a separate file with function definitions which you can also import into your code.
log_july
for a logarithm of values for July.
g.copy vector=precip_30ynormals,my_precip_30ynormals v.db.addcolumn map=my_precip_30ynormals columns="log_july double precision"
log
function
from the math
Python package (which is imported by default):
v.db.pyupdate map=my_precip_30ynormals column="log_july" expression="math.log(jul)"
v.db.select map=my_precip_30ynormals columns=jul,log_july
jul|logjuly 132.842|4.88916045210132 127|4.84418708645859 124.206|4.82194147751127 104.648|4.65060233738593 98.298|4.58800368106618 ...
v.db.pyupdate map=my_precip_30ynormals column="log_july" expression="log(jul)" packages=math -s
math
package needs to be explicitly requested.
v.db.pyupdate map=buildings column="name" expression="f'Building num. {building_number}'" where="name is null"
Available at: v.db.pyupdate source code (history)
Latest change: Monday Nov 11 18:04:48 2024 in commit: 59e289fdb093de6dd98d5827973e41128196887d
Main index | Vector index | Topics index | Keywords index | Graphical index | Full index
© 2003-2024 GRASS Development Team, GRASS GIS 8.3.3dev Reference Manual