GRASS 8 Programmer's Manual 8.6.0dev(2026)-5a31d549cc
Loading...
Searching...
No Matches
vector/Vlib/write.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/write.c
3
4 \brief Vector library - write vector features
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 Supported operations:
9 - Write a new feature
10 - Rewrite existing feature
11 - Delete existing feature
12 - Restore deleted feature
13
14 (C) 2001-2010, 2012-2013 by the GRASS Development Team
15
16 This program is free software under the GNU General Public License
17 (>=v2). Read the file COPYING that comes with GRASS for details.
18
19 \author Radim Blazek
20 \author Updated by Martin Landa <landa.martin gmail.com> (restore lines, OGR
21 & PostGIS support)
22 */
23
24#include <inttypes.h>
25#include <sys/types.h>
26#include <grass/glocale.h>
27#include <grass/vector.h>
28
29static off_t write_dummy(struct Map_info *Map G_UNUSED, int type G_UNUSED,
30 const struct line_pnts *points G_UNUSED,
31 const struct line_cats *cats G_UNUSED)
32{
33 G_warning("Vect_write_line() %s", _("for this format/level not supported"));
34 return -1;
35}
36
37static off_t rewrite_dummy(struct Map_info *Map G_UNUSED, off_t line G_UNUSED,
38 int type G_UNUSED,
39 const struct line_pnts *points G_UNUSED,
40 const struct line_cats *cats G_UNUSED)
41{
42 G_warning("Vect_rewrite_line() %s",
43 _("for this format/level not supported"));
44 return -1;
45}
46
47static int delete_dummy(struct Map_info *Map G_UNUSED, off_t line G_UNUSED)
48{
49 G_warning("Vect_delete_line() %s",
50 _("for this format/level not supported"));
51 return -1;
52}
53
54static int restore_dummy(struct Map_info *Map G_UNUSED, off_t offset G_UNUSED,
55 off_t line G_UNUSED)
56{
57 G_warning("Vect_restore_line() %s",
58 _("for this format/level not supported"));
59 return -1;
60}
61
62#if !defined HAVE_POSTGRES
63static int format(struct Map_info *Map G_UNUSED, off_t line G_UNUSED)
64{
65 G_fatal_error(_("Requested format is not compiled in this version"));
66 return 0;
67}
68
69static int format2(struct Map_info *Map G_UNUSED, off_t offset G_UNUSED,
70 off_t line G_UNUSED)
71{
72 G_fatal_error(_("Requested format is not compiled in this version"));
73 return 0;
74}
75
76static off_t format_l(struct Map_info *Map G_UNUSED, int type G_UNUSED,
77 const struct line_pnts *points G_UNUSED,
78 const struct line_cats *cats G_UNUSED)
79{
80 G_fatal_error(_("Requested format is not compiled in this version"));
81 return 0;
82}
83
84static off_t format_l2(struct Map_info *Map G_UNUSED, off_t line G_UNUSED,
85 int type G_UNUSED,
86 const struct line_pnts *points G_UNUSED,
87 const struct line_cats *cats G_UNUSED)
88{
89 G_fatal_error(_("Requested format is not compiled in this version"));
90 return 0;
91}
92#endif
93
94static off_t (*Vect_write_line_array[][3])(struct Map_info *, int,
95 const struct line_pnts *,
96 const struct line_cats *) = {
100#ifdef HAVE_POSTGRES
101 ,
102 {write_dummy, V1_write_line_pg, V2_write_line_pg}
103#else
104 ,
105 {write_dummy, format_l, format_l}
106#endif
107};
108
109static off_t (*Vect_rewrite_line_array[][3])(struct Map_info *, off_t, int,
110 const struct line_pnts *,
111 const struct line_cats *) = {
115#ifdef HAVE_POSTGRES
116 ,
117 {rewrite_dummy, V1_rewrite_line_pg, V2_rewrite_line_pg}
118#else
119 ,
120 {rewrite_dummy, format_l2, format_l2}
121#endif
122};
123
124static int (*Vect_delete_line_array[][3])(struct Map_info *, off_t) = {
125 {delete_dummy, V1_delete_line_nat, V2_delete_line_nat},
126 {delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa},
128#ifdef HAVE_POSTGRES
129 ,
130 {delete_dummy, V1_delete_line_pg, V2_delete_line_pg}
131#else
132 ,
133 {delete_dummy, format, format}
134#endif
135};
136
137static int (*Vect_restore_line_array[][3])(struct Map_info *, off_t, off_t) = {
139 {restore_dummy, restore_dummy, restore_dummy},
140 {restore_dummy, restore_dummy, restore_dummy}
141#ifdef HAVE_POSTGRES
142 ,
143 {restore_dummy, restore_dummy, restore_dummy}
144#else
145 ,
146 {restore_dummy, format2, format2}
147#endif
148};
149
150static int check_map(struct Map_info *);
151
152/*!
153 \brief Writes a new feature
154
155 New feature is written to the end of file (in the case of native
156 format). Topological level is not required.
157
158 A warning is printed on error.
159
160 \param Map pointer to Map_info structure
161 \param type feature type (see dig_defines.h for supported types)
162 \param points pointer to line_pnts structure (feature geometry)
163 \param cats pointer to line_cats structure (feature categories)
164
165 \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
166 \return offset into file where the feature starts (on level 1)
167 \return -1 on error
168 */
170 const struct line_pnts *points,
171 const struct line_cats *cats)
172{
173 off_t offset;
174
175 G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
176 Map->name, Map->format, Map->level);
177
178 if (!check_map(Map))
179 return -1;
180
181 offset = (*Vect_write_line_array[Map->format][Map->level])(Map, type,
182 points, cats);
183
184 if (offset < 0)
185 G_warning(_("Unable to write feature in vector map <%s>"),
187
188 return offset;
189}
190
191/*!
192 \brief Rewrites existing feature (topological level required)
193
194 Note: Topology must be built at level >= GV_BUILD_BASE
195
196 A warning is printed on error.
197
198 The number of points or cats or type may change. If necessary, the
199 old feature is deleted and new is written.
200
201 \param Map pointer to Map_info structure
202 \param line feature id (level 2) or feature offset (level 1)
203 \param type feature type (GV_POINT, GV_LINE, ...)
204 \param points feature geometry
205 \param cats feature categories
206
207 \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
208 \return offset into file where the feature starts (on level 1)
209 \return -1 on error
210 */
212 const struct line_pnts *points,
213 const struct line_cats *cats)
214{
215 off_t ret;
216
217 G_debug(3,
218 "Vect_rewrite_line(): name = %s, format = %d, level = %d, "
219 "line/offset = %" PRId64,
220 Map->name, Map->format, Map->level, line);
221
222 if (!check_map(Map))
223 return -1;
224
225 ret = (*Vect_rewrite_line_array[Map->format][Map->level])(Map, line, type,
226 points, cats);
227 if (ret == -1)
228 G_warning(_("Unable to rewrite feature/offset %" PRId64
229 " in vector map <%s>"),
230 line, Vect_get_name(Map));
231
232 return ret;
233}
234
235/*!
236 \brief Delete existing feature (topological level required)
237
238 Note: Topology must be built at level >= GV_BUILD_BASE
239
240 A warning is printed on error.
241
242 \param Map pointer to Map_info structure
243 \param line feature id (level 2) or feature offset (level 1)
244
245 \return 0 on success
246 \return -1 on error
247 */
249{
250 int ret;
251
252 G_debug(3, "Vect_delete_line(): name = %s, line/offset = %" PRId64,
253 Map->name, line);
254
255 if (!check_map(Map))
256 return -1;
257
258 ret = (*Vect_delete_line_array[Map->format][Map->level])(Map, line);
259
260 if (ret == -1)
261 G_warning(_("Unable to delete feature/offset %" PRId64
262 " from vector map <%s>"),
263 line, Vect_get_name(Map));
264
265 return ret;
266}
267
268/*!
269 \brief Restore previously deleted feature (topological level required)
270
271 Note: Topology must be built at level >= GV_BUILD_BASE
272
273 A warning is printed on error.
274
275 \param Map pointer to Map_info structure
276 \param offset feature offset to be restored
277 \param line feature id to be restored (used only on level 2)
278
279 \return 0 on success
280 \return -1 on error
281 */
282int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
283{
284 int ret;
285
286 G_debug(3,
287 "Vect_restore_line(): name = %s, level = %d, offset = %" PRId64
288 ", line = %" PRId64,
289 Map->name, Map->level, offset, line);
290
291 if (!check_map(Map))
292 return -1;
293
294 ret =
295 (*Vect_restore_line_array[Map->format][Map->level])(Map, offset, line);
296
297 if (ret == -1)
298 G_warning(_("Unable to restore feature/offset %" PRId64
299 " in vector map <%s>"),
300 offset, Vect_get_name(Map));
301
302 return ret;
303}
304
305int check_map(struct Map_info *Map)
306{
307 if (!VECT_OPEN(Map)) {
308 G_warning(_("Vector map <%s> is not opened"), Vect_get_name(Map));
309 return 0;
310 }
311
312 if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
313 G_warning(_("Vector map <%s> is not opened in write mode"),
315 return 0;
316 }
317
318 return 1;
319}
AMI_err name(char **stream_name)
Definition ami_stream.h:426
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
off_t V1_rewrite_line_ogr(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature at the given offset on level 1 (OGR interface)
Definition write_ogr.c:82
off_t V2_write_line_sfa(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature on level 2 (OGR/PostGIS interface, pseudo-topological level)
Definition write_sfa.c:50
int V1_delete_line_pg(struct Map_info *, off_t)
Deletes feature at the given offset (level 1)
Definition write_pg.c:324
off_t V1_write_line_nat(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature to 'coor' file at level 1 (internal use only)
Definition write_nat.c:45
off_t V1_write_line_ogr(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature on level 1 (OGR interface)
Definition write_ogr.c:61
int V2_delete_line_pg(struct Map_info *, off_t)
Deletes feature on topological level (PostGIS interface)
Definition write_pg.c:387
off_t V2_write_line_pg(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature on topological level (PostGIS interface)
Definition write_pg.c:152
const char * Vect_get_name(struct Map_info *)
Get name of vector map.
off_t V2_rewrite_line_sfa(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature at the given offset on level 2 (OGR/PostGIS interface, pseudo-topological level)
Definition write_sfa.c:145
off_t V2_write_line_nat(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature to 'coor' file at topological level (internal use only)
Definition write_nat.c:67
int V2_delete_line_nat(struct Map_info *, off_t)
Deletes feature at topological level (internal use only)
Definition write_nat.c:290
int V1_restore_line_nat(struct Map_info *, off_t, off_t)
Restores feature at level 1 (internal use only)
Definition write_nat.c:350
int V1_delete_line_ogr(struct Map_info *, off_t)
Deletes feature at the given offset on level 1 (OGR interface)
Definition write_ogr.c:107
off_t V2_rewrite_line_nat(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature to 'coor' file at topological level (internal use only)
Definition write_nat.c:161
off_t V1_rewrite_line_nat(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature to 'coor' file at level 1 (internal use only)
Definition write_nat.c:105
int V2_restore_line_nat(struct Map_info *, off_t, off_t)
Restores feature at topological level (internal use only)
Definition write_nat.c:398
int V1_delete_line_nat(struct Map_info *, off_t)
Deletes feature at level 1 (internal use only)
Definition write_nat.c:248
off_t V2_rewrite_line_pg(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature at topological level (PostGIS interface, internal use only)
Definition write_pg.c:229
int V2_delete_line_sfa(struct Map_info *, off_t)
Deletes feature on level 2 (OGR/PostGIS interface)
Definition write_sfa.c:181
off_t V1_write_line_pg(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature on level 1 (PostGIS interface)
Definition write_pg.c:109
off_t V1_rewrite_line_pg(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature at the given offset (level 1) (PostGIS interface, internal use only)
Definition write_pg.c:190
#define VECT_OPEN(Map)
Check if vector map is open.
#define GV_MODE_WRITE
Write vector map open mode.
#define GV_MODE_RW
Read-write vector map open mode.
#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.
int type
Feature type constraint.
int format
Map format (native, ogr, postgis)
Feature category info.
Feature geometry info - coordinates.
int Vect_delete_line(struct Map_info *Map, off_t line)
Delete existing feature (topological level required)
off_t Vect_write_line(struct Map_info *Map, int type, const struct line_pnts *points, const struct line_cats *cats)
Writes a new feature.
int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
Restore previously deleted feature (topological level required)
off_t Vect_rewrite_line(struct Map_info *Map, off_t line, int type, const struct line_pnts *points, const struct line_cats *cats)
Rewrites existing feature (topological level required)