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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*!
\file lib/vector/Vlib/write.c
\brief Vector library - write vector features
Higher level functions for reading/writing/manipulating vectors.
Supported operations:
- Write a new feature
- Rewrite existing feature
- Delete existing feature
- Restore deleted feature
(C) 2001-2010, 2012-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 Radim Blazek
\author Updated by Martin Landa <landa.martin gmail.com> (restore lines, OGR
& PostGIS support)
*/
#include <inttypes.h>
#include <sys/types.h>
#include <grass/glocale.h>
#include <grass/vector.h>
static off_t write_dummy(struct Map_info *Map UNUSED, int type UNUSED,
const struct line_pnts *points UNUSED,
const struct line_cats *cats UNUSED)
{
G_warning("Vect_write_line() %s", _("for this format/level not supported"));
return -1;
}
static off_t rewrite_dummy(struct Map_info *Map UNUSED, off_t line UNUSED,
int type UNUSED,
const struct line_pnts *points UNUSED,
const struct line_cats *cats UNUSED)
{
G_warning("Vect_rewrite_line() %s",
_("for this format/level not supported"));
return -1;
}
static int delete_dummy(struct Map_info *Map UNUSED, off_t line UNUSED)
{
G_warning("Vect_delete_line() %s",
_("for this format/level not supported"));
return -1;
}
static int restore_dummy(struct Map_info *Map UNUSED, off_t offset UNUSED,
off_t line UNUSED)
{
G_warning("Vect_restore_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, off_t line UNUSED)
{
G_fatal_error(_("Requested format is not compiled in this version"));
return 0;
}
static int format2(struct Map_info *Map UNUSED, off_t offset UNUSED,
off_t line UNUSED)
{
G_fatal_error(_("Requested format is not compiled in this version"));
return 0;
}
static off_t format_l(struct Map_info *Map UNUSED, int type UNUSED,
const struct line_pnts *points UNUSED,
const struct line_cats *cats UNUSED)
{
G_fatal_error(_("Requested format is not compiled in this version"));
return 0;
}
static off_t format_l2(struct Map_info *Map UNUSED, off_t line UNUSED,
int type UNUSED, const struct line_pnts *points UNUSED,
const struct line_cats *cats UNUSED)
{
G_fatal_error(_("Requested format is not compiled in this version"));
return 0;
}
#endif
static off_t (*Vect_write_line_array[][3])(struct Map_info *, int,
const struct line_pnts *,
const struct line_cats *) = {
{write_dummy, V1_write_line_nat, V2_write_line_nat}
#ifdef HAVE_OGR
,
{write_dummy, V1_write_line_ogr, V2_write_line_sfa},
{write_dummy, V1_write_line_ogr, V2_write_line_sfa}
#else
,
{write_dummy, format_l, format_l},
{write_dummy, format_l, format_l}
#endif
#ifdef HAVE_POSTGRES
,
{write_dummy, V1_write_line_pg, V2_write_line_pg}
#else
,
{write_dummy, format_l, format_l}
#endif
};
static off_t (*Vect_rewrite_line_array[][3])(struct Map_info *, off_t, int,
const struct line_pnts *,
const struct line_cats *) = {
{rewrite_dummy, V1_rewrite_line_nat, V2_rewrite_line_nat}
#ifdef HAVE_OGR
,
{rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_sfa},
{rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_sfa}
#else
,
{rewrite_dummy, format_l2, format_l2},
{rewrite_dummy, format_l2, format_l2}
#endif
#ifdef HAVE_POSTGRES
,
{rewrite_dummy, V1_rewrite_line_pg, V2_rewrite_line_pg}
#else
,
{rewrite_dummy, format_l2, format_l2}
#endif
};
static int (*Vect_delete_line_array[][3])(struct Map_info *, off_t) = {
{delete_dummy, V1_delete_line_nat, V2_delete_line_nat}
#ifdef HAVE_OGR
,
{delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa},
{delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa}
#else
,
{delete_dummy, format, format},
{delete_dummy, format, format}
#endif
#ifdef HAVE_POSTGRES
,
{delete_dummy, V1_delete_line_pg, V2_delete_line_pg}
#else
,
{delete_dummy, format, format}
#endif
};
static int (*Vect_restore_line_array[][3])(struct Map_info *, off_t, off_t) = {
{restore_dummy, V1_restore_line_nat, V2_restore_line_nat}
#ifdef HAVE_OGR
,
{restore_dummy, restore_dummy, restore_dummy},
{restore_dummy, restore_dummy, restore_dummy}
#else
,
{restore_dummy, format2, format2},
{restore_dummy, format2, format2}
#endif
#ifdef HAVE_POSTGRES
,
{restore_dummy, restore_dummy, restore_dummy}
#else
,
{restore_dummy, format2, format2}
#endif
};
static int check_map(struct Map_info *);
/*!
\brief Writes a new feature
New feature is written to the end of file (in the case of native
format). Topological level is not required.
A warning is printed on error.
\param Map pointer to Map_info structure
\param type feature type (see dig_defines.h for supported types)
\param points pointer to line_pnts structure (feature geometry)
\param cats pointer to line_cats structure (feature categories)
\return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
\return offset into file where the feature starts (on level 1)
\return -1 on error
*/
off_t Vect_write_line(struct Map_info *Map, int type,
const struct line_pnts *points,
const struct line_cats *cats)
{
off_t offset;
G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
Map->name, Map->format, Map->level);
if (!check_map(Map))
return -1;
offset = (*Vect_write_line_array[Map->format][Map->level])(Map, type,
points, cats);
if (offset < 0)
G_warning(_("Unable to write feature in vector map <%s>"),
Vect_get_name(Map));
return offset;
}
/*!
\brief Rewrites existing feature (topological level required)
Note: Topology must be built at level >= GV_BUILD_BASE
A warning is printed on error.
The number of points or cats or type may change. If necessary, the
old feature is deleted and new is written.
\param Map pointer to Map_info structure
\param line feature id (level 2) or feature offset (level 1)
\param type feature type (GV_POINT, GV_LINE, ...)
\param points feature geometry
\param cats feature categories
\return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
\return offset into file where the feature starts (on level 1)
\return -1 on error
*/
off_t Vect_rewrite_line(struct Map_info *Map, off_t line, int type,
const struct line_pnts *points,
const struct line_cats *cats)
{
off_t ret;
G_debug(3,
"Vect_rewrite_line(): name = %s, format = %d, level = %d, "
"line/offset = %" PRId64,
Map->name, Map->format, Map->level, line);
if (!check_map(Map))
return -1;
ret = (*Vect_rewrite_line_array[Map->format][Map->level])(Map, line, type,
points, cats);
if (ret == -1)
G_warning(_("Unable to rewrite feature/offset %" PRId64
" in vector map <%s>"),
line, Vect_get_name(Map));
return ret;
}
/*!
\brief Delete existing feature (topological level required)
Note: Topology must be built at level >= GV_BUILD_BASE
A warning is printed on error.
\param Map pointer to Map_info structure
\param line feature id (level 2) or feature offset (level 1)
\return 0 on success
\return -1 on error
*/
int Vect_delete_line(struct Map_info *Map, off_t line)
{
int ret;
G_debug(3, "Vect_delete_line(): name = %s, line/offset = %" PRId64,
Map->name, line);
if (!check_map(Map))
return -1;
ret = (*Vect_delete_line_array[Map->format][Map->level])(Map, line);
if (ret == -1)
G_warning(_("Unable to delete feature/offset %" PRId64
" from vector map <%s>"),
line, Vect_get_name(Map));
return ret;
}
/*!
\brief Restore previously deleted feature (topological level required)
Note: Topology must be built at level >= GV_BUILD_BASE
A warning is printed on error.
\param Map pointer to Map_info structure
\param offset feature offset to be restored
\param line feature id to be restored (used only on level 2)
\return 0 on success
\return -1 on error
*/
int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
{
int ret;
G_debug(3,
"Vect_restore_line(): name = %s, level = %d, offset = %" PRId64
", line = %" PRId64,
Map->name, Map->level, offset, line);
if (!check_map(Map))
return -1;
ret =
(*Vect_restore_line_array[Map->format][Map->level])(Map, offset, line);
if (ret == -1)
G_warning(_("Unable to restore feature/offset %" PRId64
" in vector map <%s>"),
offset, Vect_get_name(Map));
return ret;
}
int check_map(struct Map_info *Map)
{
if (!VECT_OPEN(Map)) {
G_warning(_("Vector map <%s> is not opened"), Vect_get_name(Map));
return 0;
}
if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
G_warning(_("Vector map <%s> is not opened in write mode"),
Vect_get_name(Map));
return 0;
}
return 1;
}
|