[GRASS-user] batch job

Andres Solarte e.andres.solartec at gmail.com
Thu Mar 16 15:04:22 PDT 2017


Hello Carmine, what I do is create a script (connect.py) to start session
as follows:

# -*- coding: utf-8 -*-

import os
import sys

def launch(location,mapset,db):
# path to the GRASS GIS launch script
grass7bin = '/usr/bin/grass72'

# Set GISDBASE environment variable
os.environ['GISDBASE'] = gisdb
# query GRASS 7 itself for its GISBASE
gisbase = "/usr/lib/grass72"
os.environ['GISBASE'] = gisbase

# define GRASS-Python environment
gpydir = "/usr/lib/grass72/etc/python"
sys.path.append(gpydir)
# import GRASS Python bindings
import grass.script.setup as gsetup
############ launch session
gsetup.init(gisbase, gisdb, location, mapset)



After that I call the function from my main script and execute the
functions as follows


#!/usr/bin/env python # -*- coding: UTF-8 -*- import connect #this is the
script to connect with grass

gisdb = "/path/to/the/main/folder/"
location = "/path/to/the/location/"
mapset = "/path/to/the/mapset/"

#Connect connect.launch(location,mapset,gisdb) import grass.script as
gscript

#Now I can execute the functions using gscript.run_command as follows:

#Load map
gscript.run_command("r.in.gdal", input="input image", flags='e' ,output =
"map name")

#Set region gscript.run_command('g.region', n=north, s=south, e=east,
w=west, ewres=resX, nsres=resY)



I hope this helps

Regards,

Andrés.


2017-03-16 12:53 GMT-03:00 Carmine Massarelli <
carmine.massarelli at ba.irsa.cnr.it>:

> Dear,
> I have prepared a couple of scripts to execute a batch job: the first
> opens a csv file and formats the data for the second (invoked from the
> first) that include some grass functionality.
> The second script uses grass functionality without starting grass
> explicitly.
> If I execute the first script from shell all it is working, but if i
> invoke it from a php button (shell_exec('python path/script.py')) I receive
> the error: *Cannot find GRASS GIS 7 start script (['grass72', '--config',
> 'path'])*
> I saw that it is related to subprocess (here the code):
>
> *p = subprocess.Popen(startcmd, shell=False,stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)*
> *out, err = p.communicate()*
> *if p.returncode != 0:*
> *    print >>sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script
> (%s)" % startcmd*
> *    sys.exit(-1)*
> *gisbase = out.strip('\n\r')*
>
> but I am not expert about this, so I ask for your help.
> What could I do?
> I have already set sudoers   www-data ALL=NOPASSWD: ALL   and checked
> permission.
> Thanks in advance
>
> carmine
>
>
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20170316/a80f28b5/attachment.html>


More information about the grass-user mailing list