1 | /*************************************************************************** |
---|
2 | * Copyright (C) 2008 by Daniel Wendt * |
---|
3 | * gentoo.murray@gmail.com * |
---|
4 | * * |
---|
5 | * This program is free software; you can redistribute it and/or modify * |
---|
6 | * it under the terms of the GNU General Public License as published by * |
---|
7 | * the Free Software Foundation; either version 2 of the License, or * |
---|
8 | * (at your option) any later version. * |
---|
9 | * * |
---|
10 | * This program is distributed in the hope that it will be useful, * |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
13 | * GNU General Public License for more details. * |
---|
14 | * * |
---|
15 | * You should have received a copy of the GNU General Public License * |
---|
16 | * along with this program; if not, write to the * |
---|
17 | * Free Software Foundation, Inc., * |
---|
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
---|
19 | ***************************************************************************/ |
---|
20 | |
---|
21 | #include "stdafx.h" |
---|
22 | #include "Export2DB.h" |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | Export2DB::Export2DB(std::string host, std::string user, std::string dbname, std::string port, std::string passwd) |
---|
27 | :mycon(0) |
---|
28 | { |
---|
29 | |
---|
30 | this->conninf="host="+host+" user="+user+" dbname="+ dbname +" port="+port; |
---|
31 | if(!passwd.empty()) |
---|
32 | this->conninf+=" password="+passwd; |
---|
33 | |
---|
34 | } |
---|
35 | |
---|
36 | Export2DB::~Export2DB() |
---|
37 | { |
---|
38 | PQfinish(mycon); |
---|
39 | } |
---|
40 | |
---|
41 | int Export2DB::connect() |
---|
42 | { |
---|
43 | cout << conninf<< endl; |
---|
44 | //mycon =PQconnectdb("user=postgres dbname=template1 hostaddr=127.0.0.1 port=5432"); |
---|
45 | mycon =PQconnectdb(conninf.c_str()); |
---|
46 | |
---|
47 | ConnStatusType type =PQstatus(mycon); |
---|
48 | if(type==CONNECTION_BAD) |
---|
49 | { |
---|
50 | cout << "connection failed"<< endl; |
---|
51 | return 1; |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | cout << "connection success"<< endl; |
---|
56 | return 0; |
---|
57 | } |
---|
58 | /*** |
---|
59 | CONNECTION_STARTED: Waiting for connection to be made. |
---|
60 | CONNECTION_MADE: Connection OK; waiting to send. |
---|
61 | CONNECTION_AWAITING_RESPONSE: Waiting for a response from the postmaster. |
---|
62 | CONNECTION_AUTH_OK: Received authentication; waiting for backend start-up. |
---|
63 | CONNECTION_SETENV: Negotiating environment. |
---|
64 | ***/ |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | void Export2DB::createTables() |
---|
69 | { |
---|
70 | PGresult *result = PQexec(mycon, "CREATE TABLE nodes (ID integer PRIMARY KEY, lon decimal(11,8), lat decimal(11,8), numOfUse smallint);"); |
---|
71 | std::cout << "Nodes table created" << std::endl; |
---|
72 | result = PQexec(mycon, "CREATE TABLE ways (gid integer, class_id integer, length double precision, name char(200), x1 double precision, y1 double precision, x2 double precision,y2 double precision, reverse_cost double precision,rule text, to_cost double precision, PRIMARY KEY(gid)); SELECT AddGeometryColumn('ways','the_geom',4326,'MULTILINESTRING',2);"); |
---|
73 | std::cout << "Ways table created" << std::endl; |
---|
74 | result = PQexec(mycon, "CREATE TABLE types (id integer, name char(200));"); |
---|
75 | std::cout << "Types table created" << std::endl; |
---|
76 | result = PQexec(mycon, "CREATE TABLE classes (id integer, type_id integer, name char(200), cost double precision);"); |
---|
77 | std::cout << "Classes table created" << std::endl; |
---|
78 | } |
---|
79 | |
---|
80 | void Export2DB::dropTables() |
---|
81 | { |
---|
82 | PGresult *result = PQexec(mycon, "DROP TABLE ways; DROP TABLE nodes; DROP TABLE types; DROP TABLE classes;"); |
---|
83 | } |
---|
84 | |
---|
85 | void Export2DB::exportNode(long long id, double lon, double lat, ushort numOfUse ) |
---|
86 | { |
---|
87 | char tmp_id[20]; |
---|
88 | char tmp_lon[15]; |
---|
89 | char tmp_lat[15]; |
---|
90 | |
---|
91 | sprintf(tmp_id,"%lld",id); |
---|
92 | gcvt(lon,12,tmp_lon); |
---|
93 | gcvt(lat,12,tmp_lat); |
---|
94 | |
---|
95 | std::string query = "INSERT into nodes(id,lon,lat) values("; |
---|
96 | query+= tmp_id; |
---|
97 | query+=","; |
---|
98 | query+= tmp_lon; |
---|
99 | query+=","; |
---|
100 | query+= tmp_lat; |
---|
101 | query+=");"; |
---|
102 | |
---|
103 | PGresult *result = PQexec(mycon, query.c_str()); |
---|
104 | } |
---|
105 | |
---|
106 | void Export2DB::exportWay(Way* way) |
---|
107 | { |
---|
108 | std::string query = "INSERT into ways(gid, class_id, length, x1, y1, x2, y2, the_geom, reverse_cost"; |
---|
109 | if(!way->name.empty()) |
---|
110 | query+=", name"; |
---|
111 | query+=") values("; |
---|
112 | |
---|
113 | query+=boost::lexical_cast<std::string>(way->id) + ", (SELECT id FROM classes WHERE name ='" + boost::lexical_cast<std::string>(way->clss) + "')," + boost::lexical_cast<std::string>(way->length) + "," |
---|
114 | + boost::lexical_cast<std::string>(way->m_NodeRefs.front()->lon) + ","+ boost::lexical_cast<std::string>(way->m_NodeRefs.front()->lat) + "," |
---|
115 | + boost::lexical_cast<std::string>(way->m_NodeRefs.back()->lon) + ","+ boost::lexical_cast<std::string>(way->m_NodeRefs.back()->lat) + ","; |
---|
116 | query+="GeometryFromText('" + way->geom +"', 4326)"; |
---|
117 | |
---|
118 | if(way->oneway) |
---|
119 | { |
---|
120 | query+=", "+ boost::lexical_cast<std::string>(way->length*1000000); |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | query+=", "+ boost::lexical_cast<std::string>(way->length); |
---|
125 | } |
---|
126 | |
---|
127 | if(!way->name.empty()) |
---|
128 | query+=",$$"+ way->name +"$$"; |
---|
129 | query+=");"; |
---|
130 | //std::cout << query <<std::endl; |
---|
131 | PGresult *result = PQexec(mycon, query.c_str()); |
---|
132 | } |
---|
133 | |
---|
134 | void Export2DB::exportType(Type* type) |
---|
135 | { |
---|
136 | std::string query = "INSERT into types(id, name) values("; |
---|
137 | |
---|
138 | query+=boost::lexical_cast<std::string>(type->id) + ", '" + type->name +"');"; |
---|
139 | PGresult *result = PQexec(mycon, query.c_str()); |
---|
140 | } |
---|
141 | |
---|
142 | void Export2DB::exportClass(Type* type, Class* clss) |
---|
143 | { |
---|
144 | std::string query = "INSERT into classes(id, type_id, name) values("; |
---|
145 | |
---|
146 | query+=boost::lexical_cast<std::string>(clss->id) + ", " + boost::lexical_cast<std::string>(type->id) + ", '" + clss->name +"');"; |
---|
147 | PGresult *result = PQexec(mycon, query.c_str()); |
---|
148 | } |
---|
149 | |
---|
150 | void Export2DB::createTopology() |
---|
151 | { |
---|
152 | PGresult *result = PQexec(mycon,"ALTER TABLE ways ADD COLUMN source integer;"); |
---|
153 | result = PQexec(mycon,"ALTER TABLE ways ADD COLUMN target integer;"); |
---|
154 | result = PQexec(mycon,"CREATE INDEX source_idx ON ways(source);"); |
---|
155 | result = PQexec(mycon,"CREATE INDEX target_idx ON ways(target);"); |
---|
156 | result = PQexec(mycon,"CREATE INDEX geom_idx ON ways USING GIST(the_geom GIST_GEOMETRY_OPS);"); |
---|
157 | result = PQexec(mycon,"SELECT assign_vertex_id('ways', 0.00001, 'the_geom', 'gid');"); |
---|
158 | |
---|
159 | } |
---|