78 static void get_cond(
char **,
char *,
DCELL);
79 static int get_fmt(
char **,
char *,
int *);
80 static int cmp(
const void *,
const void *);
82 static void write_cats(
const char *
element,
const char *
name,
107 switch (read_cats(
"cats", name, mapset, pcats, 1)) {
109 G_warning(
_(
"Category support for <%s@%s> missing"), name, mapset);
112 G_warning(
_(
"Category support for <%s@%s> invalid"), name, mapset);
143 switch (read_cats(
"dig_cats", name, mapset, pcats, 1)) {
145 G_warning(
_(
"Category support for vector map <%s@%s> missing"),
149 G_warning(
_(
"Category support for vector map <%s@%s> invalid"),
197 if (strncmp(
element,
"dig", 3) == 0)
206 if (
G_getl(buff,
sizeof buff, fd) == 0)
209 if (sscanf(buff,
"# %ld", &num) == 1)
211 else if (sscanf(buff,
"%ld", &num) == 1)
222 if (
G_getl(buff,
sizeof buff, fd) == 0)
233 float m1, a1, m2, a2;
235 if (
G_getl(fmt,
sizeof fmt, fd) == 0)
238 if (
G_getl(buff,
sizeof buff, fd) == 0)
240 if (sscanf(buff,
"%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
246 for (cat1 = 0;; cat1++) {
249 if (
G_getl(buff,
sizeof buff, fd) == 0)
255 if (sscanf(buff,
"%1s", label) != 1)
262 && sscanf(buff,
"%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
265 && sscanf(buff,
"%d:%d:%[^\n]", &cat1, &cat2, label) == 3)
267 else if (sscanf(buff,
"%d:%[^\n]", &cat1, label) >= 1)
269 else if (sscanf(buff,
"%lf:%[^\n]", &val1, label) >= 1)
399 static char label[1024];
404 char fmt[30], value_str[30];
407 sprintf(label,
"no data");
416 G_debug(5,
"Rast_get_cat(): val %lf found i %d", val, i);
428 a[0] = (float)val *pcats->
m1 + pcats->
a1;
429 a[1] = (
float)val *pcats->
m2 + pcats->
a2;
437 else if (*f ==
'?') {
439 get_cond(&f, v = value_str, val);
443 else if (get_fmt(&f, fmt, &i)) {
444 sprintf(v = value_str, fmt, a[i]);
475 for (i = 0; i < pcats->
ncats; i++)
567 while (ncols-- > 0) {
572 if (i > pcats->
ncats)
619 if (pcats->
marks[i]) {
628 *count = pcats->
marks[i];
677 void *rast1,
void *rast2,
689 static int get_fmt(
char **f,
char *fmt,
int *i)
720 while (*ff >=
'0' && *ff <=
'9')
728 static void get_cond(
char **f,
char *value,
DCELL val)
831 for (i = 0; i < pcats->
ncats; i++) {
833 if ((dtmp1 == *rast1 && dtmp2 == *rast2)
834 || (dtmp1 == *rast2 && dtmp2 == *rast1)) {
850 len = (pcats->
nalloc + 256) *
sizeof(
char *);
852 if (len != (
int)len) {
868 len = (pcats->
nalloc + 256) *
sizeof(
int);
869 if (len != (
int)len) {
888 if ((
CELL) * rast1 > pcats->
num)
890 if ((
CELL) * rast2 > pcats->
num)
940 write_cats(
"cats", name, cats);
957 write_cats(
"dig_cats", name, cats);
960 static void write_cats(
const char *
element,
const char *
name,
967 char str1[100], str2[100];
974 fprintf(fd,
"# %ld categories\n", (
long)cats->
num);
980 fprintf(fd,
"%s\n", cats->
fmt !=
NULL ? cats->
fmt :
"");
981 fprintf(fd,
"%.2f %.2f %.2f %.2f\n",
982 cats->
m1, cats->
a1, cats->
m2, cats->
a2);
985 if (strncmp(
element,
"dig", 3) == 0)
995 if ((cats->
fmt && cats->
fmt[0])
996 || (descr && descr[0])) {
998 sprintf(str1,
"%.10f", val1);
1000 fprintf(fd,
"%s:%s\n", str1, descr !=
NULL ? descr :
"");
1003 sprintf(str1,
"%.10f", val1);
1005 sprintf(str2,
"%.10f", val2);
1007 fprintf(fd,
"%s:%s:%s\n", str1, str2,
1008 descr !=
NULL ? descr :
"");
1034 if (i > pcats->
ncats) {
1040 return pcats->
labels[index];
1058 int i,
void *rast1,
void *rast2)
1085 int i,
void *rast1,
void *rast2)
1225 if (pcats->
ncats > 0) {
1226 for (i = 0; i < pcats->
ncats; i++)
1256 for (i = 0; i < pcats_from->
ncats; i++) {
1271 return pcats->
ncats;
1284 int *indexes, i, ncats;
1288 if (pcats->
ncats <= 1)
1291 ncats = pcats->
ncats;
1292 G_debug(3,
"Rast_sort_cats(): Copying to save cats buffer");
1296 indexes = (
int *)
G_malloc(
sizeof(
int) * ncats);
1297 for (i = 0; i < ncats; i++)
1300 qsort(indexes, ncats,
sizeof(
int), cmp);
1302 for (i = 0; i < ncats; i++) {
1304 G_debug(4,
" Write sorted cats, pcats = %p pcats->labels = %p",
1305 (
void *)pcats, (
void *)pcats->
labels);
1313 static int cmp(
const void *aa,
const void *bb)
1315 const int *a = aa, *
b = bb;
1316 DCELL min_rast1, min_rast2, max_rast1, max_rast2;
1320 &min_rast1, &max_rast1, &index, &index);
1322 &min_rast2, &max_rast2, &index, &index);
1323 if (min_rast1 < min_rast2)
1325 if (min_rast1 > min_rast2)
CELL Rast_get_max_c_cat(const char *name, const char *mapset)
Get the max category number.
int G_getl(char *, int, FILE *)
Gets a line of text from a file.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Rast_mark_cats(const void *rast_row, int ncols, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Looks up the category label for each raster value (DCELL).
DCELL Rast_get_d_value(const void *, RASTER_MAP_TYPE)
Retrieves the value of given type from pointer p (DCELL)
int Rast_read_cats(const char *name, const char *mapset, struct Categories *pcats)
Read raster category file.
#define Rast_is_d_null_value(dcellVal)
char * Rast_get_ith_f_cat(const struct Categories *pcats, int i, void *rast1, void *rast2)
Get category description (FCELL)
char * Rast_get_f_cat(FCELL *rast, struct Categories *pcats)
Get a raster category label (FCELL)
void Rast_mark_c_cats(const CELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value (CELL).
void G_free(void *)
Free allocated memory.
int Rast_read_vector_cats(const char *name, const char *mapset, struct Categories *pcats)
Read vector category file.
#define G_incr_void_ptr(ptr, size)
char * Rast_get_c_cat(CELL *rast, struct Categories *pcats)
Get a raster category label (CELL)
void G_strip(char *)
Removes all leading and trailing white space from string.
char * Rast_get_d_cat(DCELL *rast, struct Categories *pcats)
Get a raster category label (DCELL)
void Rast_mark_f_cats(const FCELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value (FCELL).
int Rast_set_c_cat(const CELL *rast1, const CELL *rast2, const char *label, struct Categories *pcats)
Set a raster category label (CELL)
void Rast_set_cats_fmt(const char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
Set category fmt (?)
int Rast_number_of_cats(struct Categories *pcats)
Get number of raster categories.
void Rast_quant_init(struct Quant *)
Initialize the structure.
int Rast_quant_nof_rules(const struct Quant *)
Returns the number of quantization rules defined.
int Rast_read_range(const char *, const char *, struct Range *)
Read raster range (CELL)
void Rast_quant_free(struct Quant *)
Resets and frees allocated memory.
int Rast_set_f_cat(const FCELL *rast1, const FCELL *rast2, const char *label, struct Categories *pcats)
Set a raster category label (FCELL)
char * Rast_get_next_marked_f_cat(struct Categories *pcats, FCELL *rast1, FCELL *rast2, long *count)
Get next marked raster categories (FCELL)
CELL Rast_quant_get_cell_value(struct Quant *, DCELL)
Returns a CELL category for the floating-point value based on the quantization rules in q...
char * Rast_get_cat(void *rast, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Get a raster category label.
void Rast_rewind_cats(struct Categories *pcats)
Rewind raster categories.
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
char * Rast_get_ith_d_cat(const struct Categories *pcats, int i, DCELL *rast1, DCELL *rast2)
Get category description (DCELL)
void Rast_init_cats(const char *title, struct Categories *pcats)
Initialize category structure.
char * Rast_get_ith_cat(const struct Categories *pcats, int i, void *rast1, void *rast2, RASTER_MAP_TYPE data_type)
Get category description.
size_t Rast_cell_size(RASTER_MAP_TYPE)
Returns size of a raster cell in bytes.
char * Rast_get_next_marked_cat(struct Categories *pcats, void *rast1, void *rast2, long *count, RASTER_MAP_TYPE data_type)
Get next marked raster categories.
char * Rast_get_cats_title(const struct Categories *pcats)
Get title from category structure struct.
char * Rast_get_next_marked_c_cat(struct Categories *pcats, CELL *rast1, CELL *rast2, long *count)
Get next marked raster categories (CELL)
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
int Rast_set_d_cat(const DCELL *rast1, const DCELL *rast2, const char *label, struct Categories *pcats)
Set a raster category label (DCELL)
void Rast_set_d_value(void *, DCELL, RASTER_MAP_TYPE)
Places a DCELL raster value.
void Rast_copy_cats(struct Categories *pcats_to, const struct Categories *pcats_from)
Copy raster categories.
void Rast_quant_add_rule(struct Quant *, DCELL, DCELL, CELL, CELL)
Adds a new rule to the set of quantization rules.
const char * G_mapset(void)
Get current mapset name.
void Rast_quant_get_ith_rule(const struct Quant *, int, DCELL *, DCELL *, CELL *, CELL *)
Returns the i'th quantization rule.
void G_warning(const char *,...) __attribute__((format(printf
char * Rast_get_next_marked_d_cat(struct Categories *pcats, DCELL *rast1, DCELL *rast2, long *count)
Get next marked raster categories (DCELL)
void Rast_unmark_cats(struct Categories *pcats)
Sets marks for all categories to 0.
char * G_store(const char *)
Copy string to allocated memory.
void Rast_write_vector_cats(const char *name, struct Categories *cats)
Write vector category file.
char * Rast_get_ith_c_cat(const struct Categories *pcats, int i, void *rast1, void *rast2)
Get category description (CELL)
void Rast_mark_d_cats(const DCELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value (DCELL).
int Rast_set_cat(const void *rast1, const void *rast2, const char *label, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Set a raster category label.
void Rast_write_cats(const char *name, struct Categories *cats)
Write raster category file.
void Rast_set_cats_title(const char *title, struct Categories *pcats)
Set title in category structure.
#define Rast_is_c_null_value(cellVal)
int G_debug(int, const char *,...) __attribute__((format(printf
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
int Rast_map_is_fp(const char *, const char *)
Check if raster map is floating-point.
void Rast_get_range_min_max(const struct Range *, CELL *, CELL *)
Get range min and max.
int Rast_is_null_value(const void *, RASTER_MAP_TYPE)
To check if a raster value is set to NULL.
void Rast_free_cats(struct Categories *pcats)
Free category structure memory.
int Rast_sort_cats(struct Categories *pcats)
Sort categories.
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
void G_newlines_to_spaces(char *)