6
5
#include <grass/gis.h>
6
#include <grass/raster.h>
7
#include <grass/spawn.h>
10
static char *mk_command(const char *fmt, int nargs, ...)
11
static void run_stats(const char *mapname, const char *tempfile)
12
/* asprintf() would solve this problem better */
13
size_t len = strlen(fmt) + 1;
20
cmd = va_arg(ap, char *);
17
argv[argc++] = "r.stats";
23
argv[argc++] = type == COUNT
27
argv[argc++] = mapname;
30
sprintf(buf, "nsteps=%d", nsteps);
30
vsprintf(cmd, fmt, ap);
34
argv[argc++] = SF_REDIRECT_FILE;
35
argv[argc++] = SF_STDOUT;
36
argv[argc++] = SF_MODE_OUT;
37
argv[argc++] = tempfile;
41
if (G_vspawn_ex(argv[0], argv) != 0)
42
G_fatal_error("error running r.stats");
37
int get_stats(char *mapname, char *mapset, struct stat_list *dist_stats, /* linked list of stats */
45
int get_stats(const char *mapname, struct stat_list *dist_stats) /* linked list of stats */
40
47
char buf[1024]; /* input buffer for reading stats */
42
49
char *tempfile; /* temp file name */
45
50
FILE *fd; /* temp file pointer */
47
52
long int cat; /* a category value */
52
57
/* write stats to a temp file */
53
58
tempfile = G_tempfile();
54
fullname = G_fully_qualified_name(mapname, mapset);
55
is_fp = G_raster_map_is_fp(mapname, mapset);
59
is_fp = Rast_map_is_fp(mapname, "");
58
if (G_read_raster_cats(mapname, mapset, &cats) < 0)
62
if (Rast_read_cats(mapname, "", &cats) < 0)
59
63
G_fatal_error("Can't read category file");
60
if (G_number_of_raster_cats(&cats) <= 0) {
64
if (Rast_number_of_cats(&cats) <= 0) {
61
65
G_warning("There are no labeled cats, using nsteps argument");
65
if (G_read_fp_range(map_name, mapset, &fp_range) <= 0)
69
if (Rast_read_fp_range(map_name, "", &fp_range) <= 0)
66
70
G_fatal_error("Can't read frange file");
69
cmd = mk_command("r.stats -Cr%s%s \"%s\" > \"%s\"\n", 4,
70
type == COUNT ? "c" : "a", quiet ? "q" : "",
74
sprintf(buf, "%d", nsteps);
75
cmd = mk_command("r.stats -r%s%s \"%s\" nsteps=%s > \"%s\"\n", 5,
76
type == COUNT ? "c" : "a", quiet ? "q" : "",
77
fullname, buf, tempfile);
81
G_fatal_error("%s: ERROR running r.stats", G_program_name());
73
run_stats(mapname, tempfile);
83
75
/* open temp file and read the stats into a linked list */
84
76
fd = fopen(tempfile, "r");