GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-60e3ce7357
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 
22 static int read_dummy(struct Map_info *Map UNUSED,
23  struct line_pnts *line_p UNUSED,
24  struct line_cats *line_c UNUSED)
25 {
26  G_warning("Vect_read_line() %s", _("for this format/level not supported"));
27  return -1;
28 }
29 
30 #if !defined HAVE_OGR || !defined HAVE_POSTGRES
31 static int format(struct Map_info *Map UNUSED, struct line_pnts *line_p UNUSED,
32  struct line_cats *line_c UNUSED)
33 {
34  G_fatal_error(_("Requested format is not compiled in this version"));
35  return 0;
36 }
37 
38 static int format2(struct Map_info *Map UNUSED, struct line_pnts *line_p UNUSED,
39  struct line_cats *line_c UNUSED, int line UNUSED)
40 {
41  G_fatal_error(_("Requested format is not compiled in this version"));
42  return 0;
43 }
44 #endif
45 
46 static int (*Read_next_line_array[][3])(struct Map_info *, struct line_pnts *,
47  struct line_cats *) = {
49 #ifdef HAVE_OGR
50  ,
53 #else
54  ,
55  {read_dummy, format, format},
56  {read_dummy, format, format}
57 #endif
58 #ifdef HAVE_POSTGRES
59  ,
61 #else
62  ,
63  {read_dummy, format, format}
64 #endif
65 };
66 
67 static int (*Read_line_array[])(struct Map_info *, struct line_pnts *,
68  struct line_cats *, int) = {V2_read_line_nat
69 #ifdef HAVE_OGR
70  ,
73 #else
74  ,
75  format2, format2
76 #endif
77 #ifdef HAVE_POSTGRES
78  ,
80 #else
81  ,
82  format2
83 #endif
84 };
85 
86 /*!
87  \brief Get line id for sequential reading.
88 
89  This function returns id of feature which has been read by calling
90  Vect_read_next_line().
91 
92  \param Map pointer to Map_info struct
93 
94  \return feature id
95  \return -1 on error
96  */
98 {
99  G_debug(3, "Vect_get_next_line()");
100 
101  if (!VECT_OPEN(Map)) {
102  G_warning(_("Vector map is not open for reading"));
103  return -1;
104  }
105 
106  return Map->next_line - 1;
107 }
108 
109 /*!
110  \brief Read next vector feature
111 
112  This function implements sequential access, constraints are
113  reflected, see Vect_set_constraint_region(),
114  Vect_set_constraint_type(), or Vect_set_constraint_field() for
115  details.
116 
117  Use Vect_rewind() to reset reading. Topological level is not
118  required.
119 
120  A warning is printed on failure.
121 
122  \param Map pointer Map_info struct
123  \param[out] line_p feature geometry (pointer to line_pnts struct)
124  \param[out] line_c feature categories (pointer to line_cats struct)
125 
126  \return feature type (GV_POINT, GV_LINE, ...)
127  \return -1 on error
128  \return -2 nothing to read
129  */
130 int Vect_read_next_line(struct Map_info *Map, struct line_pnts *line_p,
131  struct line_cats *line_c)
132 {
133  int ret;
134 
135  G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);
136 
137  if (!VECT_OPEN(Map)) {
138  G_warning(_("Vector map is not open for reading"));
139  return -1;
140  }
141 
142  ret = (*Read_next_line_array[Map->format][Map->level])(Map, line_p, line_c);
143  if (ret == -1) {
144  const char *map_name = Vect_get_full_name(Map);
145  G_warning(_("Unable to read feature %d from vector map <%s>"),
146  Map->next_line, map_name);
147  G_free((void *)map_name);
148  }
149 
150  return ret;
151 }
152 
153 /*!
154  \brief Read vector feature (topological level required)
155 
156  This function implements random access. Constraints are ignored.
157 
158  Note: Topology must be built at level >= GV_BUILD_BASE
159 
160  A warning is printed on failure.
161 
162  \param Map pointer to vector map
163  \param[out] line_p feature geometry (pointer to line_pnts struct)
164  \param[out] line_c feature categories (pointer to line_cats struct)
165  \param line feature id (starts at 1)
166 
167  \return feature type
168  \return -1 on failure
169  \return -2 nothing to read
170  */
171 int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p,
172  struct line_cats *line_c, int line)
173 {
174  int ret;
175 
176  G_debug(3, "Vect_read_line(): line = %d", line);
177 
178  if (!VECT_OPEN(Map)) {
179  G_warning(_("Vector map is not open for reading"));
180  return -1;
181  }
182 
183  if (line < 1 || line > Map->plus.n_lines) {
184  G_warning(_("Attempt to access feature with invalid id (%d)"), line);
185  return -1;
186  }
187 
188  ret = (*Read_line_array[Map->format])(Map, line_p, line_c, line);
189 
190  if (ret == -1) {
191  const char *map_name = Vect_get_full_name(Map);
192  G_warning(_("Unable to read feature %d from vector map <%s>"), line,
193  map_name);
194  G_free((void *)map_name);
195  }
196 
197  return ret;
198 }
199 
200 /*!
201  \brief Check if feature is alive or dead (topological level required)
202 
203  Note: Topology must be built at level >= GV_BUILD_BASE
204 
205  \param Map pointer to Map_info structure
206  \param line feature id
207 
208  \return 1 feature alive
209  \return 0 feature is dead or index is out of range
210  */
211 int Vect_line_alive(struct Map_info *Map, int line)
212 {
213  if (line < 1 || line > Map->plus.n_lines) {
214  G_warning(_("Line index is out of range"));
215  return 0;
216  }
217 
218  if (Map->plus.Line[line] != NULL)
219  return 1;
220 
221  return 0;
222 }
223 
224 /*!
225  \brief Check if node is alive or dead (topological level required)
226 
227  Note: Topology must be built at level >= GV_BUILD_BASE
228 
229  \param Map pointer to Map_info structure
230  \param node node id
231 
232  \return 1 node alive
233  \return 0 node is dead or index is out of range
234  */
235 int Vect_node_alive(struct Map_info *Map, int node)
236 {
237  if (node < 1 || node > Map->plus.n_nodes) {
238  G_warning(_("Node index is out of range"));
239  return 0;
240  }
241 
242  if (Map->plus.Node[node] != NULL)
243  return 1;
244 
245  return 0;
246 }
247 
248 /*!
249  \brief Check if area is alive or dead (topological level required)
250 
251  Note: Topology must be built at level >= GV_BUILD_AREAS
252 
253  \param Map pointer to Map_info structure
254  \param area area id
255 
256  \return 1 area alive
257  \return 0 area is dead or index is out of range
258  */
259 int Vect_area_alive(struct Map_info *Map, int area)
260 {
261  if (area < 1 || area > Map->plus.n_areas) {
262  G_warning(_("Area index is out of range"));
263  return 0;
264  }
265 
266  if (Map->plus.Area[area] != NULL)
267  return 1;
268 
269  return 0;
270 }
271 
272 /*!
273  \brief Check if isle is alive or dead (topological level required)
274 
275  Note: Topology must be built at level >= GV_BUILD_AREAS
276 
277  \param Map pointer to Map_info structure
278  \param isle isle id
279 
280  \return 1 isle alive
281  \return 0 isle is dead or index is out of range
282  */
283 int Vect_isle_alive(struct Map_info *Map, int isle)
284 {
285  if (isle < 1 || isle > Map->plus.n_isles) {
286  G_warning(_("Isle index is out of range"));
287  return 0;
288  }
289 
290  if (Map->plus.Isle[isle] != NULL)
291  return 1;
292 
293  return 0;
294 }
295 
296 /*!
297  \brief Get feature offset (topological level required)
298 
299  Note: Topology must be built at level >= GV_BUILD_BASE
300 
301  Used for Vect_restore_line().
302 
303  \param Map pointer to Map_info structure
304  \param line feature id
305 
306  \return feature offset
307  \return -1 on error
308  */
309 off_t Vect_get_line_offset(struct Map_info *Map, int line)
310 {
311  if (line < 1 || line > Map->plus.n_lines) {
312  return -1;
313  }
314 
315  if (Map->plus.Line[line] != NULL) {
316  return Map->plus.Line[line]->offset;
317  }
318 
319  return -1;
320 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
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:136
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:178
int V1_read_next_line_pg(struct Map_info *, struct line_pnts *, struct line_cats *)
int V2_read_line_pg(struct Map_info *, struct line_pnts *, struct line_cats *, int)
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
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 *)
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:49
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:75
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.
Definition: dig_defines.h:137
#define 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.
Definition: dig_structs.h:1243
int level
Topology level.
Definition: dig_structs.h:1297
plus_t next_line
Feature id for sequential access.
Definition: dig_structs.h:1338
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1255
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1270
off_t offset
Offset in coor file for line.
Definition: dig_structs.h:1571
struct P_line ** Line
Array of vector geometries.
Definition: dig_structs.h:871
plus_t n_lines
Current number of lines.
Definition: dig_structs.h:931
plus_t n_nodes
Current number of topological features derived from vector geometries.
Definition: dig_structs.h:923
struct P_area ** Area
Array of areas.
Definition: dig_structs.h:875
plus_t n_isles
Current number of isles.
Definition: dig_structs.h:939
struct P_isle ** Isle
Array of isles.
Definition: dig_structs.h:879
struct P_node ** Node
Array of nodes.
Definition: dig_structs.h:867
plus_t n_areas
Current number of areas.
Definition: dig_structs.h:935
Feature category info.
Definition: dig_structs.h:1677
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
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.