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