GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 & PostGIS support)
21  */
22 
23 #include <sys/types.h>
24 #include <grass/glocale.h>
25 #include <grass/vector.h>
26 
27 static off_t write_dummy()
28 {
29  G_warning("Vect_write_line() %s",
30  _("for this format/level not supported"));
31  return -1;
32 }
33 static off_t rewrite_dummy()
34 {
35  G_warning("Vect_rewrite_line() %s",
36  _("for this format/level not supported"));
37  return -1;
38 }
39 static int delete_dummy()
40 {
41  G_warning("Vect_delete_line() %s",
42  _("for this format/level not supported"));
43  return -1;
44 }
45 
46 static int restore_dummy()
47 {
48  G_warning("Vect_restore_line() %s",
49  _("for this format/level not supported"));
50  return -1;
51 }
52 
53 #if !defined HAVE_OGR || !defined HAVE_POSTGRES
54 static int format()
55 {
56  G_fatal_error(_("Requested format is not compiled in this version"));
57  return 0;
58 }
59 
60 static off_t format_l()
61 {
62  G_fatal_error(_("Requested format is not compiled in this version"));
63  return 0;
64 }
65 #endif
66 
67 static off_t (*Vect_write_line_array[][3]) () = {
68  {
70 #ifdef HAVE_OGR
71  , {
73  , {
74  write_dummy, V1_write_line_ogr, V2_write_line_sfa}
75 #else
76  , {
77  write_dummy, format_l, format_l}
78  , {
79  write_dummy, format_l, format_l}
80 #endif
81 #ifdef HAVE_POSTGRES
82  , {
83  write_dummy, V1_write_line_pg, V2_write_line_pg}
84 #else
85  , {
86  write_dummy, format_l, format_l}
87 #endif
88 };
89 
90 static off_t (*Vect_rewrite_line_array[][3]) () = {
91  {
93 #ifdef HAVE_OGR
94  , {
96  , {
97  rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_sfa}
98 #else
99  , {
100  rewrite_dummy, format_l, format_l}
101  , {
102  rewrite_dummy, format_l, format_l}
103 #endif
104 #ifdef HAVE_POSTGRES
105  , {
106  rewrite_dummy, V1_rewrite_line_pg, V2_rewrite_line_pg}
107 #else
108  , {
109  rewrite_dummy, format_l, format_l}
110 #endif
111 };
112 
113 static int (*Vect_delete_line_array[][3]) () = {
114  {
115  delete_dummy, V1_delete_line_nat, V2_delete_line_nat}
116 #ifdef HAVE_OGR
117  , {
118  delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa}
119  , {
120  delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa}
121 #else
122  , {
123  delete_dummy, format, format}
124  , {
125  delete_dummy, format, format}
126 #endif
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 
136 static int (*Vect_restore_line_array[][3]) () = {
137  {
139 #ifdef HAVE_OGR
140  , {
141  restore_dummy, restore_dummy, restore_dummy}
142  , {
143  restore_dummy, restore_dummy, restore_dummy}
144 #else
145  , {
146  restore_dummy, format, format}
147  , {
148  restore_dummy, format, format}
149 #endif
150 #ifdef HAVE_POSTGRES
151  , {
152  restore_dummy, restore_dummy, restore_dummy}
153 #else
154  , {
155  restore_dummy, format, format}
156 #endif
157 };
158 
159 static int check_map(const struct Map_info *);
160 
161 /*!
162  \brief Writes a new feature
163 
164  New feature is written to the end of file (in the case of native
165  format). Topological level is not required.
166 
167  A warning is printed on error.
168 
169  \param Map pointer to Map_info structure
170  \param type feature type (see dig_defines.h for supported types)
171  \param points pointer to line_pnts structure (feature geometry)
172  \param cats pointer to line_cats structure (feature categories)
173 
174  \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
175  \return offset into file where the feature starts (on level 1)
176  \return -1 on error
177  */
178 off_t Vect_write_line(struct Map_info *Map, int type,
179  const struct line_pnts *points, const struct line_cats *cats)
180 {
181  off_t offset;
182 
183  G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
184  Map->name, Map->format, Map->level);
185 
186  if (!check_map(Map))
187  return -1;
188 
189  offset =
190  (*Vect_write_line_array[Map->format][Map->level]) (Map, type,
191  points, cats);
192 
193  if (offset < 0)
194  G_warning(_("Unable to write feature in vector map <%s>"),
195  Vect_get_name(Map));
196 
197  return offset;
198 }
199 
200 /*!
201  \brief Rewrites existing feature (topological level required)
202 
203  Note: Topology must be built at level >= GV_BUILD_BASE
204 
205  A warning is printed on error.
206 
207  The number of points or cats or type may change. If necessary, the
208  old feature is deleted and new is written.
209 
210  \param Map pointer to Map_info structure
211  \param line feature id (level 2) or feature offset (level 1)
212  \param type feature type (GV_POINT, GV_LINE, ...)
213  \param points feature geometry
214  \param cats feature categories
215 
216  \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
217  \return offset into file where the feature starts (on level 1)
218  \return -1 on error
219  */
220 off_t Vect_rewrite_line(struct Map_info *Map, off_t line, int type,
221  const struct line_pnts *points, const struct line_cats *cats)
222 {
223  off_t ret;
224 
225  G_debug(3, "Vect_rewrite_line(): name = %s, format = %d, level = %d, line/offset = %" PRI_OFF_T,
226  Map->name, Map->format, Map->level, line);
227 
228  if (!check_map(Map))
229  return -1;
230 
231  ret = (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type,
232  points, cats);
233  if (ret == -1)
234  G_warning(_("Unable to rewrite feature/offset %" PRI_OFF_T " in vector map <%s>"),
235  line, Vect_get_name(Map));
236 
237  return ret;
238 }
239 
240 /*!
241  \brief Delete existing feature (topological level required)
242 
243  Note: Topology must be built at level >= GV_BUILD_BASE
244 
245  A warning is printed on error.
246 
247  \param Map pointer to Map_info structure
248  \param line feature id (level 2) or feature offset (level 1)
249 
250  \return 0 on success
251  \return -1 on error
252  */
253 int Vect_delete_line(struct Map_info *Map, off_t line)
254 {
255  int ret;
256 
257  G_debug(3, "Vect_delete_line(): name = %s, line/offset = %" PRI_OFF_T,
258  Map->name, line);
259 
260  if (!check_map(Map))
261  return -1;
262 
263  ret = (*Vect_delete_line_array[Map->format][Map->level]) (Map, line);
264 
265  if (ret == -1)
266  G_warning(_("Unable to delete feature/offset %" PRI_OFF_T " from vector map <%s>"),
267  line, Vect_get_name(Map));
268 
269  return ret;
270 }
271 
272 /*!
273  \brief Restore previously deleted feature (topological level required)
274 
275  Note: Topology must be built at level >= GV_BUILD_BASE
276 
277  A warning is printed on error.
278 
279  \param Map pointer to Map_info structure
280  \param offset feature offset to be restored
281  \param line feature id to be restored (used only on level 2)
282 
283  \return 0 on success
284  \return -1 on error
285  */
286 int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
287 {
288  int ret;
289 
290  G_debug(3, "Vect_restore_line(): name = %s, level = %d, offset = %" PRI_OFF_T ", line = %" PRI_OFF_T,
291  Map->name, Map->level, offset, line);
292 
293  if (!check_map(Map))
294  return -1;
295 
296  ret = (*Vect_restore_line_array[Map->format][Map->level]) (Map, offset, line);
297 
298  if (ret == -1)
299  G_warning(_("Unable to restore feature/offset %" PRI_OFF_T " in vector map <%s>"),
300  offset, Vect_get_name(Map));
301 
302  return ret;
303 }
304 
305 int check_map(const 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"),
314  Vect_get_name(Map));
315  return 0;
316  }
317 
318  return 1;
319 }
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int V2_delete_line_pg(struct Map_info *, off_t)
Deletes feature on topological level (PostGIS interface)
Definition: write_pg.c:373
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)
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.
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:219
int V1_delete_line_pg(struct Map_info *, off_t)
Deletes feature at the given offset (level 1)
Definition: write_pg.c:311
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:119
off_t V1_write_line_nat(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature to &#39;coor&#39; file at level 1 (internal use only)
Definition: write_nat.c:43
int V2_restore_line_nat(struct Map_info *, off_t, off_t)
Restores feature at topological level (internal use only)
Definition: write_nat.c:385
#define PRI_OFF_T
Definition: gis.h:69
int level
Topology level.
Definition: dig_structs.h:1313
int Vect_delete_line(struct Map_info *Map, off_t line)
Delete existing feature (topological level required)
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:62
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
Feature category info.
Definition: dig_structs.h:1702
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:99
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:136
off_t V2_rewrite_line_nat(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature to &#39;coor&#39; file at topological level (internal use only)
Definition: write_nat.c:156
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:179
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:88
off_t V1_rewrite_line_nat(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites feature to &#39;coor&#39; file at level 1 (internal use only)
Definition: write_nat.c:101
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:151
Vector map info.
Definition: dig_structs.h:1259
int V1_delete_line_nat(struct Map_info *, off_t)
Deletes feature at level 1 (internal use only)
Definition: write_nat.c:239
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_restore_line_nat(struct Map_info *, off_t, off_t)
Restores feature at level 1 (internal use only)
Definition: write_nat.c:340
int V2_delete_line_nat(struct Map_info *, off_t)
Deletes feature at topological level (internal use only)
Definition: write_nat.c:281
off_t V2_write_line_nat(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes feature to &#39;coor&#39; file at topological level (internal use only)
Definition: write_nat.c:64
int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
Restore previously deleted feature (topological level required)
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
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:142
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1271
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
const char * Vect_get_name(const struct Map_info *)
Get name of vector map.
int G_debug(int, const char *,...) __attribute__((format(printf
int V2_delete_line_sfa(struct Map_info *, off_t)
Deletes feature on level 2 (OGR/PostGIS interface)
Definition: write_sfa.c:191