GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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  G_warning(_("Unable to read feature %d from vector map <%s>"),
145  Map->next_line, Vect_get_full_name(Map));
146 
147  return ret;
148 }
149 
150 /*!
151  \brief Read vector feature (topological level required)
152 
153  This function implements random access. Constraints are ignored.
154 
155  Note: Topology must be built at level >= GV_BUILD_BASE
156 
157  A warning is printed on failure.
158 
159  \param Map pointer to vector map
160  \param[out] line_p feature geometry (pointer to line_pnts struct)
161  \param[out] line_c feature categories (pointer to line_cats struct)
162  \param line feature id (starts at 1)
163 
164  \return feature type
165  \return -1 on failure
166  \return -2 nothing to read
167  */
168 int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p,
169  struct line_cats *line_c, int line)
170 {
171  int ret;
172 
173  G_debug(3, "Vect_read_line(): line = %d", line);
174 
175  if (!VECT_OPEN(Map)) {
176  G_warning(_("Vector map is not open for reading"));
177  return -1;
178  }
179 
180  if (line < 1 || line > Map->plus.n_lines) {
181  G_warning(_("Attempt to access feature with invalid id (%d)"), line);
182  return -1;
183  }
184 
185  ret = (*Read_line_array[Map->format])(Map, line_p, line_c, line);
186 
187  if (ret == -1)
188  G_warning(_("Unable to read feature %d from vector map <%s>"), line,
189  Vect_get_full_name(Map));
190 
191  return ret;
192 }
193 
194 /*!
195  \brief Check if feature is alive or dead (topological level required)
196 
197  Note: Topology must be built at level >= GV_BUILD_BASE
198 
199  \param Map pointer to Map_info structure
200  \param line feature id
201 
202  \return 1 feature alive
203  \return 0 feature is dead or index is out of range
204  */
205 int Vect_line_alive(struct Map_info *Map, int line)
206 {
207  if (line < 1 || line > Map->plus.n_lines) {
208  G_warning(_("Line index is out of range"));
209  return 0;
210  }
211 
212  if (Map->plus.Line[line] != NULL)
213  return 1;
214 
215  return 0;
216 }
217 
218 /*!
219  \brief Check if node is alive or dead (topological level required)
220 
221  Note: Topology must be built at level >= GV_BUILD_BASE
222 
223  \param Map pointer to Map_info structure
224  \param node node id
225 
226  \return 1 node alive
227  \return 0 node is dead or index is out of range
228  */
229 int Vect_node_alive(struct Map_info *Map, int node)
230 {
231  if (node < 1 || node > Map->plus.n_nodes) {
232  G_warning(_("Node index is out of range"));
233  return 0;
234  }
235 
236  if (Map->plus.Node[node] != NULL)
237  return 1;
238 
239  return 0;
240 }
241 
242 /*!
243  \brief Check if area is alive or dead (topological level required)
244 
245  Note: Topology must be built at level >= GV_BUILD_AREAS
246 
247  \param Map pointer to Map_info structure
248  \param area area id
249 
250  \return 1 area alive
251  \return 0 area is dead or index is out of range
252  */
253 int Vect_area_alive(struct Map_info *Map, int area)
254 {
255  if (area < 1 || area > Map->plus.n_areas) {
256  G_warning(_("Area index is out of range"));
257  return 0;
258  }
259 
260  if (Map->plus.Area[area] != NULL)
261  return 1;
262 
263  return 0;
264 }
265 
266 /*!
267  \brief Check if isle is alive or dead (topological level required)
268 
269  Note: Topology must be built at level >= GV_BUILD_AREAS
270 
271  \param Map pointer to Map_info structure
272  \param isle isle id
273 
274  \return 1 isle alive
275  \return 0 isle is dead or index is out of range
276  */
277 int Vect_isle_alive(struct Map_info *Map, int isle)
278 {
279  if (isle < 1 || isle > Map->plus.n_isles) {
280  G_warning(_("Isle index is out of range"));
281  return 0;
282  }
283 
284  if (Map->plus.Isle[isle] != NULL)
285  return 1;
286 
287  return 0;
288 }
289 
290 /*!
291  \brief Get feature offset (topological level required)
292 
293  Note: Topology must be built at level >= GV_BUILD_BASE
294 
295  Used for Vect_restore_line().
296 
297  \param Map pointer to Map_info structure
298  \param line feature id
299 
300  \return feature offset
301  \return -1 on error
302  */
303 off_t Vect_get_line_offset(struct Map_info *Map, int line)
304 {
305  if (line < 1 || line > Map->plus.n_lines) {
306  return -1;
307  }
308 
309  if (Map->plus.Line[line] != NULL) {
310  return Map->plus.Line[line]->offset;
311  }
312 
313  return -1;
314 }
#define NULL
Definition: ccmath.h:32
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:47
#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.