From fec134c85020acddca53645ddec86c0ec4652369 Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Wed, 4 Dec 2024 08:06:45 +0100 Subject: [PATCH 1/4] tree analyses remove duplicated calculation of NDWI and NDGB --- .../analyse_trees_lib/analyse_trees_lib.py | 36 +++++++++++++++++++ .../r.trees.mltrain/r.trees.mltrain.py | 10 +++--- .../r.trees.postprocess.py | 17 +++++---- .../r.trees.traindata/r.trees.traindata.py | 10 +++--- .../v.trees.species/v.trees.species.py | 12 ++++--- 5 files changed, 59 insertions(+), 26 deletions(-) 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..a9cd741 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,39 @@ 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_ndwi(green, nir, ndwi): + """Calculate NDWI if does not exists""" + if not grass.find_file(name=ndwi, element="cell")["file"]: + grass.mapcalc( + f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))" + ) + else: + grass.warning( + _( + f"Map <{ndwi}> already exists." + "If you want to recalculate all existing data use --o " + f"and if you only want to recalculate {ndwi}, " + "please delete the map first with:\n" + f"" + ) + ) + + +def calculate_ndgb(green, blue, ndgb): + """Calculate NDGB if does not exists""" + if not grass.find_file(name=ndgb, element="cell")["file"]: + grass.mapcalc( + f"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))" + ) + else: + grass.warning( + _( + f"Map <{ndgb}> already exists." + "If you want to recalculate all existing data use --o " + f"and if you only want to recalculate {ndgb}, " + "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..a403981 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,8 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( + calculate_ndgb, + calculate_ndwi, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -258,15 +260,11 @@ def main(): if not ndwi: ndwi = "ndwi" - grass.mapcalc( - f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))" - ) + calculate_ndwi(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - grass.mapcalc( - f"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))" - ) + calculate_ndgb(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..cd6665c 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,12 @@ 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_ndgb, + calculate_ndwi, + set_nprocs, + test_memory, + ) except Exception: grass.fatal("analyse_trees_lib missing.") @@ -256,17 +261,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_ndwi(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_ndgb(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..009bf4f 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,8 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( + calculate_ndgb, + calculate_ndwi, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -255,15 +257,11 @@ def main(): if not ndwi: ndwi = "ndwi" - grass.mapcalc( - f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))" - ) + calculate_ndwi(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - grass.mapcalc( - f"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))" - ) + calculate_ndgb(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..21cd3dd 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_ndwi, + 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_ndwi(green, nir, ndwi) grass.message(_("Classifying deciduous and coniferous trees ...")) classification_group = f"classification_group_{tmp_name}" From a8c98ec795041bd4b92366caf6751b04e6948b9b Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Wed, 4 Dec 2024 09:59:55 +0100 Subject: [PATCH 2/4] MM review --- .../analyse_trees_lib/analyse_trees_lib.py | 37 ++++++------------- .../r.trees.mltrain/r.trees.mltrain.py | 7 ++-- .../r.trees.traindata/r.trees.traindata.py | 7 ++-- .../v.trees.species/v.trees.species.py | 4 +- 4 files changed, 20 insertions(+), 35 deletions(-) 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 a9cd741..cfe6480 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 @@ -450,37 +450,24 @@ def compute_ndvi_neighbors(ndvi, nprocs, memory, rm_rasters): return f"{ndvi_split}_max2" -def calculate_ndwi(green, nir, ndwi): - """Calculate NDWI if does not exists""" - if not grass.find_file(name=ndwi, element="cell")["file"]: - grass.mapcalc( - f"{ndwi} = round(127.5 * (1.0 + float({green} - {nir}) / float({green} + {nir})))" - ) - else: - grass.warning( - _( - f"Map <{ndwi}> already exists." - "If you want to recalculate all existing data use --o " - f"and if you only want to recalculate {ndwi}, " - "please delete the map first with:\n" - f"" - ) - ) - - -def calculate_ndgb(green, blue, ndgb): - """Calculate NDGB if does not exists""" - if not grass.find_file(name=ndgb, element="cell")["file"]: +def calculate_index(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"{ndgb} = round(127.5 * (1.0 + float({green} - {blue}) / float({green} + {blue})))" + f"{output} = round(127.5 * (1.0 + float({band1} - {band2}) / float({band1} + {band2})))" ) else: grass.warning( _( - f"Map <{ndgb}> already exists." + f"Map <{output}> already exists." "If you want to recalculate all existing data use --o " - f"and if you only want to recalculate {ndgb}, " + f"and if you only want to recalculate {output}, " "please delete the map first with:\n" - f"" + 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 a403981..adc54b1 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,8 +216,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_ndgb, - calculate_ndwi, + calculate_index, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -260,11 +259,11 @@ def main(): if not ndwi: ndwi = "ndwi" - calculate_ndwi(green, nir, ndwi) + calculate_index(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - calculate_ndgb(green, blue, ndgb) + calculate_index(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.traindata/r.trees.traindata.py b/grass-gis-addons/m.analyse.trees/r.trees.traindata/r.trees.traindata.py index 009bf4f..27fac5a 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,8 +212,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_ndgb, - calculate_ndwi, + calculate_index, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -257,11 +256,11 @@ def main(): if not ndwi: ndwi = "ndwi" - calculate_ndwi(green, nir, ndwi) + calculate_index(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - calculate_ndgb(green, blue, ndgb) + calculate_index(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 21cd3dd..d1cac4c 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 @@ -208,7 +208,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_ndwi, + calculate_index, reset_region, set_nprocs, test_memory, @@ -285,7 +285,7 @@ def main(): grass.message(_("Computing NDWI ...")) ndwi = f"ndwi_{tmp_name}" rm_rasters.append(ndwi) - calculate_ndwi(green, nir, ndwi) + calculate_index(green, nir, ndwi) grass.message(_("Classifying deciduous and coniferous trees ...")) classification_group = f"classification_group_{tmp_name}" From f9bbf68d512d4af702797bbcdb9ddad9f5ec982c Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Wed, 4 Dec 2024 10:01:02 +0100 Subject: [PATCH 3/4] MM review --- .../m.analyse.trees/r.trees.mltrain/r.trees.mltrain.py | 6 +++--- .../r.trees.postprocess/r.trees.postprocess.py | 7 +++---- .../m.analyse.trees/r.trees.traindata/r.trees.traindata.py | 6 +++--- .../m.analyse.trees/v.trees.species/v.trees.species.py | 4 ++-- 4 files changed, 11 insertions(+), 12 deletions(-) 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 adc54b1..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,7 +216,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_index, + calculate_nd, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -259,11 +259,11 @@ def main(): if not ndwi: ndwi = "ndwi" - calculate_index(green, nir, ndwi) + calculate_nd(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - calculate_index(green, blue, ndgb) + 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 cd6665c..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 @@ -213,8 +213,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_ndgb, - calculate_ndwi, + calculate_nd, set_nprocs, test_memory, ) @@ -261,11 +260,11 @@ def main(): if not ndwi: ndwi = "ndwi" - calculate_ndwi(green, nir, ndwi) + calculate_nd(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - calculate_ndgb(green, blue, ndgb) + 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 27fac5a..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,7 +212,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_index, + calculate_nd, create_nearest_pixel_ndvi, set_nprocs, test_memory, @@ -256,11 +256,11 @@ def main(): if not ndwi: ndwi = "ndwi" - calculate_index(green, nir, ndwi) + calculate_nd(green, nir, ndwi) if not ndgb: ndgb = "ndgb" - calculate_index(green, blue, ndgb) + 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 d1cac4c..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 @@ -208,7 +208,7 @@ def main(): sys.path.append(path) try: from analyse_trees_lib import ( - calculate_index, + calculate_nd, reset_region, set_nprocs, test_memory, @@ -285,7 +285,7 @@ def main(): grass.message(_("Computing NDWI ...")) ndwi = f"ndwi_{tmp_name}" rm_rasters.append(ndwi) - calculate_index(green, nir, ndwi) + calculate_nd(green, nir, ndwi) grass.message(_("Classifying deciduous and coniferous trees ...")) classification_group = f"classification_group_{tmp_name}" From 2a537600cf4729c85251b010b4135760444e2a0b Mon Sep 17 00:00:00 2001 From: anikaweinmann Date: Wed, 4 Dec 2024 11:24:51 +0100 Subject: [PATCH 4/4] MM review --- .../m.analyse.trees/analyse_trees_lib/analyse_trees_lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cfe6480..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 @@ -450,7 +450,7 @@ def compute_ndvi_neighbors(ndvi, nprocs, memory, rm_rasters): return f"{ndvi_split}_max2" -def calculate_index(band1, band2, output): +def calculate_nd(band1, band2, output): """Calculate NDWI or NDGB if does not exists Args: band1(string): Name of green raster map