[GRASS-user] vector attribute calculation in grass python

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

On 29/08/16 17:27, alassane toure wrote:

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))

It seems that you are trying to get the length of a variable par_sqf that is not defined in the code.

Moritz

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@gmail.com>
Date: Mon, Aug 29, 2016 at 9:27 AM
Subject: vector attribute calculation in grass python
To: GRASS user list <grass-user@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

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@lists.osgeo.org] im Auftrag von alassane toure [atourej@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@gmail.com<mailto:atourej@gmail.com>>
Date: Mon, Aug 29, 2016 at 9:27 AM
Subject: vector attribute calculation in grass python
To: GRASS user list <grass-user@lists.osgeo.org<mailto:grass-user@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

(attachments)

image.png

It works great…thanks Stefan!

···

On Mon, Aug 29, 2016 at 1:23 PM, Blumentrath, Stefan <Stefan.Blumentrath@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@lists.osgeo.org] im Auftrag von alassane toure [atourej@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@gmail.commailto:[atourej@gmail.com](mailto:atourej@gmail.com)>
Date: Mon, Aug 29, 2016 at 9:27 AM
Subject: vector attribute calculation in grass python

To: GRASS user list <grass-user@lists.osgeo.orgmailto:[grass-user@lists.osgeo.org](mailto:grass-user@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

Dear Alassane,

Please keep conversations on the list.

On 29/08/16 19:49, alassane toure wrote:

Dear Moritz,
I am a newbi in python programming. Following your instructions i added
the followings..

par_sqf=
c_number=
pchng=

but pchng stayed NULL for all rows (see below)!

You set pchng = range(len(par_sqf)),
but par_sqf is only defined as a Python variable by

par_sqf=

so it is normal that it is NULL.

Don't mix up Python variables and vector map column names.

If you want to set the attribute column pchng to contain a value that is equal to the length of the value of the attribute column par_sqf (i.e. count the number of digits (plus decimal point) in the area value), then you have to use v.db.update:

v.db.update map=parcel column=pchng qcol="length(par_sqf)"

Although I don't understand what the sens of such a length would be.

Maybe it would be easier if you explained us what you are trying to do, instead of only how you are doing it.

Moritz

Problem solved! Suggestion was made to use db.execute and it worked without variable declaration. Thanks for your insight!

On Aug 30, 2016 3:17 AM, “Moritz Lennert” <mlennert@club.worldonline.be> wrote:

Dear Alassane,

Please keep conversations on the list.

On 29/08/16 19:49, alassane toure wrote:

Dear Moritz,
I am a newbi in python programming. Following your instructions i added
the followings…

par_sqf=
c_number=
pchng=

but pchng stayed NULL for all rows (see below)!

You set pchng = range(len(par_sqf)),
but par_sqf is only defined as a Python variable by

par_sqf=

so it is normal that it is NULL.

Don’t mix up Python variables and vector map column names.

If you want to set the attribute column pchng to contain a value that is equal to the length of the value of the attribute column par_sqf (i.e. count the number of digits (plus decimal point) in the area value), then you have to use v.db.update:

v.db.update map=parcel column=pchng qcol=“length(par_sqf)”

Although I don’t understand what the sens of such a length would be.

Maybe it would be easier if you explained us what you are trying to do, instead of only how you are doing it.

Moritz