00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 #include <stdlib.h>
00324 #include <string.h>
00325 #include <grass/gis.h>
00326 #include <grass/glocale.h>
00327
00328 static int get_cond(char **, char *, DCELL);
00329 static int get_fmt(char **, char *, int *);
00330 static int cmp(const void *, const void *);
00331
00332
00347 int G_read_cats(const char *name,
00348 const char *mapset, struct Categories *pcats)
00349 {
00350 return G_read_raster_cats(name, mapset, pcats);
00351 }
00352
00353
00365 int G_read_raster_cats(const char *name,
00366 const char *mapset, struct Categories *pcats)
00367 {
00368 char *type;
00369
00370 switch (G__read_cats("cats", name, mapset, pcats, 1)) {
00371 case -2:
00372 type = "missing";
00373 break;
00374 case -1:
00375 type = "invalid";
00376 break;
00377 default:
00378 return 0;
00379 }
00380
00381 G_warning(_("category support for [%s] in mapset [%s] %s"),
00382 name, mapset, type);
00383 return -1;
00384 }
00385
00386
00401 int G_read_vector_cats(const char *name,
00402 const char *mapset, struct Categories *pcats)
00403 {
00404 char *type;
00405
00406 switch (G__read_cats("dig_cats", name, mapset, pcats, 1)) {
00407 case -2:
00408 type = "missing";
00409 break;
00410 case -1:
00411 type = "invalid";
00412 break;
00413 default:
00414 return 0;
00415 }
00416
00417 G_warning(_("category support for vector map [%s] in mapset [%s] %s"),
00418 name, mapset, type);
00419 return -1;
00420 }
00421
00422 CELL G_number_of_cats(const char *name, const char *mapset)
00423 {
00424 struct Range range;
00425 CELL min, max;
00426
00427
00428 if (G_read_range(name, mapset, &range) < 0)
00429 return -1;
00430 G_get_range_min_max(&range, &min, &max);
00431 if (G_is_c_null_value(&max))
00432 max = 0;
00433 return max;
00434 }
00435
00436 CELL G__read_cats(const char *element,
00437 const char *name,
00438 const char *mapset, struct Categories * pcats, int full)
00439 {
00440 FILE *fd;
00441 char buff[1024];
00442 CELL cat;
00443 DCELL val1, val2;
00444 int old = 0, fp_map;
00445 long num = -1;
00446
00447
00448 if (strncmp(element, "dig", 3) == 0)
00449 fp_map = 0;
00450 else
00451 fp_map = G_raster_map_is_fp(name, mapset);
00452
00453 if (!(fd = G_fopen_old(element, name, mapset)))
00454 return -2;
00455
00456
00457 if (G_getl(buff, sizeof buff, fd) == 0)
00458 goto error;
00459
00460 if (sscanf(buff, "# %ld", &num) == 1)
00461 old = 0;
00462 else if (sscanf(buff, "%ld", &num) == 1)
00463 old = 1;
00464
00465 if (!full) {
00466 fclose(fd);
00467 if (num < 0)
00468 return 0;
00469 return (CELL) num;
00470 }
00471
00472
00473 if (G_getl(buff, sizeof buff, fd) == 0)
00474 goto error;
00475 G_strip(buff);
00476
00477
00478 G_init_raster_cats(buff, pcats);
00479 if (num >= 0)
00480 pcats->num = num;
00481
00482 if (!old) {
00483 char fmt[256];
00484 float m1, a1, m2, a2;
00485
00486 if (G_getl(fmt, sizeof fmt, fd) == 0)
00487 goto error;
00488
00489 if (G_getl(buff, sizeof buff, fd) == 0)
00490 goto error;
00491 if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
00492 goto error;
00493 G_set_raster_cats_fmt(fmt, m1, a1, m2, a2, pcats);
00494 }
00495
00496
00497 for (cat = 0;; cat++) {
00498 char label[1024];
00499
00500 if (G_getl(buff, sizeof buff, fd) == 0)
00501 break;
00502 if (old)
00503 G_set_cat(cat, buff, pcats);
00504 else {
00505 *label = 0;
00506 if (sscanf(buff, "%1s", label) != 1)
00507 continue;
00508 if (*label == '#')
00509 continue;
00510 *label = 0;
00511
00512 if (fp_map
00513 && sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
00514 G_set_raster_cat(&val1, &val2, label, pcats, DCELL_TYPE);
00515 else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
00516 G_set_raster_cat(&cat, &cat, label, pcats, CELL_TYPE);
00517 else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
00518 G_set_raster_cat(&val1, &val1, label, pcats, DCELL_TYPE);
00519 else
00520 goto error;
00521 }
00522 }
00523
00524 fclose(fd);
00525 return 0;
00526 error:
00527 fclose(fd);
00528 return -1;
00529 }
00530
00531
00545 char *G_get_cats_title(const struct Categories *pcats)
00546 {
00547 return G_get_raster_cats_title(pcats);
00548 }
00549
00550
00560 char *G_get_raster_cats_title(const struct Categories *pcats)
00561 {
00562 static char *none = "";
00563
00564 return pcats->title ? pcats->title : none;
00565 }
00566
00567
00583 char *G_get_cat(CELL num, struct Categories *pcats)
00584 {
00585 return G_get_c_raster_cat(&num, pcats);
00586 }
00587
00588
00600 char *G_get_c_raster_cat(CELL * rast, struct Categories *pcats)
00601 {
00602 return G_get_raster_cat(rast, pcats, CELL_TYPE);
00603 }
00604
00605
00617 char *G_get_f_raster_cat(FCELL * rast, struct Categories *pcats)
00618 {
00619 return G_get_raster_cat(rast, pcats, FCELL_TYPE);
00620 }
00621
00622
00634 char *G_get_d_raster_cat(DCELL * rast, struct Categories *pcats)
00635 {
00636 return G_get_raster_cat(rast, pcats, DCELL_TYPE);
00637 }
00638
00639
00652 char *G_get_raster_cat(void *rast,
00653 struct Categories *pcats, RASTER_MAP_TYPE data_type)
00654 {
00655 static char label[1024];
00656 char *f, *l, *v;
00657 CELL i;
00658 DCELL val;
00659 float a[2];
00660 char fmt[30], value_str[30];
00661
00662 if (G_is_null_value(rast, data_type)) {
00663 sprintf(label, "no data");
00664 return label;
00665 }
00666
00667
00668 *label = 0;
00669 val = G_get_raster_value_d(rast, data_type);
00670 i = G_quant_get_cell_value(&pcats->q, val);
00671
00672 if (!G_is_c_null_value(&i) && i < pcats->ncats) {
00673 if (pcats->labels[i] != NULL)
00674 return pcats->labels[i];
00675 return label;
00676 }
00677
00678
00679 if ((f = pcats->fmt) == NULL)
00680 return label;
00681
00682 a[0] = (float)val *pcats->m1 + pcats->a1;
00683 a[1] = (float)val *pcats->m2 + pcats->a2;
00684
00685 l = label;
00686 while (*f) {
00687 if (*f == '$') {
00688 f++;
00689 if (*f == '$')
00690 *l++ = *f++;
00691 else if (*f == '?') {
00692 f++;
00693 get_cond(&f, v = value_str, val);
00694 while (*v)
00695 *l++ = *v++;
00696 }
00697 else if (get_fmt(&f, fmt, &i)) {
00698 sprintf(v = value_str, fmt, a[i]);
00699 while (*v)
00700 *l++ = *v++;
00701 }
00702 else
00703 *l++ = '$';
00704 }
00705 else {
00706 *l++ = *f++;
00707 }
00708 }
00709 *l = 0;
00710 return label;
00711 }
00712
00713
00729 int G_unmark_raster_cats(struct Categories *pcats)
00730 {
00731 register int i;
00732
00733 for (i = 0; i < pcats->ncats; i++)
00734 pcats->marks[i] = 0;
00735 return 0;
00736 }
00737
00738
00753 int G_mark_c_raster_cats(CELL * rast_row,
00754 int ncols, struct Categories *pcats)
00755 {
00756 G_mark_raster_cats(rast_row, ncols, pcats, CELL_TYPE);
00757 return 0;
00758 }
00759
00760
00775 int G_mark_f_raster_cats(FCELL * rast_row,
00776 int ncols, struct Categories *pcats)
00777 {
00778 G_mark_raster_cats(rast_row, ncols, pcats, FCELL_TYPE);
00779 return 0;
00780 }
00781
00782
00797 int G_mark_d_raster_cats(DCELL * rast_row,
00798 int ncols, struct Categories *pcats)
00799 {
00800 G_mark_raster_cats(rast_row, ncols, pcats, DCELL_TYPE);
00801 return 0;
00802 }
00803
00804
00821 int G_mark_raster_cats(void *rast_row,
00822 int ncols, struct Categories *pcats,
00823 RASTER_MAP_TYPE data_type)
00824 {
00825 CELL i;
00826
00827 while (ncols-- > 0) {
00828 i = G_quant_get_cell_value(&pcats->q,
00829 G_get_raster_value_d(rast_row, data_type));
00830 if (G_is_c_null_value(&i))
00831 continue;
00832 if (i > pcats->ncats)
00833 return -1;
00834 pcats->marks[i]++;
00835 rast_row = G_incr_void_ptr(rast_row, G_raster_size(data_type));
00836 }
00837 return 1;
00838 }
00839
00840
00852 int G_rewind_raster_cats(struct Categories *pcats)
00853 {
00854 pcats->last_marked_rule = -1;
00855 return 0;
00856 }
00857
00858 char *G_get_next_marked_d_raster_cat(struct Categories *pcats,
00859 DCELL * rast1, DCELL * rast2,
00860 long *count)
00861 {
00862 char *descr = NULL;
00863 int found, i;
00864
00865 found = 0;
00866
00867
00868
00869
00870 for (i = pcats->last_marked_rule + 1; i < G_quant_nof_rules(&pcats->q);
00871 i++) {
00872 descr = G_get_ith_d_raster_cat(pcats, i, rast1, rast2);
00873
00874 if (pcats->marks[i]) {
00875 found = 1;
00876 break;
00877 }
00878 }
00879
00880 if (!found)
00881 return NULL;
00882
00883 *count = pcats->marks[i];
00884 pcats->last_marked_rule = i;
00885 return descr;
00886 }
00887
00888 char *G_get_next_marked_c_raster_cat(struct Categories *pcats,
00889 CELL * rast1, CELL * rast2,
00890 long *count)
00891 {
00892 return G_get_next_marked_raster_cat(pcats, rast1, rast2, count,
00893 CELL_TYPE);
00894 }
00895
00896 char *G_get_next_marked_f_raster_cat(struct Categories *pcats,
00897 FCELL * rast1, FCELL * rast2,
00898 long *count)
00899 {
00900 return G_get_next_marked_raster_cat(pcats, rast1, rast2, count,
00901 FCELL_TYPE);
00902 }
00903
00904 char *G_get_next_marked_raster_cat(struct Categories *pcats,
00905 void *rast1, void *rast2,
00906 long *count, RASTER_MAP_TYPE data_type)
00907 {
00908 DCELL val1, val2;
00909 char *lab;
00910
00911 lab = G_get_next_marked_d_raster_cat(pcats, &val1, &val2, count);
00912 G_set_raster_value_d(rast1, val1, data_type);
00913 G_set_raster_value_d(rast2, val2, data_type);
00914 return lab;
00915 }
00916
00917 static int get_fmt(char **f, char *fmt, int *i)
00918 {
00919 char *ff;
00920
00921 ff = *f;
00922 if (*ff == 0)
00923 return 0;
00924 if (*ff == '$') {
00925 *f = ff + 1;
00926 return 0;
00927 }
00928 switch (*ff++) {
00929 case '1':
00930 *i = 0;
00931 break;
00932 case '2':
00933 *i = 1;
00934 break;
00935 default:
00936 return 0;
00937 }
00938 *fmt++ = '%';
00939 *fmt++ = '.';
00940 if (*ff++ != '.') {
00941 *f = ff - 1;
00942 *fmt++ = '0';
00943 *fmt++ = 'f';
00944 *fmt = 0;
00945 return 1;
00946 }
00947 *fmt = '0';
00948 while (*ff >= '0' && *ff <= '9')
00949 *fmt++ = *ff++;
00950 *fmt++ = 'f';
00951 *fmt = 0;
00952 *f = ff;
00953 return 1;
00954 }
00955
00956 static int get_cond(char **f, char *value, DCELL val)
00957 {
00958 char *ff;
00959
00960 ff = *f;
00961 if (val == 1.) {
00962 while (*ff)
00963 if (*ff++ == '$')
00964 break;
00965 }
00966
00967 while (*ff)
00968 if (*ff == '$') {
00969 ff++;
00970 break;
00971 }
00972 else
00973 *value++ = *ff++;
00974
00975 if (val != 1.) {
00976 while (*ff)
00977 if (*ff++ == '$')
00978 break;
00979 }
00980 *value = 0;
00981 *f = ff;
00982
00983 return 0;
00984 }
00985
00986
00999 int G_set_cat(CELL num, char *label, struct Categories *pcats)
01000 {
01001 CELL tmp = num;
01002
01003 return G_set_c_raster_cat(&tmp, &tmp, label, pcats);
01004 }
01005
01006
01019 int G_set_c_raster_cat(CELL * rast1, CELL * rast2,
01020 char *label, struct Categories *pcats)
01021 {
01022 return G_set_raster_cat(rast1, rast2, label, pcats, CELL_TYPE);
01023 }
01024
01025
01038 int G_set_f_raster_cat(FCELL * rast1, FCELL * rast2,
01039 char *label, struct Categories *pcats)
01040 {
01041 return G_set_raster_cat(rast1, rast2, label, pcats, FCELL_TYPE);
01042 }
01043
01044
01057 int G_set_d_raster_cat(DCELL * rast1, DCELL * rast2,
01058 char *label, struct Categories *pcats)
01059 {
01060 long len;
01061 DCELL dtmp1, dtmp2;
01062 int i;
01063 char *descr;
01064
01065
01066
01067 if (G_is_d_null_value(rast1))
01068 return 0;
01069 if (G_is_d_null_value(rast2))
01070 return 0;
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080 for (i = 0; i < pcats->ncats; i++) {
01081 descr = G_get_ith_d_raster_cat(pcats, i, &dtmp1, &dtmp2);
01082 if ((dtmp1 == *rast1 && dtmp2 == *rast2)
01083 || (dtmp1 == *rast2 && dtmp2 == *rast1)) {
01084 if (pcats->labels[i] != NULL)
01085 G_free(pcats->labels[i]);
01086 pcats->labels[i] = G_store(label);
01087 G_newlines_to_spaces(pcats->labels[i]);
01088 G_strip(pcats->labels[i]);
01089 return 1;
01090 }
01091 }
01092
01093
01094 G_quant_add_rule(&pcats->q, *rast1, *rast2, pcats->ncats, pcats->ncats);
01095 pcats->ncats++;
01096 if (pcats->nalloc < pcats->ncats) {
01097
01098 len = (pcats->nalloc + 256) * sizeof(char *);
01099
01100 if (len != (int)len) {
01101 pcats->ncats--;
01102 return -1;
01103 }
01104
01105 if (pcats->nalloc) {
01106
01107 pcats->labels =
01108 (char **)G_realloc((char *)pcats->labels, (int)len);
01109 }
01110 else {
01111
01112 pcats->labels = (char **)G_malloc((int)len);
01113 }
01114
01115
01116 len = (pcats->nalloc + 256) * sizeof(int);
01117 if (len != (int)len) {
01118 pcats->ncats--;
01119 return -1;
01120 }
01121 if (pcats->nalloc)
01122 pcats->marks = (int *)G_realloc((char *)pcats->marks, (int)len);
01123 else
01124 pcats->marks = (int *)G_malloc((int)len);
01125 pcats->nalloc += 256;
01126 }
01127
01128 pcats->labels[pcats->ncats - 1] = G_store(label);
01129 G_newlines_to_spaces(pcats->labels[pcats->ncats - 1]);
01130 G_strip(pcats->labels[pcats->ncats - 1]);
01131
01132
01133
01134
01135
01136 if ((CELL) * rast1 > pcats->num)
01137 pcats->num = (CELL) * rast1;
01138 if ((CELL) * rast2 > pcats->num)
01139 pcats->num = (CELL) * rast2;
01140
01141
01142 return 1;
01143 }
01144
01145
01158 int G_set_raster_cat(void *rast1, void *rast2,
01159 char *label,
01160 struct Categories *pcats, RASTER_MAP_TYPE data_type)
01161 {
01162 DCELL val1, val2;
01163
01164 val1 = G_get_raster_value_d(rast1, data_type);
01165 val2 = G_get_raster_value_d(rast2, data_type);
01166 return G_set_d_raster_cat(&val1, &val2, label, pcats);
01167 }
01168
01169
01183 int G_write_cats(char *name, struct Categories *cats)
01184 {
01185 return G__write_cats("cats", name, cats);
01186 }
01187
01188
01199 int G_write_raster_cats(char *name, struct Categories *cats)
01200 {
01201 return G__write_cats("cats", name, cats);
01202 }
01203
01204
01219 int G_write_vector_cats(char *name, struct Categories *cats)
01220 {
01221 return G__write_cats("dig_cats", name, cats);
01222 }
01223
01224 int G__write_cats(char *element, char *name, struct Categories *cats)
01225 {
01226 FILE *fd;
01227 int i, fp_map;
01228 char *descr;
01229 DCELL val1, val2;
01230 char str1[100], str2[100];
01231
01232
01233 if (!(fd = G_fopen_new(element, name)))
01234 return -1;
01235
01236
01237 fprintf(fd, "# %ld categories\n", (long)cats->num);
01238
01239
01240 fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
01241
01242
01243 fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
01244 fprintf(fd, "%.2f %.2f %.2f %.2f\n",
01245 cats->m1, cats->a1, cats->m2, cats->a2);
01246
01247
01248 if (strncmp(element, "dig", 3) == 0)
01249 fp_map = 0;
01250 else
01251 fp_map = G_raster_map_is_fp(name, G_mapset());
01252
01253 if (!fp_map)
01254 G_sort_cats(cats);
01255
01256
01257 for (i = 0; i < G_quant_nof_rules(&cats->q); i++) {
01258 descr = G_get_ith_d_raster_cat(cats, i, &val1, &val2);
01259 if ((cats->fmt && cats->fmt[0])
01260 || (descr && descr[0])) {
01261 if (val1 == val2) {
01262 sprintf(str1, "%.10f", val1);
01263 G_trim_decimal(str1);
01264 fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
01265 }
01266 else {
01267 sprintf(str1, "%.10f", val1);
01268 G_trim_decimal(str1);
01269 sprintf(str2, "%.10f", val2);
01270 G_trim_decimal(str2);
01271 fprintf(fd, "%s:%s:%s\n", str1, str2,
01272 descr != NULL ? descr : "");
01273 }
01274 }
01275 }
01276 fclose(fd);
01277
01278 return (1);
01279 }
01280
01281
01296 char *G_get_ith_d_raster_cat(const struct Categories *pcats,
01297 int i, DCELL * rast1, DCELL * rast2)
01298 {
01299 int index;
01300
01301 if (i > pcats->ncats) {
01302 G_set_d_null_value(rast1, 1);
01303 G_set_d_null_value(rast2, 1);
01304 return "";
01305 }
01306 G_quant_get_ith_rule(&pcats->q, i, rast1, rast2, &index, &index);
01307 return pcats->labels[index];
01308 }
01309
01310
01325 char *G_get_ith_f_raster_cat(const struct Categories *pcats,
01326 int i, void *rast1, void *rast2)
01327 {
01328 RASTER_MAP_TYPE data_type = FCELL_TYPE;
01329 char *tmp;
01330 DCELL val1, val2;
01331
01332 tmp = G_get_ith_d_raster_cat(pcats, i, &val1, &val2);
01333 G_set_raster_value_d(rast1, val1, data_type);
01334 G_set_raster_value_d(rast2, val2, data_type);
01335 return tmp;
01336 }
01337
01338
01353 char *G_get_ith_c_raster_cat(const struct Categories *pcats,
01354 int i, void *rast1, void *rast2)
01355 {
01356 RASTER_MAP_TYPE data_type = CELL_TYPE;
01357 char *tmp;
01358 DCELL val1, val2;
01359
01360 tmp = G_get_ith_d_raster_cat(pcats, i, &val1, &val2);
01361 G_set_raster_value_d(rast1, val1, data_type);
01362 G_set_raster_value_d(rast2, val2, data_type);
01363 return tmp;
01364 }
01365
01366
01383 char *G_get_ith_raster_cat(const struct Categories *pcats, int i, void *rast1,
01384 void *rast2, RASTER_MAP_TYPE data_type)
01385 {
01386 char *tmp;
01387 DCELL val1, val2;
01388
01389 tmp = G_get_ith_d_raster_cat(pcats, i, &val1, &val2);
01390 G_set_raster_value_d(rast1, val1, data_type);
01391 G_set_raster_value_d(rast2, val2, data_type);
01392 return tmp;
01393 }
01394
01395
01415 int G_init_cats(CELL num, const char *title, struct Categories *pcats)
01416 {
01417 G_init_raster_cats(title, pcats);
01418 pcats->num = num;
01419 return 0;
01420 }
01421
01422
01435 int G_init_raster_cats(const char *title, struct Categories *pcats)
01436 {
01437 G_set_raster_cats_title(title, pcats);
01438 pcats->labels = NULL;
01439 pcats->nalloc = 0;
01440 pcats->ncats = 0;
01441 pcats->num = 0;
01442 pcats->fmt = NULL;
01443 pcats->m1 = 0.0;
01444 pcats->a1 = 0.0;
01445 pcats->m2 = 0.0;
01446 pcats->a2 = 0.0;
01447 pcats->last_marked_rule = -1;
01448 G_quant_init(&pcats->q);
01449 return 0;
01450 }
01451
01452
01464 int G_set_cats_title(const char *title, struct Categories *pcats)
01465 {
01466 G_set_raster_cats_title(title, pcats);
01467 return 0;
01468 }
01469
01470
01481 int G_set_raster_cats_title(const char *title, struct Categories *pcats)
01482 {
01483 if (title == NULL)
01484 title = "";
01485 pcats->title = G_store(title);
01486 G_newlines_to_spaces(pcats->title);
01487 G_strip(pcats->title);
01488 return 0;
01489 }
01490
01491 int G_set_cats_fmt(const char *fmt, double m1, double a1, double m2,
01492 double a2, struct Categories *pcats)
01493 {
01494 G_set_raster_cats_fmt(fmt, m1, a1, m2, a2, pcats);
01495 return 0;
01496 }
01497
01498
01513 int G_set_raster_cats_fmt(const char *fmt, double m1, double a1, double m2,
01514 double a2, struct Categories *pcats)
01515 {
01516 pcats->m1 = m1;
01517 pcats->a1 = a1;
01518 pcats->m2 = m2;
01519 pcats->a2 = a2;
01520
01521 pcats->fmt = G_store(fmt);
01522 G_newlines_to_spaces(pcats->fmt);
01523 G_strip(pcats->fmt);
01524 return 0;
01525 }
01526
01527
01538 int G_free_cats(struct Categories *pcats)
01539 {
01540 G_free_raster_cats(pcats);
01541 return 0;
01542 }
01543
01544
01554 int G_free_raster_cats(struct Categories *pcats)
01555 {
01556 int i;
01557
01558 if (pcats->title != NULL) {
01559 G_free(pcats->title);
01560 pcats->title = NULL;
01561 }
01562 if (pcats->fmt != NULL) {
01563 G_free(pcats->fmt);
01564 pcats->fmt = NULL;
01565 }
01566 if (pcats->ncats > 0) {
01567 for (i = 0; i < pcats->ncats; i++)
01568 if (pcats->labels[i] != NULL)
01569 G_free(pcats->labels[i]);
01570 G_free(pcats->labels);
01571 G_free(pcats->marks);
01572 pcats->labels = NULL;
01573 }
01574 G_quant_free(&pcats->q);
01575 pcats->ncats = 0;
01576 pcats->nalloc = 0;
01577 return 0;
01578 }
01579
01580
01595 int
01596 G_copy_raster_cats(struct Categories *pcats_to,
01597 const struct Categories *pcats_from)
01598 {
01599 int i;
01600 char *descr;
01601 DCELL d1, d2;
01602
01603 G_init_raster_cats(pcats_from->title, pcats_to);
01604 for (i = 0; i < pcats_from->ncats; i++) {
01605 descr = G_get_ith_d_raster_cat(pcats_from, i, &d1, &d2);
01606 G_set_d_raster_cat(&d1, &d2, descr, pcats_to);
01607 }
01608 return 0;
01609 }
01610
01611 int G_number_of_raster_cats(struct Categories *pcats)
01612 {
01613 return pcats->ncats;
01614 }
01615
01616 static struct Categories save_cats;
01617
01618 int G_sort_cats(struct Categories *pcats)
01619 {
01620 int *indexes, i, ncats;
01621 char *descr;
01622 DCELL d1, d2;
01623
01624 if (pcats->ncats <= 1)
01625 return -1;
01626
01627 ncats = pcats->ncats;
01628
01629 G_copy_raster_cats(&save_cats, pcats);
01630 G_free_raster_cats(pcats);
01631
01632 indexes = (int *)G_malloc(sizeof(int) * ncats);
01633 for (i = 0; i < ncats; i++)
01634 indexes[i] = i;
01635
01636 qsort(indexes, ncats, sizeof(int), cmp);
01637 G_init_raster_cats(save_cats.title, pcats);
01638 for (i = 0; i < ncats; i++) {
01639 descr = G_get_ith_d_raster_cat(&save_cats, indexes[i], &d1, &d2);
01640
01641 G_set_d_raster_cat(&d1, &d2, descr, pcats);
01642
01643 }
01644 G_free_raster_cats(&save_cats);
01645
01646
01647
01648 return 0;
01649 }
01650
01651 static int cmp(const void *aa, const void *bb)
01652 {
01653 const int *a = aa, *b = bb;
01654 DCELL min_rast1, min_rast2, max_rast1, max_rast2;
01655 CELL index;
01656
01657 G_quant_get_ith_rule(&(save_cats.q), *a,
01658 &min_rast1, &max_rast1, &index, &index);
01659 G_quant_get_ith_rule(&(save_cats.q), *b,
01660 &min_rast2, &max_rast2, &index, &index);
01661 if (min_rast1 < min_rast2)
01662 return -1;
01663 if (min_rast1 > min_rast2)
01664 return 1;
01665 return 0;
01666 }