Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
hru-delin-dev
hru-delin-qgis
Commits
c55ad925
Unverified
Commit
c55ad925
authored
Apr 18, 2020
by
Julien Veyssier
Browse files
make grass.script accessible in qgis python => OK
Signed-off-by:
Julien Veyssier
<
eneiluj@posteo.net
>
parent
cc569865
Changes
2
Hide whitespace changes
Inline
Side-by-side
hrudelin_dockwidget.py
View file @
c55ad925
...
...
@@ -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
):
"""Constructor."""
super
(
HruDelinDockWidget
,
self
).
__init__
(
parent
)
prepareGrassEnv
()
# 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
):
task
.
setProgress
(
0
)
# TODO move environment building from bash to hrudelin core
# TODO adapt hrudelin core to be executed AND to be imported as a module
#HruDelinCore.step1(self.configFilePath)
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(os.environ['GISBASE'])
#print('--- PATH')
#print(os.environ['PATH'])
#print('--- LD_LIB')
#print(os.environ['LD_LIBRARY_PATH'])
#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')
##.decode('utf-8')
#print('RESSS %s' % res)
return
True
def
processStep2
(
self
,
task
):
...
...
pluginUtils/tools.py
View file @
c55ad925
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
():
pass
elif
isMac
():
pass
else
:
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
:
sys
.
path
.
append
(
grassPythonPath
)
break
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment