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