GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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()
23 {
24  G_warning("Vect_read_line() %s",
25  _("for this format/level not supported"));
26  return -1;
27 }
28 
29 #if !defined HAVE_OGR || !defined HAVE_POSTGRES
30 static int format()
31 {
32  G_fatal_error(_("Requested format is not compiled in this version"));
33  return 0;
34 }
35 #endif
36 
37 static int (*Read_next_line_array[][3]) () = {
38  {
40 #ifdef HAVE_OGR
41  , {
43  , {
44  read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr}
45 #else
46  , {
47  read_dummy, format, format}
48  , {
49  read_dummy, format, format}
50 #endif
51 #ifdef HAVE_POSTGRES
52  , {
54 #else
55  , {
56  read_dummy, format, format}
57 #endif
58 };
59 
60 static int (*Read_line_array[]) () = {
62 #ifdef HAVE_OGR
64  , V2_read_line_sfa
65 #else
66  , format
67  , format
68 #endif
69 #ifdef HAVE_POSTGRES
71 #else
72  , format
73 #endif
74 };
75 
76 
77 /*!
78  \brief Get line id for sequential reading.
79 
80  This function returns id of feature which has been read by calling
81  Vect_read_next_line().
82 
83  \param Map pointer to Map_info struct
84 
85  \return feature id
86  \return -1 on error
87 */
88 int Vect_get_next_line_id(const struct Map_info *Map)
89 {
90  G_debug(3, "Vect_get_next_line()");
91 
92  if (!VECT_OPEN(Map)) {
93  G_warning(_("Vector map is not open for reading"));
94  return -1;
95  }
96 
97  return Map->next_line - 1;
98 }
99 
100 /*!
101  \brief Read next vector feature
102 
103  This function implements sequential access, constraints are
104  reflected, see Vect_set_constraint_region(),
105  Vect_set_constraint_type(), or Vect_set_constraint_field() for
106  details.
107 
108  Use Vect_rewind() to reset reading. Topological level is not
109  required.
110 
111  A warning is printed on failure.
112 
113  \param Map pointer Map_info struct
114  \param[out] line_p feature geometry (pointer to line_pnts struct)
115  \param[out] line_c feature categories (pointer to line_cats struct)
116 
117  \return feature type (GV_POINT, GV_LINE, ...)
118  \return -1 on error
119  \return -2 nothing to read
120  */
121 int Vect_read_next_line(const struct Map_info *Map,
122  struct line_pnts *line_p, struct line_cats *line_c)
123 {
124  int ret;
125 
126  G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);
127 
128  if (!VECT_OPEN(Map)) {
129  G_warning(_("Vector map is not open for reading"));
130  return -1;
131  }
132 
133  ret = (*Read_next_line_array[Map->format][Map->level]) (Map, line_p,
134  line_c);
135  if (ret == -1)
136  G_warning(_("Unable to read feature %d from vector map <%s>"),
137  Map->next_line, Vect_get_full_name(Map));
138 
139  return ret;
140 }
141 
142 /*!
143  \brief Read vector feature (topological level required)
144 
145  This function implements random access. Constraits are ignored.
146 
147  Note: Topology must be built at level >= GV_BUILD_BASE
148 
149  A warning is printed on failure.
150 
151  \param Map pointer to vector map
152  \param[out] line_p feature geometry (pointer to line_pnts struct)
153  \param[out] line_c feature categories (pointer to line_cats struct)
154  \param line feature id (starts at 1)
155 
156  \return feature type
157  \return -1 on failure
158  \return -2 nothing to read
159  */
160 int Vect_read_line(const struct Map_info *Map,
161  struct line_pnts *line_p, struct line_cats *line_c, int line)
162 {
163  int ret;
164 
165  G_debug(3, "Vect_read_line(): line = %d", line);
166 
167  if (!VECT_OPEN(Map)) {
168  G_warning(_("Vector map is not open for reading"));
169  return -1;
170  }
171 
172  if (line < 1 || line > Map->plus.n_lines) {
173  G_warning(_("Attempt to access feature with invalid id (%d)"), line);
174  return -1;
175  }
176 
177  ret = (*Read_line_array[Map->format]) (Map, line_p, line_c, line);
178 
179  if (ret == -1)
180  G_warning(_("Unable to read feature %d from vector map <%s>"),
181  line, Vect_get_full_name(Map));
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  */
197 int Vect_line_alive(const 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  */
221 int Vect_node_alive(const 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 */
245 int Vect_area_alive(const 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 */
269 int Vect_isle_alive(const struct Map_info *Map, int isle)
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 */
295 off_t Vect_get_line_offset(const struct Map_info *Map, int line)
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 }
plus_t n_areas
Current number of areas.
Definition: dig_structs.h:951
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
off_t offset
Offset in coor file for line.
Definition: dig_structs.h:1593
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
struct P_line ** Line
Array of vector geometries.
Definition: dig_structs.h:887
struct P_area ** Area
Array of areas.
Definition: dig_structs.h:891
struct P_node ** Node
Array of nodes.
Definition: dig_structs.h:883
int Vect_isle_alive(const struct Map_info *Map, int isle)
Check if isle is alive or dead (topological level required)
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). t This function implements sequential access.
Definition: read_pg.c:82
int Vect_line_alive(const struct Map_info *Map, int line)
Check if feature is alive or dead (topological level required)
struct P_isle ** Isle
Array of isles.
Definition: dig_structs.h:895
plus_t n_isles
Current number of isles.
Definition: dig_structs.h:955
int level
Topology level.
Definition: dig_structs.h:1313
#define NULL
Definition: ccmath.h:32
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:112
off_t Vect_get_line_offset(const struct Map_info *Map, int line)
Get feature offset (topological level required)
plus_t n_lines
Current number of lines.
Definition: dig_structs.h:947
Feature category info.
Definition: dig_structs.h:1702
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:136
plus_t next_line
Feature id for sequential access.
Definition: dig_structs.h:1353
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
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1286
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 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:319
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:50
int Vect_get_next_line_id(const struct Map_info *Map)
Get line id for sequential reading.
Vector map info.
Definition: dig_structs.h:1259
const char * Vect_get_full_name(const struct Map_info *)
Get fully qualified name of vector map.
int Vect_node_alive(const struct Map_info *Map, int node)
Check if node is alive or dead (topological level required)
void G_warning(const char *,...) __attribute__((format(printf
int Vect_read_next_line(const struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
Read next vector feature.
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:72
#define _(str)
Definition: glocale.h:10
int Vect_read_line(const struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature (topological level required)
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1271
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_area_alive(const struct Map_info *Map, int area)
Check if area is alive or dead (topological level required)
plus_t n_nodes
Current number of topological features derived from vector geometries.
Definition: dig_structs.h:939
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:76