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 #include <stdlib.h>
00322 #include <string.h>
00323 #include <grass/gis.h>
00324 #include <grass/glocale.h>
00325
00326 static int get_cond ( char **, char *, DCELL);
00327 static int get_fmt ( char **, char *, int *);
00328 static int cmp (const void *, const void *);
00329
00330
00345 int G_read_cats (
00346 char *name ,
00347 char *mapset ,
00348 struct Categories *pcats )
00349 {
00350 return G_read_raster_cats (name, mapset, pcats);
00351 }
00352
00353
00365 int G_read_raster_cats (
00366 char *name ,
00367 char *mapset ,
00368 struct Categories *pcats )
00369 {
00370 char *type;
00371
00372 switch (G__read_cats ("cats", name, mapset, pcats, 1))
00373 {
00374 case -2:
00375 type = "missing";
00376 break;
00377 case -1:
00378 type = "invalid";
00379 break;
00380 default:
00381 return 0;
00382 }
00383
00384 G_warning (_("category support for [%s] in mapset [%s] %s"),
00385 name, mapset, type);
00386 return -1;
00387 }
00388
00389
00404 int G_read_vector_cats (
00405 char *name ,
00406 char *mapset ,
00407 struct Categories *pcats )
00408 {
00409 char *type;
00410
00411 switch (G__read_cats ("dig_cats", name, mapset, pcats, 1))
00412 {
00413 case -2:
00414 type = "missing";
00415 break;
00416 case -1:
00417 type = "invalid";
00418 break;
00419 default:
00420 return 0;
00421 }
00422
00423 G_warning (_("category support for vector file [%s] in mapset [%s] %s"),
00424 name, mapset, type);
00425 return -1;
00426 }
00427
00428 CELL G_number_of_cats (
00429 char *name ,
00430 char *mapset)
00431 {
00432 struct Range range;
00433 CELL min, max;
00434
00435
00436 if(G_read_range(name, mapset, &range) < 0)
00437 return -1;
00438 G_get_range_min_max(&range, &min, &max);
00439 if(G_is_c_null_value(&max)) max = 0;
00440 return max;
00441 }
00442
00443 CELL G__read_cats (
00444 char *element,
00445 char *name ,
00446 char *mapset ,
00447 struct Categories *pcats,
00448 int full)
00449 {
00450 FILE *fd ;
00451 char buff[1024] ;
00452 CELL cat;
00453 DCELL val1, val2;
00454 int old=0, fp_map;
00455 long num=-1;
00456
00457
00458 if(strncmp(element, "dig", 3)== 0)
00459 fp_map = 0;
00460 else
00461 fp_map = G_raster_map_is_fp(name, mapset);
00462
00463 if (!(fd = G_fopen_old (element, name, mapset)))
00464 return -2 ;
00465
00466
00467 if (G_getl(buff,sizeof buff,fd) == 0)
00468 goto error;
00469
00470 if (sscanf ( buff, "# %ld" , &num) == 1)
00471 old = 0;
00472 else if (sscanf ( buff, "%ld" , &num) == 1)
00473 old = 1;
00474
00475 if (!full)
00476 {
00477 fclose (fd);
00478 if(num < 0) return 0;
00479 return (CELL) num;
00480 }
00481
00482
00483 if (G_getl(buff,sizeof buff,fd) == 0)
00484 goto error;
00485 G_strip (buff);
00486
00487
00488 G_init_raster_cats (buff, pcats);
00489 if(num >= 0) pcats->num = num;
00490
00491 if (!old)
00492 {
00493 char fmt[256];
00494 float m1,a1,m2,a2;
00495 if (G_getl(fmt,sizeof fmt,fd) == 0)
00496 goto error;
00497
00498 if (G_getl(buff,sizeof buff,fd) == 0)
00499 goto error;
00500 if(sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
00501 goto error;
00502 G_set_raster_cats_fmt (fmt, m1, a1, m2, a2, pcats);
00503 }
00504
00505
00506 for (cat=0;;cat++)
00507 {
00508 char label[1024];
00509 if (G_getl(buff, sizeof buff, fd) == 0)
00510 break;
00511 if (old)
00512 G_set_cat (cat, buff, pcats) ;
00513 else
00514 {
00515 *label = 0;
00516 if (sscanf (buff, "%1s", label) != 1)
00517 continue;
00518 if(*label == '#')
00519 continue;
00520 *label = 0;
00521
00522 if (fp_map
00523 && sscanf (buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
00524 G_set_raster_cat (&val1, &val2, label, pcats, DCELL_TYPE);
00525 else if (sscanf (buff, "%d:%[^\n]", &cat, label) >= 1)
00526 G_set_raster_cat (&cat, &cat, label, pcats, CELL_TYPE);
00527 else if (sscanf (buff, "%lf:%[^\n]", &val1, label) >= 1)
00528 G_set_raster_cat (&val1, &val1, label, pcats, DCELL_TYPE);
00529 else goto error;
00530 }
00531 }
00532
00533 fclose (fd);
00534 return 0 ;
00535 error:
00536 fclose (fd);
00537 return -1 ;
00538 }
00539
00540
00554 char *G_get_cats_title (struct Categories *pcats)
00555 {
00556 return G_get_raster_cats_title (pcats);
00557 }
00558
00559
00569 char *G_get_raster_cats_title (struct Categories *pcats)
00570 {
00571 static char *none = "";
00572 return pcats->title ? pcats->title : none;
00573 }
00574
00575
00591 char *G_get_cat (CELL num, struct Categories *pcats)
00592 {
00593 CELL tmp=num;
00594 return G_get_c_raster_cat(&tmp, pcats);
00595 }
00596
00597
00609 char *G_get_c_raster_cat ( CELL *rast, struct Categories *pcats)
00610 {
00611 return G_get_raster_cat(rast, pcats, CELL_TYPE);
00612 }
00613
00614
00626 char *G_get_f_raster_cat (
00627 FCELL *rast,
00628 struct Categories *pcats )
00629 {
00630 return G_get_raster_cat(rast, pcats, FCELL_TYPE);
00631 }
00632
00633
00645 char *G_get_d_raster_cat (
00646 DCELL *rast,
00647 struct Categories *pcats)
00648 {
00649 return G_get_raster_cat(rast, pcats, DCELL_TYPE);
00650 }
00651
00652
00665 char *G_get_raster_cat (
00666 void *rast,
00667 struct Categories *pcats ,
00668 RASTER_MAP_TYPE data_type)
00669 {
00670 static char label[1024] ;
00671 char *f, *l, *v;
00672 CELL i;
00673 DCELL val;
00674 float a[2];
00675 char fmt[30], value_str[30];
00676
00677 if(G_is_null_value(rast, data_type))
00678 {
00679 sprintf(label, "no data");
00680 return label;
00681 }
00682
00683
00684 *label = 0;
00685 val = G_get_raster_value_d(rast, data_type);
00686 i = G_quant_get_cell_value(&pcats->q, val);
00687
00688 if(!G_is_c_null_value(&i) && i < pcats->ncats)
00689 {
00690 if(pcats->labels[i] != NULL) return pcats->labels[i];
00691 return label;
00692 }
00693
00694
00695 if ((f = pcats->fmt) == NULL)
00696 return label;
00697
00698 a[0] = (float)val*pcats->m1+pcats->a1 ;
00699 a[1] = (float)val*pcats->m2+pcats->a2 ;
00700 l = label;
00701 while (*f)
00702 {
00703 if (*f == '$')
00704 {
00705 f++;
00706 if (*f == '$')
00707 *l++ = *f++;
00708 else if (*f == '?')
00709 {
00710 f++;
00711 get_cond (&f, v = value_str, val);
00712 while (*v)
00713 *l++ = *v++;
00714 }
00715 else if (get_fmt (&f, fmt, &i))
00716 {
00717 sprintf (v = value_str, fmt, a[i]);
00718 while (*v)
00719 *l++ = *v++;
00720 }
00721 else
00722 *l++ = '$';
00723 }
00724 else
00725 {
00726 *l++ = *f++;
00727 }
00728 }
00729 *l = 0;
00730 return label;
00731 }
00732
00733
00749 int G_unmark_raster_cats (
00750 struct Categories *pcats)
00751 {
00752 register int i;
00753 for(i=0; i<pcats->ncats; i++)
00754 pcats->marks[i] = 0;
00755 return 0;
00756 }
00757
00758
00773 int G_mark_c_raster_cats (
00774 CELL *rast_row,
00775 int ncols,
00776 struct Categories *pcats)
00777 {
00778 G_mark_raster_cats (rast_row, ncols, pcats, CELL_TYPE);
00779 return 0;
00780 }
00781
00782
00797 int G_mark_f_raster_cats (
00798 FCELL *rast_row,
00799 int ncols,
00800 struct Categories *pcats)
00801 {
00802 G_mark_raster_cats (rast_row, ncols, pcats, FCELL_TYPE);
00803 return 0;
00804 }
00805
00806
00821 int G_mark_d_raster_cats (
00822 DCELL *rast_row,
00823 int ncols,
00824 struct Categories *pcats)
00825 {
00826 G_mark_raster_cats (rast_row, ncols, pcats, DCELL_TYPE);
00827 return 0;
00828 }
00829
00830
00847 int G_mark_raster_cats (
00848 void *rast_row,
00849 int ncols,
00850 struct Categories *pcats,
00851 RASTER_MAP_TYPE data_type)
00852 {
00853 CELL i;
00854
00855 while(ncols-- > 0)
00856 {
00857 i = G_quant_get_cell_value(&pcats->q,
00858 G_get_raster_value_d(rast_row, data_type));
00859 if(G_is_c_null_value(&i)) continue;
00860 if(i> pcats->ncats) return -1;
00861 pcats->marks[i]++;
00862 rast_row = G_incr_void_ptr(rast_row, G_raster_size(data_type));
00863 }
00864 return 1;
00865 }
00866
00867
00879 int G_rewind_raster_cats (struct Categories *pcats)
00880 {
00881 pcats->last_marked_rule = -1;
00882 return 0;
00883 }
00884
00885 char *G_get_next_marked_d_raster_cat(
00886 struct Categories *pcats,
00887 DCELL *rast1,DCELL *rast2,
00888 long *count)
00889 {
00890 char *descr=NULL;
00891 int found, i;
00892
00893 found = 0;
00894
00895
00896
00897
00898 for (i = pcats->last_marked_rule + 1; i < G_quant_nof_rules(&pcats->q); i++)
00899 {
00900 descr = G_get_ith_d_raster_cat(pcats, i, rast1, rast2);
00901
00902 if(pcats->marks[i])
00903 {
00904 found = 1;
00905 break;
00906 }
00907 }
00908
00909 if(!found) return NULL;
00910
00911 *count = pcats->marks[i];
00912 pcats->last_marked_rule = i;
00913 return descr;
00914 }
00915
00916 char *G_get_next_marked_c_raster_cat(
00917 struct Categories *pcats,
00918 CELL *rast1,CELL *rast2,
00919 long *count)
00920 {
00921 return G_get_next_marked_raster_cat(pcats, rast1, rast2, count, CELL_TYPE);
00922 }
00923
00924 char *G_get_next_marked_f_raster_cat(
00925 struct Categories *pcats,
00926 FCELL *rast1,FCELL *rast2,
00927 long *count)
00928 {
00929 return G_get_next_marked_raster_cat(pcats, rast1, rast2, count, FCELL_TYPE);
00930 }
00931
00932 char *G_get_next_marked_raster_cat(
00933 struct Categories *pcats,
00934 void *rast1,void *rast2,
00935 long *count,
00936 RASTER_MAP_TYPE data_type)
00937 {
00938 DCELL val1, val2;
00939 char *lab;
00940
00941 lab = G_get_next_marked_d_raster_cat(pcats, &val1, &val2, count);
00942 G_set_raster_value_d(rast1, val1, data_type);
00943 G_set_raster_value_d(rast2, val2, data_type);
00944 return lab;
00945 }
00946
00947 static int get_fmt ( char **f, char *fmt, int *i)
00948 {
00949 char *ff;
00950
00951 ff = *f;
00952 if (*ff == 0) return 0;
00953 if (*ff == '$')
00954 {
00955 *f = ff+1;
00956 return 0;
00957 }
00958 switch (*ff++)
00959 {
00960 case '1': *i = 0; break;
00961 case '2': *i = 1; break;
00962 default: return 0;
00963 }
00964 *fmt++ = '%';
00965 *fmt++ = '.';
00966 if (*ff++ != '.')
00967 {
00968 *f = ff-1;
00969 *fmt++ = '0';
00970 *fmt++ = 'f';
00971 *fmt = 0;
00972 return 1;
00973 }
00974 *fmt = '0';
00975 while (*ff >= '0' && *ff <= '9')
00976 *fmt++ = *ff++;
00977 *fmt++ = 'f';
00978 *fmt = 0;
00979 *f = ff;
00980 return 1;
00981 }
00982
00983 static int get_cond ( char **f, char *value, DCELL val)
00984 {
00985 char *ff;
00986
00987 ff = *f;
00988 if (val == 1.)
00989 {
00990 while (*ff)
00991 if (*ff++ == '$')
00992 break;
00993 }
00994
00995 while (*ff)
00996 if (*ff == '$')
00997 {
00998 ff++;
00999 break;
01000 }
01001 else
01002 *value++ = *ff++;
01003
01004 if (val != 1.)
01005 {
01006 while (*ff)
01007 if (*ff++ == '$')
01008 break;
01009 }
01010 *value = 0;
01011 *f = ff;
01012
01013 return 0;
01014 }
01015
01016
01029 int G_set_cat (
01030 CELL num ,
01031 char *label ,
01032 struct Categories *pcats )
01033 {
01034 CELL tmp=num;
01035 return G_set_c_raster_cat(&tmp, &tmp, label, pcats);
01036 }
01037
01038
01051 int G_set_c_raster_cat (
01052 CELL *rast1,CELL *rast2,
01053 char *label ,
01054 struct Categories *pcats )
01055 {
01056 return G_set_raster_cat (rast1, rast2, label, pcats, CELL_TYPE);
01057 }
01058
01059
01072 int G_set_f_raster_cat (
01073 FCELL *rast1,FCELL *rast2,
01074 char *label ,
01075 struct Categories *pcats)
01076 {
01077 return G_set_raster_cat (rast1, rast2, label, pcats, FCELL_TYPE);
01078 }
01079
01080
01093 int G_set_d_raster_cat (
01094 DCELL *rast1,DCELL *rast2,
01095 char *label ,
01096 struct Categories *pcats )
01097 {
01098 long len;
01099 DCELL dtmp1, dtmp2;
01100 int i;
01101 char *descr;
01102
01103
01104
01105 if(G_is_d_null_value(rast1)) return 0;
01106 if(G_is_d_null_value(rast2)) return 0;
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116 for(i=0; i< pcats->ncats; i++)
01117 {
01118 descr = G_get_ith_d_raster_cat(pcats, i, &dtmp1, &dtmp2);
01119 if((dtmp1==*rast1 && dtmp2==*rast2)
01120 ||(dtmp1==*rast2 && dtmp2==*rast1))
01121 {
01122 if(pcats->labels[i] != NULL) G_free(pcats->labels[i]);
01123 pcats->labels[i] = G_store(label);
01124 G_newlines_to_spaces (pcats->labels[i]);
01125 G_strip (pcats->labels[i]);
01126 return 1;
01127 }
01128 }
01129
01130
01131 G_quant_add_rule(&pcats->q, *rast1, *rast2, pcats->ncats, pcats->ncats);
01132 pcats->ncats++;
01133 if(pcats->nalloc < pcats->ncats)
01134 {
01135
01136 len = (pcats->nalloc + 256) * sizeof(char *);
01137
01138 if (len != (int) len)
01139 {
01140 pcats->ncats--;
01141 return -1;
01142 }
01143
01144 if(pcats->nalloc) {
01145
01146 pcats->labels = (char**) G_realloc((char*) pcats->labels, (int) len);
01147 } else {
01148
01149 pcats->labels = (char**) G_malloc((int) len);
01150 }
01151
01152
01153 len = (pcats->nalloc + 256) * sizeof(int);
01154 if (len != (int) len)
01155 {
01156 pcats->ncats--;
01157 return -1;
01158 }
01159 if(pcats->nalloc)
01160 pcats->marks = (int*) G_realloc((char *) pcats->marks, (int) len);
01161 else
01162 pcats->marks = (int*) G_malloc((int) len);
01163 pcats->nalloc += 256;
01164 }
01165
01166 pcats->labels[pcats->ncats - 1] = G_store(label) ;
01167 G_newlines_to_spaces (pcats->labels[pcats->ncats - 1]);
01168 G_strip (pcats->labels[pcats->ncats - 1]);
01169
01170
01171
01172
01173
01174 if ((CELL) *rast1 > pcats->num)
01175 pcats->num = (CELL) *rast1;
01176 if ((CELL) *rast2 > pcats->num)
01177 pcats->num = (CELL) *rast2;
01178
01179
01180 return 1;
01181 }
01182
01183
01196 int G_set_raster_cat (
01197 void *rast1,void *rast2,
01198 char *label ,
01199 struct Categories *pcats ,
01200 RASTER_MAP_TYPE data_type)
01201 {
01202 DCELL val1, val2;
01203
01204 val1 = G_get_raster_value_d(rast1, data_type);
01205 val2 = G_get_raster_value_d(rast2, data_type);
01206 return G_set_d_raster_cat (&val1, &val2, label, pcats);
01207 }
01208
01209
01223 int G_write_cats ( char *name , struct Categories *cats )
01224 {
01225 return G__write_cats ("cats", name, cats);
01226 }
01227
01228
01239 int G_write_raster_cats ( char *name , struct Categories *cats )
01240 {
01241 return G__write_cats ("cats", name, cats);
01242 }
01243
01244
01259 int G_write_vector_cats ( char *name , struct Categories *cats )
01260 {
01261 return G__write_cats ("dig_cats", name, cats);
01262 }
01263
01264 int G__write_cats( char *element, char *name, struct Categories *cats)
01265 {
01266 FILE *fd ;
01267 int i, fp_map;
01268 char *descr;
01269 DCELL val1, val2;
01270 char str1[100], str2[100];
01271
01272
01273 if (!(fd = G_fopen_new (element, name)))
01274 return -1;
01275
01276
01277 fprintf(fd,"# %ld categories\n", (long) cats->num);
01278
01279
01280 fprintf(fd,"%s\n", cats->title!=NULL?cats->title:"") ;
01281
01282
01283 fprintf(fd,"%s\n", cats->fmt!=NULL?cats->fmt:"") ;
01284 fprintf(fd,"%.2f %.2f %.2f %.2f\n",
01285 cats->m1, cats->a1, cats->m2, cats->a2) ;
01286
01287
01288 if(strncmp(element, "dig", 3)== 0)
01289 fp_map = 0;
01290 else
01291 fp_map = G_raster_map_is_fp(name, G_mapset());
01292
01293 if(!fp_map) G_sort_cats (cats);
01294
01295
01296 for (i = 0; i < G_quant_nof_rules(&cats->q); i++)
01297 {
01298 descr = G_get_ith_d_raster_cat(cats, i, &val1, &val2);
01299 if ((cats->fmt && cats->fmt[0])
01300 || (descr && descr[0]))
01301 {
01302 if(val1 == val2)
01303 {
01304 sprintf(str1, "%.10f", val1);
01305 G_trim_decimal (str1);
01306 fprintf(fd,"%s:%s\n", str1,
01307 descr!=NULL?descr:"");
01308 }
01309 else
01310 {
01311 sprintf(str1, "%.10f", val1);
01312 G_trim_decimal (str1);
01313 sprintf(str2, "%.10f", val2);
01314 G_trim_decimal (str2);
01315 fprintf(fd,"%s:%s:%s\n", str1, str2,
01316 descr!=NULL?descr:"");
01317 }
01318 }
01319 }
01320 fclose (fd) ;
01321
01322 return(1) ;
01323 }
01324
01325
01340 char *G_get_ith_d_raster_cat (
01341 struct Categories *pcats,
01342 int i,
01343 DCELL *rast1,DCELL *rast2)
01344 {
01345 int index;
01346 if(i > pcats->ncats)
01347 {
01348 G_set_d_null_value(rast1, 1);
01349 G_set_d_null_value(rast2, 1);
01350 return "";
01351 }
01352 G_quant_get_ith_rule(&pcats->q, i, rast1, rast2, &index, &index);
01353 return pcats->labels[index];
01354 }
01355
01356
01371 char *G_get_ith_f_raster_cat (
01372 struct Categories *pcats,
01373 int i,
01374 void *rast1,void *rast2)
01375 {
01376 RASTER_MAP_TYPE data_type = FCELL_TYPE;
01377 char *tmp;
01378 DCELL val1, val2;
01379 tmp = G_get_ith_d_raster_cat (pcats, i, &val1, &val2);
01380 G_set_raster_value_d(rast1, val1, data_type);
01381 G_set_raster_value_d(rast2, val2, data_type);
01382 return tmp;
01383 }
01384
01385
01400 char *G_get_ith_c_raster_cat (
01401 struct Categories *pcats,
01402 int i,
01403 void *rast1,void *rast2)
01404 {
01405 RASTER_MAP_TYPE data_type = CELL_TYPE;
01406 char *tmp;
01407 DCELL val1, val2;
01408 tmp = G_get_ith_d_raster_cat (pcats, i, &val1, &val2);
01409 G_set_raster_value_d(rast1, val1, data_type);
01410 G_set_raster_value_d(rast2, val2, data_type);
01411 return tmp;
01412 }
01413
01414
01431 char *
01432 G_get_ith_raster_cat (struct Categories *pcats, int i, void *rast1, void *rast2, RASTER_MAP_TYPE data_type)
01433 {
01434 char *tmp;
01435 DCELL val1, val2;
01436 tmp = G_get_ith_d_raster_cat (pcats, i, &val1, &val2);
01437 G_set_raster_value_d(rast1, val1, data_type);
01438 G_set_raster_value_d(rast2, val2, data_type);
01439 return tmp;
01440 }
01441
01442
01462 int G_init_cats (
01463 CELL num,
01464 char *title,
01465 struct Categories *pcats)
01466 {
01467 G_init_raster_cats (title, pcats);
01468 pcats->num = num;
01469 return 0;
01470 }
01471
01472
01485 int
01486 G_init_raster_cats (char *title, struct Categories *pcats)
01487 {
01488 G_set_raster_cats_title (title, pcats);
01489 pcats->labels = NULL;
01490 pcats->nalloc = 0;
01491 pcats->ncats = 0;
01492 pcats->num = 0;
01493 pcats->fmt = NULL;
01494 pcats->m1 = 0.0;
01495 pcats->a1 = 0.0;
01496 pcats->m2 = 0.0;
01497 pcats->a2 = 0.0;
01498 pcats->last_marked_rule = -1;
01499 G_quant_init(&pcats->q);
01500 return 0;
01501 }
01502
01503
01515 int
01516 G_set_cats_title (char *title, struct Categories *pcats)
01517 {
01518 G_set_raster_cats_title (title, pcats);
01519 return 0;
01520 }
01521
01522
01533 int
01534 G_set_raster_cats_title (char *title, struct Categories *pcats)
01535 {
01536 if (title == NULL) title="";
01537 pcats->title = G_store (title);
01538 G_newlines_to_spaces (pcats->title);
01539 G_strip (pcats->title);
01540 return 0;
01541 }
01542
01543 int G_set_cats_fmt (char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
01544 {
01545 G_set_raster_cats_fmt (fmt, m1, a1, m2, a2, pcats);
01546 return 0;
01547 }
01548
01549
01564 int G_set_raster_cats_fmt (char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
01565 {
01566 pcats->m1 = m1;
01567 pcats->a1 = a1;
01568 pcats->m2 = m2;
01569 pcats->a2 = a2;
01570
01571 pcats->fmt = G_store (fmt);
01572 G_newlines_to_spaces (pcats->fmt);
01573 G_strip(pcats->fmt);
01574 return 0;
01575 }
01576
01577
01588 int G_free_cats (struct Categories *pcats)
01589 {
01590 G_free_raster_cats (pcats);
01591 return 0;
01592 }
01593
01594
01604 int G_free_raster_cats (struct Categories *pcats)
01605 {
01606 int i;
01607
01608 if (pcats->title != NULL)
01609 {
01610 G_free (pcats->title);
01611 pcats->title = NULL;
01612 }
01613 if (pcats->fmt != NULL)
01614 {
01615 G_free (pcats->fmt);
01616 pcats->fmt = NULL;
01617 }
01618 if (pcats->ncats > 0)
01619 {
01620 for (i = 0; i < pcats->ncats; i++)
01621 if (pcats->labels[i] != NULL)
01622 G_free (pcats->labels[i]);
01623 G_free (pcats->labels);
01624 G_free (pcats->marks);
01625 pcats->labels = NULL;
01626 }
01627 G_quant_free (&pcats->q);
01628 pcats->ncats = 0;
01629 pcats->nalloc = 0;
01630 return 0;
01631 }
01632
01633
01648 int
01649 G_copy_raster_cats (struct Categories *pcats_to, struct Categories *pcats_from)
01650 {
01651 int i;
01652 char *descr;
01653 DCELL d1, d2;
01654
01655 G_init_raster_cats(pcats_from->title, pcats_to);
01656 for(i = 0; i < pcats_from->ncats; i++)
01657 {
01658 descr = G_get_ith_d_raster_cat (pcats_from, i, &d1, &d2);
01659 G_set_d_raster_cat(&d1, &d2, descr, pcats_to);
01660 }
01661 return 0;
01662 }
01663
01664 int G_number_of_raster_cats (struct Categories *pcats)
01665 {
01666 return pcats->ncats;
01667 }
01668
01669 static struct Categories save_cats;
01670
01671 int G_sort_cats (struct Categories *pcats)
01672 {
01673 int *indexes, i , ncats;
01674 char *descr;
01675 DCELL d1, d2;
01676
01677 if (pcats->ncats <= 1) return -1;
01678
01679 ncats = pcats->ncats;
01680
01681 G_copy_raster_cats(&save_cats, pcats);
01682 G_free_raster_cats(pcats);
01683
01684 indexes = (int *) G_malloc(sizeof(int) * ncats);
01685 for(i = 0; i < ncats; i++)
01686 indexes[i] = i;
01687
01688 qsort (indexes, ncats, sizeof (int), cmp);
01689 G_init_raster_cats(save_cats.title, pcats);
01690 for(i = 0; i < ncats; i++)
01691 {
01692 descr = G_get_ith_d_raster_cat (&save_cats, indexes[i], &d1, &d2);
01693
01694 G_set_d_raster_cat(&d1, &d2, descr, pcats);
01695
01696 }
01697 G_free_raster_cats(&save_cats);
01698
01699
01700
01701 return 0;
01702 }
01703
01704 static int cmp (const void *aa, const void *bb)
01705 {
01706 const int *a = aa, *b = bb;
01707 DCELL min_rast1, min_rast2, max_rast1, max_rast2;
01708 CELL index;
01709 G_quant_get_ith_rule(&(save_cats.q), *a,
01710 &min_rast1, &max_rast1, &index, &index);
01711 G_quant_get_ith_rule(&(save_cats.q), *b,
01712 &min_rast2, &max_rast2, &index, &index);
01713 if(min_rast1 < min_rast2)
01714 return -1;
01715 if(min_rast1 > min_rast2)
01716 return 1;
01717 return 0;
01718 }
01719
01720