Skip to content

Commit

Permalink
add min_first_ndvi option
Browse files Browse the repository at this point in the history
  • Loading branch information
griembauer committed Aug 15, 2024
1 parent 8643530 commit bf20583
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
3 changes: 3 additions & 0 deletions i.sentinel_2.ndvidiff/i.sentinel_2.ndvidiff.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ <h2>NOTES</h2>
With the <em>min_size</em> option, small areas of detected NDVI loss may be removed
<p>
The <em>rgbi_basename</em> option can be used to define a basename in case the aggregated RGBI maps should be persistent in GRASS
<p>
A minimum NDVI value can be passed by the <em>relevant_min_ndvi</em> option. In this case, identification of significant NDVI loss areas is<br>
delimited to pixels where the first aggregated NDVI map has a value of at least <em>relevant_min_ndvi</em>.

<h2>EXAMPLE</h2>

Expand Down
33 changes: 28 additions & 5 deletions i.sentinel_2.ndvidiff/i.sentinel_2.ndvidiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
# % description: threshold to apply to NDVI difference map. If none is given, the threshold is calculated from the NDVI diff map using thr=Q1-1.5*(Q3-Q1)
# %end

# %option
# % key: relevant_min_ndvi
# % type: double
# % required: no
# % multiple: no
# % description: delimit identification of NDVI loss areas to pixels with an NDVI value of at least <relevant_min_ndvi> in the first time interval
# %end

# %option
# % key: nprocs
# % type: integer
Expand Down Expand Up @@ -166,9 +174,9 @@

# cleanup function (can be extended)
def cleanup():
"""Cleanup function (can be extended)"""
from grass_gis_helpers import cleanup

"""Cleanup function (can be extended)"""
grass.run_command("g.region", region=cur_region)
cleanup.general_cleanup(
rm_vectors=rm_vec,
Expand All @@ -181,6 +189,7 @@ def cleanup():


def main():
"""do the work"""
from grass_gis_helpers import general

global rm_vec, rm_rast, rm_reg, rm_strds_w_rasters, rm_groups, cur_region
Expand Down Expand Up @@ -325,8 +334,10 @@ def main():
"output": out_rast,
"method": options[
"aggregation_method"
], # even if clouds are left, this should remove them (but may add shadows)
"granularity": "all", # this produces a single raster instead of a strds
], # even if clouds are left, this should remove them
# (but may add shadows)
"granularity": "all", # this produces a single raster
# instead of a strds
"nprocs": nprocs,
}
if "B02" in item:
Expand Down Expand Up @@ -389,7 +400,10 @@ def main():
ndvi_map = f"{strds_name}_ndvi"
rm_rast.append(ndvi_map)
ndvi_rasters.append(ndvi_map)
ndvi_exp = f"{ndvi_map}=float({nir_band}-{red_band})/({nir_band} + {red_band})"
ndvi_exp = (
f"{ndvi_map}=float({nir_band}-{red_band})/"
f"({nir_band} + {red_band})"
)
grass.run_command("r.mapcalc", expression=ndvi_exp, quiet=True)

grass.message(_("Calculating NDVI difference and loss maps..."))
Expand Down Expand Up @@ -417,7 +431,16 @@ def main():
rm_rast.append(ndvi_loss_map)

grass.run_command("r.mask", vector=options["aoi"])
loss_exp = f"{ndvi_loss_map} = if({ndvi_diff_map}<={ndvi_diff_threshold},1,null())"
if options["relevant_min_ndvi"]:
loss_exp = (
f"{ndvi_loss_map} = if(({ndvi_diff_map}<={ndvi_diff_threshold}"
f" && {ndvi_rasters[0]}>={options['relevant_min_ndvi']}),1,null())"
)
else:
loss_exp = (
f"{ndvi_loss_map} = if({ndvi_diff_map}<="
f"{ndvi_diff_threshold},1,null())"
)
grass.run_command("r.mapcalc", expression=loss_exp, quiet=True)
if options["ndvi_loss_map_vect"]:
ndvi_loss_map_vect = options["ndvi_loss_map_vect"]
Expand Down

0 comments on commit bf20583

Please sign in to comment.