[GRASS-user] Grass SQLite driver & math funtions

Pietro peter.zamb at gmail.com
Thu Sep 26 07:46:46 PDT 2013


On Thu, Sep 26, 2013 at 2:18 PM, Enrico Gallo <enrico.gallo at gmail.com> wrote:
> So there is not a way to use this kind of funtions with vector attributes
> without compile extra library and load it in SQLIte?
> Any python workaround?

this example works only in grass7, otherwise you have to write your
python code to work directly with the sqlite table.

{{{
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.vector import VectorTopo

# copy the vector map
g.copy(vect=['railroads', 'foo'], overwrite=True)

# define the column
cols = [('power', 'double precision'),
        ('distance', 'double precision'),
        ('level', 'double precision')]

# instantiate the vector map
foo = VectorTopo('foo')
foo.open('rw')

# add the new columns
for cname, ctype in cols:
    foo.table.columns.add(cname, ctype)

# add some values
foo.table.execute('UPDATE foo SET power=80')
foo.table.execute('UPDATE foo SET distance=100')
foo.table.conn.commit()

#------------------------------------------------
# work around
import math

for geo in foo:
    geo.attrs['level'] = geo.attrs['power'] - math.log10(geo.attrs['distance'])

}}}

Note that this is far to be efficient...

If you compile sqlite including the support for the extra functions,
you simply need to substitute the workaroud with:

{{{
foo.table.execute('UPDATE foo SET level=power - log10(distance)')
foo.table.conn.commit()
}}}

Best regards

Pietro


More information about the grass-user mailing list