GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
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 UNUSED, int type UNUSED,
30 const struct line_pnts *points UNUSED,
31 const struct line_cats *cats 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 UNUSED, off_t line UNUSED,
38 int type UNUSED,
39 const struct line_pnts *points UNUSED,
40 const struct line_cats *cats 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 UNUSED, off_t line 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 UNUSED, off_t offset UNUSED,
55 off_t line 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 UNUSED, off_t line 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 UNUSED, off_t offset UNUSED,
70 off_t line 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 UNUSED, int type UNUSED,
77 const struct line_pnts *points UNUSED,
78 const struct line_cats *cats 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 UNUSED, off_t line UNUSED,
85 int type UNUSED, const struct line_pnts *points UNUSED,
86 const struct line_cats *cats UNUSED)
87{
88 G_fatal_error(_("Requested format is not compiled in this version"));
89 return 0;
90}
91#endif
92
93static off_t (*Vect_write_line_array[][3])(struct Map_info *, int,
94 const struct line_pnts *,
95 const struct line_cats *) = {
99#ifdef HAVE_POSTGRES
100 ,
101 {write_dummy, V1_write_line_pg, V2_write_line_pg}
102#else
103 ,
104 {write_dummy, format_l, format_l}
105#endif
106};
107
108static off_t (*Vect_rewrite_line_array[][3])(struct Map_info *, off_t, int,
109 const struct line_pnts *,
110 const struct line_cats *) = {
114#ifdef HAVE_POSTGRES
115 ,
116 {rewrite_dummy, V1_rewrite_line_pg, V2_rewrite_line_pg}
117#else
118 ,
119 {rewrite_dummy, format_l2, format_l2}
120#endif
121};
122
123static int (*Vect_delete_line_array[][3])(struct Map_info *, off_t) = {
124 {delete_dummy, V1_delete_line_nat, V2_delete_line_nat},
125 {delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa},
127#ifdef HAVE_POSTGRES
128 ,
129 {delete_dummy, V1_delete_line_pg, V2_delete_line_pg}
130#else
131 ,
132 {delete_dummy, format, format}
133#endif
134};
135
136static int (*Vect_restore_line_array[][3])(struct Map_info *, off_t, off_t) = {
138 {restore_dummy, restore_dummy, restore_dummy},
139 {restore_dummy, restore_dummy, restore_dummy}
140#ifdef HAVE_POSTGRES
141 ,
142 {restore_dummy, restore_dummy, restore_dummy}
143#else
144 ,
145 {restore_dummy, format2, format2}
146#endif
147};
148
149static int check_map(struct Map_info *);
150
151/*!
152 \brief Writes a new feature
153
154 New feature is written to the end of file (in the case of native
155 format). Topological level is not required.
156
157 A warning is printed on error.
158
159 \param Map pointer to Map_info structure
160 \param type feature type (see dig_defines.h for supported types)
161 \param points pointer to line_pnts structure (feature geometry)
162 \param cats pointer to line_cats structure (feature categories)
163
164 \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
165 \return offset into file where the feature starts (on level 1)
166 \return -1 on error
167 */
169 const struct line_pnts *points,
170 const struct line_cats *cats)
171{
172 off_t offset;
173
174 G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
175 Map->name, Map->format, Map->level);
176
177 if (!check_map(Map))
178 return -1;
179
180 offset = (*Vect_write_line_array[Map->format][Map->level])(Map, type,
181 points, cats);
182
183 if (offset < 0)
184 G_warning(_("Unable to write feature in vector map <%s>"),
186
187 return offset;
188}
189
190/*!
191 \brief Rewrites existing feature (topological level required)
192
193 Note: Topology must be built at level >= GV_BUILD_BASE
194
195 A warning is printed on error.
196
197 The number of points or cats or type may change. If necessary, the
198 old feature is deleted and new is written.
199
200 \param Map pointer to Map_info structure
201 \param line feature id (level 2) or feature offset (level 1)
202 \param type feature type (GV_POINT, GV_LINE, ...)
203 \param points feature geometry
204 \param cats feature categories
205
206 \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
207 \return offset into file where the feature starts (on level 1)
208 \return -1 on error
209 */
211 const struct line_pnts *points,
212 const struct line_cats *cats)
213{
214 off_t ret;
215
216 G_debug(3,
217 "Vect_rewrite_line(): name = %s, format = %d, level = %d, "
218 "line/offset = %" PRId64,
219 Map->name, Map->format, Map->level, line);
220
221 if (!check_map(Map))
222 return -1;
223
224 ret = (*Vect_rewrite_line_array[Map->format][Map->level])(Map, line, type,
225 points, cats);
226 if (ret == -1)
227 G_warning(_("Unable to rewrite feature/offset %" PRId64
228 " in vector map <%s>"),
229 line, Vect_get_name(Map));
230
231 return ret;
232}
233
234/*!
235 \brief Delete existing feature (topological level required)
236
237 Note: Topology must be built at level >= GV_BUILD_BASE
238
239 A warning is printed on error.
240
241 \param Map pointer to Map_info structure
242 \param line feature id (level 2) or feature offset (level 1)
243
244 \return 0 on success
245 \return -1 on error
246 */
248{
249 int ret;
250
251 G_debug(3, "Vect_delete_line(): name = %s, line/offset = %" PRId64,
252 Map->name, line);
253
254 if (!check_map(Map))
255 return -1;
256
257 ret = (*Vect_delete_line_array[Map->format][Map->level])(Map, line);
258
259 if (ret == -1)
260 G_warning(_("Unable to delete feature/offset %" PRId64
261 " from vector map <%s>"),
262 line, Vect_get_name(Map));
263
264 return ret;
265}
266
267/*!
268 \brief Restore previously deleted feature (topological level required)
269
270 Note: Topology must be built at level >= GV_BUILD_BASE
271
272 A warning is printed on error.
273
274 \param Map pointer to Map_info structure
275 \param offset feature offset to be restored
276 \param line feature id to be restored (used only on level 2)
277
278 \return 0 on success
279 \return -1 on error
280 */
281int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
282{
283 int ret;
284
285 G_debug(3,
286 "Vect_restore_line(): name = %s, level = %d, offset = %" PRId64
287 ", line = %" PRId64,
288 Map->name, Map->level, offset, line);
289
290 if (!check_map(Map))
291 return -1;
292
293 ret =
294 (*Vect_restore_line_array[Map->format][Map->level])(Map, offset, line);
295
296 if (ret == -1)
297 G_warning(_("Unable to restore feature/offset %" PRId64
298 " in vector map <%s>"),
299 offset, Vect_get_name(Map));
300
301 return ret;
302}
303
304int check_map(struct Map_info *Map)
305{
306 if (!VECT_OPEN(Map)) {
307 G_warning(_("Vector map <%s> is not opened"), Vect_get_name(Map));
308 return 0;
309 }
310
311 if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
312 G_warning(_("Vector map <%s> is not opened in write mode"),
314 return 0;
315 }
316
317 return 1;
318}
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 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)