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
|
/*!
\file lib/vector/Vlib/close.c
\brief Vector library - Close vector map
Higher level functions for reading/writing/manipulating vectors.
(C) 2001-2015 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 Original author CERL, probably Dave Gerdes or Mike Higgins.
\author Update to GRASS 5.7 Radim Blazek and David D. Gray.
\author Update to GRASS 7 Martin Landa <landa.martin gmail.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#include "local_proto.h"
static int clo_dummy(struct Map_info *map UNUSED)
{
return -1;
}
#if !defined HAVE_OGR || !defined HAVE_POSTGRES
static int format(struct Map_info *map UNUSED)
{
G_fatal_error(_("Requested format is not compiled in this version"));
return 0;
}
#endif
static int (*Close_array[][2])(struct Map_info *) = {{clo_dummy, V1_close_nat}
#ifdef HAVE_OGR
,
{clo_dummy, V1_close_ogr},
{clo_dummy, V1_close_ogr}
#else
,
{clo_dummy, format},
{clo_dummy, format}
#endif
#ifdef HAVE_POSTGRES
,
{clo_dummy, V1_close_pg}
#else
,
{clo_dummy, format}
#endif
};
static void unlink_file(struct Map_info *, const char *);
/*!
\brief Close vector map
\param Map pointer to Map_info
\return 0 on success
\return non-zero on error
*/
int Vect_close(struct Map_info *Map)
{
int create_link; /* used for external formats only */
struct Coor_info CInfo;
G_debug(1,
"Vect_close(): name = %s, mapset = %s, format = %d, level = %d, "
"is_tmp = %d",
Map->name, Map->mapset, Map->format, Map->level, Map->temporary);
if (Map->temporary && (Map->fInfo.ogr.dsn || Map->fInfo.pg.conninfo)) {
/* transfer features for external output format */
struct Map_info Out;
putenv("GRASS_VECTOR_EXTERNAL_IMMEDIATE=1");
if (-1 == Vect_open_new(&Out, Vect_get_name(Map), Vect_is_3d(Map))) {
G_warning(_("Unable to create vector map <%s>"),
Vect_get_name(Map));
return 1;
}
/* copy metadata */
Vect_hist_copy(Map, &Out);
Vect_copy_head_data(Map, &Out);
/* copy dblinks (temporary map -> output map) to transfer
(input map -> output map) attributes */
Vect_copy_map_dblinks(Map, &Out, TRUE);
/* afterwords, dblinks must be removed from temporary map
otherwise when deleting temporary map also original
attribute tables would be deteled */
Vect_map_del_dblink(Map, -1); /* delete db links for all layers */
if (0 != Vect_copy_map_lines_field(
Map, 1, &Out)) { /* always layer = 1 for OGR/PG maps */
G_warning(_("Copying features failed"));
return -1;
}
Vect_build(&Out);
Vect_close(&Out);
putenv("GRASS_VECTOR_EXTERNAL_IMMEDIATE="); /* unset variable */
}
/* check for external formats whether to create a link */
create_link = TRUE;
if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_POSTGIS) {
char *def_file;
if (Map->format == GV_FORMAT_POSTGIS) {
if (getenv("GRASS_VECTOR_PGFILE"))
def_file = getenv("GRASS_VECTOR_PGFILE");
else
def_file = "PG";
}
else {
def_file = "OGR";
}
if (G_find_file2("", def_file, G_mapset())) {
FILE *fp;
const char *p;
struct Key_Value *key_val;
fp = G_fopen_old("", def_file, G_mapset());
if (!fp) {
G_warning(_("Unable to open %s file"), def_file);
}
else {
key_val = G_fread_key_value(fp);
fclose(fp);
/* create a vector link in the current mapset ? */
p = G_find_key_value("link", key_val);
if (p && G_strcasecmp(p, "no") == 0) {
create_link = FALSE;
}
else {
p = G_find_key_value("link_name", key_val);
if (p) {
/* use different name for a link */
G_free(Map->name);
Map->name = G_store(p);
}
}
}
}
}
/* store support files for vector maps in the current mapset if in
write mode on level 2 */
if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
Map->plus.built == GV_BUILD_ALL && create_link) {
unlink_file(Map, GV_TOPO_ELEMENT); /* topo */
unlink_file(Map, GV_SIDX_ELEMENT); /* sidx */
unlink_file(Map, GV_CIDX_ELEMENT); /* cidx */
if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_POSTGIS) {
unlink_file(Map, GV_FIDX_ELEMENT); /* fidx */
}
Vect_coor_info(Map, &CInfo);
Map->plus.coor_size = CInfo.size;
Map->plus.coor_mtime = CInfo.mtime;
/* write out topo file */
Vect_save_topo(Map);
/* write out sidx file */
Map->plus.Spidx_new = TRUE; /* force writing */
Vect_save_sidx(Map);
/* write out cidx file */
Vect_cidx_save(Map);
/* write out fidx file */
if (Map->format == GV_FORMAT_OGR)
V2_close_ogr(Map);
else if (Map->format == GV_FORMAT_POSTGIS)
V2_close_pg(Map);
}
/* spatial index must also be closed when opened with topo but not
* modified */
if (Map->plus.spidx_fp.file && Map->plus.Spidx_built == TRUE &&
!Map->support_updated && Map->plus.built == GV_BUILD_ALL) {
G_debug(1, "spatial index file closed");
fclose(Map->plus.spidx_fp.file);
}
/* release memory if required */
if (Map->level > 1 && Map->plus.release_support) {
G_debug(1, "free topology, spatial index, and category index");
dig_free_plus(&(Map->plus));
}
G_debug(1, "close history file");
if (Map->hist_fp)
fclose(Map->hist_fp);
/* close level 1 files / data sources if not head_only */
if (!Map->head_only) {
if (create_link && ((*Close_array[Map->format][1])(Map)) != 0) {
G_warning(_("Unable to close vector <%s>"),
Vect_get_full_name(Map));
return 1;
}
}
G_free(Map->name);
G_free(Map->mapset);
G_free(Map->location);
G_free(Map->gisdbase);
Map->open = VECT_CLOSED_CODE;
return 0;
}
/*!
\brief Save format definition file for vector map
\param Map pointer to Map_info structure
\return 1 on success
\return 0 on error
*/
int Vect_save_frmt(struct Map_info *Map)
{
FILE *fd;
char buf[GPATH_MAX];
if (Map->format != GV_FORMAT_OGR && Map->format != GV_FORMAT_POSTGIS) {
G_warning(_("Invalid request for writing frmt file - map format is %d"),
Map->format);
return 0;
}
/* create frmt file */
sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
if (fd == NULL) {
G_fatal_error("Unable to create file '%s'", buf);
}
if (Map->format == GV_FORMAT_POSTGIS) {
#ifdef HAVE_POSTGRES
fprintf(fd, "format: postgis\n");
fprintf(fd, "conninfo: %s\n", Map->fInfo.pg.conninfo);
fprintf(fd, "schema: %s\n", Map->fInfo.pg.schema_name);
fprintf(fd, "table: %s\n", Map->fInfo.pg.table_name);
#else
G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
return 0;
#endif
}
else if (Map->format == GV_FORMAT_OGR) {
#ifdef HAVE_OGR
fprintf(fd, "format: ogr\n");
fprintf(fd, "dsn: %s\n", Map->fInfo.ogr.dsn);
fprintf(fd, "layer: %s\n", Map->fInfo.ogr.layer_name);
#else
G_fatal_error(_("GRASS is not compiled with OGR support"));
return 0;
#endif
}
G_verbose_message(_("Link to vector map <%s> created"), Map->name);
/* close frmt file */
fclose(fd);
return 1;
}
/*! Free memory of line cache
\param cache pointer to lines cache to be freed
*/
void Vect__free_cache(struct Format_info_cache *cache)
{
int i;
/* destroy lines in cache */
for (i = 0; i < cache->lines_alloc; i++) {
Vect_destroy_line_struct(cache->lines[i]);
}
G_free(cache->lines);
G_free(cache->lines_types);
G_free(cache->lines_cats);
G_zero(cache, sizeof(struct Format_info_cache));
}
/*! Free memory of offset array
\param cache pointer to offset array to be freed
*/
void Vect__free_offset(struct Format_info_offset *offset)
{
G_free(offset->array);
G_zero(offset, sizeof(struct Format_info_offset));
}
void unlink_file(struct Map_info *Map, const char *name)
{
char path[GPATH_MAX];
/* delete old support files if available */
Vect__get_element_path(path, Map, name);
if (access(path, F_OK) == 0) { /* file exists? */
G_debug(2, "\t%s: unlink", path);
unlink(path);
}
}
|