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];
663 sprintf(label,
"no data");
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)
1001 int G_set_cat(CELL num,
char *label,
struct Categories *pcats)
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])) {
1264 sprintf(str1,
"%.10f", val1);
1266 fprintf(fd,
"%s:%s\n", str1, descr !=
NULL ? descr :
"");
1269 sprintf(str1,
"%.10f", val1);
1271 sprintf(str2,
"%.10f", val2);
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)