Skip to content

Commit

Permalink
Merge pull request #16 from mundialis/tmp_location
Browse files Browse the repository at this point in the history
Add functions to create a new temporary location
  • Loading branch information
anikaweinmann authored Dec 18, 2023
2 parents 2710f9c + 89efd33 commit 145d1e2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/grass_gis_helpers/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
76 changes: 75 additions & 1 deletion src/grass_gis_helpers/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -16,6 +16,7 @@
#
#############################################################################

import os
import subprocess

import grass.script as grass
Expand All @@ -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()}"
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": f"EPSG:{epsg}"}
# create temp location from input without import
grass.verbose(_(f"Creating temporary location with EPSG:{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)

0 comments on commit 145d1e2

Please sign in to comment.