GRASS GIS – GRASS Geoprocessing in Python Script

geoprocessinggrassopen-source-gispythonpython-2.6

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.

Best Answer

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

Related Question