Skip to content

Commit

Permalink
tree analyses remove duplicated calculation of NDWI and NDGB
Browse files Browse the repository at this point in the history
  • Loading branch information
anikaweinmann committed Dec 4, 2024
1 parent aa70c10 commit fec134c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"<g.remove -rf type=raster name={ndwi}>"
)
)


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"<g.remove -rf type=raster name={ndgb}>"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

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

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

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_ndwi,
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_ndwi(green, nir, ndwi)

grass.message(_("Classifying deciduous and coniferous trees ..."))
classification_group = f"classification_group_{tmp_name}"
Expand Down

0 comments on commit fec134c

Please sign in to comment.