Skip to content

r3.univar: Bring r3.univar implementation closer to r.univar #5939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/raster/cats.c
Original file line number Diff line number Diff line change
@@ -217,7 +217,7 @@ static CELL read_cats(const char *element, const char *name, const char *mapset,
}

/* Read the title for the file */
if (G_getl(buff, sizeof buff, fd) == 0)
if (G_getl(buff, sizeof(buff), fd) == 0)
goto error;
G_strip(buff);
/* G_ascii_check(buff) ; */
@@ -230,10 +230,10 @@ static CELL read_cats(const char *element, const char *name, const char *mapset,
char fmt[256];
float m1, a1, m2, a2;

if (G_getl(fmt, sizeof fmt, fd) == 0)
if (G_getl(fmt, sizeof(fmt), fd) == 0)
goto error;
/* next line contains equation coefficients */
if (G_getl(buff, sizeof buff, fd) == 0)
if (G_getl(buff, sizeof(buff), fd) == 0)
goto error;
if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
goto error;
@@ -244,7 +244,7 @@ static CELL read_cats(const char *element, const char *name, const char *mapset,
for (cat1 = 0;; cat1++) {
char label[1024];

if (G_getl(buff, sizeof buff, fd) == 0)
if (G_getl(buff, sizeof(buff), fd) == 0)
break;
if (old)
Rast_set_c_cat(&cat1, &cat1, buff, pcats);
1 change: 1 addition & 0 deletions raster/r.univar/globals.h
Original file line number Diff line number Diff line change
@@ -69,5 +69,6 @@ int print_stats(univar_stat *stats, enum OutputFormat format);
int print_stats_table(univar_stat *stats);
univar_stat *create_univar_stat_struct(int map_type, int n_perc);
void free_univar_stat_struct(univar_stat *stats);
univar_stat *univar_stat_with_percentiles(int map_type);

#endif
51 changes: 14 additions & 37 deletions raster/r.univar/r.univar_main.c
Original file line number Diff line number Diff line change
@@ -102,7 +102,6 @@ void set_params(void)
param.shell_style->description = _(
"This flag is deprecated and will be removed in a future release. Use "
"format=shell instead.");

param.shell_style->guisection = _("Formatting");

param.extended = G_define_flag();
@@ -137,7 +136,6 @@ void set_params(void)
}

static int open_raster(const char *infile);
static univar_stat *univar_stat_with_percentiles(int map_type);
static void process_raster(univar_stat *stats, thread_workspace *tw,
const struct Cell_head *region, int nprocs,
enum OutputFormat format);
@@ -153,7 +151,7 @@ int main(int argc, char *argv[])
struct Cell_head region;
struct GModule *module;
univar_stat *stats;
char **p, *z;
char **infile, *zonemap;
int cell_type, min, max;
struct Range zone_range;
const char *mapset, *name;
@@ -259,20 +257,20 @@ int main(int argc, char *argv[])
thread_workspace *tw = G_malloc(nprocs * sizeof *tw);

/* open zoning raster */
if ((z = param.zonefile->answer)) {
mapset = G_find_raster2(z, "");
if ((zonemap = param.zonefile->answer)) {
mapset = G_find_raster2(zonemap, "");

for (t = 0; t < nprocs; ++t)
tw[t].fdz = open_raster(z);
tw[t].fdz = open_raster(zonemap);

cell_type = Rast_get_map_type(tw->fdz);
if (cell_type != CELL_TYPE)
G_fatal_error("Zoning raster must be of type CELL");

if (Rast_read_range(z, mapset, &zone_range) == -1)
if (Rast_read_range(zonemap, mapset, &zone_range) == -1)
G_fatal_error("Can not read range for zoning raster");
Rast_get_range_min_max(&zone_range, &min, &max);
if (Rast_read_cats(z, mapset, &(zone_info.cats)))
if (Rast_read_cats(zonemap, mapset, &(zone_info.cats)))
G_warning("No category support for zoning raster");

zone_info.min = min;
@@ -281,21 +279,22 @@ int main(int argc, char *argv[])
}

/* count the input rasters given */
for (p = (char **)param.inputfile->answers, rasters = 0; *p; p++, rasters++)
for (infile = (char **)param.inputfile->answers, rasters = 0; *infile;
infile++, rasters++)
;

/* process all input rasters */
int map_type = param.extended->answer ? -2 : -1;

stats = ((map_type == -1) ? create_univar_stat_struct(-1, 0) : 0);

for (p = param.inputfile->answers; *p; p++) {
for (infile = param.inputfile->answers; *infile; infile++) {

/* Check if the native extent and resolution
of the input map should be used */
if (param.use_rast_region->answer) {
mapset = G_find_raster2(*p, "");
Rast_get_cellhd(*p, mapset, &region);
mapset = G_find_raster2(*infile, "");
Rast_get_cellhd(*infile, mapset, &region);
/* Set the computational region */
Rast_set_window(&region);
}
@@ -304,7 +303,7 @@ int main(int argc, char *argv[])
}

for (t = 0; t < nprocs; t++)
tw[t].fd = open_raster(*p);
tw[t].fd = open_raster(*infile);

if (map_type != -1) {
/* NB: map_type must match when doing extended stats */
@@ -318,7 +317,7 @@ int main(int argc, char *argv[])
stats = univar_stat_with_percentiles(map_type);
}
else if (this_type != map_type) {
G_fatal_error(_("Raster <%s> type mismatch"), *p);
G_fatal_error(_("Raster <%s> type mismatch"), *infile);
}
}

@@ -330,7 +329,7 @@ int main(int argc, char *argv[])
}

/* close zoning raster */
if (z) {
if (zonemap) {
for (t = 0; t < nprocs; t++)
Rast_close(tw[t].fdz);
}
@@ -363,28 +362,6 @@ static int open_raster(const char *infile)
return fd;
}

static univar_stat *univar_stat_with_percentiles(int map_type)
{
univar_stat *stats;
unsigned int i, j;
unsigned int n_zones = zone_info.n_zones;

if (n_zones == 0)
n_zones = 1;

i = 0;
while (param.percentile->answers[i])
i++;
stats = create_univar_stat_struct(map_type, i);
for (i = 0; i < n_zones; i++) {
for (j = 0; j < stats[i].n_perc; j++) {
sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
}
}

return stats;
}

static void process_raster(univar_stat *stats, thread_workspace *tw,
const struct Cell_head *region, int nprocs,
enum OutputFormat format)
33 changes: 9 additions & 24 deletions raster/r.univar/r3.univar_main.c
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ void set_params(void)
param.output_file->required = NO;
param.output_file->description =
_("Name for output file (if omitted or \"-\" output to stdout)");
param.output_file->guisection = _("Output settings");

param.percentile = G_define_option();
param.percentile->key = "percentile";
@@ -50,6 +51,7 @@ void set_params(void)
param.percentile->answer = "90";
param.percentile->description =
_("Percentile to calculate (requires extended statistics flag)");
param.percentile->guisection = _("Extended");

param.separator = G_define_standard_option(G_OPT_F_SEP);
param.separator->answer = NULL;
@@ -62,10 +64,12 @@ void set_params(void)
param.shell_style->description = _(
"This flag is deprecated and will be removed in a future release. Use "
"format=shell instead.");
param.shell_style->guisection = _("Formatting");

param.extended = G_define_flag();
param.extended->key = 'e';
param.extended->description = _("Calculate extended statistics");
param.extended->guisection = _("Extended");

param.table = G_define_flag();
param.table->key = 't';
@@ -74,6 +78,7 @@ void set_params(void)
param.table->description = _(
"This flag is deprecated and will be removed in a future release. Use "
"format=csv instead.");
param.table->guisection = _("Formatting");

param.format = G_define_standard_option(G_OPT_F_FORMAT);
param.format->options = "plain,shell,csv,json";
@@ -94,20 +99,17 @@ int main(int argc, char *argv[])
FCELL val_f; /* for misc use */
DCELL val_d; /* for misc use */
int map_type, zmap_type;
RASTER3D_Region region;
struct GModule *module;
univar_stat *stats;

char *infile, *zonemap;
void *map, *zmap = NULL;
RASTER3D_Region region;
unsigned int i;
unsigned int rows, cols, depths;
unsigned int x, y, z;
double dmin, dmax;
int zone, n_zones /* , use_zone = 0 */;
int zone /* , use_zone = 0 */;
const char *mapset, *name;

struct GModule *module;

enum OutputFormat format;

G_gisinit(argv[0]);
@@ -253,24 +255,7 @@ int main(int argc, char *argv[])
Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), infile);

map_type = Rast3d_tile_type_map(map);

i = 0;
while (param.percentile->answers[i])
i++;

n_zones = zone_info.n_zones;

if (n_zones == 0)
n_zones = 1;

stats = create_univar_stat_struct(map_type, i);
for (i = 0; i < (unsigned int)n_zones; i++) {
unsigned int j;

for (j = 0; j < stats[i].n_perc; j++) {
sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
}
}
stats = univar_stat_with_percentiles(map_type);

for (z = 0; z < depths; z++) { /* From the bottom to the top */
if (format != SHELL)
22 changes: 22 additions & 0 deletions raster/r.univar/stats.c
Original file line number Diff line number Diff line change
@@ -726,3 +726,25 @@ int print_stats_table(univar_stat *stats)

return 1;
}

univar_stat *univar_stat_with_percentiles(int map_type)
{
univar_stat *stats;
unsigned int i, j;
unsigned int n_zones = zone_info.n_zones;

if (n_zones == 0)
n_zones = 1;

i = 0;
while (param.percentile->answers[i])
i++;
stats = create_univar_stat_struct(map_type, i);
for (i = 0; i < n_zones; i++) {
for (j = 0; j < stats[i].n_perc; j++) {
sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
}
}

return stats;
}