gisvectorlib.dox 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*! \page gisvectorlib Vector File Processing
  2. <!-- doxygenized from "GRASS 5 Programmer's Manual"
  3. by M. Neteler 2/2004, 2006
  4. -->
  5. See especially \ref Vector_Library for details (extra page).
  6. - \subpage gisvectintro
  7. - \subpage Creating_and_Opening_New_Vector_Files
  8. - \subpage Vector_Category_File
  9. \section gisvectintro GRASS Vector File Processing
  10. Authors:
  11. <BR>
  12. (Written by CERL, with contributions from David D. Gray)
  13. <P>
  14. The <I>GIS Library</I> contains some functions related to vector file
  15. processing. These include prompting the user for vector files,
  16. locating vector files in the database, opening vector files, and a few
  17. others.
  18. <P>
  19. <B>Note.</B> Most vector file processing, however, is handled by
  20. routines in the <I>Vector Library</I>, which is described in
  21. Vector_Library.
  22. <P>
  23. \subsection Prompting_for_Vector_Files Prompting for Vector Files
  24. <P>
  25. The following routines interactively prompt the user for a vector file
  26. name. In each, the <B>prompt</B> string will be printed as the first
  27. line of the full prompt which asks the user to enter a vector file
  28. name. If <B>prompt</B> is the empty string "" then an appropriate
  29. prompt will be substituted. The name that the user enters is copied
  30. into the <B>name</B> buffer. The size of name should be large enough
  31. to hold any GRASS file name. Most systems allow file names to be quite
  32. long. It is recommended that name be declared <tt>char name</tt>.
  33. These routines have a built-in 'list' capability which allows the user
  34. to get a list of existing vector files.
  35. <P>
  36. The user is required to enter a valid vector file name, or else hit
  37. the RETURN key to cancel the request. If the user enters an invalid
  38. response, a message is printed, and the user is prompted again. If the
  39. user cancels the request, the NULL pointer is returned. Otherwise the
  40. mapset where the vector file lives or is to be created is
  41. returned. Both the name and the mapset are used in other routines to
  42. refer to the vector file.
  43. <P>
  44. char *G_ask_vector_old(char *prompt, char *name) prompt for an
  45. existing vector file
  46. Asks the user to enter the name of an existing vector file in any
  47. mapset in the database.
  48. <P>
  49. char *G_ask_vector_in_mapset(char *prompt, char *name) prompt for an
  50. existing vector file
  51. Asks the user to enter the name of an existing vector file in the
  52. current mapset.
  53. <P>
  54. char *G_ask_vector_new(char *prompt, char *name) prompt for a new
  55. vector file
  56. Asks the user to enter a name for a vector file which does not exist
  57. in the current mapset.
  58. <P>
  59. Here is an example of how to use these routines. Note that the
  60. programmer must handle the NULL return properly:
  61. \verbatim
  62. char *mapset;
  63. char name[GNAME_MAX];
  64. mapset = G_ask_vector_old("Enter vector file to be processed", name);
  65. if (mapset == NULL)
  66. exit(0);
  67. \endverbatim
  68. \subsection Finding_Vector_Files_in_the_Database Finding Vector Files
  69. in the Database
  70. <P>
  71. Noninteractive modules cannot make use of the interactive prompting
  72. routines described above. For example, a command line driven module
  73. may require a vector file name as one of the command arguments. GRASS
  74. allows the user to specify vector file names (or any other database
  75. file) either as a simple unqualified name, such as "roads", or as a
  76. fully qualified name, such as "roads in <I>mapset</I>", where
  77. <I>mapset</I> is the mapset where the vector file is to be
  78. found. Often only the unqualified vector file name is provided on the
  79. command line.
  80. <P>
  81. The following routines search the database for vector files:
  82. <P>
  83. int G_find_vector(char *name, char *mapset) find a vector file
  84. <P>
  85. int G_find_vector2(char *name, char *mapset) find a vector
  86. file
  87. Look for the vector file <B>name</B> in the database. The
  88. <B>mapset</B> parameter can either be the empty string "", which means
  89. search all the mapsets in the user's current mapset search path (see
  90. Mapset_Search_Path for more details about the search path), or it can
  91. be a specific mapset name, which means look for the vector file only
  92. in this one mapset (for example, in the current mapset). If found, the
  93. mapset where the vector file lives is returned. If not found, the NULL
  94. pointer is returned.
  95. <P>
  96. The difference between these two routines is that if the user
  97. specifies a fully qualified vector file which exists, then
  98. <I>G_find_vector2()</I> modifies <B>name</B> by removing the "in
  99. <I>mapset</I>" while <I>G_find_vector()</I> does not. Be warned that
  100. G_find_vector2() should not be used directly on a command line
  101. argument, since modifying argv[ ] may not be valid. The argument
  102. should be copied to another character buffer which is then passed to
  103. G_find_vector2(). Normally, the GRASS programmer need not worry about
  104. qualified vs. unqualified names since all library routines handle
  105. both forms. However, if the programmer wants the name to be returned
  106. unqualified (for displaying the name to the user, or storing it in a
  107. data file, etc.), then <I>G_find_vector2()</I> should be used.
  108. <P>
  109. For example, to find a vector file anywhere in the database:
  110. \verbatim
  111. char name[GNAME_MAX];
  112. char *mapset;
  113. if ((mapset = G_find_vector(name,"")) == NULL)
  114. /* not found */
  115. \endverbatim
  116. \verbatim
  117. char name[GNAME_MAX];
  118. if (G_find_vector(name,G_mapset()) == NULL)
  119. /* not found */
  120. \endverbatim
  121. To check that the vector file exists in the current mapset:
  122. \subsection Opening_an_Existing_Vector_File Opening an Existing Vector File
  123. The following routine opens the vector file <B>name</B> in
  124. <B>mapset</B> for reading.
  125. <P>
  126. The vector file <B>name</B> and <B>mapset</B> can be obtained
  127. interactively using <I>G_ask_vector_old()</I> or <I>
  128. G_ask_vector_in_mapset()</I>, and noninteractively using
  129. <I>G_find_vector()</I> or <I>G_find_vector2().</I>
  130. <P>
  131. FILE *G_fopen_vector_old(char *name, char *mapset) open an existing
  132. vector file
  133. This routine opens the vector file <B>name</B> in <B>mapset</B> for
  134. reading. A file descriptor is returned if the open is
  135. successful. Otherwise the NULL pointer is returned (no diagnostic
  136. message is printed).
  137. <P>
  138. The file descriptor can then be used with routines in the <I>Dig
  139. Library</I> to read the vector file (See \ref Vector_Library).
  140. <P>
  141. <B>Note.</B> This routine does not call any routines in the <I>Dig
  142. Library</I>; No initialization of the vector file is done by this
  143. routine, directly or indirectly.
  144. \section Creating_and_Opening_New_Vector_Files Creating and Opening
  145. New Vector Files
  146. The following routine creates the new vector file <B>name</B> in the
  147. current mapset (GRASS does not allow files to be created outside the
  148. current mapset. See \ref Database_Access_Rules) and opens it for
  149. writing. The vector file <B>name</B> should be obtained interactively
  150. using <I>G_ask_vector_new().</I> If obtained noninteractively (e.g.,
  151. from the command line), <I>G_legal_filename()</I> should be called
  152. first to make sure that <B>name</B> is a valid GRASS file name.
  153. <P>
  154. <B>Warning.</B> If <B>name</B> already exists, it will be erased and
  155. re-created empty. The interactive routine <I>G_ask_vector_new()</I>
  156. guarantees that <B>name</B> will not exist, but if <B>name</B> is
  157. obtained from the command line, <B>name</B> may exist. In this case
  158. <I>G_find_vector()</I> could be used to see if <B>name</B> exists.
  159. <P>
  160. FILE *G_fopen_vector_new(char *name) open a new vector file
  161. Creates and opens the vector file <B>name</B> for writing.
  162. <P>
  163. A file descriptor is returned if the open is successful. Otherwise the
  164. NULL pointer is returned (no diagnostic message is printed).
  165. <P>
  166. The file descriptor can then be used with routines in the <I>Dig
  167. Library</I> to write the <I>vector file</I> (See \ref Vector_Library.)
  168. <P>
  169. <B>Note.</B> This routine does not call any routines in the <I>Dig
  170. Library</I>; No initialization of the vector file is done by this
  171. routine, directly or indirectly. Also, only the vector file itself
  172. (i.e., the <I>dig</I> file), is created. None of the other vector
  173. support files are created, removed, or modified in any way.
  174. \subsection Reading_and_Writing_Vector_Files Reading and Writing Vector Files
  175. Reading and writing vector files is handled by routines in the <I>Dig
  176. Library.</I> See \ref Vector_Library for details.
  177. \section Vector_Category_File Vector Category File
  178. GRASS vector files have category labels associated with them. The
  179. category file is structured so that each category in the vector file
  180. can have a one-line description.
  181. <P>
  182. The routines described below read and write the vector category
  183. file. They use the <I>Categories structure</I> which is described in
  184. GIS_Library_Data_Structures.
  185. <P>
  186. <B>Note.</B> The vector category file has exactly the same structure
  187. as the raster category file. In fact, it exists so that the module
  188. <I>v.to.rast</I> can convert a vector file to a raster file that has
  189. an up-to-date category file.
  190. <P>
  191. The routines described in
  192. Querying_and_Changing_the_Categories_Structure which modify the
  193. <I>Categories</I> structure can therefore be used to set and change
  194. vector categories as well.
  195. <P>
  196. int G_read_vector_cats(char *name, name *mapset, struct Categories
  197. *cats) read vector category file
  198. The category file for vector file <B>name</B> in <B>mapset</B> is read
  199. into the <B>cats</B> structure. If there is an error reading the
  200. category file, a diagnostic message is printed and -1 is
  201. returned. Otherwise, 0 is returned.
  202. <P>
  203. int G_write_vector_cats(char *name, struct Categories *cats) write
  204. vector category file
  205. Writes the category file for the vector file <B>name</B> in the
  206. current mapset from the <B>cats</B> structure.
  207. <P>
  208. Returns 1 if successful. Otherwise, -1 is returned (no diagnostic is
  209. printed).
  210. */