GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
vector/Vlib/read.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/read.c
3
4 \brief Vector library - read features
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 SPDX-FileCopyrightText: 2001-2009, 2011-2013 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
12 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
13 \author Update to GRASS 7 Martin Landa <landa.martin gmail.com>
14 */
15
16#include <sys/types.h>
17#include <grass/vector.h>
18#include <grass/glocale.h>
19
20static int read_dummy(struct Map_info *Map G_UNUSED,
23{
24 G_warning("Vect_read_line() %s", _("for this format/level not supported"));
25 return -1;
26}
27
28#if !defined HAVE_POSTGRES
29static int format(struct Map_info *Map G_UNUSED,
32{
33 G_fatal_error(_("Requested format is not compiled in this version"));
34 return 0;
35}
36
37static int format2(struct Map_info *Map G_UNUSED,
39 struct line_cats *line_c G_UNUSED, int line G_UNUSED)
40{
41 G_fatal_error(_("Requested format is not compiled in this version"));
42 return 0;
43}
44#endif
45
46static int (*Read_next_line_array[][3])(struct Map_info *, struct line_pnts *,
47 struct line_cats *) = {
51#ifdef HAVE_POSTGRES
52 ,
54#else
55 ,
56 {read_dummy, format, format}
57#endif
58};
59
60static int (*Read_line_array[])(struct Map_info *, struct line_pnts *,
61 struct line_cats *, int) = {
63#ifdef HAVE_POSTGRES
64 ,
66#else
67 ,
69#endif
70};
71
72/*!
73 \brief Get line id for sequential reading.
74
75 This function returns id of feature which has been read by calling
76 Vect_read_next_line().
77
78 \param Map pointer to Map_info struct
79
80 \return feature id
81 \return -1 on error
82 */
84{
85 G_debug(3, "Vect_get_next_line()");
86
87 if (!VECT_OPEN(Map)) {
88 G_warning(_("Vector map is not open for reading"));
89 return -1;
90 }
91
92 return Map->next_line - 1;
93}
94
95/*!
96 \brief Read next vector feature
97
98 This function implements sequential access, constraints are
99 reflected, see Vect_set_constraint_region(),
100 Vect_set_constraint_type(), or Vect_set_constraint_field() for
101 details.
102
103 Use Vect_rewind() to reset reading. Topological level is not
104 required.
105
106 A warning is printed on failure.
107
108 \param Map pointer Map_info struct
109 \param[out] line_p feature geometry (pointer to line_pnts struct)
110 \param[out] line_c feature categories (pointer to line_cats struct)
111
112 \return feature type (GV_POINT, GV_LINE, ...)
113 \return -1 on error
114 \return -2 nothing to read
115 */
117 struct line_cats *line_c)
118{
119 int ret;
120
121 G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);
122
123 if (!VECT_OPEN(Map)) {
124 G_warning(_("Vector map is not open for reading"));
125 return -1;
126 }
127
128 ret = (*Read_next_line_array[Map->format][Map->level])(Map, line_p, line_c);
129 if (ret == -1) {
130 const char *map_name = Vect_get_full_name(Map);
131 G_warning(_("Unable to read feature %d from vector map <%s>"),
132 Map->next_line, map_name);
133 G_free((void *)map_name);
134 }
135
136 return ret;
137}
138
139/*!
140 \brief Read vector feature (topological level required)
141
142 This function implements random access. Constraints are ignored.
143
144 Note: Topology must be built at level >= GV_BUILD_BASE
145
146 A warning is printed on failure.
147
148 \param Map pointer to vector map
149 \param[out] line_p feature geometry (pointer to line_pnts struct)
150 \param[out] line_c feature categories (pointer to line_cats struct)
151 \param line feature id (starts at 1)
152
153 \return feature type
154 \return -1 on failure
155 \return -2 nothing to read
156 */
158 struct line_cats *line_c, int line)
159{
160 int ret;
161
162 G_debug(3, "Vect_read_line(): line = %d", line);
163
164 if (!VECT_OPEN(Map)) {
165 G_warning(_("Vector map is not open for reading"));
166 return -1;
167 }
168
169 if (line < 1 || line > Map->plus.n_lines) {
170 G_warning(_("Attempt to access feature with invalid id (%d)"), line);
171 return -1;
172 }
173
174 ret = (*Read_line_array[Map->format])(Map, line_p, line_c, line);
175
176 if (ret == -1) {
177 const char *map_name = Vect_get_full_name(Map);
178 G_warning(_("Unable to read feature %d from vector map <%s>"), line,
179 map_name);
180 G_free((void *)map_name);
181 }
182
183 return ret;
184}
185
186/*!
187 \brief Check if feature is alive or dead (topological level required)
188
189 Note: Topology must be built at level >= GV_BUILD_BASE
190
191 \param Map pointer to Map_info structure
192 \param line feature id
193
194 \return 1 feature alive
195 \return 0 feature is dead or index is out of range
196 */
197int Vect_line_alive(struct Map_info *Map, int line)
198{
199 if (line < 1 || line > Map->plus.n_lines) {
200 G_warning(_("Line index is out of range"));
201 return 0;
202 }
203
204 if (Map->plus.Line[line] != NULL)
205 return 1;
206
207 return 0;
208}
209
210/*!
211 \brief Check if node is alive or dead (topological level required)
212
213 Note: Topology must be built at level >= GV_BUILD_BASE
214
215 \param Map pointer to Map_info structure
216 \param node node id
217
218 \return 1 node alive
219 \return 0 node is dead or index is out of range
220 */
221int Vect_node_alive(struct Map_info *Map, int node)
222{
223 if (node < 1 || node > Map->plus.n_nodes) {
224 G_warning(_("Node index is out of range"));
225 return 0;
226 }
227
228 if (Map->plus.Node[node] != NULL)
229 return 1;
230
231 return 0;
232}
233
234/*!
235 \brief Check if area is alive or dead (topological level required)
236
237 Note: Topology must be built at level >= GV_BUILD_AREAS
238
239 \param Map pointer to Map_info structure
240 \param area area id
241
242 \return 1 area alive
243 \return 0 area is dead or index is out of range
244 */
245int Vect_area_alive(struct Map_info *Map, int area)
246{
247 if (area < 1 || area > Map->plus.n_areas) {
248 G_warning(_("Area index is out of range"));
249 return 0;
250 }
251
252 if (Map->plus.Area[area] != NULL)
253 return 1;
254
255 return 0;
256}
257
258/*!
259 \brief Check if isle is alive or dead (topological level required)
260
261 Note: Topology must be built at level >= GV_BUILD_AREAS
262
263 \param Map pointer to Map_info structure
264 \param isle isle id
265
266 \return 1 isle alive
267 \return 0 isle is dead or index is out of range
268 */
270{
271 if (isle < 1 || isle > Map->plus.n_isles) {
272 G_warning(_("Isle index is out of range"));
273 return 0;
274 }
275
276 if (Map->plus.Isle[isle] != NULL)
277 return 1;
278
279 return 0;
280}
281
282/*!
283 \brief Get feature offset (topological level required)
284
285 Note: Topology must be built at level >= GV_BUILD_BASE
286
287 Used for Vect_restore_line().
288
289 \param Map pointer to Map_info structure
290 \param line feature id
291
292 \return feature offset
293 \return -1 on error
294 */
296{
297 if (line < 1 || line > Map->plus.n_lines) {
298 return -1;
299 }
300
301 if (Map->plus.Line[line] != NULL) {
302 return Map->plus.Line[line]->offset;
303 }
304
305 return -1;
306}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int V2_read_line_nat(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature on topological level (level 2) - native format - internal use only.
Definition read_nat.c:135
int V2_read_next_line_nat(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next vector feature on topological level (level 2) - native format - internal use only.
Definition read_nat.c:177
int V1_read_next_line_pg(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next feature from PostGIS layer. Skip empty features (level 1 without topology)....
Definition read_pg.c:86
int V2_read_line_pg(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read feature from PostGIS layer on topological level.
Definition read_pg.c:326
int V1_read_next_line_nat(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next vector feature on non-topological level (level 1) - native format - internal use only.
Definition read_nat.c:69
int V2_read_next_line_pg(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next feature from PostGIS layer on topological level (simple feature access).
Definition read_pg.c:117
int V1_read_next_line_ogr(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next feature from OGR layer. Skip empty features (level 1 without topology).
Definition read_ogr.c:45
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
int V2_read_next_line_ogr(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next feature from OGR layer on topological level.
Definition read_ogr.c:66
int V2_read_line_sfa(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Reads feature from OGR/PostGIS layer on topological level.
Definition read_sfa.c:38
#define VECT_OPEN(Map)
Check if vector map is open.
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#define _(str)
Definition glocale.h:10
Vector map info.
Feature category info.
Feature geometry info - coordinates.
off_t Vect_get_line_offset(struct Map_info *Map, int line)
Get feature offset (topological level required)
int Vect_get_next_line_id(struct Map_info *Map)
Get line id for sequential reading.
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature (topological level required)
int Vect_area_alive(struct Map_info *Map, int area)
Check if area is alive or dead (topological level required)
int Vect_node_alive(struct Map_info *Map, int node)
Check if node is alive or dead (topological level required)
int Vect_isle_alive(struct Map_info *Map, int isle)
Check if isle is alive or dead (topological level required)
int Vect_line_alive(struct Map_info *Map, int line)
Check if feature is alive or dead (topological level required)
int Vect_read_next_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
Read next vector feature.