diff --git a/grass-gis-addons/m.analyse.trees/analyse_trees_lib/analyse_trees_lib.py b/grass-gis-addons/m.analyse.trees/analyse_trees_lib/analyse_trees_lib.py index 8835f3b..73a58d6 100644 --- a/grass-gis-addons/m.analyse.trees/analyse_trees_lib/analyse_trees_lib.py +++ b/grass-gis-addons/m.analyse.trees/analyse_trees_lib/analyse_trees_lib.py @@ -448,3 +448,26 @@ def compute_ndvi_neighbors(ndvi, nprocs, memory, rm_rasters): rm_rasters.append(f"{ndvi_split}_max1") rm_rasters.append(f"{ndvi_split}_max2") return f"{ndvi_split}_max2" + + +def calculate_nd(band1, band2, output): + """Calculate NDWI or NDGB if does not exists + Args: + band1(string): Name of green raster map + band2(string): Name of nir (for NDWI) or blue (for NDGB) raster map + output(string): Name for output NDWI or NDGB raster map + """ + if not grass.find_file(name=output, element="cell")["file"]: + grass.mapcalc( + f"{output} = round(127.5 * (1.0 + float({band1} - {band2}) / float({band1} + {band2})))" + ) + else: + grass.warning( + _( + f"Map <{output}> already exists." + "If you want to recalculate all existing data use --o " + f"and if you only want to recalculate {output}, " + "please delete the map first with:\n" + f"" + ) + ) diff --git a/grass-gis-addons/m.analyse.trees/r.trees.mltrain/r.trees.mltrain.py b/grass-gis-addons/m.analyse.trees/r.trees.mltrain/r.trees.mltrain.py index 2247ef4..1a6e6dc 100644 --- a/grass-gis-addons/m.analyse.trees/r.trees.mltrain/r.trees.mltrain.py +++ b/grass-gis-addons/m.analyse.trees/r.trees.mltrain/r.trees.mltrain.py @@ -216,6 +216,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( + calculate_nd, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -258,15 +259,11 @@ def main(): if not ndwi: ndwi = "ndwi" - grass.mapcalc( - f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))" - ) + calculate_nd(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - grass.mapcalc( - f"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))" - ) + calculate_nd(green, blue, ndgb) if options["trees_raw_v"]: trees_raw_v_rast = f"trees_raw_v_rast_{os.getpid()}" diff --git a/grass-gis-addons/m.analyse.trees/r.trees.postprocess/r.trees.postprocess.py b/grass-gis-addons/m.analyse.trees/r.trees.postprocess/r.trees.postprocess.py index 63a321c..c29ce88 100644 --- a/grass-gis-addons/m.analyse.trees/r.trees.postprocess/r.trees.postprocess.py +++ b/grass-gis-addons/m.analyse.trees/r.trees.postprocess/r.trees.postprocess.py @@ -212,7 +212,11 @@ def main(): grass.fatal("Unable to find the analyse trees library directory.") sys.path.append(path) try: - from analyse_trees_lib import set_nprocs, test_memory + from analyse_trees_lib import ( + calculate_nd, + set_nprocs, + test_memory, + ) except Exception: grass.fatal("analyse_trees_lib missing.") @@ -256,17 +260,11 @@ def main(): if not ndwi: ndwi = "ndwi" - grass.mapcalc( - f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))", - overwrite=True, - ) + calculate_nd(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - grass.mapcalc( - f"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))", - overwrite=True, - ) + calculate_nd(green, blue, ndgb) # estimate trees from nearest peak IDs and various bands diff --git a/grass-gis-addons/m.analyse.trees/r.trees.traindata/r.trees.traindata.py b/grass-gis-addons/m.analyse.trees/r.trees.traindata/r.trees.traindata.py index 2b468a8..935bc55 100644 --- a/grass-gis-addons/m.analyse.trees/r.trees.traindata/r.trees.traindata.py +++ b/grass-gis-addons/m.analyse.trees/r.trees.traindata/r.trees.traindata.py @@ -212,6 +212,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( + calculate_nd, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -255,15 +256,11 @@ def main(): if not ndwi: ndwi = "ndwi" - grass.mapcalc( - f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))" - ) + calculate_nd(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - grass.mapcalc( - f"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))" - ) + calculate_nd(green, blue, ndgb) # estimate trees from nearest peak IDs and various bands diff --git a/grass-gis-addons/m.analyse.trees/v.trees.species/v.trees.species.py b/grass-gis-addons/m.analyse.trees/v.trees.species/v.trees.species.py index 48dacad..aa5f2b8 100644 --- a/grass-gis-addons/m.analyse.trees/v.trees.species/v.trees.species.py +++ b/grass-gis-addons/m.analyse.trees/v.trees.species/v.trees.species.py @@ -207,7 +207,12 @@ def main(): grass.fatal("Unable to find the analyse trees library directory") sys.path.append(path) try: - from analyse_trees_lib import reset_region, set_nprocs, test_memory + from analyse_trees_lib import ( + calculate_nd, + reset_region, + set_nprocs, + test_memory, + ) except Exception: grass.fatal("analyse_trees_lib missing.") @@ -280,10 +285,7 @@ def main(): grass.message(_("Computing NDWI ...")) ndwi = f"ndwi_{tmp_name}" rm_rasters.append(ndwi) - grass.mapcalc( - f"{ndwi} = round(255 * (1.0 + ( float({green} - {nir})/" - f"({green} + {nir}) ))/2)" - ) + calculate_nd(green, nir, ndwi) grass.message(_("Classifying deciduous and coniferous trees ...")) classification_group = f"classification_group_{tmp_name}"