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´m trying to get some GRASS geoprocessig tools running in a python script outside GRASS. I´m able to import grass.script, but I´m not able to get the tools running yet. Does anybody know what I´m doing wrong?

import os, sys
import subprocess as subp

gisbase = os.environ['GISBASE'] = "C:/GRASS GIS 7"
gisdbase = os.path.join(os.environ['HOME'])
location = "Martin"
mapset   = "PERMANENT"

sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))

import grass.script as grass
import grass.script.setup as gsetup

gsetup.init(gisbase, gisdbase, location, mapset)
grass.run_command("v.hull", input="D:\Python_Test\1.shp", output="D:\Python_Test\5.shp")

It runs up till grass.run_command, but as a result I only get -1073741515 in pythonwin. Once I get this running, I want to combine ArcPy and GRASS tools in one script. Could it have to do something with the fact that I´m using the ArcGIS Python installation (2.6)?

Sorry if this is a stupid question, I´m still a python beginner with not too much programming experience.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

You cannot use a shapefile directly. You must first import it as a Grass layer in your mapset "Martin". A name beginning with a number (1.shp) is not allowed in Grass Also, in order to use GRASS functionality via Python from outside, some environment variables have to be set (see GRASS and Python):

after that you can use grass.script in the Python Shell:

import grass.script as grass
import grass.script.setup as gsetup
gisbase = os.environ['GISBASE']
gisdb="...your grassdata folder"
location="Martin"
mapset="PERMANENT"
gsetup.init(gisbase, gisdb, location, mymapset)
grass.run_command("v.hull", input="layer", output="layer2")
or
grass.parse_command(""v.hull", input="layer", output="layer2")

....

see GRASS Python Scripting Library or Python Scripts For GRASS GIS

share|improve this answer
    
Thanks for your suggestions. Unfortunally it´still not working. I still get the same error on: grass.run_command('v.in.ogr', dsn="D:/Python_Test/a.shp", layer="a", output="a") grass.run_command('v.hull', input="a", output="b") grass.run_command('v.out.ogr', input="b", dsn="D:/Python_Test/b.shp", format="ESRI_Shapefile"). I guess it has to do with the different the Python versions. ArcGIS 10.0 still uses 2.6 while GRASS uses 2.7 –  Martin Feb 6 '13 at 7:25
    
After using ArcPy and GRASS both with Python 2.7 it´s actually workig now. I´m accepting this answer because the points mentioned are important to keep in mind. –  Martin Aug 15 '13 at 18:37
    
Just FYI, in GRASS GIS 7 there is now also grasswiki.osgeo.org/wiki/Python/pygrass –  markusN yesterday
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.