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