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 | result = PQexec(mycon, "CREATE TABLE ways (ID integer , cost double precision, name char(200), x1 double precision, y1 double precision, x2 double precision,y2 double precision, PRIMARY KEY(ID)); SELECT AddGeometryColumn('ways','the_geom',4326,'MULTILINESTRING',2);"); |
---|
72 | } |
---|
73 | |
---|
74 | void Export2DB::dropTables() |
---|
75 | { |
---|
76 | PGresult *result = PQexec(mycon, "DROP TABLE ways; DROP TABLE nodes;"); |
---|
77 | } |
---|
78 | |
---|
79 | void Export2DB::exportNode(long long id, double lon, double lat, ushort numOfUse ) |
---|
80 | { |
---|
81 | char tmp_id[20]; |
---|
82 | char tmp_lon[15]; |
---|
83 | char tmp_lat[15]; |
---|
84 | |
---|
85 | sprintf(tmp_id,"%lld",id); |
---|
86 | gcvt(lon,12,tmp_lon); |
---|
87 | gcvt(lat,12,tmp_lat); |
---|
88 | |
---|
89 | std::string query = "INSERT into nodes(id,lon,lat) values("; |
---|
90 | query+= tmp_id; |
---|
91 | query+=","; |
---|
92 | query+= tmp_lon; |
---|
93 | query+=","; |
---|
94 | query+= tmp_lat; |
---|
95 | query+=");"; |
---|
96 | |
---|
97 | PGresult *result = PQexec(mycon, query.c_str()); |
---|
98 | } |
---|
99 | |
---|
100 | void Export2DB::exportWay(Way* way) |
---|
101 | { |
---|
102 | std::string query = "INSERT into ways(id, cost, x1, y1, x2, y2, the_geom"; |
---|
103 | if(!way->name.empty()) |
---|
104 | query+=", name"; |
---|
105 | query+=") values("; |
---|
106 | |
---|
107 | query+=boost::lexical_cast<std::string>(way->id) + "," + boost::lexical_cast<std::string>(way->length) + "," |
---|
108 | + boost::lexical_cast<std::string>(way->m_NodeRefs.front()->lon) + ","+ boost::lexical_cast<std::string>(way->m_NodeRefs.front()->lat) + "," |
---|
109 | + boost::lexical_cast<std::string>(way->m_NodeRefs.back()->lon) + ","+ boost::lexical_cast<std::string>(way->m_NodeRefs.back()->lat) + ","; |
---|
110 | query+="GeometryFromText('" + way->geom +"', 4326)"; |
---|
111 | if(!way->name.empty()) |
---|
112 | query+=",'"+ way->name +"'"; |
---|
113 | query+=");"; |
---|
114 | //std::cout << query <<std::endl; |
---|
115 | PGresult *result = PQexec(mycon, query.c_str()); |
---|
116 | } |
---|
117 | |
---|
118 | void Export2DB::createTopology() |
---|
119 | { |
---|
120 | PGresult *result = PQexec(mycon,"ALTER TABLE ways ADD COLUMN source integer;"); |
---|
121 | result = PQexec(mycon,"ALTER TABLE ways ADD COLUMN target integer;"); |
---|
122 | result = PQexec(mycon,"CREATE INDEX source_idx ON ways(source);"); |
---|
123 | result = PQexec(mycon,"CREATE INDEX target_idx ON ways(target);"); |
---|
124 | result = PQexec(mycon,"CREATE INDEX geom_idx ON ways USING GIST(the_geom GIST_GEOMETRY_OPS);"); |
---|
125 | result = PQexec(mycon,"SELECT assign_vertex_id('ways', 0.0001, 'the_geom', 'id');"); |
---|
126 | |
---|
127 | } |
---|