[GRASS-dev] [grass-code R][409] diversity func. in r.statistics

Glynn Clements glynn at gclements.plus.com
Thu May 24 16:49:49 EDT 2007

grass-coder at wald.intevation.org wrote:

> code R item #409, was opened at 2007-05-24 16:41
> Status: Open
> Priority: 3
> Submitted By: Martin Wegmann (wegmann)
> Assigned to: Nobody (None)
> Summary: diversity func. in r.statistics 
> Issue status: None
> GRASS component: raster
> Operating system: Linux
> Operating system version: debian
> Initial Comment:
> hello, 
> it would be very handy to have the c_divr.c Funktion (Diversity of
> values) in r.statistics.
> I already tried it with my very limited C skills but failed because
> r.statistics is not like r.series pointing to lib/stats, which would
> have maked it easier for me.

Try the attached file (untested).

Glynn Clements <glynn at gclements.plus.com>

-------------- next part --------------
#include <stdlib.h>
#include <unistd.h>
#include <grass/gis.h>
#include "method.h"

/* function prototypes */
static long divr (struct stats *);

o_divr (char *basemap, char *covermap, char *outputmap, int usecats, struct Categories *cats)
  char command[1024];
  FILE *stats_fd, *reclass_fd;
  int first;
  long basecat, covercat, catb, catc;
  double area;
    sprintf(command, "r.stats -an input='%s,%s' fs=space", basemap, covermap);
    stats_fd = popen (command, "r");

    sprintf (command, "r.reclass i='%s' o='%s'",basemap, outputmap);
    reclass_fd = popen (command, "w");

    first = 1;
    while (read_stats(stats_fd, &basecat, &covercat, &area))
	if (first)
	    first = 0;
	    catb = basecat;
	    catc = 0;
	if (basecat != catb)
	    write_reclass (reclass_fd, catb, catc, G_get_cat (catc, cats),usecats);
	    catb = basecat;
	    catc = 0;
    if (!first)
	write_reclass (reclass_fd, catb, catc, G_get_cat (catc, cats), usecats);

    pclose (stats_fd);
    pclose (reclass_fd);

    exit (EXIT_SUCCESS);

More information about the grass-dev mailing list