summaryrefslogtreecommitdiff
path: root/navit/maptool/osm_psql.c
blob: c4a936723bdb5551b5486975b0cb7da522cd76d1 (plain)
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
/*
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2011 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "maptool.h"
#include "debug.h"
#include "linguistics.h"
#include "file.h"
#ifdef HAVE_POSTGRESQL
#include <postgresql/libpq-fe.h>

int map_collect_data_osm_db(char *dbstr, struct maptool_osm *osm) {
    PGconn *conn;
    PGresult *res;
    char query[256];

    sig_alrm(0);
    conn=PQconnectdb(dbstr);
    if (! conn) {
        fprintf(stderr,"Failed to connect to database with '%s'\n",dbstr);
        exit(1);
    }
    fprintf(stderr,"connected to database with '%s'\n",dbstr);
    res=PQexec(conn, "begin");
    if (! res) {
        fprintf(stderr, "Cannot begin transaction: %s\n", PQerrorMessage(conn));
        PQclear(res);
        exit(1);
    }
    res=PQexec(conn, "set transaction isolation level serializable");
    if (! res) {
        fprintf(stderr, "Cannot set isolation level: %s\n", PQerrorMessage(conn));
        PQclear(res);
        exit(1);
    }
    res=PQexec(conn, "declare nodes cursor for select id, ST_Y(geom), ST_X(geom) from nodes order by id");
    if (! res) {
        fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
        PQclear(res);
        exit(1);
    }
    res=PQexec(conn, "declare ways cursor for select id from ways order by id");
    if (! res) {
        fprintf(stderr, "Cannot setup cursor for ways: %s\n", PQerrorMessage(conn));
        PQclear(res);
        exit(1);
    }
    res=PQexec(conn, "declare relations cursor for select id from relations order by id");
    if (! res) {
        fprintf(stderr, "Cannot setup cursor for relations: %s\n", PQerrorMessage(conn));
        PQclear(res);
        exit(1);
    }

    for (;;) {
        int j=0, count=0;
        long min, max, id, tag_id;
        PGresult *node, *tag;
        node=PQexec(conn, "fetch 100000 from nodes");
        if (! node) {
            fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
            PQclear(node);
            exit(1);
        }
        count=PQntuples(node);
        fprintf(stderr, "fetch got %i nodes\n", count);
        if (! count)
            break;
        min=atol(PQgetvalue(node, 0, 0));
        max=atol(PQgetvalue(node, count-1, 0));
        sprintf(query,"select node_id,k,v from node_tags where node_id >= %ld and node_id <= %ld order by node_id", min, max);
        tag=PQexec(conn, query);
        if (! tag) {
            fprintf(stderr, "Cannot query node_tag: %s\n", PQerrorMessage(conn));
            exit(1);
        }
        fprintf(stderr, "query node_tag got : %i tuples\n", PQntuples(tag));
        for (int i = 0 ; i < count ; i++) {
            id=atol(PQgetvalue(node, i, 0));
            osm_add_node(id, atof(PQgetvalue(node, i, 1)), atof(PQgetvalue(node, i, 2)));
            processed_nodes++;
            while (j < PQntuples(tag)) {
                tag_id=atol(PQgetvalue(tag, j, 0));
                if (tag_id == id) {
                    osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
                    j++;
                }
                if (tag_id < id)
                    j++;
                if (tag_id > id)
                    break;
            }
            osm_end_node(osm);
        }
        PQclear(tag);
        PQclear(node);
    }

    for (;;) {
        int j=0, k=0, count=0, tagged=0;
        long min, max, id, tag_id, node_id;
        PGresult *node,*way,*tag;
        way=PQexec(conn, "fetch 25000 from ways");
        if (! way) {
            fprintf(stderr, "Cannot setup cursor for ways: %s\n", PQerrorMessage(conn));
            PQclear(way);
            exit(1);
        }
        count=PQntuples(way);
        fprintf(stderr, "fetch got %i ways\n", count);
        if (! count)
            break;
        min=atol(PQgetvalue(way, 0, 0));
        max=atol(PQgetvalue(way, count-1, 0));
        fprintf(stderr, "continue with %i ways\n", count);
        sprintf(query,"select way_id,node_id from way_nodes where way_id >= %ld and way_id <= %ld order by way_id,sequence_id",
                min, max);
        node=PQexec(conn, query);
        if (! node) {
            fprintf(stderr, "Cannot query way_node: %s\n", PQerrorMessage(conn));
            exit(1);
        }
        sprintf(query,"select way_id,k,v from way_tags where way_id >= %ld and way_id <= %ld order by way_id", min, max);
        tag=PQexec(conn, query);
        if (! tag) {
            fprintf(stderr, "Cannot query way_tag: %s\n", PQerrorMessage(conn));
            exit(1);
        }
        for (int i = 0 ; i < count ; i++) {
            id=atol(PQgetvalue(way, i, 0));
            osm_add_way(id);
            tagged=0;
            processed_ways++;
            while (k < PQntuples(node)) {
                node_id=atol(PQgetvalue(node, k, 0));
                if (node_id == id) {
                    osm_add_nd(atoll(PQgetvalue(node, k, 1)));
                    tagged=1;
                    k++;
                }
                if (node_id < id)
                    k++;
                if (node_id > id)
                    break;
            }
            while (j < PQntuples(tag)) {
                tag_id=atol(PQgetvalue(tag, j, 0));
                if (tag_id == id) {
                    osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
                    tagged=1;
                    j++;
                }
                if (tag_id < id)
                    j++;
                if (tag_id > id)
                    break;
            }
            if (tagged)
                osm_end_way(osm);
        }
        PQclear(tag);
        PQclear(node);
        PQclear(way);
    }

    for (;;) {
        int j=0, k=0, count=0, tagged=0;
        long min, max, id;
        PGresult *tag, *relation, *member;
        relation=PQexec(conn, "fetch 40000 from relations");
        if (! relation) {
            fprintf(stderr, "Cannot setup cursor for relations: %s\n", PQerrorMessage(conn));
            PQclear(relation);
            exit(1);
        }
        count=PQntuples(relation);
        fprintf(stderr, "Got %i relations\n", count);
        if (! count)
            break;
        min=atol(PQgetvalue(relation, 0, 0));
        max=atol(PQgetvalue(relation, count-1, 0));
        sprintf(query,
                "select relation_id,k,v from relation_tags where relation_id >= %ld and relation_id <= %ld order by relation_id", min,
                max);
        tag=PQexec(conn, query);
        if (! tag) {
            fprintf(stderr, "Cannot query relation_tag: %s\n", PQerrorMessage(conn));
            exit(1);
        }
        sprintf(query,
                "select relation_id, member_id, member_type, member_role from relation_members where relation_id >= %ld and relation_id <= %ld order by relation_id, sequence_id",
                min, max);
        member=PQexec(conn, query);
        if (! member) {
            fprintf(stderr, "Cannot query relation_members: %s\n", PQerrorMessage(conn));
            exit(1);
        }
        for (int i = 0 ; i < count ; i++) {
            id=atol(PQgetvalue(relation, i, 0));
            osm_add_relation(id);
            tagged = 0;
            while (j < PQntuples(tag)) {
                long tag_relation_id=atol(PQgetvalue(tag, j, 0));
                if (tag_relation_id == id) {
                    osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
                    tagged=1;
                    j++;
                }
                if (tag_relation_id < id)
                    j++;
                if (tag_relation_id > id)
                    break;
            }
            while (k < PQntuples(member)) {
                long member_relation_id=atol(PQgetvalue(member, k, 0));
                if (member_relation_id == id) {
                    int relmember_type=0; //type unknown
                    if (!g_strcmp0(PQgetvalue(member,k, 2),"W")) {
                        relmember_type=2;
                    } else {
                        if (!g_strcmp0(PQgetvalue(member,k, 2),"N")) {
                            relmember_type=1;
                        } else {
                            if (!g_strcmp0(PQgetvalue(member,k, 2),"R")) {
                                relmember_type=3;
                            }
                        }
                    }
                    osm_add_member(relmember_type,atoll(PQgetvalue(member,k, 1)),PQgetvalue(member,k, 3));
                    k++;
                }
                if (member_relation_id < id)
                    k++;
                if (member_relation_id > id)
                    break;
            }
            if (tagged)
                osm_end_relation(osm);
        }
        PQclear(relation);
        PQclear(member);
        PQclear(tag);
    }
    res=PQexec(conn, "commit");
    if (! res) {
        fprintf(stderr, "Cannot commit transaction: %s\n", PQerrorMessage(conn));
        PQclear(res);
        exit(1);
    }
    sig_alrm(0);
    sig_alrm_end();
    return 1;
}
#endif