[GRASS-user] python/grass script not working on virtual machine but ok on local machine

alassane toure atourej at gmail.com
Mon Jan 1 22:28:59 PST 2018


Group,
I need help with the following.  The attached script works as expected on
the local machine (pyhon 2.7; Grass70) but does not work on the virtual
machine (python 2.7 and Grass70).  The process gets stuck at ...

.....
out, err = p.communicate()
if p.returncode != 0:
    print >>sys.stderr, 'ERROR: %s' % err
    print >>sys.stderr, 'ERROR: Cannot generate location (%s)' % startcmd
    sys.exit(-1)
else:
    print 'Created location %s' % location_path
.....

No error is reported and it does not create the temporary location.


Any help/suggestion is appreciated.

Alassane
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20180102/a5b1067f/attachment.html>
-------------- next part --------------
#!/usr/bin/env python
 
# Python script to generate a new GRASS GIS 7 location simply from metadata
# Markus Neteler, 2014
 
########### DESCRIPTION ###################################
# This program computes a changed map using, solely, multi- 
# spectral images (3 bands or 4 bands) collected at date1 
# and date2  
########################################################### 
# Some predefined variables
###########################
import os
import sys 

sys.path.insert(0,"/usr/lib/python2.7/dist-packages")

import subprocess
import shutil
import binascii
import tempfile

from osgeo import gdal
from gdalconst import * 
from datetime import datetime

start_time = datetime.now()

dir1=os.path.dirname(sys.argv[1])  #access to directory

##++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

###########################################################################
# Python script to generate a new GRASS GIS 7 location simply from metadata
# Markus Neteler, 2014
########################################################################### 
 
# Linux
grass7bin = 'grass70'

myfile = sys.argv[1]
 
startcmd = grass7bin + ' --config path'
 
p = subprocess.Popen(startcmd, shell=True, 
					 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
	print >>sys.stderr, 'ERROR: %s' % err
	print >>sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)" % startcmd
	sys.exit(-1)
if sys.platform.startswith('linux'):
	gisbase = out.strip('\n')
elif sys.platform.startswith('win'):
    if out.find("OSGEO4W home is") != -1:
		gisbase = out.strip().split('\n')[1]
    else:
		gisbase = out.strip('\n')
    os.environ['GRASS_SH'] = os.path.join(gisbase, 'msys', 'bin', 'sh.exe')
 
# Set GISBASE environment variable
os.environ['GISBASE'] = gisbase
# define GRASS-Python environment
gpydir = os.path.join(gisbase, "etc", "python")
sys.path.append(gpydir)

#########################################################################
#  define GRASS DATABASE
if sys.platform.startswith('win'):
    gisdb = os.path.join(os.getenv('APPDATA', 'grassdata'))
else:
    gisdb = os.path.join(os.getenv('HOME', 'grassdata'))
 
# override for now with TEMP dir
gisdb = os.path.join(tempfile.gettempdir(), 'grassdata')
try:
    os.stat(gisdb)
except:
    os.mkdir(gisdb)
 
# location/mapset: use random names for batch jobs
#string_length = 16
string_length = 6
location = binascii.hexlify(os.urandom(string_length))
mapset   = 'PERMANENT'
location_path = os.path.join(gisdb, location)
 
startcmd = grass7bin + ' -c ' + myfile + ' -e ' + location_path
 
print startcmd
p = subprocess.Popen(startcmd, shell=True, 
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
    print >>sys.stderr, 'ERROR: %s' % err
    print >>sys.stderr, 'ERROR: Cannot generate location (%s)' % startcmd
    sys.exit(-1)
else:
    print 'Created location %s' % location_path
 
# Now the location with PERMANENT mapset exists.
###################################################
# Now we can use PyGRASS or GRASS Scripting library etc. after 
# having started the session with gsetup.init() etc
# Set GISDBASE environment variable
##################################################
os.environ['GISDBASE'] = gisdb
 
# Linux: Set path to GRASS libs (TODO: NEEDED?)
path = os.getenv('LD_LIBRARY_PATH')
dir  = os.path.join(gisbase, 'lib')
if path:
    path = dir + os.pathsep + path
else:
    path = dir
os.environ['LD_LIBRARY_PATH'] = path
 
# language
os.environ['LANG'] = 'en_US'
os.environ['LOCALE'] = 'C'

## Import GRASS Python bindings
import grass.script as grass
import grass.script.setup as gsetup
 
###########
# Launch session and do something
gsetup.init(gisbase, gisdb, location, mapset)
 
# say hello
grass.message('--- GRASS GIS 7: Current GRASS GIS 7 environment:')
print grass.gisenv()
 
# do something in GRASS now...
grass.message('--- GRASS GIS 7: Checking projection info:')
in_proj = grass.read_command('g.proj', flags = 'jf')
# selective proj parameter printing
kv = grass.parse_key_val(in_proj)
print kv
print kv['+proj']
 
# print full proj parameter printing
in_proj = in_proj.strip()
#grass.message("--- Found projection parameters: '%s'" % in_proj)
 
# show current region:
grass.message('--- GRASS GIS 7: Checking computational region info:')
in_region = grass.region()
#grass.message("--- Computational region: '%s'" % in_region)
# ***********************************************
# GRASS Location is now set. Begin processing.
# ***********************************************

end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))
sys.exit(0)



More information about the grass-user mailing list