read.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 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[][4]) () = {
  29. {
  30. read_dummy, V1_read_next_line_nat, V2_read_next_line_nat, read_dummy}
  31. #ifdef HAVE_OGR
  32. , {
  33. read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr, read_dummy}
  34. , {
  35. read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr, read_dummy}
  36. #else
  37. , {
  38. read_dummy, format, format, format}
  39. , {
  40. read_dummy, format, format, format}
  41. #endif
  42. #ifdef HAVE_POSTGRES
  43. , {
  44. read_dummy, V1_read_next_line_pg, V2_read_next_line_pg, V2_read_next_line_pg}
  45. #else
  46. , {
  47. read_dummy, format, format, format}
  48. #endif
  49. };
  50. static int (*Read_line_array[][2]) () = {
  51. {
  52. V2_read_line_nat, read_dummy}
  53. #ifdef HAVE_OGR
  54. , {
  55. V2_read_line_sfa, read_dummy}
  56. , {
  57. V2_read_line_sfa, read_dummy}
  58. #else
  59. , {
  60. format, format}
  61. , {
  62. format, format}
  63. #endif
  64. #ifdef HAVE_POSTGRES
  65. , {
  66. V2_read_line_sfa, V3_read_line_pg}
  67. #else
  68. , {
  69. format, format}
  70. #endif
  71. };
  72. /*!
  73. \brief Get line id for sequential reading.
  74. This function returns id of feature which has been read by calling
  75. Vect_read_next_line().
  76. \param Map pointer to Map_info struct
  77. \return feature id
  78. */
  79. int Vect_get_next_line_id(const struct Map_info *Map)
  80. {
  81. G_debug(3, "Vect_get_next_line()");
  82. if (!VECT_OPEN(Map))
  83. G_fatal_error(_("Vector map is not open for reading"));
  84. return Map->next_line - 1;
  85. }
  86. /*!
  87. \brief Read next vector feature
  88. This function implements sequential access, constraints are
  89. reflected, see Vect_set_constraint_region(),
  90. Vect_set_constraint_type(), or Vect_set_constraint_field().
  91. Use Vect_rewind() to reset reading.
  92. G_fatal_error() is called on failure.
  93. \param Map pointer Map_info struct
  94. \param[out] line_p feature geometry
  95. (pointer to line_pnts struct)
  96. \param[out] line_c feature categories
  97. (pointer to line_cats struct)
  98. \return feature type (GV_POINT, GV_LINE, ...)
  99. \return -1 on error
  100. \return -2 nothing to read
  101. */
  102. int Vect_read_next_line(const struct Map_info *Map,
  103. struct line_pnts *line_p, struct line_cats *line_c)
  104. {
  105. int ret;
  106. G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);
  107. if (!VECT_OPEN(Map))
  108. G_fatal_error(_("Vector map is not open for reading"));
  109. ret = (*Read_next_line_array[Map->format][Map->level]) (Map, line_p,
  110. line_c);
  111. /*
  112. if (ret == -1)
  113. G_fatal_error(_("Unable to read feature %d vector map <%s>"),
  114. Map->next_line, Vect_get_full_name(Map));
  115. */
  116. return ret;
  117. }
  118. /*!
  119. \brief Read vector feature
  120. This function implements random access. Note that constraits are
  121. ignored!
  122. G_fatal_error() is called on failure.
  123. \param Map pointer to vector map
  124. \param[out] line_p feature geometry
  125. (pointer to line_pnts struct)
  126. \param[out] line_c feature categories
  127. (pointer to line_cats struct)
  128. \param line feature id (starts at 1)
  129. \return feature type
  130. \return -1 on failure
  131. \return -2 nothing to read
  132. */
  133. int Vect_read_line(const struct Map_info *Map,
  134. struct line_pnts *line_p, struct line_cats *line_c, int line)
  135. {
  136. int ret;
  137. G_debug(3, "Vect_read_line(): line = %d", line);
  138. if (!VECT_OPEN(Map))
  139. G_fatal_error(_("Vector map is not open for reading"));
  140. if (line < 1 || line > Map->plus.n_lines)
  141. G_fatal_error(_("Requested feature id %d is not reasonable"
  142. "(max features in vector map <%s>: %d)"),
  143. line, Vect_get_full_name(Map), Map->plus.n_lines);
  144. ret = (*Read_line_array[Map->format][Map->level == 3 ? 1 : 0]) (Map, line_p, line_c, line);
  145. /*
  146. if (ret == -1)
  147. G_fatal_error(_("Unable to read feature %d from vector map <%s>"),
  148. line, Vect_get_full_name(Map));
  149. */
  150. return ret;
  151. }
  152. /*!
  153. \brief Check if feature is alive or dead (level 2 required)
  154. \param Map pointer to Map_info structure
  155. \param line feature id
  156. \return 1 feature alive
  157. \return 0 feature is dead
  158. */
  159. int Vect_line_alive(const struct Map_info *Map, int line)
  160. {
  161. if (Map->plus.Line[line] != NULL)
  162. return 1;
  163. return 0;
  164. }
  165. /*!
  166. \brief Check if node is alive or dead (level 2 required)
  167. \param Map pointer to Map_info structure
  168. \param node node id
  169. \return 1 node alive
  170. \return 0 node is dead
  171. */
  172. int Vect_node_alive(const struct Map_info *Map, int node)
  173. {
  174. if (Map->plus.Node[node] != NULL)
  175. return 1;
  176. return 0;
  177. }
  178. /*!
  179. \brief Check if area is alive or dead (level 2 required)
  180. \param Map pointer to Map_info structure
  181. \param area area id
  182. \return 1 area alive
  183. \return 0 area is dead
  184. */
  185. int Vect_area_alive(const struct Map_info *Map, int area)
  186. {
  187. if (Map->plus.Area[area] != NULL)
  188. return 1;
  189. return 0;
  190. }
  191. /*!
  192. \brief Check if isle is alive or dead (level 2 required)
  193. \param Map pointer to Map_info structure
  194. \param isle isle id
  195. \return 1 isle alive
  196. \return 0 isle is dead
  197. */
  198. int Vect_isle_alive(const struct Map_info *Map, int isle)
  199. {
  200. if (Map->plus.Isle[isle] != NULL)
  201. return 1;
  202. return 0;
  203. }
  204. /*!
  205. \brief Get feature offset
  206. Used for Vect_restore_line().
  207. \param Map pointer to Map_info structure
  208. \param line feature id
  209. \return feature offset
  210. \return -1 on error
  211. */
  212. off_t Vect_get_line_offset(const struct Map_info *Map, int line)
  213. {
  214. if (line < 1 || line > Map->plus.n_lines)
  215. return -1;
  216. if (Map->plus.Line[line] != NULL) {
  217. return Map->plus.Line[line]->offset;
  218. }
  219. return -1;
  220. }