From 880b61b772608307f0cac120a7ad17165fb2c3a6 Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Fri, 15 Dec 2023 14:55:36 +0100 Subject: [PATCH 1/2] add functions to create a new temporary location --- src/grass_gis_helpers/cleanup.py | 21 ++++++++- src/grass_gis_helpers/location.py | 76 ++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/src/grass_gis_helpers/cleanup.py b/src/grass_gis_helpers/cleanup.py index 3fca33e..74e20d4 100644 --- a/src/grass_gis_helpers/cleanup.py +++ b/src/grass_gis_helpers/cleanup.py @@ -21,7 +21,7 @@ import gc import grass.script as grass -from .location import get_location_size +from .location import get_location_size, switch_back_original_location def general_cleanup( @@ -118,3 +118,22 @@ def reset_region(region): if grass.find_file(name=region, element="windows")["file"]: grass.run_command("g.region", region=region) grass.run_command("g.remove", type="region", name=region, **kwargs) + + +def cleaning_tmp_location(original_gisrc, tmp_loc, gisdbase, tmp_gisrc): + """Cleaning up things from temporary location + + Args: + original_gisrc (str): The path to the original GISRC file + tmp_loc (str): The name of the temporary location + gisdbase (str): The GISDBASE info + tmp_gisrc (str): The path to the temporary GISRC file + """ + # switch back to original gisrc + if original_gisrc: + switch_back_original_location(original_gisrc) + # remove temporary location + if tmp_loc: + grass.try_rmdir(os.path.join(gisdbase, tmp_loc)) + if tmp_gisrc: + grass.try_remove(tmp_gisrc) diff --git a/src/grass_gis_helpers/location.py b/src/grass_gis_helpers/location.py index ff9f08c..fdb437d 100644 --- a/src/grass_gis_helpers/location.py +++ b/src/grass_gis_helpers/location.py @@ -4,7 +4,7 @@ # # MODULE: lib with location related helper functions for GRASS GIS # -# AUTHOR(S): Lina Krisztian +# AUTHOR(S): Lina Krisztian, Anika Weinmann # # PURPOSE: lib with location related helper functions for GRASS GIS # @@ -16,6 +16,7 @@ # ############################################################################# +import os import subprocess import grass.script as grass @@ -35,3 +36,76 @@ def get_location_size(): ), ) ) + + +def create_tmp_location(epsg=4326): + """Creation of a new temporary location + + Args: + epsg (int): The number of the EPSG code + + Returns: + tmp_loc (str): The name of the temporary location + tmp_gisrc (str): The path to the original GISRC file + """ + current_gisdbase = grass.gisenv()["GISDBASE"] + srcgisrc = grass.tempfile() + tmp_loc = f"temp_epsg{epsg}_location_{os.getpid()}" + f = open(srcgisrc, "w") + f.write("MAPSET: PERMANENT\n") + f.write("GISDBASE: %s\n" % current_gisdbase) + f.write("LOCATION_NAME: %s\n" % tmp_loc) + f.write("GUI: text\n") + f.close() + + proj_test = grass.parse_command("g.proj", flags="g") + if "epsg" in proj_test: + epsg_arg = {"epsg": epsg} + else: + epsg_arg = {"srid": "EPSG:{}".format(epsg)} + # create temp location from input without import + grass.verbose(_("Creating temporary location with EPSG:%d...") % epsg) + grass.run_command( + "g.proj", flags="c", location=tmp_loc, quiet=True, **epsg_arg + ) + + # switch to temp location + os.environ["GISRC"] = str(srcgisrc) + proj = grass.parse_command("g.proj", flags="g") + if "epsg" in proj: + new_epsg = proj["epsg"] + else: + new_epsg = proj["srid"].split("EPSG:")[1] + if new_epsg != str(epsg): + grass.fatal(_("Creation of temporary location failed!")) + + return tmp_loc, srcgisrc + + +def get_current_location(): + """Get infos to current location + + Returns: + loc (str): The name of the current location + mapset (str): The name of the current mapset + gisdbase (str): The current GISDBASE info + gisrc (str): The path to the current GISRC file + """ + # get current location, mapset, ... + grassenv = grass.gisenv() + loc = grassenv["LOCATION_NAME"] + mapset = grassenv["MAPSET"] + gisdbase = grassenv["GISDBASE"] + gisrc = os.environ["GISRC"] + return loc, mapset, gisdbase, gisrc + + +def switch_back_original_location(original_gisrc): + """Switching back to original location after the computation in tmp + location + + Args: + original_gisrc (str): The path to the original GISRC file + """ + # switch to target location + os.environ["GISRC"] = str(original_gisrc) From 89efd33e4bd7e60ea97f704c7694b67afd736e1e Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Fri, 15 Dec 2023 14:59:12 +0100 Subject: [PATCH 2/2] pylint --- src/grass_gis_helpers/location.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/grass_gis_helpers/location.py b/src/grass_gis_helpers/location.py index fdb437d..494f8af 100644 --- a/src/grass_gis_helpers/location.py +++ b/src/grass_gis_helpers/location.py @@ -51,20 +51,20 @@ def create_tmp_location(epsg=4326): current_gisdbase = grass.gisenv()["GISDBASE"] srcgisrc = grass.tempfile() tmp_loc = f"temp_epsg{epsg}_location_{os.getpid()}" - f = open(srcgisrc, "w") - f.write("MAPSET: PERMANENT\n") - f.write("GISDBASE: %s\n" % current_gisdbase) - f.write("LOCATION_NAME: %s\n" % tmp_loc) - f.write("GUI: text\n") - f.close() + gisrc_file = open(srcgisrc, "w") + gisrc_file.write("MAPSET: PERMANENT\n") + gisrc_file.write(f"GISDBASE: {current_gisdbase}\n") + gisrc_file.write(f"LOCATION_NAME: {tmp_loc}\n") + gisrc_file.write("GUI: text\n") + gisrc_file.close() proj_test = grass.parse_command("g.proj", flags="g") if "epsg" in proj_test: epsg_arg = {"epsg": epsg} else: - epsg_arg = {"srid": "EPSG:{}".format(epsg)} + epsg_arg = {"srid": f"EPSG:{epsg}"} # create temp location from input without import - grass.verbose(_("Creating temporary location with EPSG:%d...") % epsg) + grass.verbose(_(f"Creating temporary location with EPSG:{epsg}...")) grass.run_command( "g.proj", flags="c", location=tmp_loc, quiet=True, **epsg_arg )