Skip to content

Commit

Permalink
v.trees.param: make input required (#76)
Browse files Browse the repository at this point in the history
* make inputs required

* change to f-strings, remove message

* improve messages

* check files also when @ in name
  • Loading branch information
juleshaas authored Apr 2, 2024
1 parent 67276b4 commit 5b83bf7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -664,33 +664,25 @@ def main():
# switch to another mapset for parallel postprocessing
gisrc, newgisrc, old_mapset = switch_to_new_mapset(new_mapset)
# create fully qualified names
if ndsm:
if "height" in treeparamset:
if "@" not in ndsm:
if not grass.find_file(
name=f"{ndsm}@{old_mapset}", element="cell"
)["file"]:
grass.fatal(_("Input map %s not available!") % ndsm)
ndsm = f"{ndsm}@{old_mapset}"
if ndvi:
if not grass.find_file(name=ndsm, element="cell")["file"]:
grass.fatal(_(f"Input map <{ndsm}> not available!"))
if "ndvi" in treeparamset:
if "@" not in ndvi:
if not grass.find_file(
name=f"{ndvi}@{old_mapset}", element="cell"
)["file"]:
grass.fatal(_("Input map %s not available!") % ndvi)
ndvi = f"{ndvi}@{old_mapset}"
if buildings:
if not grass.find_file(name=ndvi, element="cell")["file"]:
grass.fatal(_(f"Input map <{ndvi}> not available!"))
if "dist_building" in treeparamset:
if "@" not in buildings:
if not grass.find_file(
name=f"{buildings}@{old_mapset}", element="vector"
)["file"]:
grass.fatal(_("Input map %s not available!") % buildings)
buildings = f"{buildings}@{old_mapset}"
if not grass.find_file(name=buildings, element="vector")["file"]:
grass.fatal(_(f"Input map <{buildings}> not available!"))
if "@" not in treecrowns_complete:
if not grass.find_file(
name=f"{treecrowns_complete}@{old_mapset}", element="vector"
)["file"]:
grass.fatal(_("Input map %s not available!") % treecrowns_complete)
treecrowns_complete = f"{treecrowns_complete}@{old_mapset}"
if not grass.find_file(name=treecrowns_complete, element="vector")["file"]:
grass.fatal(_(f"Input map <{treecrowns_complete}> not available!"))

# need vector map in current mapset, for some GRASS modules
# (e.g. v.rast.stats)
Expand Down
21 changes: 9 additions & 12 deletions grass-gis-addons/m.analyse.trees/v.trees.param/v.trees.param.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,20 @@

# %option G_OPT_R_INPUT
# % key: ndsm
# % required: no
# % label: Name of the nDSM raster
# % answer: ndsm
# % guisection: Input
# %end

# %option G_OPT_R_INPUT
# % key: ndvi
# % required: no
# % label: Name of the NDVI raster
# % answer: top_ndvi_02
# % guisection: Input
# %end

# %option G_OPT_V_INPUT
# % key: buildings
# % required: no
# % label: Name of the buildings vector map
# % answer: reference_buildings
# % guisection: Input
Expand All @@ -72,7 +69,7 @@
# %option
# % key: distance_building
# % type: integer
# % required: no
# % required: yes
# % label: Range in which is searched for neighbouring buildings
# % answer: 500
# % guisection: Parameters
Expand All @@ -81,7 +78,7 @@
# %option
# % key: distance_tree
# % type: integer
# % required: no
# % required: yes
# % label: Range in which is searched for neighbouring trees
# % answer: 500
# % guisection: Parameters
Expand Down Expand Up @@ -131,7 +128,7 @@ def cleanup():
try:
os.remove(rmfile)
except Exception as e:
grass.warning(_("Cannot remove file <%s>: %s" % (rmfile, e)))
grass.warning(_(f"Cannot remove file <{rmfile}>: {e}"))


def main():
Expand Down Expand Up @@ -214,7 +211,12 @@ def main():
use_memory = round(memory / nprocs)
subset_ind = 0
try:
grass.message(_("Creating treecrown subsets ..."))
grass.message(
_(
"Creating treecrown subsets and calculating tree parameters:"
f" {treeparamset}..."
)
)
for num in range(nprocs):
# use pid to create a unique mapset and vector subset name
sid = f"{num}_{pid}"
Expand All @@ -238,11 +240,6 @@ def main():
quiet=True,
)
# Module
grass.message(
_(
f"Starting calculation of tree parameters: {treeparamset}..."
)
)
new_mapset = "tmp_mapset_treeparam_" + sid
mapset_names.append(new_mapset)
param = {
Expand Down

0 comments on commit 5b83bf7

Please sign in to comment.