Skip to content

Commit

Permalink
QGIS plugin: check for patch in GRASS 8.3 (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
landam authored May 31, 2024
1 parent dcc5cae commit 7c03735
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,3 @@ Alternatively set up environment variables in command line before starting QGIS:
```sh
PYTHONPATH=`pwd` QGIS_PLUGINPATH=`pwd`/bin/qgis qgis tests/data/nucice/qgis_project.qgz
```

#### Known issue

On MS Windows QGIS plugin suffers by poping-up windows when starting computation.
This can be solved by copying ``core.py`` file located in ``smoderp2d\bin\qgis\grass_patch``
to a GRASS target directory.

GRASS target directory is typically located in:

- ``C:\Program Files\QGIS 3.**.*\apps\grass\grass83\etc\python\grass\script`` in the case that QGIS has been installed by standalone installer, or
- ``C:\OSGeo4W\apps\grass\grass83\etc\python\grass\script`` in the case that QGIS has been installed by OSGeo4W network installer.

Update: This bug has been fixed in GRASS GIS 8.4.
2 changes: 1 addition & 1 deletion bin/qgis/grass_patch/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Core functions to be used in Python scripts.
Core functions to be used in Python scripts (SMODERP2D patch applied).
Usage:
Expand Down
12 changes: 12 additions & 0 deletions bin/qgis/smoderp2d-plugin/smoderp_2D_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""

import os
import sys
import glob
import datetime
import tempfile
Expand Down Expand Up @@ -71,6 +72,17 @@ def run(self):
"""Run the task in a parallel thread."""
try:
self.runner = QGISRunner(self.setProgress)
# check for GRASS 8.3 patched version
if sys.platform == 'win32' and self.runner.grass_version() == [8, 3]:
from grass.script import core as gs_core
from itertools import islice
with open(gs_core.__file__) as fin:
for line in islice(fin, 1, 2):
if 'SMODERP2D patch applied' not in line:
raise ImportError(
'Your GRASS GIS installation needs to be fixed. Check the '
'<a href="https://storm-fsv-cvut.github.io/smoderp2d-manual/providers.html#known-issue">documentation</a>.'
)
self.runner.create_location(QgsProject.instance().crs().authid())
self.runner.set_options(self.input_params)
self.runner.import_data()
Expand Down
25 changes: 17 additions & 8 deletions smoderp2d/runners/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ def _get_provider(self):
from smoderp2d.providers.grass import GrassGisProvider
return GrassGisProvider()

@staticmethod
def grass_version():
"""Get GRASS GIS version.
:return list: GRASS version as a list (major, minor only)
"""
# gs.version() requires g.gisenv which is not possible to use when no location is active
# if list(map(int, gs.version()['version'].split('.')[:-1])) < [8, 3]:
with open(os.path.join(os.environ["GISBASE"], "etc", "VERSIONNUMBER")) as fd:
grass_version = fd.read().split(' ')[0]

return list(map(int, grass_version.split('.')[:-1]))

def _set_environment(self):
"""Set GRASS environment.
Expand All @@ -92,20 +105,16 @@ def _set_environment(self):
str_out = out.decode("utf-8")
gisbase = str_out.strip()

# check version
# gs.version() requires g.gisenv which is not possible to use when no location is active
# if list(map(int, gs.version()['version'].split('.')[:-1])) < [8, 3]:
with open(os.path.join(gisbase, "etc", "VERSIONNUMBER")) as fd:
grass_version = fd.read().split(' ')[0]
if list(map(int, grass_version.split('.')[:-1])) < [8, 3]:
raise ProviderError("GRASS GIS version 8.3+ required")

# initialize GRASS runtime enviroment
sys.path.append(os.path.join(gisbase, "etc", "python"))

from grass.script.setup import setup_runtime_env
setup_runtime_env(gisbase)

# check version
if self.grass_version() < [8, 3]:
raise ProviderError("GRASS GIS version 8.3+ required")

def create_location(self, epsg):
"""Create GRASS location.
Expand Down

0 comments on commit 7c03735

Please sign in to comment.