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

alassane toure atourej at gmail.com
Mon Aug 29 11:25:41 PDT 2016


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)!
[image: Inline image 2]
Any suggestions how to resolve this?

Regards,
Alassane
---------- Forwarded message ----------
From: alassane toure <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>


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/c32ffcac/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 78650 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20160829/c32ffcac/attachment-0001.png>


More information about the grass-user mailing list