/** * 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 #include #include #include #include "maptool.h" #include "debug.h" #include "linguistics.h" #include "file.h" #ifdef HAVE_POSTGRESQL #include int map_collect_data_osm_db(char *dbstr, FILE *out_ways, FILE *out_nodes, FILE *out_turn_restrictions, FILE *out_boundaries) { PGconn *conn; PGresult *res,*node,*way,*tag; int count,tagged,i,j,k; long min, max, id, tag_id, node_id; char query[256]; sig_alrm(0); conn=PQconnectdb(dbstr); if (! conn) { fprintf(stderr,"Failed to connect to database with '%s'\n",dbstr); exit(1); } 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 node cursor for select id,x(coordinate),y(coordinate) from node order by id"); if (! res) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } res=PQexec(conn, "declare way cursor for select id from way order by id"); if (! res) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } res=PQexec(conn, "declare relation cursor for select id from relation order by id"); if (! res) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } for (;;) { node=PQexec(conn, "fetch 100000 from node"); if (! node) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(node); exit(1); } count=PQntuples(node); if (! count) break; min=atol(PQgetvalue(node, 0, 0)); max=atol(PQgetvalue(node, count-1, 0)); sprintf(query,"select node_id,name,value from node_tag 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); } j=0; for (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))); tagged=0; 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)); tagged=1; j++; } if (tag_id < id) j++; if (tag_id > id) break; } osm_end_node(out_nodes); } PQclear(tag); PQclear(node); } for (;;) { way=PQexec(conn, "fetch 100000 from way"); if (! way) { fprintf(stderr, "Cannot setup cursor for ways: %s\n", PQerrorMessage(conn)); PQclear(node); exit(1); } count=PQntuples(way); if (! count) break; min=atol(PQgetvalue(way, 0, 0)); max=atol(PQgetvalue(way, count-1, 0)); 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); node=PQexec(conn, query); if (! node) { fprintf(stderr, "Cannot query way_node: %s\n", PQerrorMessage(conn)); exit(1); } sprintf(query,"select way_id,name,value from way_tag 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); } j=0; k=0; for (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(out_ways); } PQclear(tag); PQclear(node); PQclear(way); } 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