GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4e4cb0fe9
Spatial and category indices

by GRASS Development Team (https://grass.osgeo.org)

Vector library spatial index management

Spatial index (based on R*-tree) is created with topology.

Spatial index occupies a lot of memory but it is necessary for topology building. Also, it takes some time to release the memory occupied by spatial index (see dig_spidx_free()). The spatial index can also be built in file to save memory by setting the environment variable GRASS_VECTOR_LOWMEM.

The function building topology - Vect_build() - is usually called at the end of modules (before Vect_close()) so it is faster to call exit() and operating system releases all the memory much faster. By default the memory is not released.

It is possible to call Vect_set_release_support() before Vect_close() to enforce memory release, but it takes some time on large files.

The spatial index is stored in file and not loaded for old vectors that are not updated, saving a lot of memory. Spatial queries are done in file.

Currently most of the modules do not release the memory occupied for spatial index and work like this (pseudocode):

int main
{
/* writing new vector */
Vect_close(); /* memory is not released */
}
int Vect_open_new(struct Map_info *, const char *, int)
Create new vector map for reading/writing.
int Vect_close(struct Map_info *)
Close vector map.
int Vect_build(struct Map_info *)
Build topology for vector map.
Definition: build.c:586
int main(int argc, char *argv[])
Definition: main.c:25

In general it is possible to free the memory with Vect_set_release_support() such as:

int main
{
/* writing new vector */
Vect_close(); /* memory is released */
}
void Vect_set_release_support(struct Map_info *)
Set spatial index to be released when vector is closed.
Definition: map.c:545

but it takes a bit longer.

It makes sense to release the spatial index if it is used only at the beginning of a module or in permanently running programs like QGIS. Note that this applies only when creating a new vector or updating an old vector. For example:

int main
{
/* select features using spatial index, e.g. Vect_select_lines_by_box() */
Vect_close(); /* memory is released */
/* do some processing which needs memory */
}
int Vect_open_update(struct Map_info *, const char *, const char *)
Open existing vector map for reading/writing.

See also spatial_index data structure.

Sidx file format specification

Spatial index file ('sidx') is read by Vect_open_sidx().

Header

Note: plus is instance of Plus_head structure.

NameTypeNumber

Description

plus->spidx_Version_Major C1file version (major)
plus->spidx_Version_Minor C1file version (minor)
plus->spidx_Back_MajorC1supported from GRASS version (major)
plus->spidx_Back_MinorC1

supported from GRASS version (minor)

plus->spidx_port->byte_orderC1

little or big endian flag; files are written in machine native order but files in both little and big endian order may be readl; zero for little endian

plus->spidx_port.off_t_sizeC1

off_t size (LFS)

plus->spidx_head_sizeL1

header size

plus->spidx_with_zC1

2D/3D vector data

ndimsC1

Number of dimensions

nsidesC1

Number of sides

nodesizeI1

Node size

nodecardI1

Node card (?)

leafcardI1

Leaf card (?)

min_node_fillI1

Minimum node fill (?)

min_leaf_fillI1

Minimum leaf fill (?)

plus->Node_spidx->n_nodesI1

Number of nodes

plus->Node_spidx->n_leafsI1

Number of leafs

plus->Node_spidx->n_levelsI1

Number of levels

plus->Node_spidx_offsetO1

Node offset

plus->Line_spidx->n_nodesI1

Number of nodes

plus->Line_spidx->n_leafsI1

Number of leafs

plus->Line_spidx->n_levelsI1

Number of levels

plus->Line_spidx_offsetO1

Line offset

plus->Area_spidx->n_nodesI1

Number of nodes

plus->Area_spidx->n_leafsI1

Number of leafs

plus->Area_spidx->n_levelsI1

Number of levels

plus->Area_spidx_offsetO1

Area offset

plus->Isle_spidx->n_nodesI1

Number of nodes

plus->Isle_spidx->n_leafsI1

Number of leafs

plus->Isle_spidx->n_levelsI1

Number of levels

plus->Isle_spidx_offsetO1

Isle offset

plus->Face_spidx_offsetO1

Face offset

plus->Volume_spidx_offsetO1

Volume offset

plus->Hole_spidx_offsetO1

Hole offset

plus->coor_sizeO1Coor file size

Vector library category index management

The category index (stored in the cidx file) improves the performance of all selections by cats/attributes (SQL, e.g. d.vect cats=27591, v.extract list=20000-21000). This avoids that all selections have to be made by looping through all vector lines. Category index is also essential for simple feature representation of GRASS vectors.

Category index is created for each field. In memory, it is stored in Cat_index data structure.

Category index is built with topology, but it is not updated if vector is edited on level 2. Category index is stored in 'cidx' file, 'cat' array is written/read by one call of dig__fwrite_port_I() or dig__fread_port_I().

Stored values can be retrieved either by index in 'cat' array (if all features of given field are required) or by category value (one or few features), always by Vect_cidx_*() functions.

To create category index, it will be necessary to rebuild topology for all existing vectors. This is an opportunity to make (hopefully) last changes in 'topo', 'cidx' formats.

Cidx file format specification

Category index file ('cidx') is read by Vect_cidx_open().

Header

Note: plus is instance of Plus_head structure.

NameTypeNumber

Description

plus->cpidx_Version_Major C1file version (major)
plus->cpidx_Version_Minor C1file version (minor)
plus->cpidx_Back_MajorC1supported from GRASS version (major)
plus->cpidx_Back_MinorC1

supported from GRASS version (minor)

plus->cidx_port->byte_orderC1

little or big endian flag; files are written in machine native order but files in both little and big endian order may be readl; zero for little endian

plus->cidx_head_sizeL1

cidx head size

plus->n_cidxI1

number of fields

fieldIn_cidx

field number

n_catsIn_cidx

number of categories

n_ucatsIn_cidx

number of unique categories

n_typesIn_cidx

number of feature types

rtypeIn_cidx * n_types

Feature type

type[t]In_cidx * n_types

Number of items