4
#include <grass/glocale.h>
6
#include "local_proto.h"
8
static void file_handler(void *);
10
char *create_pgfile(const char *dsn, const char *schema, const char *olink,
11
char **options, int topo,
12
char **fid_column, char **geom_column)
16
char *filename, *conninfo;
20
struct Key_Value *key_val;
23
G_asprintf(&filename, "PG_%d", (int) getpid());
24
G_debug(1, "PG file: %s", filename);
26
fp = G_fopen_new("", filename);
28
G_fatal_error(_("Unable to create <%s> file"), filename);
29
sprintf(buf, "GRASS_VECTOR_PGFILE=%s", filename);
31
G_add_error_handler(file_handler, filename);
33
key_val = G_create_key_value();
35
/* be friendly, ignored 'PG:' prefix for GRASS-PostGIS data driver */
36
if (G_strncasecmp(dsn, "PG:", 3) == 0) {
40
conninfo = (char *) G_malloc(length - 2);
41
for (i = 3; i < length; i++)
42
conninfo[i-3] = dsn[i];
43
conninfo[length-3] = '\0';
46
conninfo = G_store(dsn);
49
/* required options */
50
G_set_key_value("conninfo", conninfo, key_val);
52
G_set_key_value("schema", schema, key_val);
54
G_set_key_value("topology", "yes", key_val);
57
epsg = G_database_epsg_code();
63
for(i = 0; options[i]; i++) {
64
tokens = G_tokenize(options[i], "=");
65
if (G_number_of_tokens(tokens) != 2) {
66
G_warning(_("Invalid option skipped: %s"), options[i]);
69
G_debug(1, "option: %s=%s", tokens[0], tokens[1]);
70
/* force lower case */
71
G_str_to_lower(tokens[0]);
73
if (strcmp(tokens[0], "srid") && epsg)
74
G_warning(_("EPSG code (%s) defined for current location will be ignored"),
77
G_set_key_value(tokens[0], tokens[1], key_val);
79
if (strcmp(tokens[0], "fid") == 0)
80
G_asprintf(fid_column, tokens[1]);
81
if (strcmp(tokens[0], "geometry_name") == 0)
82
G_asprintf(geom_column, tokens[1]);
84
G_free_tokens(tokens);
88
/* check EPSG code if defined as an option */
89
if (epsg && !G_find_key_value("srid", key_val))
90
G_set_key_value("srid", epsg, key_val);
93
/* create a link for output feature table */
94
G_set_key_value("link", "yes", key_val);
95
G_set_key_value("link_name", olink, key_val);
98
G_set_key_value("link", "no", key_val);
101
if (G_fwrite_key_value(fp, key_val) < 0)
102
G_fatal_error(_("Error writing <%s> file"), filename);
111
void file_handler(void *p) {
112
const char *filename = (const char *) p;
114
G_debug(1, "file_handler: %s", filename);
115
G_remove("", filename);
116
putenv("GRASS_VECTOR_PGFILE=");