<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Ubuntu">thanks</font><br>
<br>
<div class="moz-cite-prefix">Il 03/16/2017 11:04 PM, Andres Solarte
ha scritto:<br>
</div>
<blockquote
cite="mid:CANDf6nW591qBn88Q1He+m8frSHpnxaHQLYizxU=Lzy51DvAktA@mail.gmail.com"
type="cite">
<div dir="ltr">Hello Carmine, what I do is create a script
(connect.py) to start session as follows:
<div><br>
</div>
<div>
<div># -*- coding: utf-8 -*-</div>
<div> </div>
<div>import os</div>
<div>import sys</div>
<div><br>
</div>
<div>def launch(location,mapset,db):</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>#
path to the GRASS GIS launch script</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>grass7bin
= '/usr/bin/grass72'</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>#
Set GISDBASE environment variable</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>os.environ['GISDBASE']
= gisdb</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>#
query GRASS 7 itself for its GISBASE</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>gisbase
= "/usr/lib/grass72"</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>os.environ['GISBASE']
= gisbase</div>
<div><br>
</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>#
define GRASS-Python environment</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>gpydir
= "/usr/lib/grass72/etc/python"</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>sys.path.append(gpydir)</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>#
import GRASS Python bindings</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>import
grass.script.setup as gsetup</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span></div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>############
launch session</div>
<div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>gsetup.init(gisbase,
gisdb, location, mapset)</div>
<div><br>
</div>
</div>
<div><br>
</div>
<div><br>
</div>
<div><span style="white-space:pre">After that I call the function from my main script and execute the functions as follows</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import connect #this is the script to connect with grass </span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">gisdb = "/path/to/the/main/folder/"</span></div>
<div><span style="white-space:pre">location = "</span><span style="white-space:pre">/path/to/the/</span><span style="white-space:pre">location/"</span></div>
<div><span style="white-space:pre">mapset = "</span><span style="white-space:pre">/path/to/the/</span><span style="white-space:pre">mapset/"</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">#Connect
connect.launch(location,mapset,gisdb)
import grass.script as gscript</span><br>
</div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">#Now I can execute the functions using gscript.run_command as follows:</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">#Load map</span></div>
<div><span style="white-space:pre">gscript.run_command("r.in.gdal", input="input image", flags='e' ,output = "map name")</span><br>
</div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">#Set region
gscript.run_command('g.region', n=north, s=south, e=east, w=west, ewres=resX, nsres=resY)</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">I hope this helps</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">Regards,</span></div>
<div><span style="white-space:pre">
</span></div>
<div><span style="white-space:pre">Andrés.</span></div>
<div><span style="white-space:pre">
</span></div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">2017-03-16 12:53 GMT-03:00 Carmine
Massarelli <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:carmine.massarelli@ba.irsa.cnr.it"
target="_blank">carmine.massarelli@ba.irsa.cnr.it</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"> <font face="Ubuntu">Dear,<br>
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.<br>
The second script uses grass functionality without
starting grass explicitly.</font><br>
<font face="Ubuntu">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: </font><font face="Ubuntu"><i>Cannot find
GRASS GIS 7 start script (['grass72', '--config',
'path'])</i><br>
I saw that it is related to subprocess (here the code):<br>
<br>
<i>p = subprocess.Popen(startcmd,
shell=False,stdout=subprocess.<wbr>PIPE,
stderr=subprocess.PIPE)</i><i><br>
</i><i>out, err = p.communicate()</i><i><br>
</i><i>if p.returncode != 0:</i><i><br>
</i><i> print >>sys.stderr, "ERROR: Cannot find
GRASS GIS 7 start script (%s)" % startcmd</i><i><br>
</i><i> sys.exit(-1)</i><i><br>
</i><i>gisbase = out.strip('\n\r')</i><br>
<br>
but I am not expert about this, so I ask for your help.<br>
What could I do?<br>
I have already set sudoers www-data ALL=NOPASSWD:
ALL and checked permission.<br>
Thanks in advance<span class="HOEnZb"><font
color="#888888"><br>
<br>
carmine<br>
</font></span></font>
<div class="m_8746496830545291321moz-signature"><br>
</div>
</div>
<br>
______________________________<wbr>_________________<br>
grass-user mailing list<br>
<a moz-do-not-send="true"
href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>
<a moz-do-not-send="true"
href="https://lists.osgeo.org/mailman/listinfo/grass-user"
rel="noreferrer" target="_blank">https://lists.osgeo.org/<wbr>mailman/listinfo/grass-user</a><br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
<div class="moz-signature">
<table style="border-left: separate!important; table-layout:
fixed;" border-left="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="content-td" align="left" nowrap="nowrap"
valign="top" width="300">
<div class="content-pad" style="">
<p style="font-family: Helvetica,Arial,sans-serif;
font-size: 8px; line-height: 8px; color: rgb(33, 33,
33);">
<span style="font-weight: bold; color: rgb(33, 33,
33); display: inline;" class="txt
signature_name-target sig-hide">Dr. Nat. Carmine
Massarelli, Ph.D.</span>
<br>
<span style="color: rgb(33, 33, 33); display: inline;"
class="txt signature_jobtitle-target sig-hide">Environmental
Technologist</span>
</p>
<p style="font-family: Helvetica,Arial,sans-serif;
font-size: 8px; line-height: 8px; margin-bottom: 8px;">
<span style="font-weight: bold; color: rgb(33, 33,
33); display: inline;" class="txt
signature_companyname-target sig-hide">IRSA-CNR</span>
<span style="display: inline;" class="company-sep
break"><br>
</span>
<span style="color: rgb(33, 33, 33); display: inline;"
class="txt signature_officephone-target sig-hide">Mob:
+39 339 32 57 311 <br>
Tel: +39 080 582 05 06</span> <span style="color:
#212121;" class="txt signature_fax-target sig-hide"></span>
<span style="display: inline;" class="address-sep
break"><br>
</span> <span style="color: rgb(33, 33, 33); display:
inline;" class="txt signature_address-target
sig-hide">Addr: Via De Blasio, n.5 - 70132 (Ba) - IT</span>
</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>