File: read.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (314 lines) | stat: -rw-r--r-- 8,410 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*!
   \file lib/vector/Vlib/read.c

   \brief Vector library - read features

   Higher level functions for reading/writing/manipulating vectors.

   (C) 2001-2009, 2011-2013 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2). Read the file COPYING that comes with GRASS for details.

   \author Original author CERL, probably Dave Gerdes or Mike Higgins.
   \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
   \author Update to GRASS 7 Martin Landa <landa.martin gmail.com>
 */

#include <sys/types.h>
#include <grass/vector.h>
#include <grass/glocale.h>

static int read_dummy(struct Map_info *Map UNUSED,
                      struct line_pnts *line_p UNUSED,
                      struct line_cats *line_c UNUSED)
{
    G_warning("Vect_read_line() %s", _("for this format/level not supported"));
    return -1;
}

#if !defined HAVE_OGR || !defined HAVE_POSTGRES
static int format(struct Map_info *Map UNUSED, struct line_pnts *line_p UNUSED,
                  struct line_cats *line_c UNUSED)
{
    G_fatal_error(_("Requested format is not compiled in this version"));
    return 0;
}

static int format2(struct Map_info *Map UNUSED, struct line_pnts *line_p UNUSED,
                   struct line_cats *line_c UNUSED, int line UNUSED)
{
    G_fatal_error(_("Requested format is not compiled in this version"));
    return 0;
}
#endif

static int (*Read_next_line_array[][3])(struct Map_info *, struct line_pnts *,
                                        struct line_cats *) = {
    {read_dummy, V1_read_next_line_nat, V2_read_next_line_nat}
#ifdef HAVE_OGR
    ,
    {read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr},
    {read_dummy, V1_read_next_line_ogr, V2_read_next_line_ogr}
#else
    ,
    {read_dummy, format, format},
    {read_dummy, format, format}
#endif
#ifdef HAVE_POSTGRES
    ,
    {read_dummy, V1_read_next_line_pg, V2_read_next_line_pg}
#else
    ,
    {read_dummy, format, format}
#endif
};

static int (*Read_line_array[])(struct Map_info *, struct line_pnts *,
                                struct line_cats *, int) = {V2_read_line_nat
#ifdef HAVE_OGR
                                                            ,
                                                            V2_read_line_sfa,
                                                            V2_read_line_sfa
#else
                                                            ,
                                                            format2, format2
#endif
#ifdef HAVE_POSTGRES
                                                            ,
                                                            V2_read_line_pg
#else
                                                            ,
                                                            format2
#endif
};

/*!
   \brief Get line id for sequential reading.

   This function returns id of feature which has been read by calling
   Vect_read_next_line().

   \param Map pointer to Map_info struct

   \return feature id
   \return -1 on error
 */
int Vect_get_next_line_id(struct Map_info *Map)
{
    G_debug(3, "Vect_get_next_line()");

    if (!VECT_OPEN(Map)) {
        G_warning(_("Vector map is not open for reading"));
        return -1;
    }

    return Map->next_line - 1;
}

/*!
   \brief Read next vector feature

   This function implements sequential access, constraints are
   reflected, see Vect_set_constraint_region(),
   Vect_set_constraint_type(), or Vect_set_constraint_field() for
   details.

   Use Vect_rewind() to reset reading. Topological level is not
   required.

   A warning is printed on failure.

   \param Map pointer Map_info struct
   \param[out] line_p feature geometry (pointer to line_pnts struct)
   \param[out] line_c feature categories (pointer to line_cats struct)

   \return feature type (GV_POINT, GV_LINE, ...)
   \return -1 on error
   \return -2 nothing to read
 */
int Vect_read_next_line(struct Map_info *Map, struct line_pnts *line_p,
                        struct line_cats *line_c)
{
    int ret;

    G_debug(3, "Vect_read_next_line(): next_line = %d", Map->next_line);

    if (!VECT_OPEN(Map)) {
        G_warning(_("Vector map is not open for reading"));
        return -1;
    }

