GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
vector/Vlib/cats.c File Reference

Vector library - Category management. More...

#include <stdlib.h>
#include <string.h>
#include <grass/vector.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
Include dependency graph for vector/Vlib/cats.c:

Go to the source code of this file.

Functions

struct line_catsVect_new_cats_struct ()
 Creates and initializes line_cats structure. More...
 
void Vect_destroy_cats_struct (struct line_cats *p)
 Frees all memory associated with line_cats structure, including the struct itself. More...
 
int Vect_cat_set (struct line_cats *Cats, int field, int cat)
 Add new field/cat to category structure if doesn't exist yet. More...
 
int Vect_cat_get (const struct line_cats *Cats, int field, int *cat)
 Get first found category of given field. More...
 
int Vect_field_cat_get (const struct line_cats *Cats, int field, struct ilist *cats)
 Get list of categories of given field. More...
 
int Vect_cat_del (struct line_cats *Cats, int field)
 Delete all categories of given layer. More...
 
int Vect_field_cat_del (struct line_cats *Cats, int field, int cat)
 Delete field/cat from line_cats structure. More...
 
int Vect_reset_cats (struct line_cats *Cats)
 Reset category structure to make sure cats structure is clean to be re-used. More...
 
struct cat_listVect_new_cat_list ()
 Allocate memory for cat_list structure. More...
 
void Vect_destroy_cat_list (struct cat_list *p)
 Frees allocated cat_list memory. More...
 
int Vect_str_to_cat_list (const char *str, struct cat_list *list)
 Converts string of categories and cat ranges separated by commas to cat_list. More...
 
int Vect_array_to_cat_list (const int *vals, int nvals, struct cat_list *list)
 Convert ordered array of integers to cat_list structure. More...
 
int Vect_cat_list_to_array (const struct cat_list *list, int **vals, int *nvals)
 Convert cat_list struct to ordered array of unique integers. More...
 
int Vect_cat_in_cat_list (int cat, const struct cat_list *list)
 Check if category number is in list. More...
 
struct cat_listVect_cats_set_constraint (struct Map_info *Map, int layer, char *where, char *catstr)
 Set category constraints using 'where' or 'cats' option and layer number. More...
 
int Vect_cats_in_constraint (struct line_cats *Cats, int layer, struct cat_list *list)
 Check if categories match with category constraints. More...
 
int Vect_cat_in_array (int cat, const int *array, int ncats)
 Check if category is in ordered array of integers. More...
 

Detailed Description

Vector library - Category management.

Higher level functions for reading/writing/manipulating vectors.

(C) 2001-2012 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.

Author
Original author CERL, probably Dave Gerdes or Mike Higgins
Update to GRASS 5.7 Radim Blazek and David D. Gray.
Various updates by Martin Landa <landa.martin gmail.com>
Various updates by Markus Metz

Definition in file vector/Vlib/cats.c.

Function Documentation

◆ Vect_array_to_cat_list()

int Vect_array_to_cat_list ( const int *  vals,
int  nvals,
struct cat_list list 
)

Convert ordered array of integers to cat_list structure.

Parameters
valsarray of integers
nvalsnumber of values
[in,out]listpointer to cat_list structure
Returns
number of ranges

Definition at line 444 of file vector/Vlib/cats.c.

References cat_list::alloc_ranges, G_debug(), G_realloc, cat_list::max, cat_list::min, and cat_list::n_ranges.

◆ Vect_cat_del()

int Vect_cat_del ( struct line_cats Cats,
int  field 
)

Delete all categories of given layer.

Parameters
[in,out]Catsline_cats structure
fieldlayer number
Returns
number of categories deleted
0 layer does not exist

Definition at line 221 of file vector/Vlib/cats.c.

References line_cats::cat, line_cats::field, and line_cats::n_cats.

Referenced by Vect_field_cat_del().

◆ Vect_cat_get()

int Vect_cat_get ( const struct line_cats Cats,
int  field,
int *  cat 
)

Get first found category of given field.

cat is set to first category found or -1 if field was not found

Parameters
Catspointer line_cats structure
fieldlayer number
[out]catpointer to variable where cat will be written (can be NULL)
Returns
number of found cats for given field (first reported)
0 layer does not exist

Definition at line 156 of file vector/Vlib/cats.c.

References line_cats::cat, line_cats::field, GV_FIELD_MAX, and line_cats::n_cats.

Referenced by NetA_initialise_varray().

◆ Vect_cat_in_array()

int Vect_cat_in_array ( int  cat,
const int *  array,
int  ncats 
)

Check if category is in ordered array of integers.

Parameters
catcategory number
arrayordered array of integers
ncatsnumber of categories in array
Returns
TRUE if cat is in list
FALSE if it is not

Definition at line 707 of file vector/Vlib/cats.c.

◆ Vect_cat_in_cat_list()

int Vect_cat_in_cat_list ( int  cat,
const struct cat_list list 
)

Check if category number is in list.

Parameters
catcategory number
listcat_list structure
Returns
TRUE if cat is in list
FALSE if not

Definition at line 544 of file vector/Vlib/cats.c.

References FALSE, max, cat_list::min, cat_list::n_ranges, and TRUE.

Referenced by Vect_cats_in_constraint().

◆ Vect_cat_list_to_array()

int Vect_cat_list_to_array ( const struct cat_list list,
int **  vals,
int *  nvals 
)

Convert cat_list struct to ordered array of unique integers.

Output array do not contain duplicate items.

Allocated array should be freed by G_free().

Parameters
cat_listpointer to cat_list struct
[out]valsarray of integers
[out]nvalsnumber of values
Returns
0 on success
-1 on failure

Definition at line 489 of file vector/Vlib/cats.c.

