/[zanavi_public1]/navit/navit/maptool/osm_psql.c
ZANavi

Contents of /navit/navit/maptool/osm_psql.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (show annotations) (download)
Sat Mar 8 17:10:49 2014 UTC (10 years ago) by zoff99
File MIME type: text/plain
File size: 5122 byte(s)
new market version, lots of new features
1 /**
2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2011 Navit Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #include <string.h>
20 #include <stdlib.h>
21 #include <math.h>
22 #include <unistd.h>
23 #include "maptool.h"
24 #include "debug.h"
25 #include "linguistics.h"
26 #include "file.h"
27 #ifdef HAVE_POSTGRESQL
28 #include <libpq-fe.h>
29
30 int
31 map_collect_data_osm_db(char *dbstr, struct maptool_osm *osm)
32 {
33 PGconn *conn;
34 PGresult *res,*node,*way,*tag;
35 int count,tagged,i,j,k;
36 long min, max, id, tag_id, node_id;
37 char query[256];
38
39 sig_alrm(0);
40 conn=PQconnectdb(dbstr);
41 if (! conn) {
42 fprintf(stderr,"Failed to connect to database with '%s'\n",dbstr);
43 exit(1);
44 }
45 res=PQexec(conn, "begin");
46 if (! res) {
47 fprintf(stderr, "Cannot begin transaction: %s\n", PQerrorMessage(conn));
48 PQclear(res);
49 exit(1);
50 }
51 res=PQexec(conn, "set transaction isolation level serializable");
52 if (! res) {
53 fprintf(stderr, "Cannot set isolation level: %s\n", PQerrorMessage(conn));
54 PQclear(res);
55 exit(1);
56 }
57 res=PQexec(conn, "declare node cursor for select id,x(coordinate),y(coordinate) from node order by id");
58 if (! res) {
59 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
60 PQclear(res);
61 exit(1);
62 }
63 res=PQexec(conn, "declare way cursor for select id from way order by id");
64 if (! res) {
65 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
66 PQclear(res);
67 exit(1);
68 }
69 res=PQexec(conn, "declare relation cursor for select id from relation order by id");
70 if (! res) {
71 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
72 PQclear(res);
73 exit(1);
74 }
75 for (;;) {
76 node=PQexec(conn, "fetch 100000 from node");
77 if (! node) {
78 fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn));
79 PQclear(node);
80 exit(1);
81 }
82 count=PQntuples(node);
83 if (! count)
84 break;
85 min=atol(PQgetvalue(node, 0, 0));
86 max=atol(PQgetvalue(node, count-1, 0));
87 sprintf(query,"select node_id,name,value from node_tag where node_id >= %ld and node_id <= %ld order by node_id", min, max);
88 tag=PQexec(conn, query);
89 if (! tag) {
90 fprintf(stderr, "Cannot query node_tag: %s\n", PQerrorMessage(conn));
91 exit(1);
92 }
93 j=0;
94 for (i = 0 ; i < count ; i++) {
95 id=atol(PQgetvalue(node, i, 0));
96 osm_add_node(id, atof(PQgetvalue(node, i, 1)), atof(PQgetvalue(node, i, 2)));
97 tagged=0;
98 processed_nodes++;
99 while (j < PQntuples(tag)) {
100 tag_id=atol(PQgetvalue(tag, j, 0));
101 if (tag_id == id) {
102 osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
103 tagged=1;
104 j++;
105 }
106 if (tag_id < id)
107 j++;
108 if (tag_id > id)
109 break;
110 }
111 osm_end_node(osm);
112 }
113 PQclear(tag);
114 PQclear(node);
115 }
116 for (;;) {
117 way=PQexec(conn, "fetch 100000 from way");
118 if (! way) {
119 fprintf(stderr, "Cannot setup cursor for ways: %s\n", PQerrorMessage(conn));
120 PQclear(node);
121 exit(1);
122 }
123 count=PQntuples(way);
124 if (! count)
125 break;
126 min=atol(PQgetvalue(way, 0, 0));
127 max=atol(PQgetvalue(way, count-1, 0));
128 sprintf(query,"select way_id,node_id from way_node where way_id >= %ld and way_id <= %ld order by way_id,sequence_id", min, max);
129 node=PQexec(conn, query);
130 if (! node) {
131 fprintf(stderr, "Cannot query way_node: %s\n", PQerrorMessage(conn));
132 exit(1);
133 }
134 sprintf(query,"select way_id,name,value from way_tag where way_id >= %ld and way_id <= %ld order by way_id", min, max);
135 tag=PQexec(conn, query);
136 if (! tag) {
137 fprintf(stderr, "Cannot query way_tag: %s\n", PQerrorMessage(conn));
138 exit(1);
139 }
140 j=0;
141 k=0;
142 for (i = 0 ; i < count ; i++) {
143 id=atol(PQgetvalue(way, i, 0));
144 osm_add_way(id);
145 tagged=0;
146 processed_ways++;
147 while (k < PQntuples(node)) {
148 node_id=atol(PQgetvalue(node, k, 0));
149 if (node_id == id) {
150 osm_add_nd(atoll(PQgetvalue(node, k, 1)));
151 tagged=1;
152 k++;
153 }
154 if (node_id < id)
155 k++;
156 if (node_id > id)
157 break;
158 }
159 while (j < PQntuples(tag)) {
160 tag_id=atol(PQgetvalue(tag, j, 0));
161 if (tag_id == id) {
162 osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2));
163 tagged=1;
164 j++;
165 }
166 if (tag_id < id)
167 j++;
168 if (tag_id > id)
169 break;
170 }
171 if (tagged)
172 osm_end_way(osm);
173 }
174 PQclear(tag);
175 PQclear(node);
176 PQclear(way);
177 }
178
179 res=PQexec(conn, "commit");
180 if (! res) {
181 fprintf(stderr, "Cannot commit transaction: %s\n", PQerrorMessage(conn));
182 PQclear(res);
183 exit(1);
184 }
185 sig_alrm(0);
186 sig_alrm_end();
187 return 1;
188 }
189 #endif

   
Visit the ZANavi Wiki