read.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*!
  2. \file lib/vector/Vlib/read.c
  3. \brief Vector library - read features
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 2011-2013 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7 Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <sys/types.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. static int read_dummy()
  16. {
  17. G_warning("Vect_read_line() %s",
  18. _("for this format/level not supported"));
  19. return -1;
  20. }
  21. #if !defined HAVE_OGR || !defined HAVE_POSTGRES
  22. static int format()
  23. {
  24. G_fatal_error(_("Requested format is not compiled in this version"));
  25. return 0;
  26. }
  27. #endif
  28. static int (*Read_next_line_array[][3]) () = {
  29. {
  30. read_dummy, V1_read_next_line_nat, V2_read_next_line_nat}
  31. #ifdef HAVE_OGR
  32. , {
  33. read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr}
  34. , {
  35. read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr}
  36. #else
  37. , {
  38. read_dummy, format, format}
  39. , {
  40. read_dummy, format, format}
  41. #endif
  42. #ifdef HAVE_POSTGRES
  43. , {
  44. read_dummy, V1_read_next_line_pg, V2_read_next_line_pg}
  45. #else
  46. , {
  47. read_dummy, format, format}
  48. #endif
  49. };
  50. static int (*Read_line_array[]) () = {
  51. V2_read_line_nat
  52. #ifdef HAVE_OGR
  53. , V2_read_line_sfa
  54. , V2_read_line_sfa
  55. #else
  56. , format
  57. , format
  58. #endif
  59. #ifdef HAVE_POSTGRES
  60. , V2_read_line_pg
  61. #else
  62. , format
  63. #endif
  64. };
  65. /*!
  66. \brief Get line id for sequential reading.
  67. This function returns id of feature which has been read by calling
  68. Vect_read_next_line().
  69. \param Map pointer to Map_info struct
  70. \return feature id
  71. \return -1 on error
  72. */
  73. int Vect_get_next_line_id(const struct Map_info *Map)
  74. {
  75. G_debug(3, "Vect_get_next_line()");
  76. if (!VECT_OPEN(Map)) {
  77. G_warning(_("Vector map is not open for reading"));
  78. return -1;
  79. }
  80. return Map->next_line - 1;
  81. }
  82. /*!
  83. \brief Read next vector feature
  84. This function implements sequential access, constraints are
  85. reflected, see Vect_set_constraint_region(),
  86. Vect_set_constraint_type(), or Vect_set_constraint_field() for
  87. details.
  88. Use Vect_rewind() to reset reading. Topological level is not
  89. required.
  90. A warning is printed on failure.
  91. \param Map pointer Map_info struct
  92. \param[out] line_p feature geometry (pointer to line_pnts struct)
  93. \param[out] line_c feature categories (pointer to line_cats struct)
  94. \return feature type (GV_POINT, GV_LINE, ...)
  95. \return -1 on error
  96. \return -2 nothing to read
  97. */
  98. int Vect_read_next_line(const struct Map_info *Map,
  99. struct line_pnts *line_p, struct line_cats *line_c)
  100. {
  101. int ret;
  102. G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);
  103. if (!VECT_OPEN(Map)) {
  104. G_warning(_("Vector map is not open for reading"));
  105. return -1;
  106. }
  107. ret = (*Read_next_line_array[Map->format][Map->level]) (Map, line_p,
  108. line_c);
  109. if (ret == -1)
  110. G_warning(_("Unable to read feature %d from vector map <%s>"),
  111. Map->next_line, Vect_get_full_name(Map));
  112. return ret;
  113. }
  114. /*!
  115. \brief Read vector feature (topological level required)
  116. This function implements random access. Constraits are ignored.
  117. Note: Topology must be built at level >= GV_BUILD_BASE
  118. A warning is printed on failure.
  119. \param Map pointer to vector map
  120. \param[out] line_p feature geometry (pointer to line_pnts struct)
  121. \param[out] line_c feature categories (pointer to line_cats struct)
  122. \param line feature id (starts at 1)
  123. \return feature type
  124. \return -1 on failure
  125. \return -2 nothing to read
  126. */
  127. int Vect_read_line(const struct Map_info *Map,
  128. struct line_pnts *line_p, struct line_cats *line_c, int line)
  129. {
  130. int ret;
  131. G_debug(3, "Vect_read_line(): line = %d", line);
  132. if (!VECT_OPEN(Map)) {
  133. G_warning(_("Vector map is not open for reading"));
  134. return -1;
  135. }
  136. if (line < 1 || line > Map->plus.n_lines) {
  137. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  138. return -1;
  139. }
  140. ret = (*Read_line_array[Map->format]) (Map, line_p, line_c, line);
  141. if (ret == -1)
  142. G_warning(_("Unable to read feature %d from vector map <%s>"),
  143. line, Vect_get_full_name(Map));
  144. return ret;
  145. }
  146. /*!
  147. \brief Check if feature is alive or dead (topological level required)
  148. Note: Topology must be built at level >= GV_BUILD_BASE
  149. \param Map pointer to Map_info structure
  150. \param line feature id
  151. \return 1 feature alive
  152. \return 0 feature is dead or index is out of range
  153. */
  154. int Vect_line_alive(const struct Map_info *Map, int line)
  155. {
  156. if (line < 1 || line > Map->plus.n_lines) {
  157. G_warning(_("Line index is out of range"));
  158. return 0;
  159. }
  160. if (Map->plus.Line[line] != NULL)
  161. return 1;
  162. return 0;
  163. }
  164. /*!
  165. \brief Check if node is alive or dead (topological level required)
  166. Note: Topology must be built at level >= GV_BUILD_BASE
  167. \param Map pointer to Map_info structure
  168. \param node node id
  169. \return 1 node alive
  170. \return 0 node is dead or index is out of range
  171. */
  172. int Vect_node_alive(const struct Map_info *Map, int node)
  173. {
  174. if (node < 1 || node > Map->plus.n_nodes) {
  175. G_warning(_("Node index is out of range"));
  176. return 0;
  177. }
  178. if (Map->plus.Node[node] != NULL)
  179. return 1;
  180. return 0;
  181. }
  182. /*!
  183. \brief Check if area is alive or dead (topological level required)
  184. Note: Topology must be built at level >= GV_BUILD_AREAS
  185. \param Map pointer to Map_info structure
  186. \param area area id
  187. \return 1 area alive
  188. \return 0 area is dead or index is out of range
  189. */
  190. int Vect_area_alive(const struct Map_info *Map, int area)
  191. {
  192. if (area < 1 || area > Map->plus.n_areas) {
  193. G_warning(_("Area index is out of range"));
  194. return 0;
  195. }
  196. if (Map->plus.Area[area] != NULL)
  197. return 1;
  198. return 0;
  199. }
  200. /*!
  201. \brief Check if isle is alive or dead (topological level required)
  202. Note: Topology must be built at level >= GV_BUILD_AREAS
  203. \param Map pointer to Map_info structure
  204. \param isle isle id
  205. \return 1 isle alive
  206. \return 0 isle is dead or index is out of range
  207. */
  208. int Vect_isle_alive(const struct Map_info *Map, int isle)
  209. {
  210. if (isle < 1 || isle > Map->plus.n_isles) {
  211. G_warning(_("Isle index is out of range"));
  212. return 0;
  213. }
  214. if (Map->plus.Isle[isle] != NULL)
  215. return 1;
  216. return 0;
  217. }
  218. /*!
  219. \brief Get feature offset (topological level required)
  220. Note: Topology must be built at level >= GV_BUILD_BASE
  221. Used for Vect_restore_line().
  222. \param Map pointer to Map_info structure
  223. \param line feature id
  224. \return feature offset
  225. \return -1 on error
  226. */
  227. off_t Vect_get_line_offset(const struct Map_info *Map, int line)
  228. {
  229. if (line < 1 || line > Map->plus.n_lines) {
  230. return -1;
  231. }
  232. if (Map->plus.Line[line] != NULL) {
  233. return Map->plus.Line[line]->offset;
  234. }
  235. return -1;
  236. }