~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to vector/v.out.postgis/create.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <unistd.h>
 
2
 
 
3
#include <grass/gis.h>
 
4
#include <grass/glocale.h>
 
5
 
 
6
#include "local_proto.h"
 
7
 
 
8
static void file_handler(void *);
 
9
 
 
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)
 
13
{
 
14
    int   i;
 
15
    const char *epsg;
 
16
    char *filename, *conninfo;
 
17
    char buf[GPATH_MAX];
 
18
    FILE *fp;
 
19
    
 
20
    struct Key_Value *key_val;
 
21
    
 
22
    filename = NULL;
 
23
    G_asprintf(&filename, "PG_%d", (int) getpid());
 
24
    G_debug(1, "PG file: %s", filename);
 
25
    
 
26
    fp = G_fopen_new("", filename);
 
27
    if (!fp)
 
28
        G_fatal_error(_("Unable to create <%s> file"), filename);
 
29
    sprintf(buf, "GRASS_VECTOR_PGFILE=%s", filename);
 
30
    putenv(G_store(buf));
 
31
    G_add_error_handler(file_handler, filename);
 
32
 
 
33
    key_val = G_create_key_value();
 
34
    
 
35
   /* be friendly, ignored 'PG:' prefix for GRASS-PostGIS data driver */
 
36
    if (G_strncasecmp(dsn, "PG:", 3) == 0) {
 
37
        int length;
 
38
        
 
39
        length = strlen(dsn);
 
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';
 
44
    }
 
45
    else {
 
46
        conninfo = G_store(dsn);
 
47
    }
 
48
    
 
49
    /* required options */
 
50
    G_set_key_value("conninfo", conninfo, key_val);
 
51
    if (schema)
 
52
        G_set_key_value("schema", schema, key_val);
 
53
    if (topo)
 
54
        G_set_key_value("topology", "yes", key_val);
 
55
 
 
56
    /* is EPSG defined */
 
57
    epsg = G_database_epsg_code();
 
58
    
 
59
    /* extra options */
 
60
    if (options) {
 
61
        char **tokens;
 
62
 
 
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]);
 
67
                continue;
 
68
            }
 
69
            G_debug(1, "option: %s=%s", tokens[0], tokens[1]);
 
70
            /* force lower case */
 
71
            G_str_to_lower(tokens[0]);
 
72
 
 
73
            if (strcmp(tokens[0], "srid") && epsg)
 
74
                G_warning(_("EPSG code (%s) defined for current location will be ignored"),
 
75
                          epsg);
 
76
            
 
77
            G_set_key_value(tokens[0], tokens[1], key_val);
 
78
            
 
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]);
 
83
 
 
84
            G_free_tokens(tokens);
 
85
        }
 
86
    }
 
87
    
 
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);
 
91
        
 
92
    if (olink) {
 
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);
 
96
    }
 
97
    else {
 
98
        G_set_key_value("link", "no", key_val);
 
99
    }
 
100
 
 
101
    if (G_fwrite_key_value(fp, key_val) < 0)
 
102
        G_fatal_error(_("Error writing <%s> file"), filename);
 
103
 
 
104
    fclose(fp);
 
105
 
 
106
    G_free(conninfo);
 
107
    
 
108
    return filename;
 
109
}
 
110
 
 
111
void file_handler(void *p) {
 
112
    const char *filename = (const char *) p;
 
113
    
 
114
    G_debug(1, "file_handler: %s", filename);
 
115
    G_remove("", filename);
 
116
    putenv("GRASS_VECTOR_PGFILE=");
 
117
}