Hi all!
I wrote a python script with grass to be run from a php web page.
The python script works perfectly if I run it from the terminal but when I try 
to run it from the php web page, the grass related part seems to be ignored and 
the grass session is not launched. I set the grass gis environment following 
this link [0].
>From the php side, I try to launch the python script using both exec(), 
>system() and also a bash script to run the python script but I always got the 
>same error.
Attached you can find the python script.

Does anybody have any ideas on how to fix it?

Thanks in advance for the help!
Roberta

[0] 
https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly#Python:_GRASS_GIS_7_with_existing_location
Working with GRASS without starting it explicitly - GRASS-Wiki - GRASS-Wiki - 
GRASS-Wiki<https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly#Python:_GRASS_GIS_7_with_existing_location>
GRASS sessions. It is possible to access GRASS modules without explicitly 
starting a "GRASS session". GRASS libraries require certain environment 
variables to be set. In fact a "GRASS session" is just a set of processes (e.g. 
a shell and/or GUI) which have the necessary environment settings, specifically:
grasswiki.osgeo.org

#!/usr/bin/env python
# coding=utf-8

import os
import sys
import time
from sys import argv
import subprocess
from os.path import basename


def main():
    gisdb = os.path.join(os.path.expanduser("~"), "/home/ubuntu/grass_DB")

    print(gisdb)
    # specify (existing) Location and Mapset
    location = "wgs84"
    mapset = "casella"
    print('1')
    #grass7bin = r'C:\OSGeo4W64\bin\grass78.bat'
    grass7bin = '/usr/bin/grass'

    # query GRASS GIS itself for its GISBASE
    startcmd = [grass7bin, '--config', 'path']
    try:
        p = subprocess.Popen(startcmd, shell=False,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        print(out)
        out = out.decode('utf-8')
        print(out)
    except OSError as error:
        sys.exit("ERROR: Cannot find GRASS GIS start script"
                 " {cmd}: {error}".format(cmd=startcmd[0], error=error))
    if p.returncode != 0:
        sys.exit("ERROR: Issues running GRASS GIS start script"
                " {cmd}: {error}"
                 .format(cmd=' '.join(startcmd), error=err))
    gisbase = out.strip(os.linesep)
    print(gisbase)
    print('2')
    
    # set GISBASE environment variable
    gisbase = '/usr/lib/grass74'
    os.environ['GISBASE'] = gisbase

    # define GRASS-Python environment
    grass_pydir = os.path.join(gisbase, "etc", "python")
    sys.path.append(grass_pydir)
    
    #print(grass_pydir)

    # import (some) GRASS Python bindings
    import grass.script as gscript
    import grass.script.setup as gsetup
    
    #print("init" in dir(grass.script.setup))
    print(gisbase)
    print(gisdb)
    #print(location)
    print(mapset)
    print(mapset)
    print('ok')

    # launch session
    rcfile = gsetup.init(gisbase, gisdb, location, mapset)
    print(rcfile)
    
    print('session ok')

    # example calls
    #gscript.message('Current GRASS GIS 7 environment:')
    print (gscript.gisenv())
    

if __name__ == "__main__":
	main()
_______________________________________________
grass-user mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/grass-user

Reply via email to