~ubuntu-branches/ubuntu/wily/grass/wily

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
/****************************************************************
 *
 * MODULE:       v.external
 * 
 * AUTHOR(S):    Radim Blazek
 *               Updated to GRASS 7 by Martin Landa <landa.martin gmail.com>
 *               
 * PURPOSE:      Create a new vector as a link to OGR layer
 *               
 * COPYRIGHT:    (C) 2003-2011 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.
 *
 **************************************************************/

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/vector.h>
#include <grass/glocale.h>

#include <ogr_api.h>

#include "local_proto.h"

int main(int argc, char *argv[])
{
    struct GModule *module;
    struct _options options;
    struct _flags   flags;

    struct Map_info Map;
    
    FILE *fd;
    
    int ilayer, use_ogr;
    char buf[GPATH_MAX], *dsn;
    const char *output;
    
    G_gisinit(argv[0]);
    
    module = G_define_module();
    G_add_keyword(_("vector"));
    G_add_keyword(_("import"));
    G_add_keyword(_("external")); 
    G_add_keyword("OGR");
    G_add_keyword("PostGIS");
    module->description = _("Creates a new pseudo-vector map as a link to an OGR-supported layer "
                            "or a PostGIS feature table.");
    parse_args(argc, argv,
               &options, &flags);

    use_ogr = TRUE;
    G_debug(1, "GRASS_VECTOR_OGR defined? %s",
            getenv("GRASS_VECTOR_OGR") ? "yes" : "no");
    if(options.dsn->answer &&
       G_strncasecmp(options.dsn->answer, "PG:", 3) == 0) {
        /* -> PostgreSQL */
#if defined HAVE_OGR && defined HAVE_POSTGRES
        if (getenv("GRASS_VECTOR_OGR"))
            use_ogr = TRUE;
        else
            use_ogr = FALSE;
#else
#ifdef HAVE_POSTGRES
        if (getenv("GRASS_VECTOR_OGR"))
            G_warning(_("Environment variable GRASS_VECTOR_OGR defined, "
                        "but GRASS is compiled with OGR support. "
                        "Using GRASS-PostGIS data driver instead."));
        use_ogr = FALSE;
#else /* -> force using OGR */
        G_warning(_("GRASS is not compiled with PostgreSQL support. "
                    "Using OGR-PostgreSQL driver instead of native "
                    "GRASS-PostGIS data driver."));
        use_ogr = TRUE;
#endif /* HAVE_POSTRES */
#endif /* HAVE_OGR && HAVE_POSTGRES */
    }
    
#ifdef HAVE_OGR
    if (use_ogr)
        OGRRegisterAll();
#endif

    if (flags.format->answer) {
        /* list formats */
        list_formats(stdout);
        exit(EXIT_SUCCESS);
    }

    /* be friendly, ignored 'PG:' prefix for PostGIS links */
    dsn = NULL;
    if (options.dsn->answer) {
        if (!use_ogr) {
            int i, length;
            
            length = strlen(options.dsn->answer);
            dsn = (char *) G_malloc(length - 3);
            for (i = 3; i < length; i++)
                dsn[i-3] = options.dsn->answer[i];
            dsn[length-3] = '\0';
        }
        else {
            dsn = G_store(options.dsn->answer);
        }
    }
    
    if (flags.list->answer || flags.tlist->answer) {
        /* list layers */
        if (!dsn)
            G_fatal_error(_("Required parameter <%s> not set"), options.dsn->key);
        list_layers(stdout, dsn, NULL,
                    flags.tlist->answer ? TRUE : FALSE, use_ogr);
        exit(EXIT_SUCCESS);
    }

    /* define name for output */
    if (!options.output->answer)
        output = options.layer->answer;
    else
        output = options.output->answer;
    

    /* get layer index */
    ilayer = list_layers(NULL, dsn, options.layer->answer,
                         FALSE, use_ogr);
    if (ilayer == -1) {
        G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
    }
    
    G_debug(2, "layer '%s' was found", options.layer->answer);

    if (G_find_vector2(output, G_mapset()) && !G_check_overwrite(argc, argv)) {
        G_fatal_error(_("option <%s>: <%s> exists."),
                      options.output->key, output);
    }
    
    /* create new vector map */
    putenv("GRASS_VECTOR_EXTERNAL_IGNORE=1");
    if (Vect_open_new(&Map, output, WITHOUT_Z) < 0) /* dimension is set later from data source */
	G_fatal_error(_("Unable to create vector map <%s>"), output);
    Vect_set_error_handler_io(NULL, &Map);
    
    Vect_hist_command(&Map);
    Vect_close(&Map);
    
    /* Vect_open_new created 'head', 'coor', 'hist'
       -> delete 'coor' and create 'frmt' */
    sprintf(buf, "%s/%s/%s/%s/coor", G_location_path(), G_mapset(),
            GV_DIRECTORY, output);
    G_debug(2, "Delete '%s'", buf);
    if (unlink(buf) == -1) {
        G_fatal_error(_("Unable to delete '%s'"), buf);
    }

    /* create frmt file */
    sprintf(buf, "%s/%s", GV_DIRECTORY, output);
    fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
    if (fd == NULL)
        G_fatal_error(_("Unable to create file '%s/%s'"), buf, GV_FRMT_ELEMENT);
    
    if (!use_ogr) {
        char *table_name, *schema_name;
        
        get_table_name(options.layer->answer, &table_name, &schema_name);
        
        fprintf(fd, "format: postgis\n");
        fprintf(fd, "conninfo: %s\n", dsn);
        if (schema_name)
            fprintf(fd, "schema: %s\n", schema_name);
        fprintf(fd, "table: %s\n", table_name);

        G_free(table_name);
        G_free(schema_name);
    }
    else {
        fprintf(fd, "format: ogr\n");
        fprintf(fd, "dsn: %s\n", dsn);
        fprintf(fd, "layer: %s\n", options.layer->answer);
    }
    fclose(fd);
    
    if (!flags.topo->answer) {
        Vect_set_open_level(1);
        if (Vect_open_old(&Map, output, G_mapset()) < 0)
	    G_fatal_error(_("Unable to open vector map <%s>"), output);
        Vect_build(&Map);
        Vect_close(&Map);
    }
    
    G_done_msg(_("Link to vector map <%s> created."), output);
    
    exit(EXIT_SUCCESS);
}