Example #1
0
int write_form_cat_colors (char* raster, CATCOLORS* ccolors) {
struct Colors colors;
struct Categories cats;
int i;

Rast_init_colors(&colors);

	for(i=1;i<CNT;++i)
Rast_add_color_rule(
	&ccolors[i].cat, ccolors[i].r, ccolors[i].g, ccolors[i].b,
	&ccolors[i].cat, ccolors[i].r, ccolors[i].g, ccolors[i].b,
	&colors, CELL_TYPE);
Rast_write_colors(raster, G_mapset(), &colors);
Rast_free_colors(&colors);
Rast_init_cats("Forms", &cats);
	for(i=1;i<CNT;++i)
Rast_set_cat(&ccolors[i].cat, &ccolors[i].cat, ccolors[i].label, &cats, CELL_TYPE);
Rast_write_cats(raster, &cats);
Rast_free_cats(&cats);
return 0;
}
Example #2
0
/*!
 * \brief Set a raster category label (FCELL)
 *
 * Adds the label for range <i>rast1</i> through <i>rast2</i> in
 * category structure <i>pcats</i>.
 *
 * \param rast1, rast2 raster values (range)
 * \param label category label
 * \param pcats pointer to Categories structure
 *
 * \return
 */
int Rast_set_f_cat(const FCELL * rast1, const FCELL * rast2,
		   const char *label, struct Categories *pcats)
{
    return Rast_set_cat(rast1, rast2, label, pcats, FCELL_TYPE);
}
Example #3
0
static CELL read_cats(const char *element,
		      const char *name,
		      const char *mapset, struct Categories *pcats, int full)
{
    FILE *fd;
    char buff[1024];
    CELL cat;
    DCELL val1, val2;
    int old = 0, fp_map;
    long num = -1;


    if (strncmp(element, "dig", 3) == 0)
	fp_map = 0;
    else
	fp_map = Rast_map_is_fp(name, mapset);

    if (!(fd = G_fopen_old(element, name, mapset)))
	return -2;

    /* Read the number of categories */
    if (G_getl(buff, sizeof buff, fd) == 0)
	goto error;

    if (sscanf(buff, "# %ld", &num) == 1)
	old = 0;
    else if (sscanf(buff, "%ld", &num) == 1)
	old = 1;

    if (!full) {
	fclose(fd);
	if (num < 0)
	    return 0;		/* coorect */
	return (CELL) num;
    }

    /* Read the title for the file */
    if (G_getl(buff, sizeof buff, fd) == 0)
	goto error;
    G_strip(buff);
    /*    G_ascii_check(buff) ; */

    Rast_init_cats(buff, pcats);
    if (num >= 0)
	pcats->num = num;

    if (!old) {
	char fmt[256];
	float m1, a1, m2, a2;

	if (G_getl(fmt, sizeof fmt, fd) == 0)
	    goto error;
	/* next line contains equation coefficients */
	if (G_getl(buff, sizeof buff, fd) == 0)
	    goto error;
	if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
	    goto error;
	Rast_set_cats_fmt(fmt, m1, a1, m2, a2, pcats);
    }

    /* Read all category names */
    for (cat = 0;; cat++) {
	char label[1024];

	if (G_getl(buff, sizeof buff, fd) == 0)
	    break;
	if (old)
	    Rast_set_c_cat(&cat, &cat, buff, pcats);
	else {
	    *label = 0;
	    if (sscanf(buff, "%1s", label) != 1)
		continue;
	    if (*label == '#')
		continue;
	    *label = 0;
	    /* for fp maps try to read a range of data */
	    if (fp_map
		&& sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
		Rast_set_cat(&val1, &val2, label, pcats, DCELL_TYPE);
	    else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
		Rast_set_cat(&cat, &cat, label, pcats, CELL_TYPE);
	    else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
		Rast_set_cat(&val1, &val1, label, pcats, DCELL_TYPE);
	    else
		goto error;
	}
    }

    fclose(fd);
    return 0;
  error:
    fclose(fd);
    return -1;
}
Example #4
0
int make_support(struct rr_state *theState, int percent, double percentage)
{
    char title[100];
    struct History hist;
    struct Categories cats;
    struct Colors clr;
    char *inraster;
    struct RASTER_MAP_PTR nulls;

    /* write categories for output raster 
       use values from input or cover map
     */
    if (theState->docover == 1) {
	inraster = theState->inrcover;
	nulls = theState->cnulls;
    }
    else {
	inraster = theState->inraster;
	nulls = theState->nulls;
    }
    if (Rast_read_cats(inraster, "", &cats) >= 0) {
	sprintf(title, "Random points on <%s>", inraster);
	Rast_set_cats_title(title, &cats);
	if (theState->use_nulls)
	    Rast_set_cat(nulls.data.v,
			     nulls.data.v,
			     "Points with NULL values in original",
			     &cats, nulls.type);
	Rast_write_cats(theState->outraster, &cats);
    }

    /* write history for output raster */
    if (Rast_read_history(theState->outraster, G_mapset(), &hist) >= 0) {
	Rast_short_history(theState->outraster, "raster", &hist);
	Rast_format_history(&hist, HIST_DATSRC_1, "Based on map <%s>", inraster);
	if (percent)
	    Rast_format_history(
		&hist, HIST_DATSRC_2,
		"Random points over %.2f percent of the base map <%s>",
		percentage, inraster);
	else
	    Rast_format_history(
		&hist, HIST_DATSRC_2,
		"%ld random points on the base map <%s>",
		theState->nRand, theState->inraster);

	Rast_command_history(&hist);
	Rast_write_history(theState->outraster, &hist);
    }

    /* write commandline to output vector */
    if (theState->outvector) {
	struct Map_info map;

	Vect_open_old(&map, theState->outvector, G_mapset());
	Vect_hist_command(&map);
	Vect_close(&map);
    }

    /* set colors for output raster */
    if (Rast_read_colors(inraster, "", &clr) >= 0) {
	if (theState->use_nulls) {
	    Rast_add_color_rule(nulls.data.v, 127, 127, 127,
				nulls.data.v, 127, 127, 127, &clr,
				nulls.type);
	}
	Rast_write_colors(theState->outraster, G_mapset(), &clr);
    }

    return 0;
}
Example #5
0
static int
read_cats(const char *name, const char *mapset, struct Categories *pcats)
 /* adapted from G__read_cats */
{
    FILE *fd;
    char buff[1024];
    CELL cat;
    DCELL val1, val2;
    int old;
    long num = -1;

    fd = G_fopen_old_misc(RASTER3D_DIRECTORY, RASTER3D_CATS_ELEMENT, name, mapset);
    if (!fd)
	return -2;

    /* Read the number of categories */
    if (G_getl(buff, sizeof(buff), fd) == 0)
	goto error;

    if (sscanf(buff, "# %ld", &num) == 1)
	old = 0;
    else if (sscanf(buff, "%ld", &num) == 1)
	old = 1;

    /* Read the title for the file */
    if (G_getl(buff, sizeof(buff), fd) == 0)
	goto error;
    G_strip(buff);

    Rast_init_cats(buff, pcats);
    if (num >= 0)
	pcats->num = num;

    if (!old) {
	char fmt[256];
	float m1, a1, m2, a2;

	if (G_getl(fmt, sizeof(fmt), fd) == 0)
	    goto error;
	/* next line contains equation coefficients */
	if (G_getl(buff, sizeof(buff), fd) == 0)
	    goto error;
	if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
	    goto error;
	Rast_set_cats_fmt(fmt, m1, a1, m2, a2, pcats);
    }

    /* Read all category names */
    for (cat = 0;; cat++) {
	char label[1024];

	if (G_getl(buff, sizeof(buff), fd) == 0)
	    break;

	if (old)
	    Rast_set_c_cat(&cat, &cat, buff, pcats);
	else {
	    *label = 0;
	    if (sscanf(buff, "%1s", label) != 1)
		continue;
	    if (*label == '#')
		continue;
	    *label = 0;

	    /* try to read a range of data */
	    if (sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
		Rast_set_cat(&val1, &val2, label, pcats, DCELL_TYPE);
	    else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
		Rast_set_cat(&cat, &cat, label, pcats, CELL_TYPE);
	    else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
		Rast_set_cat(&val1, &val1, label, pcats, DCELL_TYPE);
	    else
		goto error;
	}
    }

    fclose(fd);
    return 0;

  error:
    fclose(fd);
    return -1;
}