int get_range(const char *name, long *min, long *max) { struct Range range; int nrows, ncols, row, col; CELL *cell; int fd; CELL cmin, cmax; struct Cell_head cellhd; if (Rast_read_range(name, "", &range) < 0) { Rast_init_range(&range); /* read the file to get the range */ Rast_get_cellhd(name, "", &cellhd); Rast_set_window(&cellhd); cell = Rast_allocate_c_buf(); fd = Rast_open_old(name, ""); nrows = Rast_window_rows(); ncols = Rast_window_cols(); G_message(_("Reading %s ..."), name); for (row = 0; row < nrows; row++) { G_percent(row, nrows, 2); Rast_get_c_row_nomask(fd, cell, row); for (col = 0; col < ncols; col++) Rast_update_range(cell[col], &range); } G_percent(row, nrows, 2); Rast_close(fd); G_free(cell); } Rast_get_range_min_max(&range, &cmin, &cmax); *min = cmin; *max = cmax; return 0; }
void new_stats(const char *name, struct Reclass *reclass) { struct Histogram histo, histo2; struct Range range; CELL cat, cat2; int i; CELL min, max; min = reclass->min; max = reclass->max; /* read histogram for original file */ G_suppress_warnings(1); i = Rast_read_histogram(reclass->name, reclass->mapset, &histo); G_suppress_warnings(0); if (i <= 0) return; /* compute data rage for reclass */ Rast_init_range(&range); for (i = 0; i < histo.num; i++) { cat = histo.list[i].cat; if (cat < min || cat > max) continue; cat2 = reclass->table[cat - min]; Rast_update_range(cat2, &range); } Rast_write_range(name, &range); /* now generate a histogram from the original */ /* allocate histogram list */ histo2.num += range.max - range.min + 1; histo2.list = (LIST *) G_calloc(histo2.num, sizeof(LIST)); /* set all counts to 0 */ i = 0; for (cat = range.min; cat <= range.max; cat++) { histo2.list[i].cat = cat; histo2.list[i++].count = 0; } /* go thru original histogram and add into histo2 */ for (i = 0; i < histo.num; i++) { cat = histo.list[i].cat; if (cat < min || cat > max) Rast_set_c_null_value(&cat, 1); else cat2 = reclass->table[cat - min]; if (!Rast_is_c_null_value(&cat)) histo2.list[cat2 - range.min].count += histo.list[i].count; } Rast_write_histogram(name, &histo2); }
/*! * \brief Construct default range * * Sets the integer range to [1,255] * * \param[out] r pointer to Range structure which holds range info */ void Rast_construct_default_range(struct Range *range) { Rast_update_range(DEFAULT_CELL_MIN, range); Rast_update_range(DEFAULT_CELL_MAX, range); }
/*! * \brief Read raster range (CELL) * * This routine reads the range information for the raster map * <i>name</i> in <i>mapset</i> into the <i>range</i> structure. * * A diagnostic message is printed and -1 is returned if there is an error * reading the range file. Otherwise, 0 is returned. * * Old range file (those with 4 numbers) should treat zeros in this * file as NULL-values. New range files (those with just 2 numbers) * should treat these numbers as real data (zeros are real data in * this case). An empty range file indicates that the min, max are * undefined. This is a valid case, and the result should be an * initialized range struct with no defined min/max. If the range file * is missing and the map is a floating-point map, this function will * create a default range by calling G_construct_default_range(). * * \param name map name * \param mapset mapset name * \param[out] range pointer to Range structure which holds range info * * \return -1 on error * \return 1 on success * \return 2 if range is empty * \return 3 if raster map is floating-point, get range from quant rules */ int Rast_read_range(const char *name, const char *mapset, struct Range *range) { FILE *fd; CELL x[4]; char buf[200]; int n, count; struct Quant quant; struct FPRange drange; Rast_init_range(range); fd = NULL; /* if map is not integer, read quant rules, and get limits */ if (Rast_map_type(name, mapset) != CELL_TYPE) { DCELL dmin, dmax; if (Rast_read_quant(name, mapset, &quant) < 0) { G_warning(_("Unable to read quant rules for raster map <%s>"), G_fully_qualified_name(name, mapset)); return -1; } if (Rast_quant_is_truncate(&quant) || Rast_quant_is_round(&quant)) { if (Rast_read_fp_range(name, mapset, &drange) >= 0) { Rast_get_fp_range_min_max(&drange, &dmin, &dmax); if (Rast_quant_is_truncate(&quant)) { x[0] = (CELL) dmin; x[1] = (CELL) dmax; } else { /* round */ if (dmin > 0) x[0] = (CELL) (dmin + .5); else x[0] = (CELL) (dmin - .5); if (dmax > 0) x[1] = (CELL) (dmax + .5); else x[1] = (CELL) (dmax - .5); } } else return -1; } else Rast_quant_get_limits(&quant, &dmin, &dmax, &x[0], &x[1]); Rast_update_range(x[0], range); Rast_update_range(x[1], range); return 3; } if (G_find_file2_misc("cell_misc", "range", name, mapset)) { fd = G_fopen_old_misc("cell_misc", "range", name, mapset); if (!fd) { G_warning(_("Unable to read range file for <%s>"), G_fully_qualified_name(name, mapset)); return -1; } /* if range file exists but empty */ if (!fgets(buf, sizeof buf, fd)) { if (fd) fclose(fd); return 2; } x[0] = x[1] = x[2] = x[3] = 0; count = sscanf(buf, "%d%d%d%d", &x[0], &x[1], &x[2], &x[3]); /* if wrong format */ if (count <= 0) { if (fd) fclose(fd); G_warning(_("Unable to read range file for <%s>"), G_fully_qualified_name(name, mapset)); return -1; } for (n = 0; n < count; n++) { /* if count==4, the range file is old (4.1) and 0's in it have to be ignored */ if (count < 4 || x[n]) Rast_update_range((CELL) x[n], range); } fclose(fd); } return 1; }
/* * check_stats() - Check and update statistics * * RETURN: 0 on success / 1 on failure */ int check_stats(const char *name) { RASTER_MAP_TYPE data_type; struct Histogram histogram; struct Categories cats; struct Range range; struct FPRange fprange; int i, histo_num; int cats_ok; int max; data_type = Rast_map_type(name, ""); G_message(_("Updating statistics for [%s]..."), name); if (!do_histogram(name)) return 1; if (Rast_read_histogram(name, "", &histogram) <= 0) return 1; /* Init histogram range */ if (data_type == CELL_TYPE) Rast_init_range(&range); else Rast_init_fp_range(&fprange); G_message(_("Updating histogram range...")); i = histo_num = Rast_get_histogram_num(&histogram); while (i >= 0) { G_percent(i, histo_num, 2); if (data_type == CELL_TYPE) Rast_update_range(Rast_get_histogram_cat(i--, &histogram), &range); else Rast_update_fp_range((DCELL) Rast_get_histogram_cat(i--, &histogram), &fprange); } /* Write histogram range */ if (data_type == CELL_TYPE) Rast_write_range(name, &range); else Rast_write_fp_range(name, &fprange); /* Get category status and max */ cats_ok = (Rast_read_cats(name, "", &cats) >= 0); max = (data_type == CELL_TYPE ? range.max : fprange.max); /* Further category checks */ if (!cats_ok) Rast_init_cats("", &cats); else if (cats.num != max) { cats.num = max; cats_ok = 0; } /* Update categories if needed */ if (!cats_ok) { G_message(_("Updating the number of categories for [%s]..."), name); Rast_write_cats(name, &cats); } Rast_free_histogram(&histogram); Rast_free_cats(&cats); return 0; }