GRASS GIS 8 Programmer's Manual
8.2.2dev(2023)-3d2c704037
|
Raster Library - Raster categories management. More...
#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>
Go to the source code of this file.
Functions | |
int | Rast_read_cats (const char *name, const char *mapset, struct Categories *pcats) |
Read raster category file. More... | |
int | Rast_read_vector_cats (const char *name, const char *mapset, struct Categories *pcats) |
Read vector category file. More... | |
CELL | Rast_get_max_c_cat (const char *name, const char *mapset) |
Get the max category number. More... | |
char * | Rast_get_cats_title (const struct Categories *pcats) |
Get title from category structure struct. More... | |
char * | Rast_get_c_cat (CELL *rast, struct Categories *pcats) |
Get a raster category label (CELL) More... | |
char * | Rast_get_f_cat (FCELL *rast, struct Categories *pcats) |
Get a raster category label (FCELL) More... | |
char * | Rast_get_d_cat (DCELL *rast, struct Categories *pcats) |
Get a raster category label (DCELL) More... | |
char * | Rast_get_cat (void *rast, struct Categories *pcats, RASTER_MAP_TYPE data_type) |
Get a raster category label. More... | |
void | Rast_unmark_cats (struct Categories *pcats) |
Sets marks for all categories to 0. More... | |
void | Rast_mark_c_cats (const CELL *rast_row, int ncols, struct Categories *pcats) |
Looks up the category label for each raster value (CELL). More... | |
void | Rast_mark_f_cats (const FCELL *rast_row, int ncols, struct Categories *pcats) |
Looks up the category label for each raster value (FCELL). More... | |
void | Rast_mark_d_cats (const DCELL *rast_row, int ncols, struct Categories *pcats) |
Looks up the category label for each raster value (DCELL). More... | |
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). More... | |
void | Rast_rewind_cats (struct Categories *pcats) |
Rewind raster categories. More... | |
char * | Rast_get_next_marked_d_cat (struct Categories *pcats, DCELL *rast1, DCELL *rast2, long *count) |
Get next marked raster categories (DCELL) More... | |
char * | Rast_get_next_marked_c_cat (struct Categories *pcats, CELL *rast1, CELL *rast2, long *count) |
Get next marked raster categories (CELL) More... | |
char * | Rast_get_next_marked_f_cat (struct Categories *pcats, FCELL *rast1, FCELL *rast2, long *count) |
Get next marked raster categories (FCELL) More... | |
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. More... | |
int | Rast_set_c_cat (const CELL *rast1, const CELL *rast2, const char *label, struct Categories *pcats) |
Set a raster category label (CELL) More... | |
int | Rast_set_f_cat (const FCELL *rast1, const FCELL *rast2, const char *label, struct Categories *pcats) |
Set a raster category label (FCELL) More... | |
int | Rast_set_d_cat (const DCELL *rast1, const DCELL *rast2, const char *label, struct Categories *pcats) |
Set a raster category label (DCELL) More... | |
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. More... | |
void | Rast_write_cats (const char *name, struct Categories *cats) |
Write raster category file. More... | |
void | Rast_write_vector_cats (const char *name, struct Categories *cats) |
Write vector category file. More... | |
char * | Rast_get_ith_d_cat (const struct Categories *pcats, int i, DCELL *rast1, DCELL *rast2) |
Get category description (DCELL) More... | |
char * | Rast_get_ith_f_cat (const struct Categories *pcats, int i, void *rast1, void *rast2) |
Get category description (FCELL) More... | |
char * | Rast_get_ith_c_cat (const struct Categories *pcats, int i, void *rast1, void *rast2) |
Get category description (CELL) More... | |
char * | Rast_get_ith_cat (const struct Categories *pcats, int i, void *rast1, void *rast2, RASTER_MAP_TYPE data_type) |
Get category description. More... | |
void | Rast_init_cats (const char *title, struct Categories *pcats) |
Initialize category structure. More... | |
void | Rast_set_cats_title (const char *title, struct Categories *pcats) |
Set title in category structure. More... | |
void | Rast_set_cats_fmt (const char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats) |
Set category fmt (?) More... | |
void | Rast_free_cats (struct Categories *pcats) |
Free category structure memory. More... | |
void | Rast_copy_cats (struct Categories *pcats_to, const struct Categories *pcats_from) |
Copy raster categories. More... | |
int | Rast_number_of_cats (struct Categories *pcats) |
Get number of raster categories. More... | |
int | Rast_sort_cats (struct Categories *pcats) |
Sort categories. More... | |
Raster Library - Raster categories management.
Code in this file works with category files. There are two formats: Pre 3.0 direct category encoding form:
2 categories Map Title Elevation: 1000.00 to 1005.00 feet Elevation: 1005.00 to 1010.00 feet Elevation: 1010.00 to 1015.00 feet
3.0 format
Map Title Elevation: $1.2 to $2.2 feet ## Format Statement 5.0 1000 5.0 1005 ## Coefficients
The coefficient line can be followed by explicit category labels which override the format label generation. 0:no data 2: . 5: . ## explicit category labels 7: . explicit labels can be also of the form: 5.5:5:9 label description or 15:30 label description
In the format line $1 refers to the value num*5.0+1000 (ie, using the first 2 coefficients) $2 refers to the value num*5.0+1005 (ie, using the last 2 coefficients)
$1.2 will print $1 with 2 decimal places.
Also, the form $?xxx$yyy$ translates into yyy if the category is 1, xxx otherwise. The $yyy$ is optional. Thus
$1 meter$?s
will become: 1 meter (for category 1) 2 meters (for category 2), etc.
The format and coefficients above would be used to generate the following statement in creation of the format appropriate category string for category "num":
sprintf(buff,"Elevation: %.2f to %.2f feet", num*5.0+1000, num*5.0*1005)
Note: while both the format and coefficient lines must be present a blank line for the fmt will effectively suppress automatic label generation
Note: quant rules of Categories structures are heavily dependent on the fact that rules are stored in the same order they are entered. since i-th rule and i-th label are entered at the same time, we know that i-th rule maps fp range to i, thus we know for sure that cats.labels[i] corresponds to i-th quant rule
(C) 2001-2009 by the GRASS Development Team
This program is free software under the GNU General Public License (>=v2). Read the file COPYING that comes with GRASS for details.
Definition in file raster/cats.c.
void Rast_copy_cats | ( | struct Categories * | pcats_to, |
const struct Categories * | pcats_from | ||
) |
Copy raster categories.
Allocates NEW space for quant rules and labels n pcats_to and copies all info from pcats_from cats to pcats_to cats.
pcats_to | pointer to destination Categories structure |
pcats_from | pointer to source Categories structure |
Definition at line 1248 of file raster/cats.c.
References Categories::ncats, Rast_get_ith_d_cat(), Rast_init_cats(), Rast_set_d_cat(), and Categories::title.
Referenced by Rast_sort_cats().
void Rast_free_cats | ( | struct Categories * | pcats | ) |
Free category structure memory.
Frees memory allocated by Rast_read_cats(), Rast_init_cats() and Rast_set_c_cat().
pcats | pointer to Categories structure |
Definition at line 1213 of file raster/cats.c.
References Categories::fmt, G_free(), Categories::labels, Categories::marks, Categories::nalloc, Categories::ncats, NULL, Categories::q, Rast_quant_free(), and Categories::title.
char* Rast_get_c_cat | ( | CELL * | rast, |
struct Categories * | pcats | ||
) |
Get a raster category label (CELL)
This routine looks up category rast in the pcats structure and returns a pointer to a string which is the label for the category. A legal pointer is always returned. If the category does not exist in pcats, then a pointer to the empty string "" is returned.
Warning: The pointer that is returned points to a hidden static buffer. Successive calls to Rast_get_c_cat() overwrite this buffer.
rast | cell value |
pcats | pointer to Categories structure |
Definition at line 323 of file raster/cats.c.
References CELL_TYPE, and Rast_get_cat().
char* Rast_get_cat | ( | void * | rast, |
struct Categories * | pcats, | ||
RASTER_MAP_TYPE | data_type | ||
) |
Get a raster category label.
This routine looks up category rast in the pcats structure and returns a pointer to a string which is the label for the category. A legal pointer is always returned. If the category does not exist in pcats, then a pointer to the empty string "" is returned.
Warning: The pointer that is returned points to a hidden static buffer. Successive calls to Rast_get_c_cat() overwrite this buffer.
rast | cell value |
pcats | pointer to Categories structure |
data_type | map type (CELL, FCELL, DCELL) |
Definition at line 396 of file raster/cats.c.
References l.
Referenced by Rast_get_c_cat(), Rast_get_d_cat(), and Rast_get_f_cat().
char* Rast_get_cats_title | ( | const struct Categories * | pcats | ) |
Get title from category structure struct.
Map layers store a one-line title in the category structure as well. This routine returns a pointer to the title contained in the cats structure. A legal pointer is always returned. If the map layer does not have a title, then a pointer to the empty string "" is returned.
pcats | pointer to Categories structure |
Definition at line 299 of file raster/cats.c.
References Categories::title.
char* Rast_get_d_cat | ( | DCELL * | rast, |
struct Categories * | pcats | ||
) |
Get a raster category label (DCELL)
This routine looks up category rast in the pcats structure and returns a pointer to a string which is the label for the category. A legal pointer is always returned. If the category does not exist in pcats, then a pointer to the empty string "" is returned.
Warning: The pointer that is returned points to a hidden static buffer. Successive calls to Rast_get_c_cat() overwrite this buffer.
rast | cell value |
pcats | pointer to Categories structure |
Definition at line 371 of file raster/cats.c.
References DCELL_TYPE, and Rast_get_cat().
char* Rast_get_f_cat | ( | FCELL * | rast, |
struct Categories * | pcats | ||
) |
Get a raster category label (FCELL)
This routine looks up category rast in the pcats structure and returns a pointer to a string which is the label for the category. A legal pointer is always returned. If the category does not exist in pcats, then a pointer to the empty string "" is returned.
Warning: The pointer that is returned points to a hidden static buffer. Successive calls to Rast_get_c_cat() overwrite this buffer.
rast | cell value |
pcats | pointer to Categories structure |
Definition at line 347 of file raster/cats.c.
References FCELL_TYPE, and Rast_get_cat().
char* Rast_get_ith_c_cat | ( | const struct Categories * | pcats, |
int | i, | ||
void * | rast1, | ||
void * | rast2 | ||
) |
Get category description (CELL)
Returns i-th description and i-th data range from the list of category descriptions with corresponding data ranges. end points of data interval in rast1 and rast2.
pcats | pointer to Categories structure |
i | index |
rast1,rast2 | raster values (range) |
Definition at line 1084 of file raster/cats.c.
References CELL_TYPE.
char* Rast_get_ith_cat | ( | const struct Categories * | pcats, |
int | i, | ||
void * | rast1, | ||
void * | rast2, | ||
RASTER_MAP_TYPE | data_type | ||
) |
Get category description.
Returns i-th description and i-th data range from the list of category descriptions with corresponding data ranges. end points of data interval in rast1 and rast2.
pcats | pointer to Categories structure |
i | index |
rast1,rast2 | raster values (range) |
data_type | map type |
Definition at line 1112 of file raster/cats.c.
char* Rast_get_ith_d_cat | ( | const struct Categories * | pcats, |
int | i, | ||
DCELL * | rast1, | ||
DCELL * | rast2 | ||
) |
Get category description (DCELL)
Returns i-th description and i-th data range from the list of category descriptions with corresponding data ranges. end points of data interval in rast1 and rast2.
pcats | pointer to Categories structure |
i | index |
rast1,rast2 | raster values (range) |
Definition at line 1029 of file raster/cats.c.
References Categories::labels, Categories::ncats, Categories::q, Rast_quant_get_ith_rule(), and Rast_set_d_null_value().
Referenced by Rast3d_write_cats(), Rast_copy_cats(), Rast_get_next_marked_d_cat(), and Rast_set_d_cat().
char* Rast_get_ith_f_cat | ( | const struct Categories * | pcats, |
int | i, | ||
void * | rast1, | ||
void * | rast2 | ||
) |
Get category description (FCELL)
Returns i-th description and i-th data range from the list of category descriptions with corresponding data ranges. end points of data interval in rast1 and rast2.
pcats | pointer to Categories structure |
i | index |
rast1,rast2 | raster values (range) |
Definition at line 1057 of file raster/cats.c.
References FCELL_TYPE.
CELL Rast_get_max_c_cat | ( | const char * | name, |
const char * | mapset | ||
) |
Get the max category number.
Return the max category number of a raster map of type CELL.
name | raster map name |
mapset | mapset name |
Definition at line 171 of file raster/cats.c.
References max, min, Rast_get_range_min_max(), Rast_is_c_null_value, and Rast_read_range().
char* Rast_get_next_marked_c_cat | ( | struct Categories * | pcats, |
CELL * | rast1, | ||
CELL * | rast2, | ||
long * | count | ||
) |
Get next marked raster categories (CELL)
pcats | pointer to Categories structure | |
rast1,rast2 | cell values (raster range) | |
[out] | count | count |
Definition at line 643 of file raster/cats.c.
References CELL_TYPE, and Rast_get_next_marked_cat().
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.
pcats | pointer to Categories structure | |
rast1,rast2 | cell values (raster range) | |
[out] | count | count |
data_type | map type |
Definition at line 676 of file raster/cats.c.
References Rast_get_next_marked_d_cat(), and Rast_set_d_value().
Referenced by Rast_get_next_marked_c_cat(), and Rast_get_next_marked_f_cat().
char* Rast_get_next_marked_d_cat | ( | struct Categories * | pcats, |
DCELL * | rast1, | ||
DCELL * | rast2, | ||
long * | count | ||
) |
Get next marked raster categories (DCELL)
pcats | pointer to Categories structure | |
rast1,rast2 | cell values (raster range) | |
[out] | count | count |
Definition at line 603 of file raster/cats.c.
References G_debug(), Categories::last_marked_rule, Categories::marks, NULL, Categories::q, Rast_get_ith_d_cat(), and Rast_quant_nof_rules().
Referenced by Rast_get_next_marked_cat().
char* Rast_get_next_marked_f_cat | ( | struct Categories * | pcats, |
FCELL * | rast1, | ||
FCELL * | rast2, | ||
long * | count | ||
) |
Get next marked raster categories (FCELL)
pcats | pointer to Categories structure | |
rast1,rast2 | cell values (raster range) | |
[out] | count | count |
Definition at line 659 of file raster/cats.c.
References FCELL_TYPE, and Rast_get_next_marked_cat().
void Rast_init_cats | ( | const char * | title, |
struct Categories * | pcats | ||
) |
Initialize category structure.
To construct a new category file, the structure must first be initialized. This routine initializes the cats structure, and copies the title into the structure. The number of categories is set initially to n.
For example:
title | title |
pcats | pointer to Categories structure |
Definition at line 1145 of file raster/cats.c.
References Categories::a1, Categories::a2, Categories::fmt, Categories::labels, Categories::last_marked_rule, Categories::m1, Categories::m2, Categories::nalloc, Categories::ncats, NULL, Categories::num, Categories::q, Rast_quant_init(), and Rast_set_cats_title().
Referenced by Rast_copy_cats().
void Rast_mark_c_cats | ( | const CELL * | rast_row, |
int | ncols, | ||
struct Categories * | pcats | ||
) |
Looks up the category label for each raster value (CELL).
Looks up the category label for each raster value in the rast_row and updates the marks for labels found.
Note: Non-zero mark for i-th label stores the number of of raster cells read so far which are labeled with i-th label and fall into i-th data range.
rast_row | raster row to update stats |
ncols | number of columns |
pcats | pointer to Categories structure |
Definition at line 494 of file raster/cats.c.
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).
Looks up the category label for each raster value in the rast_row and updates the marks for labels found.
Note: Non-zero mark for i-th label stores the number of of raster cells read so far which are labeled with i-th label and fall into i-th data range.
rast_row | raster row to update stats |
ncols | number of columns |
pcats | pointer to Categories structure |
data_type | map type |
Definition at line 560 of file raster/cats.c.
void Rast_mark_d_cats | ( | const DCELL * | rast_row, |
int | ncols, | ||
struct Categories * | pcats | ||
) |
Looks up the category label for each raster value (DCELL).
Looks up the category label for each raster value in the rast_row and updates the marks for labels found.
Note: Non-zero mark for i-th label stores the number of of raster cells read so far which are labeled with i-th label and fall into i-th data range.
rast_row | raster row to update stats |
ncols | number of columns |
pcats | pointer to Categories structure |
Definition at line 536 of file raster/cats.c.
void Rast_mark_f_cats | ( | const FCELL * | rast_row, |
int | ncols, | ||
struct Categories * | pcats | ||
) |
Looks up the category label for each raster value (FCELL).
Looks up the category label for each raster value in the rast_row and updates the marks for labels found.
Note: Non-zero mark for i-th label stores the number of of raster cells read so far which are labeled with i-th label and fall into i-th data range.
rast_row | raster row to update stats |
ncols | number of columns |
pcats | pointer to Categories structure |
Definition at line 515 of file raster/cats.c.
int Rast_number_of_cats | ( | struct Categories * | pcats | ) |
Get number of raster categories.
pcats | pointer to Categories structure |
Definition at line 1269 of file raster/cats.c.
References Categories::ncats.
int Rast_read_cats | ( | const char * | name, |
const char * | mapset, | ||
struct Categories * | pcats | ||
) |
Read raster category file.
The category file for raster map name in mapset is read into the cats structure. If there is an error reading the category file, a diagnostic message is printed and -1 is returned. Otherwise, 0 is returned.
name | raster map name | |
mapset | mapset name | |
[out] | pcats | pointer to Cats structure |
Definition at line 104 of file raster/cats.c.
Referenced by gsd_put_legend().
int Rast_read_vector_cats | ( | const char * | name, |
const char * | mapset, | ||
struct Categories * | pcats | ||
) |
Read vector category file.
Note: This function works with old vector format.
The category file for vector map name in mapset is read into the cats structure. If there is an error reading the category file, a diagnostic message is printed and -1 is returned. Otherwise, 0 is returned.
name | vector map name | |
mapset | mapset name | |
[out] | pcats | pointer to Cats structure |
Definition at line 140 of file raster/cats.c.
void Rast_rewind_cats | ( | struct Categories * | pcats | ) |
Rewind raster categories.
After call to this function Rast_get_next_marked_cat() returns the first marked cat label.
pcats | pointer to Categories structure |
Definition at line 588 of file raster/cats.c.
References Categories::last_marked_rule.
int Rast_set_c_cat | ( | const CELL * | rast1, |
const CELL * | rast2, | ||
const char * | label, | ||
struct Categories * | pcats | ||
) |
Set a raster category label (CELL)
Adds the label for range rast1 through rast2 in category structure pcats.
rast1,rast2 | raster values (range) |
label | category label |
pcats | pointer to Categories structure |
Definition at line 770 of file raster/cats.c.
References CELL_TYPE, and Rast_set_cat().
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.
Adds the label for range rast1 through rast2 in category structure pcats.
rast1,rast2 | raster values (range) |
label | category label |
pcats | pointer to Categories structure |
data_type | map type |
Definition at line 914 of file raster/cats.c.
References Rast_get_d_value(), and Rast_set_d_cat().
Referenced by Rast_set_c_cat(), and Rast_set_f_cat().
void Rast_set_cats_fmt | ( | const char * | fmt, |
double | m1, | ||
double | a1, | ||
double | m2, | ||
double | a2, | ||
struct Categories * | pcats | ||
) |
Set category fmt (?)
fmt | |
m1 | |
a1 | |
m2 | |
a2 | |
pcats | pointer to Categories structure |
Definition at line 1190 of file raster/cats.c.
void Rast_set_cats_title | ( | const char * | title, |
struct Categories * | pcats | ||
) |
Set title in category structure.
The title is copied into the pcats structure.
title | title |
pcats | pointer to Categories structure |
Definition at line 1171 of file raster/cats.c.
References G_newlines_to_spaces(), G_store(), G_strip(), NULL, and Categories::title.
Referenced by Rast_init_cats().
int Rast_set_d_cat | ( | const DCELL * | rast1, |
const DCELL * | rast2, | ||
const char * | label, | ||
struct Categories * | pcats | ||
) |
Set a raster category label (DCELL)
Adds the label for range rast1 through rast2 in category structure pcats.
rast1,rast2 | raster values (range) |
label | category label |
pcats | pointer to Categories structure |
Definition at line 808 of file raster/cats.c.
References G_free(), G_malloc, G_newlines_to_spaces(), G_realloc, G_store(), G_strip(), Categories::labels, Categories::marks, Categories::nalloc, Categories::ncats, NULL, Categories::num, Categories::q, Rast_get_ith_d_cat(), Rast_is_d_null_value, and Rast_quant_add_rule().
Referenced by Rast_copy_cats(), and Rast_set_cat().
int Rast_set_f_cat | ( | const FCELL * | rast1, |
const FCELL * | rast2, | ||
const char * | label, | ||
struct Categories * | pcats | ||
) |
Set a raster category label (FCELL)
Adds the label for range rast1 through rast2 in category structure pcats.
rast1,rast2 | raster values (range) |
label | category label |
pcats | pointer to Categories structure |
Definition at line 788 of file raster/cats.c.
References FCELL_TYPE, and Rast_set_cat().
int Rast_sort_cats | ( | struct Categories * | pcats | ) |
Sort categories.
pcats | pointer to Categories structure |
Definition at line 1282 of file raster/cats.c.
References G_debug(), Categories::ncats, and Rast_copy_cats().
void Rast_unmark_cats | ( | struct Categories * | pcats | ) |
Sets marks for all categories to 0.
This initializes Categories structure for subsequent calls to Rast_mark_cats() for each row of data, where non-zero mark for i-th label means that some of the cells in rast_row are labeled with i-th label and fall into i-th data range. These marks help determine from the Categories structure which labels were used and which weren't.
pcats | pointer to Categories structure |
Definition at line 471 of file raster/cats.c.
References Categories::marks, and Categories::ncats.
void Rast_write_cats | ( | const char * | name, |
struct Categories * | cats | ||
) |
Write raster category file.
Writes the category file for the raster map name in the current mapset from the cats structure.
name | map name |
cats | pointer to Categories structure |
Definition at line 938 of file raster/cats.c.
void Rast_write_vector_cats | ( | const char * | name, |
struct Categories * | cats | ||
) |
Write vector category file.
Note: Used for only old vector format!
name | map name |
cats | pointer to Categories structure |
Definition at line 955 of file raster/cats.c.