Unverified Commit c55ad925 authored by Julien Veyssier's avatar Julien Veyssier
Browse files

make grass.script accessible in qgis python => OK

Signed-off-by: default avatarJulien Veyssier <eneiluj@posteo.net>
parent cc569865
......@@ -47,7 +47,7 @@ from qgis._gui import *
import processing
from hrudelin.pluginUtils import layerstools
from hrudelin.pluginUtils.tools import isWindows, isMac, which
from hrudelin.pluginUtils.tools import isWindows, isMac, which, prepareGrassEnv
# this exception is used by the QgisTasks
class CancelException(Exception):
......@@ -64,6 +64,7 @@ class HruDelinDockWidget(QtWidgets.QDockWidget, FORM_CLASS):
def __init__(self, parent, iface):
super(HruDelinDockWidget, self).__init__(parent)
# Qgis interface, used to get main window, manipulate messageBar etc...
self.iface = iface
# can be set within the interface
......@@ -876,9 +877,30 @@ class HruDelinDockWidget(QtWidgets.QDockWidget, FORM_CLASS):
def processStep1(self, task):
# TODO move environment building from bash to hrudelin core
# TODO adapt hrudelin core to be executed AND to be imported as a module
print('inside STEP 1 task')
#os.environ['GISRC'] = '/home/julien/data/j2k_test/grass_db/grassdata/hru-delin/.grassrc'
#import grass.script as grass
#print('--- GISBASE')
#print('--- PATH')
#print('--- LD_LIB')
#grass.run_command('r.mapcalc', expression='toto=22', overwrite=True)
##import subprocess
##subp = subprocess.check_output(['r.info', 'map=toto'])
##print('SUBP %s' % subp)
#res = grass.read_command('r.info',
# #quiet=True, flags='nNc',
# map='toto')
#print('RESSS %s' % res)
return True
def processStep2(self, task):
import platform
import os
import os, sys
from pathlib import Path
def isWindows():
plat = platform.system()
......@@ -31,3 +32,46 @@ def split_list(alist, wanted_parts=1):
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
def prepareGrassEnv():
# sys.path (to be able to import grass.script)
# find grass depending on the system
if isWindows():
elif isMac():
for grassVersion in ['74', '75', '76', '77', '78', '79']:
findRes = list(Path('/usr/lib/grass%s' % grassVersion).rglob('*r.thin*'))
if len(findRes) > 0:
thinPath = str(findRes[0])
grassBasePath = os.path.dirname(os.path.dirname(thinPath))
grassPythonPath = os.path.join(grassBasePath, 'etc', 'python')
if grassPythonPath not in sys.path:
os.environ['GISBASE'] = grassBasePath
libPathToAdd = os.path.join(grassBasePath, 'lib')
existingLdLibraryPath = ''
if 'LD_LIBRARY_PATH' in os.environ:
existingLdLibraryPath = os.environ['LD_LIBRARY_PATH']
if libPathToAdd not in existingLdLibraryPath.split(':'):
os.environ['LD_LIBRARY_PATH'] = '%s:%s' % (existingLdLibraryPath, libPathToAdd)
pyPathToAdd = os.path.join(grassBasePath, 'etc', 'python')
existingPYTHONPATH = ''
if 'PYTHONPATH' in os.environ:
existingPYTHONPATH = os.environ['PYTHONPATH']
if pyPathToAdd not in existingPYTHONPATH.split(':'):
os.environ['PYTHONPATH'] = '%s:%s' % (existingPYTHONPATH, pyPathToAdd)
grassBinPath = os.path.join(grassBasePath, 'bin')
grassScriptPath = os.path.join(grassBasePath, 'scripts')
existingPath = ''
if 'PATH' in os.environ:
existingPath = os.environ['PATH']
if grassBinPath not in existingPath.split(':'):
os.environ['PATH'] = '%s:%s' % (existingPath, grassBinPath)
existingPath = os.environ['PATH']
if grassScriptPath not in existingPath.split(':'):
os.environ['PATH'] = '%s:%s' % (existingPath, grassScriptPath)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment