Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I wish to use a Grass module (v.to.db) in QGis, without the grass toolbox.

I tried with the Python console, but to no avail :

  • Using Sextante plugin but obviously it didn't know all the grass modules.

from sextante.core.Sextante import Sextante

Sextante.alghelp("grass:v.to.db")

--> Algorithm not found

  • An other test :

grass.run_command("v.to.db", map='bl@PERMANENT', layer='2', option='start', units='meters', columns='X,Y,Z')

-->Traceback (most recent call last):

File "", line 1, in module

NameError: name 'grass' is not defined

Do you know if there is an other way to use the grass module ? I don't want to use it in the grass toolbox because I can select only one colums in "attribute field", and I would like to choose several columns.

Thank you for your help

Marie

share|improve this question
    
gis.stackexchange.com/questions/21867/… shows how to import grass –  underdark Nov 8 '12 at 16:39
add comment

1 Answer

To use grass functions from a plugin or from the console, you must first import the grass module like this:

import grass.script as grass

Forgetting the import will cause the NameError for grass.

Follow detailed instructions on using grass from within python

If you are running on windows and you get a Bad Handle error when you try to import grass, this is a result of an unresolved python bug that occurs on some window systems. To work around the problem, put the code that imports and calls grass in a separate script. Then, run this script as an independent process from within your plugin.

callgrass.py:

if __name__ == '__main__':

import grass.script as grass
grass.run_command("v.to.db", map='bl@PERMANENT', layer='2', option='start', units='meters', columns='X,Y,Z')

print "Grass OUtput"

From Console/Plugin:

import subprocess

p1=subprocess.Popen(['callgrass.py', 'arg1', ...], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1.stdin.close()
ret= p1.communicate()[1] # grab grass output  
print ret 

Good Luck!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.