Skip to content

Commit

Permalink
Merge pull request #42 from mundialis/switch_to_new_mapset_changes
Browse files Browse the repository at this point in the history
switch_to_new_mapset changes to enable using existing mapset
  • Loading branch information
anikaweinmann authored Dec 19, 2024
2 parents 78fd280 + 3aef3e1 commit e46dc2f
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 184 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ cover/*
MANIFEST

*__pycache__*

# linting with shared config file
ruff-github-workflows.toml
ruff-merged.toml
90 changes: 47 additions & 43 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
# Ruff configuration file: ruff.toml

# Define the required version for Ruff
required-version = ">=0.7.0"

line-length = 79

# Specify directories or files to be excluded from Ruff linting, in addition to default exclusions
extend-exclude = [
".git",
"__pycache__",
".env",
".venv",
"env",
"venv",
"ENV",
"env.bak",
"venv.bak",
"ctypes",
"pydispatch",
]

# Uncomment the following sections as needed

# [format]
# Format settings for Ruff (quote-style and indent-style)
# quote-style = "double"
# indent-style= "tab"

# [lint]
# Define linting rules selection and ignore list
# select = [
# "A", # flake8-builtins (A)
# "COM", # flake8-commas
# "PL", # Pylint
# ]
ignore = [
"F821", # Undefined name `_`
]

# [lint.per-file-ignores]
# Define file-specific linting rule ignores
# "lib_dop/r_dop_import_lib.py" = ["ERA001", "PLR2004"]
lint.ignore = [
"A002",
"ANN001",
"ANN002",
"ANN003",
"ANN201",
"B006",
"BLE001",
"D100",
"D104",
"D205",
"D401",
"E501",
"F821",
"FBT002",
"FBT003",
"FURB101",
"I001",
"INT001",
"PERF401",
"PLC1901",
"PLR0912",
"PLR0913",
"PLR0914",
"PLR0915",
"PLR0917",
"PLR2004",
"PLW2901",
"SIM115",
"PTH103",
"PTH107",
"PTH109",
"PTH110",
"PTH112",
"PTH113",
"PTH118",
"PTH119",
"PTH123",
"PTH207",
"PTH208",
"S101",
"S113",
"S404",
"S603",
"S604",
"S605",
]
42 changes: 23 additions & 19 deletions src/grass_gis_helpers/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ def general_cleanup(
orig_region=None,
rm_mask=False,
):
"""General cleanup function"""

"""General cleanup function."""
grass.message(_("Cleaning up..."))
nulldev = open(os.devnull, "w")
nulldev = open(os.devnull, "w", encoding="utf-8")
kwargs = {"flags": "f", "quiet": True, "stderr": nulldev}
rm_groups.extend(rm_groups_wo_rasters)
for rmg in rm_groups:
Expand All @@ -52,7 +51,9 @@ def general_cleanup(
for rmg_wr in rm_groups_w_rasters:
if grass.find_file(name=rmg_wr, element="group")["file"]:
group_rasters = grass.parse_command(
"i.group", flags="lg", group=rmg_wr
"i.group",
flags="lg",
group=rmg_wr,
)
rm_rasters.extend(group_rasters)
grass.run_command("g.remove", type="group", name=rmg, **kwargs)
Expand All @@ -70,14 +71,17 @@ def general_cleanup(
shutil.rmtree(rmdir)
if orig_region is not None:
find_reg = grass.find_file(name=orig_region, element="windows")
if "file" in find_reg and find_reg["file"]:
if find_reg.get("file"):
grass.run_command("g.region", region=orig_region)
grass.run_command(
"g.remove", type="region", name=orig_region, **kwargs
"g.remove",
type="region",
name=orig_region,
**kwargs,
)
for rmreg in rm_regions:
find_reg = grass.find_file(name=rmreg, element="windows")
if "file" in find_reg and find_reg["file"]:
if find_reg.get("file"):
grass.run_command("g.remove", type="region", name=rmreg, **kwargs)
strds = grass.parse_command("t.list", type="strds", quiet=True)
stvds = grass.parse_command("t.list", type="stvds", quiet=True)
Expand Down Expand Up @@ -134,9 +138,8 @@ def general_cleanup(
quiet=True,
stderr=nulldev,
)
if rm_mask:
if grass.find_file(name="MASK", element="raster")["file"]:
grass.run_command("r.mask", flags="r")
if rm_mask and grass.find_file(name="MASK", element="raster")["file"]:
grass.run_command("r.mask", flags="r")

# get location size
get_location_size()
Expand All @@ -148,8 +151,9 @@ def general_cleanup(
def rm_vects(vects):
"""Function to remove clean vector maps
Args:
vects (list): list of vector maps which should be removed"""
nuldev = open(os.devnull, "w")
vects (list): list of vector maps which should be removed.
"""
nuldev = open(os.devnull, "w", encoding="utf-8")
kwargs = {"flags": "f", "quiet": True, "stderr": nuldev}
for rmv in vects:
if grass.find_file(name=rmv, element="vector")["file"]:
Expand All @@ -160,24 +164,24 @@ def reset_region(region):
"""Function to set the region to the given region
Args:
region (str): the name of the saved region which should be set and
deleted
deleted.
"""
nulldev = open(os.devnull, "w")
nulldev = open(os.devnull, "w", encoding="utf-8")
kwargs = {"flags": "f", "quiet": True, "stderr": nulldev}
if 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)
if region and 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
"""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:
Expand Down
44 changes: 29 additions & 15 deletions src/grass_gis_helpers/data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@


def download_and_import_tindex(tindex_url, output, download_dir):
"""Download and import tile index from url
"""Download and import tile index from url.
Args:
tindex_url (str): URL of tile index
output (str): The output name for the tile index
download_dir (str): The directory where the data should be downloaded
"""
cur_dir = os.getcwd()
zip_name = os.path.basename(tindex_url)
Expand Down Expand Up @@ -71,14 +72,15 @@ def download_and_import_tindex(tindex_url, output, download_dir):

def get_list_of_tindex_locations(tindex, aoi=None):
"""Select the locations of the tindex which overlap with the AOI or the
current region
current region.
Args:
tindex (str): Name of the tindex vector map
aoi (str): Name of the AOI vector map
Returns:
(list): List with locations which overlap with the AOI or the current
region
"""
tindex_clipped = f"clipped_tindex_vect_{grass.tempname(8)}"
try:
Expand All @@ -98,7 +100,8 @@ def get_list_of_tindex_locations(tindex, aoi=None):
tiles = [
val[0]
for val in grass.vector_db_select(
tindex_clipped, columns="location"
tindex_clipped,
columns="location",
)["values"].values()
]
finally:
Expand Down Expand Up @@ -132,9 +135,11 @@ def import_local_raster_data(
imported; e.g. for DOP import band_dict = {
1: "red", 2: "green", 3: "blue", 4: "nir"
}
Returns:
imported_local_data (bool): True if local data imported, otherwise
False
"""
grass.message(_("Importing local raster data..."))
imported_local_data = False
Expand Down Expand Up @@ -187,7 +192,7 @@ def import_local_raster_data(
err_m2 = "already exists and will be overwritten"
if err_m1 in r_import[1].decode():
continue
elif err_m2 in r_import[1].decode():
if err_m2 in r_import[1].decode():
pass
elif r_import[1].decode() != "":
grass.fatal(_(r_import[1].decode()))
Expand Down Expand Up @@ -218,7 +223,7 @@ def import_local_raster_data(


def get_xyz_file_infos(xyz_file, separator="space"):
"""Get the infos of a XYZ file to resolution, bounding box and pixelcenter
"""Get the infos of a XYZ file to resolution, bounding box and pixelcenter.
Args:
xyz_file (str): XYZ file path to import
Expand All @@ -227,6 +232,7 @@ def get_xyz_file_infos(xyz_file, separator="space"):
res (float): Resolution of the XYZ file
xyz_reg (dict): Dictionary with region of the XYZ file
shift_needed (bool): Boolean if the XYZ file hat to be shifted
"""
gdalinfo_cmd = ["gdalinfo", xyz_file]
process = grass.Popen(gdalinfo_cmd, stdout=PIPE, stderr=PIPE)
Expand All @@ -236,16 +242,16 @@ def get_xyz_file_infos(xyz_file, separator="space"):
res = float(stdout.split("Pixel Size = (")[1].split(",")[0])
# get bbox
bbox_x1 = float(
stdout.split("Upper Left")[1].replace("(", "").split(",")[0].strip()
stdout.split("Upper Left")[1].replace("(", "").split(",")[0].strip(),
)
bbox_x2 = float(
stdout.split("Upper Right")[1].replace("(", "").split(",")[0].strip()
stdout.split("Upper Right")[1].replace("(", "").split(",")[0].strip(),
)
bbox_y1 = float(
stdout.split("Upper Left")[1].split(",")[1].split(")")[0].strip()
stdout.split("Upper Left")[1].split(",")[1].split(")")[0].strip(),
)
bbox_y2 = float(
stdout.split("Lower Left")[1].split(",")[1].split(")")[0].strip()
stdout.split("Lower Left")[1].split(",")[1].split(")")[0].strip(),
)
# check if shift is needed
# The shift is only needed if the bbox does not contain the pixel centers
Expand Down Expand Up @@ -279,9 +285,12 @@ def get_xyz_file_infos(xyz_file, separator="space"):


def import_single_local_xyz_file(
xyz_file, output, use_cur_reg=False, separator="space"
xyz_file,
output,
use_cur_reg=False,
separator="space",
):
"""Import single XYZ file
"""Import single XYZ file.
Args:
xyz_file (str): XYZ file path to import
Expand All @@ -292,9 +301,11 @@ def import_single_local_xyz_file(
separator (str): Separator of XYZ file; default is "space"
Returns:
output (str): If the output is imported, otherwise return None
"""
res, xyz_reg, shift_needed = get_xyz_file_infos(
xyz_file, separator=separator
xyz_file,
separator=separator,
)
# check if aoi overlaps
if use_cur_reg:
Expand Down Expand Up @@ -369,6 +380,7 @@ def import_local_xyz_files(
will be appended
Returns:
imported_local_data (bool): True if local data imported, otherwise False
"""
grass.message(_("Importing local XYZ data..."))
imported_local_data = False
Expand Down Expand Up @@ -404,7 +416,7 @@ def import_local_xyz_files(
if name:
all_raster.append(name)
grass.message(
_(f"XYZ file <{os.path.basename(xyz_file)}> imported.")
_(f"XYZ file <{os.path.basename(xyz_file)}> imported."),
)
# check if raster were imported
if len(all_raster) > 0:
Expand All @@ -413,7 +425,7 @@ def import_local_xyz_files(


def import_local_vector_data(aoi_map, local_data_dir, rm_vectors, output):
"""Import vector data from local file path
"""Import vector data from local file path.
Args:
aoi_map (str): Name of vector map defining AOI
Expand All @@ -423,6 +435,7 @@ def import_local_vector_data(aoi_map, local_data_dir, rm_vectors, output):
Returns:
imported_local_data (bool): True if local data imported, otherwise False
"""
imported_local_data = False

Expand All @@ -432,7 +445,8 @@ def import_local_vector_data(aoi_map, local_data_dir, rm_vectors, output):
recursive=True,
)
shp_files = glob.glob(
os.path.join(local_data_dir, "**", "*.shp"), recursive=True
os.path.join(local_data_dir, "**", "*.shp"),
recursive=True,
)
files.extend(shp_files)

Expand Down
Loading

0 comments on commit e46dc2f

Please sign in to comment.