    ret = (*Read_next_line_array[Map->format][Map->level])(Map, line_p, line_c);
    if (ret == -1)
        G_warning(_("Unable to read feature %d from vector map <%s>"),
                  Map->next_line, Vect_get_full_name(Map));

    return ret;
}

/*!
   \brief Read vector feature (topological level required)

   This function implements random access. Constraints are ignored.

   Note: Topology must be built at level >= GV_BUILD_BASE

   A warning is printed on failure.

   \param Map pointer to vector map
   \param[out] line_p feature geometry (pointer to line_pnts struct)
   \param[out] line_c feature categories (pointer to line_cats struct)
   \param line feature id (starts at 1)

   \return feature type
   \return -1 on failure
   \return -2 nothing to read
 */
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p,
                   struct line_cats *line_c, int line)
{
    int ret;

    G_debug(3, "Vect_read_line(): line = %d", line);

    if (!VECT_OPEN(Map)) {
        G_warning(_("Vector map is not open for reading"));
        return -1;
    }

    if (line < 1 || line > Map->plus.n_lines) {
        G_warning(_("Attempt to access feature with invalid id (%d)"), line);
        return -1;
    }

    ret = (*Read_line_array[Map->format])(Map, line_p, line_c, line);

    if (ret == -1)
        G_warning(_("Unable to read feature %d from vector map <%s>"), line,
                  Vect_get_full_name(Map));

    return ret;
}

/*!
   \brief Check if feature is alive or dead (topological level required)

   Note: Topology must be built at level >= GV_BUILD_BASE

   \param Map pointer to Map_info structure
   \param line feature id

   \return 1 feature alive
   \return 0 feature is dead or index is out of range
 */
int Vect_line_alive(struct Map_info *Map, int line)
{
    if (line < 1 || line > Map->plus.n_lines) {
        G_warning(_("Line index is out of range"));
        return 0;
    }

    if (Map->plus.Line[line] != NULL)
        return 1;

    return 0;
}

/*!
   \brief Check if node is alive or dead (topological level required)

   Note: Topology must be built at level >= GV_BUILD_BASE

   \param Map pointer to Map_info structure
   \param node node id

   \return 1 node alive
   \return 0 node is dead or index is out of range
 */
int Vect_node_alive(struct Map_info *Map, int node)
{
    if (node < 1 || node > Map->plus.n_nodes) {
        G_warning(_("Node index is out of range"));
        return 0;
    }

    if (Map->plus.Node[node] != NULL)
        return 1;

    return 0;
}

/*!
   \brief Check if area is alive or dead (topological level required)

   Note: Topology must be built at level >= GV_BUILD_AREAS

   \param Map pointer to Map_info structure
   \param area area id

   \return 1 area alive
   \return 0 area is dead or index is out of range
 */
int Vect_area_alive(struct Map_info *Map, int area)
{
    if (area < 1 || area > Map->plus.n_areas) {
        G_warning(_("Area index is out of range"));
        return 0;
    }

    if (Map->plus.Area[area] != NULL)
        return 1;

    return 0;
}

/*!
   \brief Check if isle is alive or dead (topological level required)

   Note: Topology must be built at level >= GV_BUILD_AREAS

   \param Map pointer to Map_info structure
   \param isle isle id

   \return 1 isle alive
   \return 0 isle is dead or index is out of range
 */
int Vect_isle_alive(struct Map_info *Map, int isle)
{
    if (isle < 1 || isle > Map->plus.n_isles) {
        G_warning(_("Isle index is out of range"));
        return 0;
    }

    if (Map->plus.Isle[isle] != NULL)
        return 1;

    return 0;
}

/*!
   \brief Get feature offset (topological level required)

   Note: Topology must be built at level >= GV_BUILD_BASE

   Used for Vect_restore_line().

   \param Map pointer to Map_info structure
   \param line feature id

   \return feature offset
   \return -1 on error
 */
off_t Vect_get_line_offset(struct Map_info *Map, int line)
{
    if (line < 1 || line > Map->plus.n_lines) {
        return -1;
    }

    if (Map->plus.Line[line] != NULL) {
        return Map->plus.Line[line]->offset;
    }

    return -1;
}