read.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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[][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_sfa
  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. */
  72. int Vect_get_next_line_id(const struct Map_info *Map)
  73. {
  74. G_debug(3, "Vect_get_next_line()");
  75. if (!VECT_OPEN(Map))
  76. G_fatal_error(_("Vector map is not open for reading"));
  77. return Map->next_line - 1;
  78. }
  79. /*!
  80. \brief Read next vector feature
  81. This function implements sequential access, constraints are
  82. reflected, see Vect_set_constraint_region(),
  83. Vect_set_constraint_type(), or Vect_set_constraint_field().
  84. Use Vect_rewind() to reset reading.
  85. G_fatal_error() is called on failure.
  86. \param Map pointer Map_info struct
  87. \param[out] line_p feature geometry
  88. (pointer to line_pnts struct)
  89. \param[out] line_c feature categories
  90. (pointer to line_cats struct)
  91. \return feature type (GV_POINT, GV_LINE, ...)
  92. \return -1 on error
  93. \return -2 nothing to read
  94. */
  95. int Vect_read_next_line(const struct Map_info *Map,
  96. struct line_pnts *line_p, struct line_cats *line_c)
  97. {
  98. int ret;
  99. G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);
  100. if (!VECT_OPEN(Map))
  101. G_fatal_error(_("Vector map is not open for reading"));
  102. ret = (*Read_next_line_array[Map->format][Map->level]) (Map, line_p,
  103. line_c);
  104. if (ret == -1)
  105. G_fatal_error(_("Unable to read feature %d vector map <%s>"),
  106. Map->next_line, Vect_get_full_name(Map));
  107. return ret;
  108. }
  109. /*!
  110. \brief Read vector feature
  111. This function implements random access. Note that constraits are
  112. ignored!
  113. G_fatal_error() is called on failure.
  114. \param Map pointer to vector map
  115. \param[out] line_p feature geometry
  116. (pointer to line_pnts struct)
  117. \param[out] line_c feature categories
  118. (pointer to line_cats struct)
  119. \param line feature id (starts at 1)
  120. \return feature type
  121. \return -1 on failure
  122. \return -2 nothing to read
  123. */
  124. int Vect_read_line(const struct Map_info *Map,
  125. struct line_pnts *line_p, struct line_cats *line_c, int line)
  126. {
  127. int ret;
  128. G_debug(3, "Vect_read_line(): line = %d", line);
  129. if (!VECT_OPEN(Map))
  130. G_fatal_error(_("Vector map is not open for reading"));
  131. if (line < 1 || line > Map->plus.n_lines)
  132. G_fatal_error(_("Requested feature id %d is not reasonable"
  133. "(max features in vector map <%s>: %d)"),
  134. line, Vect_get_full_name(Map), Map->plus.n_lines);
  135. ret = (*Read_line_array[Map->format]) (Map, line_p, line_c, line);
  136. if (ret == -1)
  137. G_fatal_error(_("Unable to read feature %d from vector map <%s>"),
  138. line, Vect_get_full_name(Map));
  139. return ret;
  140. }
  141. /*!
  142. \brief Check if feature is alive or dead (level 2 required)
  143. \param Map pointer to Map_info structure
  144. \param line feature id
  145. \return 1 feature alive
  146. \return 0 feature is dead
  147. */
  148. int Vect_line_alive(const struct Map_info *Map, int line)
  149. {
  150. if (Map->plus.Line[line] != NULL)
  151. return 1;
  152. return 0;
  153. }
  154. /*!
  155. \brief Check if node is alive or dead (level 2 required)
  156. \param Map pointer to Map_info structure
  157. \param node node id
  158. \return 1 node alive
  159. \return 0 node is dead
  160. */
  161. int Vect_node_alive(const struct Map_info *Map, int node)
  162. {
  163. if (Map->plus.Node[node] != NULL)
  164. return 1;
  165. return 0;
  166. }
  167. /*!
  168. \brief Check if area is alive or dead (level 2 required)
  169. \param Map pointer to Map_info structure
  170. \param area area id
  171. \return 1 area alive
  172. \return 0 area is dead
  173. */
  174. int Vect_area_alive(const struct Map_info *Map, int area)
  175. {
  176. if (Map->plus.Area[area] != NULL)
  177. return 1;
  178. return 0;
  179. }
  180. /*!
  181. \brief Check if isle is alive or dead (level 2 required)
  182. \param Map pointer to Map_info structure
  183. \param isle isle id
  184. \return 1 isle alive
  185. \return 0 isle is dead
  186. */
  187. int Vect_isle_alive(const struct Map_info *Map, int isle)
  188. {
  189. if (Map->plus.Isle[isle] != NULL)
  190. return 1;
  191. return 0;
  192. }
  193. /*!
  194. \brief Get feature offset
  195. Used for Vect_restore_line().
  196. \param Map pointer to Map_info structure
  197. \param line feature id
  198. \return feature offset
  199. \return -1 on error
  200. */
  201. off_t Vect_get_line_offset(const struct Map_info *Map, int line)
  202. {
  203. if (line < 1 || line > Map->plus.n_lines)
  204. return -1;
  205. if (Map->plus.Line[line] != NULL) {
  206. return Map->plus.Line[line]->offset;
  207. }
  208. return -1;
  209. }