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