read.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #else
  33. , {
  34. read_next_dummy, format, format}
  35. #endif
  36. };
  37. static int (*V2_read_line_array[]) () = {
  38. V2_read_line_nat
  39. #ifdef HAVE_OGR
  40. , V2_read_line_ogr
  41. #else
  42. , format
  43. #endif
  44. };
  45. /*!
  46. \brief Read next vector feature (level 1 and 2)
  47. \param Map pointer vector map
  48. \param[out] line_p feature geometry
  49. \param[out] line_c feature categories
  50. \return feature type,
  51. \return -1 out of memory
  52. \return -2 EOF
  53. */
  54. int Vect_read_next_line(const struct Map_info *Map,
  55. struct line_pnts *line_p, struct line_cats *line_c)
  56. {
  57. G_debug(3, "Vect_read_next_line()");
  58. if (!VECT_OPEN(Map))
  59. return -1;
  60. return (*Read_next_line_array[Map->format][Map->level]) (Map, line_p,
  61. line_c);
  62. }
  63. /*!
  64. \brief Read vector feature
  65. \param Map pointer to vector map
  66. \param[out] line_p feature geometry
  67. \param[out] line_c feature categories
  68. \param line feature id
  69. \return feature type
  70. \return -1 out of memory,
  71. \return -2 EOF
  72. */
  73. int Vect_read_line(const struct Map_info *Map,
  74. struct line_pnts *line_p, struct line_cats *line_c, int line)
  75. {
  76. G_debug(3, "Vect_read_line()");
  77. if (!VECT_OPEN(Map))
  78. G_fatal_error("Vect_read_line(): %s", _("vector map is not opened"));
  79. if (line < 1 || line > Map->plus.n_lines)
  80. G_fatal_error(_("Vect_read_line(): feature id %d is not reasonable "
  81. "(max features in vector map <%s>: %d)"),
  82. line, Vect_get_full_name(Map), Map->plus.n_lines);
  83. return (*V2_read_line_array[Map->format]) (Map, line_p, line_c, line);
  84. }
  85. /*!
  86. \brief Check if feature is alive or dead
  87. \param Map pointer to vector map
  88. \param line feature id
  89. \return 1 if feature alive
  90. \return 0 if feature is dead
  91. */
  92. int Vect_line_alive(const struct Map_info *Map, int line)
  93. {
  94. if (Map->plus.Line[line] != NULL)
  95. return 1;
  96. return 0;
  97. }
  98. /*!
  99. \brief Check if node is alive or dead
  100. \param Map pointer to vector map
  101. \param node node id
  102. \return 1 if node alive
  103. \return 0 if node is dead
  104. */
  105. int Vect_node_alive(const struct Map_info *Map, int node)
  106. {
  107. if (Map->plus.Node[node] != NULL)
  108. return 1;
  109. return 0;
  110. }
  111. /*!
  112. \brief Check if area is alive or dead
  113. \param Map pointer to vector map
  114. \param area area id
  115. \return 1 if area alive
  116. \return 0 if area is dead
  117. */
  118. int Vect_area_alive(const struct Map_info *Map, int area)
  119. {
  120. if (Map->plus.Area[area] != NULL)
  121. return 1;
  122. return 0;
  123. }
  124. /*!
  125. \brief Check if isle is alive or dead
  126. \param Map pointer to vector map
  127. \param isle isle id
  128. \return 1 if isle alive
  129. \return 0 if isle is dead
  130. */
  131. int Vect_isle_alive(const struct Map_info *Map, int isle)
  132. {
  133. if (Map->plus.Isle[isle] != NULL)
  134. return 1;
  135. return 0;
  136. }
  137. /*!
  138. \brief Get feature offset
  139. Can be used for Vect_restore_line().
  140. \param Map pointer to vector map
  141. \param line feature id
  142. \return feature offset
  143. \return -1 on error
  144. */
  145. off_t Vect_get_line_offset(const const struct Map_info *Map, int line)
  146. {
  147. if (line < 1 || line > Map->plus.n_lines)
  148. return -1;
  149. if (Map->plus.Line[line] != NULL) {
  150. return Map->plus.Line[line]->offset;
  151. }
  152. return -1;
  153. }