Skip to content

Commit

Permalink
tree analyses: remove duplicated calculation of NDWI and NDGB (#88)
Browse files Browse the repository at this point in the history
* tree analyses remove duplicated calculation of NDWI and NDGB

* MM review
  • Loading branch information
anikaweinmann authored Dec 4, 2024
1 parent aa70c10 commit 508eac7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"<g.remove -rf type=raster name={output}>"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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}"
Expand Down

0 comments on commit 508eac7

Please sign in to comment.