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

« back to all changes in this revision

Viewing changes to vector/v.out.ogr/attrb.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 <grass/glocale.h>
 
2
 
 
3
#include "local_proto.h"
 
4
 
 
5
int mk_att(int cat, struct field_info *Fi, dbDriver *driver, int ncol,
 
6
           int *colctype, const char **colname, int doatt, int nocat,
 
7
           OGRFeatureH Ogr_feature, int *noatt)
 
8
{
 
9
    int j, ogrfieldnum;
 
10
    int more;
 
11
    dbTable *Table;
 
12
    static int first = 1;
 
13
    static dbString dbstring;
 
14
    dbColumn *Column;
 
15
    dbValue *Value;
 
16
    char buf[SQL_BUFFER_SIZE];
 
17
    dbCursor cursor;
 
18
 
 
19
 
 
20
    G_debug(2, "mk_att() cat = %d, doatt = %d", cat, doatt);
 
21
 
 
22
    /* init constants */
 
23
    if (first) {
 
24
        db_init_string(&dbstring);
 
25
        first = 0;
 
26
    }
 
27
 
 
28
    /* Attributes */
 
29
    /* Reset */
 
30
    if (!doatt) {
 
31
        ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, GV_KEY_COLUMN);
 
32
        if (ogrfieldnum > -1)
 
33
            OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
 
34
        /* doatt reset moved into have cat loop as the table needs to be
 
35
           open to know the OGR field ID. Hopefully this has no ill consequences */
 
36
    }
 
37
 
 
38
    /* Read & set attributes */
 
39
    if (cat >= 0) {             /* Line with category */
 
40
        if (doatt) {
 
41
            /* Fetch all attribute records for cat <cat> */ 
 
42
            /* opening and closing the cursor is slow, 
 
43
             * but the cursor really needs to be opened for each cat separately */
 
44
            sprintf(buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key, cat);
 
45
            G_debug(2, "SQL: %s", buf);
 
46
            db_set_string(&dbstring, buf);
 
47
            if (db_open_select_cursor (driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK) {
 
48
                    G_fatal_error(_("Cannot select attributes for cat = %d"),
 
49
                  cat);
 
50
            }
 
51
 
 
52
            if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
 
53
                G_fatal_error(_("Unable to fetch data from table"));
 
54
 
 
55
            if (!more) {
 
56
                /* G_warning ("No database record for cat = %d", cat); */
 
57
                /* Set at least key column to category */
 
58
                if (!nocat) {
 
59
                    ogrfieldnum =
 
60
                        OGR_F_GetFieldIndex(Ogr_feature, Fi->key);
 
61
                    OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
 
62
                    (*noatt)++;
 
63
                }
 
64
                else {
 
65
                    G_fatal_error(_("No database record for cat = %d and export of 'cat' disabled"),
 
66
                                  cat);
 
67
                }
 
68
            }
 
69
            else {
 
70
                Table = db_get_cursor_table(&cursor);
 
71
                for (j = 0; j < ncol; j++) {
 
72
                    Column = db_get_table_column(Table, j);
 
73
                    Value = db_get_column_value(Column);
 
74
                    db_convert_column_value_to_string(Column, &dbstring);       /* for debug only */
 
75
                    G_debug(2, "col %d : val = %s", j,
 
76
                            db_get_string(&dbstring));
 
77
 
 
78
                    G_debug(2, "  colctype = %d", colctype[j]);
 
79
 
 
80
                    if (nocat && strcmp(Fi->key, colname[j]) == 0)
 
81
                        continue;
 
82
 
 
83
                    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature,
 
84
                                                      colname[j]);
 
85
                    G_debug(2, "  column = %s -> fieldnum = %d",
 
86
                            colname[j], ogrfieldnum);
 
87
 
 
88
                    if (ogrfieldnum < 0) {
 
89
                        G_debug(4, "Could not get OGR field number for column %s",
 
90
                                                         colname[j]);
 
91
                        continue;
 
92
                    }
 
93
 
 
94
                    /* Reset */
 
95
                    if ((nocat && strcmp(Fi->key, colname[j]) == 0) == 0) {
 
96
                        /* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
 
97
                        OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
 
98
                    }
 
99
 
 
100
                    /* prevent writing NULL values */
 
101
                    if (!db_test_value_isnull(Value)) {
 
102
                        if ((nocat && strcmp(Fi->key, colname[j]) == 0) == 0) {
 
103
                        /* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
 
104
 
 
105
                            switch (colctype[j]) {
 
106
                            case DB_C_TYPE_INT:
 
107
                                OGR_F_SetFieldInteger(Ogr_feature,
 
108
                                                      ogrfieldnum,
 
109
                                                      db_get_value_int
 
110
                                                      (Value));
 
111
                                break;
 
112
                            case DB_C_TYPE_DOUBLE:
 
113
                                OGR_F_SetFieldDouble(Ogr_feature, ogrfieldnum,
 
114
                                                     db_get_value_double
 
115
                                                     (Value));
 
116
                                break;
 
117
                            case DB_C_TYPE_STRING:
 
118
                                OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
 
119
                                                     db_get_value_string
 
120
                                                     (Value));
 
121
                                break;
 
122
                            case DB_C_TYPE_DATETIME:
 
123
                                db_convert_column_value_to_string(Column,
 
124
                                                                  &dbstring);
 
125
                                OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
 
126
                                                     db_get_string
 
127
                                                     (&dbstring));
 
128
                                break;
 
129
                            }
 
130
                        }
 
131
                    }
 
132
                }
 
133
            }
 
134
            db_close_cursor(&cursor);
 
135
        }
 
136
        else {                  /* Use cat only */
 
137
            ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, GV_KEY_COLUMN);
 
138
            OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
 
139
        }
 
140
    }
 
141
    /* else G_warning ("Line without cat of layer %d", field); */
 
142
 
 
143
    /*
 
144
    db_free_string(&dbstring);
 
145
    */
 
146
 
 
147
    return 1;
 
148
}