[GRASS-user] Fwd: vector attribute calculation in grass python

alassane toure atourej at gmail.com
Mon Aug 29 15:22:58 PDT 2016


It works great....thanks Stefan!

On Mon, Aug 29, 2016 at 1:23 PM, Blumentrath, Stefan <
Stefan.Blumentrath at nina.no> wrote:

> Hei Alassane,
> Can't you just do:
>
> db.execute sql="UPDATE parcel SET pchng=int((0.25*c_number/par_sqf)*100)"
>
> Cheers,
> Stefan
> ________________________________________
> Von: grass-user [grass-user-bounces at lists.osgeo.org] im Auftrag von
> alassane toure [atourej at gmail.com]
> Gesendet: Montag, 29. August 2016 20:25
> An: GRASS user list
> Betreff: [GRASS-user] Fwd: vector attribute calculation in grass python
>
> Dear Moritz & group,
> I am a newbi in python programming.  Following Moritz's instructions i
> added the followings..
>
> par_sqf=[]
> c_number=[]
> pchng=[]
>
> but pchng stayed NULL for all rows (see below)!
> [Inline image 2]
> Any suggestions how to resolve this?
>
> Regards,
> Alassane
> ---------- Forwarded message ----------
> From: alassane toure <atourej at gmail.com<mailto:atourej at gmail.com>>
> Date: Mon, Aug 29, 2016 at 9:27 AM
> Subject: vector attribute calculation in grass python
> To: GRASS user list <grass-user at lists.osgeo.org<mailto:
> grass-user at lists.osgeo.org>>
>
>
> Group,
> I am in the process of developing a python/grass code to calculate an
> attribute value based on other attributes. The following error comes up:
>
> NameError: name 'par_sqf' is not defined
>
>
> Here is the code...
>
> # some predefined variables
> ###########################
> import os
> import sys
> import subprocess
> import shutil
> import binascii
> import tempfile
> import shutil
>
> import gdal
> from gdalconst import *
> ...
> ...
> grass.run_command('v.rast.stats',map='parcel',
> raster='constr_clean1',method='number',column_prefix='c')
>
> grass.run_command('v.db.addcolumn', map='parcel', columns='par_sqf double
> precision')
> grass.run_command('v.to.db', map='parcel', option='area',
> columns='par_sqf', unit='feet')
> grass.run_command('v.db.addcolumn',map='parcel',columns='pchng int')
>
> grass.read_command('v.db.select',map='parcel',columns='c_number')
> grass.read_command('v.db.select',map='parcel',columns='par_sqf')
> pchng = range(len(par_sqf))
> for i in range(len(par_sqf)):
>     pchng[i]=+int((0.25*c_number[i]/par_sqf[i])*100)
>
> for i in range(len(par_sqf)):
>      query="UPDATE parcel SET pchng=" + str(pchng[i]) + " WHERE cat = " +
> str(i+1)
>      grass.write_command("db.execute", stdin = query)
>
> ...
> ...
>
> Your insight is appreciated.
>
> Alassane
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20160829/58b18d11/attachment.html>


More information about the grass-user mailing list