325 #include <grass/gis.h>
326 #include <grass/glocale.h>
328 static int get_cond(
char **,
char *, DCELL);
329 static int get_fmt(
char **,
char *,
int *);
330 static int cmp(
const void *,
const void *);
348 const char *mapset,
struct Categories *pcats)
366 const char *mapset,
struct Categories *pcats)
381 G_warning(_(
"category support for [%s] in mapset [%s] %s"),
402 const char *mapset,
struct Categories *pcats)
406 switch (
G__read_cats(
"dig_cats", name, mapset, pcats, 1)) {
417 G_warning(_(
"category support for vector map [%s] in mapset [%s] %s"),
438 const char *mapset,
struct Categories * pcats,
int full)
448 if (strncmp(element,
"dig", 3) == 0)
457 if (
G_getl(buff,
sizeof buff, fd) == 0)
460 if (sscanf(buff,
"# %ld", &num) == 1)
462 else if (sscanf(buff,
"%ld", &num) == 1)
473 if (
G_getl(buff,
sizeof buff, fd) == 0)
484 float m1, a1, m2, a2;
486 if (
G_getl(fmt,
sizeof fmt, fd) == 0)
489 if (
G_getl(buff,
sizeof buff, fd) == 0)
491 if (sscanf(buff,
"%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
497 for (cat = 0;; cat++) {
500 if (
G_getl(buff,
sizeof buff, fd) == 0)
506 if (sscanf(buff,
"%1s", label) != 1)
513 && sscanf(buff,
"%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
515 else if (sscanf(buff,
"%d:%[^\n]", &cat, label) >= 1)
517 else if (sscanf(buff,
"%lf:%[^\n]", &val1, label) >= 1)
562 static char *none =
"";
564 return pcats->title ? pcats->title : none;
653 struct Categories *pcats, RASTER_MAP_TYPE data_type)
655 static char label[1024];
660 char fmt[30], value_str[30];
672 G_debug(5,
"G_get_raster_cat(): val %lf found i %d", val, i);
675 if (pcats->labels[i] !=
NULL)
676 return pcats->labels[i];
681 if ((f = pcats->fmt) ==
NULL)
684 a[0] = (float)val *pcats->m1 + pcats->a1;
685 a[1] = (
float)val *pcats->m2 + pcats->a2;
693 else if (*f ==
'?') {
695 get_cond(&f, v = value_str, val);
699 else if (get_fmt(&f, fmt, &i)) {
700 sprintf(v = value_str, fmt, a[i]);
735 for (i = 0; i < pcats->ncats; i++)
756 int ncols,
struct Categories *pcats)
778 int ncols,
struct Categories *pcats)
800 int ncols,
struct Categories *pcats)
824 int ncols,
struct Categories *pcats,
825 RASTER_MAP_TYPE data_type)
829 while (ncols-- > 0) {
834 if (i > pcats->ncats)
856 pcats->last_marked_rule = -1;
861 DCELL * rast1, DCELL * rast2,
876 if (pcats->marks[i]) {
885 *count = pcats->marks[i];
886 pcats->last_marked_rule = i;
891 CELL * rast1, CELL * rast2,
899 FCELL * rast1, FCELL * rast2,
907 void *rast1,
void *rast2,
908 long *
count, RASTER_MAP_TYPE data_type)
919 static int get_fmt(
char **f,
char *fmt,
int *i)
950 while (*ff >=
'0' && *ff <=
'9')
958 static int get_cond(
char **f,
char *
value, DCELL val)
1022 char *
label,
struct Categories *pcats)
1041 char *
label,
struct Categories *pcats)
1060 char *
label,
struct Categories *pcats)
1082 for (i = 0; i < pcats->ncats; i++) {
1084 if ((dtmp1 == *rast1 && dtmp2 == *rast2)
1085 || (dtmp1 == *rast2 && dtmp2 == *rast1)) {
1086 if (pcats->labels[i] !=
NULL)
1087 G_free(pcats->labels[i]);
1088 pcats->labels[i] =
G_store(label);
1098 if (pcats->nalloc < pcats->ncats) {
1100 len = (pcats->nalloc + 256) *
sizeof(
char *);
1102 if (len != (
int)len) {
1107 if (pcats->nalloc) {
1110 (
char **)G_realloc((
char *)pcats->labels, (
int)len);
1114 pcats->labels = (
char **)G_malloc((
int)len);
1118 len = (pcats->nalloc + 256) *
sizeof(
int);
1119 if (len != (
int)len) {
1124 pcats->marks = (
int *)G_realloc((
char *)pcats->marks, (
int)len);
1126 pcats->marks = (
int *)G_malloc((
int)len);
1127 pcats->nalloc += 256;
1130 pcats->labels[pcats->ncats - 1] =
G_store(label);
1132 G_strip(pcats->labels[pcats->ncats - 1]);
1138 if ((CELL) * rast1 > pcats->num)
1139 pcats->num = (CELL) * rast1;
1140 if ((CELL) * rast2 > pcats->num)
1141 pcats->num = (CELL) * rast2;
1162 struct Categories *pcats, RASTER_MAP_TYPE data_type)
1232 char str1[100], str2[100];
1239 fprintf(fd,
"# %ld categories\n", (
long)cats->num);
1242 fprintf(fd,
"%s\n", cats->title !=
NULL ? cats->title :
"");
1245 fprintf(fd,
"%s\n", cats->fmt !=
NULL ? cats->fmt :
"");
1246 fprintf(fd,
"%.2f %.2f %.2f %.2f\n",
1247 cats->m1, cats->a1, cats->m2, cats->a2);
1250 if (strncmp(element,
"dig", 3) == 0)
1261 if ((cats->fmt && cats->fmt[0])
1262 || (descr && descr[0])) {
1266 fprintf(fd,
"%s:%s\n", str1, descr !=
NULL ? descr :
"");
1273 fprintf(fd,
"%s:%s:%s\n", str1, str2,
1274 descr !=
NULL ? descr :
"");
1299 int i, DCELL * rast1, DCELL * rast2)
1303 if (i > pcats->ncats) {
1309 return pcats->labels[index];
1328 int i,
void *rast1,
void *rast2)
1330 RASTER_MAP_TYPE data_type = FCELL_TYPE;
1356 int i,
void *rast1,
void *rast2)
1358 RASTER_MAP_TYPE data_type = CELL_TYPE;
1386 void *rast2, RASTER_MAP_TYPE data_type)
1440 pcats->labels =
NULL;
1449 pcats->last_marked_rule = -1;
1487 pcats->title =
G_store(title);
1494 double a2,
struct Categories *pcats)
1516 double a2,
struct Categories *pcats)
1560 if (pcats->title !=
NULL) {
1562 pcats->title =
NULL;
1564 if (pcats->fmt !=
NULL) {
1568 if (pcats->ncats > 0) {
1569 for (i = 0; i < pcats->ncats; i++)
1570 if (pcats->labels[i] !=
NULL)
1571 G_free(pcats->labels[i]);
1574 pcats->labels =
NULL;
1599 const struct Categories *pcats_from)
1606 for (i = 0; i < pcats_from->ncats; i++) {
1615 return pcats->ncats;
1618 static struct Categories save_cats;
1622 int *indexes, i, ncats;
1626 if (pcats->ncats <= 1)
1629 ncats = pcats->ncats;
1634 indexes = (
int *)G_malloc(
sizeof(
int) * ncats);
1635 for (i = 0; i < ncats; i++)
1638 qsort(indexes, ncats,
sizeof(
int), cmp);
1640 for (i = 0; i < ncats; i++) {
1653 static int cmp(
const void *aa,
const void *bb)
1655 const int *a = aa, *
b = bb;
1656 DCELL min_rast1, min_rast2, max_rast1, max_rast2;
1660 &min_rast1, &max_rast1, &index, &index);
1662 &min_rast2, &max_rast2, &index, &index);
1663 if (min_rast1 < min_rast2)
1665 if (min_rast1 > min_rast2)
char * G_get_f_raster_cat(FCELL *rast, struct Categories *pcats)
given a FCELL value val Returns pointer to a string describing category.
char * G_mapset(void)
current mapset name
int G_is_c_null_value(const CELL *cellVal)
Returns 1 if cell is NULL, 0 otherwise. This will test if the value cell is the largest int...
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_set_cats_title(const char *title, struct Categories *pcats)
set title in category structure
int G_set_raster_cat(void *rast1, void *rast2, char *label, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Adds the label for range rast1 through rast2 in category structure pcats.
void G_free(void *buf)
Free allocated memory.
int G_set_raster_cats_title(const char *title, struct Categories *pcats)
Same as existing G_set_cats_title()
void G_quant_free(struct Quant *q)
char * G_store(const char *s)
Copy string to allocated memory.
int G_number_of_raster_cats(struct Categories *pcats)
char * G_get_ith_raster_cat(const struct Categories *pcats, int i, void *rast1, void *rast2, RASTER_MAP_TYPE data_type)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
int G_set_raster_cats_fmt(const char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
Same as existing G_set_cats_fmt()
int G_read_raster_cats(const char *name, const char *mapset, struct Categories *pcats)
Is the same as existing G_read_cats()
char * G_get_next_marked_raster_cat(struct Categories *pcats, void *rast1, void *rast2, long *count, RASTER_MAP_TYPE data_type)
void G_set_d_null_value(DCELL *dcellVals, int numVals)
int G_write_cats(char *name, struct Categories *cats)
write raster category file
int G_init_cats(CELL num, const char *title, struct Categories *pcats)
initialize category structure
int G_write_raster_cats(char *name, struct Categories *cats)
Same as existing G_write_cats()
CELL G_quant_get_cell_value(struct Quant *q, DCELL dcellVal)
Returns a CELL category for the floating-point value based on the quantization rules in q...
char * G_get_next_marked_c_raster_cat(struct Categories *pcats, CELL *rast1, CELL *rast2, long *count)
CELL G_number_of_cats(const char *name, const char *mapset)
int G_rewind_raster_cats(struct Categories *pcats)
after call to this function G_get_next_marked_raster_cat() returns the first marked cat label...
int G_set_cat(CELL num, char *label, struct Categories *pcats)
set a category label
int G_trim_decimal(char *buf)
Removes trailing zeros from decimal number.
int G_write_vector_cats(char *name, struct Categories *cats)
write vector category file
int G_set_c_raster_cat(CELL *rast1, CELL *rast2, char *label, struct Categories *pcats)
Adds the label for range rast1 through rast2 in category structure pcats.
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
CELL G__read_cats(const char *element, const char *name, const char *mapset, struct Categories *pcats, int full)
void G_newlines_to_spaces(char *s)
int G_read_range(const char *name, const char *mapset, struct Range *range)
read raster range
int G_read_cats(const char *name, const char *mapset, struct Categories *pcats)
read raster category file
int G_raster_map_is_fp(const char *name, const char *mapset)
Check if raster map is floating-point.
char * G_get_cats_title(const struct Categories *pcats)
get title from category structure struct
int G_free_cats(struct Categories *pcats)
free category structure memory
int G_read_vector_cats(const char *name, const char *mapset, struct Categories *pcats)
read vector category file
void G_quant_get_ith_rule(const struct Quant *q, int i, DCELL *dLow, DCELL *dHigh, CELL *cLow, CELL *cHigh)
int G_get_range_min_max(const struct Range *range, CELL *min, CELL *max)
get range min and max
char * G_get_cat(CELL num, struct Categories *pcats)
get a category label
int G_mark_c_raster_cats(CELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value in the rast_row and updates the marks for labels fo...
int G_is_d_null_value(const DCELL *dcellVal)
Returns 1 if dcell is NULL, 0 otherwise. This will test if the value dcell is a NaN. Same test as in G_is_f_null_value().
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
char * G_get_next_marked_d_raster_cat(struct Categories *pcats, DCELL *rast1, DCELL *rast2, long *count)
int G_set_cats_fmt(const char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
int G_init_raster_cats(const char *title, struct Categories *pcats)
Same as existing G_init_raster_cats() only ncats argument is missign. ncats has no meaning in new Cat...
int G_strip(char *buf)
Removes all leading and trailing white space from string.
int G_unmark_raster_cats(struct Categories *pcats)
Sets marks for all categories to 0. This initializes Categories structure for subsequest calls to G_m...
char * G_get_next_marked_f_raster_cat(struct Categories *pcats, FCELL *rast1, FCELL *rast2, long *count)
int G_set_f_raster_cat(FCELL *rast1, FCELL *rast2, char *label, struct Categories *pcats)
Adds the label for range rast1 through rast2 in category structure pcats.
int G_set_raster_value_d(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
Places a DCELL raster value.
char * G_get_ith_f_raster_cat(const struct Categories *pcats, int i, void *rast1, void *rast2)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
int G_mark_f_raster_cats(FCELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value in the rast_row and updates the marks for labels fo...
int G__write_cats(char *element, char *name, struct Categories *cats)
char * G_get_d_raster_cat(DCELL *rast, struct Categories *pcats)
given a DCELL value val Returns pointer to a string describing category.
int G_quant_nof_rules(const struct Quant *q)
void G_quant_add_rule(struct Quant *q, DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
int G_getl(char *buf, int n, FILE *fd)
gets a line of text from a file
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
char * G_get_c_raster_cat(CELL *rast, struct Categories *pcats)
given a CELL value val Returns pointer to a string describing category.
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_quant_init(struct Quant *quant)
Initializes the q struct.
int G_debug(int level, const char *msg,...)
Print debugging message.
char * G_get_ith_d_raster_cat(const struct Categories *pcats, int i, DCELL *rast1, DCELL *rast2)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
char * G_get_raster_cat(void *rast, struct Categories *pcats, RASTER_MAP_TYPE data_type)
given a raster value val of type data_type Returns pointer to a string describing category...
int G_free_raster_cats(struct Categories *pcats)
Same as existing G_free_cats()
DCELL G_get_raster_value_d(const void *rast, RASTER_MAP_TYPE data_type)
Retrieves the value of type data_type from pointer p,.
char * G_get_ith_c_raster_cat(const struct Categories *pcats, int i, void *rast1, void *rast2)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
int G_set_d_raster_cat(DCELL *rast1, DCELL *rast2, char *label, struct Categories *pcats)
Adds the label for range rast1 through rast2 in category structure pcats.
int G_copy_raster_cats(struct Categories *pcats_to, const struct Categories *pcats_from)
Allocates NEW space for quant rules and labels n pcats_to and copies all info from pcats_from cats to...
int G_mark_d_raster_cats(DCELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value in the rast_row and updates the marks for labels fo...
char * G_get_raster_cats_title(const struct Categories *pcats)
get raster cats title
int G_sort_cats(struct Categories *pcats)
int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
If the data_type is CELL_TYPE, calls G_is_c_null_value ((CELL *) rast); If the data_type is FCELL_TYP...
int G_mark_raster_cats(void *rast_row, int ncols, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Looks up the category label for each raster value in the rast_row (row of raster cell value) and upda...