References G_debug(), G_realloc, cat_list::max, cat_list::min, cat_list::n_ranges, and NULL.

◆ Vect_cat_set()

int Vect_cat_set ( struct line_cats Cats,
int  field,
int  cat 
)

Add new field/cat to category structure if doesn't exist yet.

Parameters
[in,out]Catsline_cats structure
[in]fieldlayer number
[in]catcategory number
Returns
number of categories
0 if no space for new category in structure, n_cats would be > GV_NCATS_MAX
-1 on out of memory
-2 if field out of range: 1 - GV_FIELD_MAX or cat out of range: 1 - GV_CAT_MAX

Definition at line 104 of file vector/Vlib/cats.c.

References _, line_cats::alloc_cats, line_cats::cat, dig_alloc_cats(), line_cats::field, G_fatal_error(), GV_NCATS_MAX, and line_cats::n_cats.

◆ Vect_cats_in_constraint()

int Vect_cats_in_constraint ( struct line_cats Cats,
int  layer,
struct cat_list list 
)

Check if categories match with category constraints.

Parameters
Catsline_cats structure
layerlayer number
listcat_list structure
Returns
0 no match, categories are outside constraints
1 match, categories are inside constraints

Definition at line 667 of file vector/Vlib/cats.c.

References _, line_cats::cat, line_cats::field, G_warning(), line_cats::n_cats, and Vect_cat_in_cat_list().

◆ Vect_cats_set_constraint()

struct cat_list* Vect_cats_set_constraint ( struct Map_info Map,
int  layer,
char *  where,
char *  catstr 
)

Set category constraints using 'where' or 'cats' option and layer number.

Parameters
Mappointer to Map_info structure
layerlayer number
wherewhere statement
catstrcategory list as string
Returns
pointer to cat_list structure or NULL

Definition at line 565 of file vector/Vlib/cats.c.

References _, field_info::database, db_close_database_shutdown_driver(), db_select_int(), db_start_driver_open_database(), field_info::driver, G_fatal_error(), G_verbose_message(), G_warning(), field_info::key, list, n_, NULL, field_info::table, and Vect_get_field().

◆ Vect_destroy_cat_list()

void Vect_destroy_cat_list ( struct cat_list p)

Frees allocated cat_list memory.

Parameters
ppointer to line_cats structure

Definition at line 326 of file vector/Vlib/cats.c.

References G_free(), cat_list::max, cat_list::min, and cat_list::n_ranges.

◆ Vect_destroy_cats_struct()

void Vect_destroy_cats_struct ( struct line_cats p)

Frees all memory associated with line_cats structure, including the struct itself.

Parameters
pline_cats structure

Definition at line 80 of file vector/Vlib/cats.c.

References line_cats::cat, line_cats::field, G_free(), and line_cats::n_cats.

Referenced by NetA_initialise_varray().

◆ Vect_field_cat_del()

int Vect_field_cat_del ( struct line_cats Cats,
int  field,
int  cat 
)

Delete field/cat from line_cats structure.

Parameters
[in,out]Catsline_cats structure
fieldlayer number
catcategory to be deleted or -1 to delete all cats of given field
Returns
number of categories deleted
0 field/category number does not exist

Definition at line 256 of file vector/Vlib/cats.c.

References line_cats::cat, line_cats::field, line_cats::n_cats, and Vect_cat_del().

◆ Vect_field_cat_get()

int Vect_field_cat_get ( const struct line_cats Cats,
int  field,
struct ilist cats 
)

Get list of categories of given field.

Parameters
Catsline_cats structure
fieldlayer number
[out]catspointer to list where cats will be written
Returns
number of found categories
-1 on invalid field

Definition at line 192 of file vector/Vlib/cats.c.

References line_cats::cat, line_cats::field, GV_FIELD_MAX, line_cats::n_cats, ilist::n_values, Vect_list_append(), and Vect_reset_list().

◆ Vect_new_cat_list()

struct cat_list* Vect_new_cat_list ( void  )

Allocate memory for cat_list structure.

Returns
pointer to allocated structure
NULL if out of memory

Definition at line 307 of file vector/Vlib/cats.c.

References G_malloc, and G_zero().

◆ Vect_new_cats_struct()

struct line_cats* Vect_new_cats_struct ( void  )

Creates and initializes line_cats structure.

This structure is used for reading and writing vector cats. The library routines handle all memory allocation.

To free allocated memory call Vect_destroy_cats_struct().

Returns
struct line_cats *
NULL on error

Definition at line 40 of file vector/Vlib/cats.c.

References NULL.

Referenced by NetA_initialise_varray(), and Vect_get_area_cat().

◆ Vect_reset_cats()

int Vect_reset_cats ( struct line_cats Cats)

Reset category structure to make sure cats structure is clean to be re-used.

I.e. it has no cats associated with it. Cats must have previously been created with Vect_new_cats_struct()

Parameters
[out]Catsline_cats structure
Returns
0

Definition at line 294 of file vector/Vlib/cats.c.

References line_cats::n_cats.

Referenced by Vect_get_area_cat(), and Vect_get_area_cats().

◆ Vect_str_to_cat_list()

int Vect_str_to_cat_list ( const char *  str,
struct cat_list list 
)

Converts string of categories and cat ranges separated by commas to cat_list.

Examples of string:
5,6,7
3-9
2,3,5-9,20
Example:
cat_list->field = 0
cat_list->n_ranges = 4
cat_list->min = {2, 3, 5, 20}
cat_list->max = {2, 3, 9, 20}
Parameters
strcategory list as a string
[in,out]listpointer to cat_list structure
Returns
number of errors in ranges

Definition at line 367 of file vector/Vlib/cats.c.

References err(), and l.