Skip to content
Open
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
40 changes: 21 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,17 @@ 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 +601,18 @@ 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)

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

gs.set_raise_on_error(old_state)
24 changes: 17 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 @@ -19,13 +19,17 @@ class TestRasterImport(TestCase):

@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 +38,14 @@ 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="="
)


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

test()
Loading