Skip to content

Commit

Permalink
MM review
Browse files Browse the repository at this point in the history
  • Loading branch information
anikaweinmann committed Dec 4, 2024
1 parent fec134c commit a8c98ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"<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"]:
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"<g.remove -rf type=raster name={ndgb}>"
f"<g.remove -rf type=raster name={output}>"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

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

0 comments on commit a8c98ec

Please sign in to comment.