read.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*!
  2. \file read.c
  3. \brief Vector library - read vector features
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 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. */
  11. #include <grass/config.h>
  12. #include <sys/types.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. static int read_next_dummy()
  16. {
  17. return -1;
  18. }
  19. #ifndef HAVE_OGR
  20. static int format()
  21. {
  22. G_fatal_error(_("Requested format is not compiled in this version"));
  23. return 0;
  24. }
  25. #endif
  26. static int (*Read_next_line_array[][3]) () = {
  27. {
  28. read_next_dummy, V1_read_next_line_nat, V2_read_next_line_nat}
  29. #ifdef HAVE_OGR
  30. , {
  31. read_next_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr}
  32. , {
  33. read_next_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr}
  34. #else
  35. , {
  36. read_next_dummy, format, format}
  37. , {
  38. read_next_dummy, format, format}
  39. #endif
  40. };
  41. static int (*V2_read_line_array[]) () = {
  42. V2_read_line_nat
  43. #ifdef HAVE_OGR
  44. , V2_read_line_ogr
  45. , V2_read_line_ogr
  46. #else
  47. , format
  48. , format
  49. #endif
  50. };
  51. /*!
  52. \brief Read next vector feature (level 1 and 2)
  53. \param Map pointer vector map
  54. \param[out] line_p feature geometry
  55. \param[out] line_c feature categories
  56. \return feature type,
  57. \return -1 out of memory
  58. \return -2 EOF
  59. */
  60. int Vect_read_next_line(const struct Map_info *Map,
  61. struct line_pnts *line_p, struct line_cats *line_c)
  62. {
  63. G_debug(3, "Vect_read_next_line()");
  64. if (!VECT_OPEN(Map))
  65. return -1;
  66. return (*Read_next_line_array[Map->format][Map->level]) (Map, line_p,
  67. line_c);
  68. }
  69. /*!
  70. \brief Read vector feature
  71. \param Map pointer to vector map
  72. \param[out] line_p feature geometry
  73. \param[out] line_c feature categories
  74. \param line feature id
  75. \return feature type
  76. \return -1 out of memory,
  77. \return -2 EOF
  78. */
  79. int Vect_read_line(const struct Map_info *Map,
  80. struct line_pnts *line_p, struct line_cats *line_c, int line)
  81. {
  82. G_debug(3, "Vect_read_line() line=%d", line);
  83. if (!VECT_OPEN(Map))
  84. G_fatal_error("Vect_read_line(): %s", _("vector map is not opened"));
  85. if (line < 1 || line > Map->plus.n_lines)
  86. G_fatal_error(_("Vect_read_line(): feature id %d is not reasonable "
  87. "(max features in vector map <%s>: %d)"),
  88. line, Vect_get_full_name(Map), Map->plus.n_lines);
  89. return (*V2_read_line_array[Map->format]) (Map, line_p, line_c, line);
  90. }
  91. /*!
  92. \brief Check if feature is alive or dead
  93. \param Map pointer to vector map
  94. \param line feature id
  95. \return 1 if feature alive
  96. \return 0 if feature is dead
  97. */
  98. int Vect_line_alive(const struct Map_info *Map, int line)
  99. {
  100. if (Map->plus.Line[line] != NULL)
  101. return 1;
  102. return 0;
  103. }
  104. /*!
  105. \brief Check if node is alive or dead
  106. \param Map pointer to vector map
  107. \param node node id
  108. \return 1 if node alive
  109. \return 0 if node is dead
  110. */
  111. int Vect_node_alive(const struct Map_info *Map, int node)
  112. {
  113. if (Map->plus.Node[node] != NULL)
  114. return 1;
  115. return 0;
  116. }
  117. /*!
  118. \brief Check if area is alive or dead
  119. \param Map pointer to vector map
  120. \param area area id
  121. \return 1 if area alive
  122. \return 0 if area is dead
  123. */
  124. int Vect_area_alive(const struct Map_info *Map, int area)
  125. {
  126. if (Map->plus.Area[area] != NULL)
  127. return 1;
  128. return 0;
  129. }
  130. /*!
  131. \brief Check if isle is alive or dead
  132. \param Map pointer to vector map
  133. \param isle isle id
  134. \return 1 if isle alive
  135. \return 0 if isle is dead
  136. */
  137. int Vect_isle_alive(const struct Map_info *Map, int isle)
  138. {
  139. if (Map->plus.Isle[isle] != NULL)
  140. return 1;
  141. return 0;
  142. }
  143. /*!
  144. \brief Get feature offset
  145. Can be used for Vect_restore_line().
  146. \param Map pointer to vector map
  147. \param line feature id
  148. \return feature offset
  149. \return -1 on error
  150. */
  151. off_t Vect_get_line_offset(const const struct Map_info *Map, int line)
  152. {
  153. if (line < 1 || line > Map->plus.n_lines)
  154. return -1;
  155. if (Map->plus.Line[line] != NULL) {
  156. return Map->plus.Line[line]->offset;
  157. }
  158. return -1;
  159. }