vectorlib_spatialindex.dox 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*! \page vlibSpatialIndex Spatial index
  2. by GRASS Development Team (http://grass.osgeo.org)
  3. \par Table of contents
  4. - \ref vlibSpidx
  5. - \ref vlibSidxFileFormat
  6. - \ref vlibCidx
  7. - \ref vlibCidxFileFormat
  8. - \ref vlibCidxFileHead
  9. \section vlibSpidx Vector library spatial index management
  10. Spatial index (based on R*-tree) is created with topology, see \ref
  11. RTree data structure.
  12. Spatial index occupies a lot of memory but it is necessary for
  13. topology building. Also, it takes some time to release the memory
  14. occupied by spatial index (see dig_spidx_free()). The spatial index can
  15. also be built in file to save memory by setting the environment variable
  16. GRASS_VECTOR_LOWMEM.
  17. The function building topology - Vect_build() - is usually called at
  18. the end of modules (before Vect_close()) so it is faster to call
  19. <tt>exit()</tt> and operating system releases all the memory much
  20. faster. By default the memory is not released.
  21. It is possible to call Vect_set_release_support() before Vect_close()
  22. to enforce memory release, but it takes some time on large files.
  23. The spatial index is stored in file and not loaded for old vectors that
  24. are not updated, saving a lot of memory. Spatial queries are done in
  25. file.
  26. Currently most of the modules do not release the memory occupied for
  27. spatial index and work like this (pseudocode):
  28. \code
  29. int main
  30. {
  31. Vect_open_new();
  32. /* writing new vector */
  33. Vect_build();
  34. Vect_close(); /* memory is not released */
  35. }
  36. \endcode
  37. In general it is possible to free the memory with Vect_set_release_support()
  38. such as:
  39. \code
  40. int main
  41. {
  42. Vect_open_new();
  43. /* writing new vector */
  44. Vect_build();
  45. Vect_set_release_support();
  46. Vect_close(); /* memory is released */
  47. }
  48. \endcode
  49. but it takes a bit longer.
  50. It makes sense to release the spatial index if it is used only at the beginning
  51. of a module or in permanently running programs like QGIS. Note that this
  52. applies only when creating a new vector or updating an old vector.
  53. For example:
  54. \code
  55. int main
  56. {
  57. Vect_open_update();
  58. /* select features using spatial index, e.g. Vect_select_lines_by_box() */
  59. Vect_set_release_support();
  60. Vect_close(); /* memory is released */
  61. /* do some processing which needs memory */
  62. }
  63. \endcode
  64. See also \ref spatial_index data structure.
  65. \subsection vlibSidxFileFormat Sidx file format specification
  66. Spatial index file ('sidx') is read by Vect_open_sidx().
  67. \subsubsection vlibSidxFileHead Header
  68. Note: <tt>plus</tt> is instance of \ref Plus_head structure.
  69. <table border="1" style="border-collapse: collapse" cellpadding="5">
  70. <tr><td><b>Name</b></td><td><b>Type</b></td><td><b>Number</b></td><td><b>Description</b></td></tr>
  71. <tr><td>plus->spidx_Version_Major </td><td>C</td><td>1</td><td>file version (major)</td></tr>
  72. <tr><td>plus->spidx_Version_Minor </td><td>C</td><td>1</td><td>file version (minor)</td></tr>
  73. <tr><td>plus->spidx_Back_Major</td><td>C</td><td>1</td><td>supported from GRASS version (major)</td></tr>
  74. <tr><td>plus->spidx_Back_Minor</td><td>C</td><td>1</td><td>supported from GRASS version (minor)</td></tr>
  75. <tr><td>plus->spidx_port->byte_order</td><td>C</td><td>1</td><td>little or big endian
  76. flag; files are written in machine native order but
  77. files in both little and big endian order may be
  78. readl; zero for little endian</td></tr>
  79. <tr><td>plus->spidx_port.off_t_size</td><td>C</td><td>1</td><td>off_t size (LFS)</td></tr>
  80. <tr><td>plus->spidx_head_size</td><td>L</td><td>1</td><td>header size</td></tr>
  81. <tr><td>plus->spidx_with_z</td><td>C</td><td>1</td><td>2D/3D vector data</td></tr>
  82. <tr><td>ndims</td><td>C</td><td>1</td><td>Number of dimensions</td></tr>
  83. <tr><td>nsides</td><td>C</td><td>1</td><td>Number of sides</td></tr>
  84. <tr><td>nodesize</td><td>I</td><td>1</td><td>%Node size</td></tr>
  85. <tr><td>nodecard</td><td>I</td><td>1</td><td>%Node card (?)</td></tr>
  86. <tr><td>leafcard</td><td>I</td><td>1</td><td>Leaf card (?)</td></tr>
  87. <tr><td>min_node_fill</td><td>I</td><td>1</td><td>Minimum node fill (?)</td></tr>
  88. <tr><td>min_leaf_fill</td><td>I</td><td>1</td><td>Minimum leaf fill (?)</td></tr>
  89. <tr><td>plus->Node_spidx->n_nodes</td><td>I</td><td>1</td><td>Number of nodes</td></tr>
  90. <tr><td>plus->Node_spidx->n_leafs</td><td>I</td><td>1</td><td>Number of leafs</td></tr>
  91. <tr><td>plus->Node_spidx->n_levels</td><td>I</td><td>1</td><td>Number of levels</td></tr>
  92. <tr><td>plus->Node_spidx_offset</td><td>O</td><td>1</td><td>%Node offset</td></tr>
  93. <tr><td>plus->Line_spidx->n_nodes</td><td>I</td><td>1</td><td>Number of nodes</td></tr>
  94. <tr><td>plus->Line_spidx->n_leafs</td><td>I</td><td>1</td><td>Number of leafs</td></tr>
  95. <tr><td>plus->Line_spidx->n_levels</td><td>I</td><td>1</td><td>Number of levels</td></tr>
  96. <tr><td>plus->Line_spidx_offset</td><td>O</td><td>1</td><td>Line offset</td></tr>
  97. <tr><td>plus->Area_spidx->n_nodes</td><td>I</td><td>1</td><td>Number of nodes</td></tr>
  98. <tr><td>plus->Area_spidx->n_leafs</td><td>I</td><td>1</td><td>Number of leafs</td></tr>
  99. <tr><td>plus->Area_spidx->n_levels</td><td>I</td><td>1</td><td>Number of levels</td></tr>
  100. <tr><td>plus->Area_spidx_offset</td><td>O</td><td>1</td><td>Area offset</td></tr>
  101. <tr><td>plus->Isle_spidx->n_nodes</td><td>I</td><td>1</td><td>Number of nodes</td></tr>
  102. <tr><td>plus->Isle_spidx->n_leafs</td><td>I</td><td>1</td><td>Number of leafs</td></tr>
  103. <tr><td>plus->Isle_spidx->n_levels</td><td>I</td><td>1</td><td>Number of levels</td></tr>
  104. <tr><td>plus->Isle_spidx_offset</td><td>O</td><td>1</td><td>Isle offset</td></tr>
  105. <tr><td>plus->Face_spidx_offset</td><td>O</td><td>1</td><td>Face offset</td></tr>
  106. <tr><td>plus->Volume_spidx_offset</td><td>O</td><td>1</td><td>Volume offset</td></tr>
  107. <tr><td>plus->Hole_spidx_offset</td><td>O</td><td>1</td><td>Hole offset</td></tr>
  108. <tr><td>plus->coor_size</td><td>O</td><td>1</td><td>Coor file size</td></tr>
  109. </table>
  110. \section vlibCidx Vector library category index management
  111. The category index (stored in the cidx file) improves the performance
  112. of all selections by cats/attributes (SQL, e.g. <tt>d.vect
  113. cats=27591</tt>, <tt>v.extract list=20000-21000</tt>). This avoids
  114. that all selections have to be made by looping through all vector
  115. lines. Category index is also essential for simple feature
  116. representation of GRASS vectors.
  117. Category index is created for each field. In memory, it is stored in
  118. \ref Cat_index data structure.
  119. Category index is built with topology, but it is <b>not updated</b> if
  120. vector is edited on level 2. Category index is stored in 'cidx' file,
  121. 'cat' array is written/read by one call of dig__fwrite_port_I() or
  122. dig__fread_port_I().
  123. Stored values can be retrieved either by index in 'cat' array (if all
  124. features of given field are required) or by category value (one or few
  125. features), always by <tt>Vect_cidx_*()</tt> functions.
  126. To create category index, it will be necessary to rebuild topology for
  127. all existing vectors. This is an opportunity to make (hopefully) last
  128. changes in 'topo', 'cidx' formats.
  129. \subsection vlibCidxFileFormat Cidx file format specification
  130. Category index file ('cidx') is read by Vect_cidx_open().
  131. \subsubsection vlibCidxFileHead Header
  132. Note: <tt>plus</tt> is instance of \ref Plus_head structure.
  133. <table border="1" style="border-collapse: collapse" cellpadding="5">
  134. <tr><td><b>Name</b></td><td><b>Type</b></td><td><b>Number</b></td><td><b>Description</b></td></tr>
  135. <tr><td>plus->cpidx_Version_Major </td><td>C</td><td>1</td><td>file version (major)</td></tr>
  136. <tr><td>plus->cpidx_Version_Minor </td><td>C</td><td>1</td><td>file version (minor)</td></tr>
  137. <tr><td>plus->cpidx_Back_Major</td><td>C</td><td>1</td><td>supported from GRASS version (major)</td></tr>
  138. <tr><td>plus->cpidx_Back_Minor</td><td>C</td><td>1</td><td>supported from GRASS version (minor)</td></tr>
  139. <tr><td>plus->cidx_port->byte_order</td><td>C</td><td>1</td><td>little or big endian
  140. flag; files are written in machine native order but
  141. files in both little and big endian order may be
  142. readl; zero for little endian</td></tr>
  143. <tr><td>plus->cidx_head_size</td><td>L</td><td>1</td><td>cidx head size</td></tr>
  144. <tr><td>plus->n_cidx</td><td>I</td><td>1</td><td>number of fields</td></tr>
  145. <tr><td>field</td><td>I</td><td>n_cidx</td><td>field number</td></tr>
  146. <tr><td>n_cats</td><td>I</td><td>n_cidx</td><td>number of categories</td></tr>
  147. <tr><td>n_ucats</td><td>I</td><td>n_cidx</td><td>number of unique categories</td></tr>
  148. <tr><td>n_types</td><td>I</td><td>n_cidx</td><td>number of feature types</td></tr>
  149. <tr><td>rtype</td><td>I</td><td>n_cidx * n_types</td><td>Feature type</td></tr>
  150. <tr><td>type[t]</td><td>I</td><td>n_cidx * n_types</td><td>Number of items</td></tr>
  151. </table>
  152. */