Skip to content

Commit

Permalink
r.volume: Work with any mask name (#4632)
Browse files Browse the repository at this point in the history
Avoid hardcoded MASK by using the Rast_mask_status function. The rest of the logic stays the same so mask is read just as the plain raster map is read.

Documentation is updated to use 'raster mask' instead of 'MASK'.
  • Loading branch information
wenzeslaus authored Nov 6, 2024
1 parent 54d613f commit 69b20cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
42 changes: 24 additions & 18 deletions raster/r.volume/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* <landa.martin gmail.com>
* PURPOSE: r.volume is a program to compute the total, and average of
* cell values within regions of a map defined by clumps or
* patches on a second map (or MASK). It also computes the
* "volume" by multiplying the total within a clump by the area
* of each cell. It also outputs the "centroid" location of each
* clump. Output is to standard out.
* patches on a second map (or by raster mask). It also computes
* the "volume" by multiplying the total within a clump by the
* area of each cell. It also outputs the "centroid" location of
* each clump. Output is to standard out.
*
* COPYRIGHT: (C) 1999-2006, 2013 by the GRASS Development Team
*
Expand Down Expand Up @@ -43,7 +43,8 @@ int main(int argc, char *argv[])
CELL i, max;

int row, col, rows, cols;
int out_mode, use_MASK;
int out_mode;
bool use_mask;
unsigned long *n, *e;
long int *count;
int fd_data, fd_clump;
Expand Down Expand Up @@ -92,7 +93,8 @@ int main(int argc, char *argv[])
opt.clump->required = NO;
opt.clump->label = _("Name of input clump raster map");
opt.clump->description = _("Preferably the output of r.clump. "
"If no clump map is given than MASK is used.");
"If no clump map is given, "
"raster mask is used instead.");

opt.centroids = G_define_standard_option(G_OPT_V_OUTPUT);
opt.centroids->key = "centroids";
Expand Down Expand Up @@ -134,24 +136,28 @@ int main(int argc, char *argv[])
out_mode = (!flag.report->answer);

/*
* see if MASK or a separate "clumpmap" raster map is to be used
* see if raster mask or a separate "clumpmap" raster map is to be used
* -- it must(!) be one of those two choices.
*/
use_MASK = 0;
use_mask = false;
char mask_name[GNAME_MAX];
char mask_mapset[GMAPSET_MAX];
if (!clumpmap) {
clumpmap = "MASK";
use_MASK = 1;
if (!G_find_raster2(clumpmap, G_mapset()))
G_fatal_error(_("No MASK found. If no clump map is given than the "
"MASK is required. "
"You need to define a clump raster map or create a "
"MASK by r.mask command."));
G_important_message(_("No clump map given, using MASK"));
bool present =
Rast_mask_status(mask_name, mask_mapset, NULL, NULL, NULL);
if (!present)
G_fatal_error(_("No clump map <%s> given and no raster mask found. "
"You need to define a clump raster map or create "
"a raster mask using r.mask."),
opt.clump->key);
clumpmap = mask_name;
use_mask = true;
G_important_message(_("No clump map given, using raster mask"));
}

/* open input and clump raster maps */
fd_data = Rast_open_old(datamap, "");
fd_clump = Rast_open_old(clumpmap, use_MASK ? G_mapset() : "");
fd_clump = Rast_open_old(clumpmap, use_mask ? mask_mapset : "");

/* initialize vector map (for centroids) if needed */
if (centroidsmap) {
Expand All @@ -175,7 +181,7 @@ int main(int argc, char *argv[])
}

/* initialize data accumulation arrays */
max = Rast_get_max_c_cat(clumpmap, use_MASK ? G_mapset() : "");
max = Rast_get_max_c_cat(clumpmap, use_mask ? mask_mapset : "");

sum = (double *)G_malloc((max + 1) * sizeof(double));
count = (long int *)G_malloc((max + 1) * sizeof(long int));
Expand Down
8 changes: 4 additions & 4 deletions raster/r.volume/r.volume.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h2>DESCRIPTION</h2>
from a <b>input</b> raster map sorted by category on a <b>clump</b>
raster map, and optionally generates a vector points map of the
centroids for each clump. If a clump map is not specified, the
current MASK is used. The MASK can be defined
current raster mask is used. The raster mask can be defined
by <em><a href="r.mask.html">r.mask</a></em>. The sum is multiplied by
the area of a cell to give the volume occupied by that cell. See below
for an example of the output table.
Expand All @@ -24,12 +24,12 @@ <h2>DESCRIPTION</h2>
<h2>NOTES</h2>

<p>
If a clump map is not given and a MASK not set, the program exits with
an error message.
If a clump map is not given and a raster mask is not set, the program exits
with an error message.

<p>
<em>r.volume</em> works in the current region and respects the current
MASK.
raster mask.

<h3>CENTROIDS</h3>

Expand Down

0 comments on commit 69b20cf

Please sign in to comment.