Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions python/grass/temporal/stds_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ def import_stds(
# Create a new location based on the projection information and switch
# into it
old_env = gs.gisenv()
target_gisrc = None
old_gisrc = os.environ.get("GISRC")
if location:
try:
proj4_string = Path(proj_file_name).read_text()
Expand All @@ -359,16 +361,18 @@ def import_stds(
_("Unable to create location %(l)s. Reason: %(e)s")
% {"l": location, "e": str(e)}
)
# Switch to the new created location
# Create a temporary environment
try:
gs.run_command(
"g.mapset",
mapset="PERMANENT",
project=location,
dbase=old_env["GISDBASE"],
target_gisrc, target_env = gs.create_environment(
old_env["GISDBASE"], location, "PERMANENT"
)
os.environ["GISRC"] = target_gisrc

except OSError as e:
gs.fatal(
_("Unable to create environment for location %s. Reason: %s")
% (location, e)
)
except CalledModuleError:
gs.fatal(_("Unable to switch to location %s") % location)
# create default database connection
try:
gs.run_command("t.connect", flags="d")
Expand Down Expand Up @@ -598,19 +602,22 @@ def import_stds(
update_cmd_list=False,
)

os.chdir(old_cwd)
# Make sure the location is switched back correctly
finally:
os.chdir(old_cwd)
if location:
# Switch to the old location
try:
gs.run_command(
"g.mapset",
mapset=old_env["MAPSET"],
project=old_env["LOCATION_NAME"],
dbase=old_env["GISDBASE"],
)
except CalledModuleError:
gs.warning(_("Switching to original location failed"))
# Restore the original session
if old_gisrc is not None:
os.environ["GISRC"] = old_gisrc
else:
os.environ.pop("GISRC", None)

from .core import init

init()

# Delete the temporary session file
if target_gisrc:
gs.try_remove(target_gisrc)

gs.set_raise_on_error(old_state)
2 changes: 1 addition & 1 deletion temporal/t.rast.import/t.rast.import.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
# %option
# % key: project
# % type: string
# % description: Create a new project and import the data into it. Do not run this module in parallel or interrupt it when a new project should be created
# % description: Create a new project and import the data into it.
# % required: no
# % multiple: no
# %end
Expand Down
49 changes: 42 additions & 7 deletions temporal/t.rast.import/testsuite/test_temporal_rast_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,30 @@
"""

import os
import pathlib
import shutil

import grass.script as gs
from grass.gunittest.case import TestCase


class TestRasterImport(TestCase):
input_ = os.path.join("data", "precip_2000.tar.bzip2")
new_project_name = "test_project_import"

@classmethod
def tearDownClass(cls):
"""Remove the temporary region"""
cls.del_temp_region()
"""Remove the imported data"""
cls.runModule("t.remove", flags="df", inputs="A")

def test_import(self):
self.assertModule(
"t.rast.import", input=self.input_, output="A", basename="a", overwrite=True
"t.rast.import",
flags="o",
input=self.input_,
output="A",
basename="a",
overwrite=True,
)
tinfo = """start_time='2000-01-01 00:00:00'
end_time='2001-01-01 00:00:00'
Expand All @@ -34,8 +42,35 @@ def test_import(self):
north=320000.0
south=10000.0
east=935000.0
west=120000.0
"""
west=120000.0"""

info = SimpleModule("t.info", flags="g", input="A")
self.assertModuleKeyValue(module=info, reference=tinfo, precision=2, sep="=")
self.assertModuleKeyValue(
module="t.info", input="A", flags="g", reference=tinfo, precision=2, sep="="
)

def test_import_new_project(self):
Comment thread
saket0187 marked this conversation as resolved.
Resolved
"""Test the project option."""
path = os.path.join(gs.gisenv()["GISDBASE"], self.new_project_name)
original_env = gs.gisenv()

shutil.rmtree(path, ignore_errors=True)
self.addCleanup(shutil.rmtree, path, ignore_errors=True)

self.assertModule(
"t.rast.import",
flags="o",
input=self.input_,
output="B",
basename="b",
project=self.new_project_name,
overwrite=True,
)

self.assertTrue(pathlib.Path(path).exists())
self.assertDictEqual(original_env, gs.gisenv())


if __name__ == "__main__":
from grass.gunittest.main import test

test()
Loading