root/tools/osm2pgrouting/trunk/src/osm2pgrouting.cpp

Revision 212, 6.8 KB (checked in by anton, 2 years ago)

osm2pgrouting code cleaned up

Line 
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 "Configuration.h"
23#include "ConfigurationParserCallback.h"
24#include "OSMDocument.h"
25#include "OSMDocumentParserCallback.h"
26#include "Way.h"
27#include "Node.h"
28#include "Export2DB.h"
29
30using namespace osm;
31using namespace xml;
32using namespace std;
33
34void _error()
35{
36                                cout << "following params are required: " << endl;
37                                cout << "-file <file>  -- name of your osm xml file" << endl;
38                                cout << "-conf <conf>  -- name of your configuration xml file" << endl;
39                                cout << "-dbname <dbname> -- name of your database" << endl;
40                                cout << "-user <user> -- name of the user, which have write access to the database" << endl;
41                                cout << "optional:" << endl;
42                                cout << "-host <host>  -- host of your postgresql database (default: 127.0.0.1)" << endl;
43                                cout << "-port <port> -- port of your database (default: 5432)" << endl;
44                                cout << "-passwd <passwd> --  password for database access" << endl;
45                                cout << "-clean -- drop peviously created tables" << endl;
46                                       
47}
48
49int main(int argc, char* argv[])
50{
51        std::string file;
52        std::string cFile;
53        std::string host="127.0.0.1";
54        std::string user;
55        std::string port="5432";
56        std::string dbname;
57        std::string passwd;
58        bool clean = false;
59        if(argc >=7 && argc <=13)
60        {
61                int i=1;
62                while( i<argc)
63                {
64                        if(strcmp(argv[i],"-file")==0)
65                        {       
66                                i++;
67                                file = argv[i];
68                        }
69
70                        else if(strcmp(argv[i],"-conf")==0)
71                        {       
72                                i++;
73                                cFile = argv[i];
74                        }
75                               
76                        else if(strcmp(argv[i],"-host")==0)
77                        {
78                                i++;
79                                host = argv[i];
80                        }
81                        else if(strcmp(argv[i],"-dbname")==0)
82                        {
83                                i++;
84                                dbname = argv[i];
85                        }
86                        else if(strcmp(argv[i],"-user")==0)
87                        {
88                                i++;
89                                user = argv[i];
90                        }
91                        else if(strcmp(argv[i],"-port")==0)
92                        {
93                                i++;
94                                port = argv[i];
95                        }
96                        else if(strcmp(argv[i],"-passwd")==0)
97                        {
98                                i++;
99                                passwd = argv[i];
100                        }
101                        else if(strcmp(argv[i],"-clean")==0)
102                        {
103                                clean = true;
104                        }
105                        else
106                        {
107                                cout << "unknown paramer: " << argv[i] << endl;
108                                _error();
109                                return 1;
110                        }
111                       
112                        i++;
113                }
114               
115        }
116        else
117        {
118                _error();
119                return 1;
120        }
121       
122        if(file.empty() || cFile.empty() || dbname.empty() || user.empty())
123        {
124                _error();
125                return 1;
126        }
127       
128        Export2DB test(host, user, dbname, port, passwd);
129        if(test.connect()==1)
130                return 1;
131
132
133        XMLParser parser;
134       
135        cout << "Trying to load config file " << cFile.c_str() << endl;
136
137        Configuration* config = new Configuration();
138        ConfigurationParserCallback cCallback( *config );
139
140        cout << "Trying to parse config" << endl;
141
142        int ret = parser.Parse( cCallback, cFile.c_str() );
143        if( ret!=0 ) return 1;
144
145        cout << "Trying to load data" << endl;
146
147        OSMDocument* document = new OSMDocument( *config );
148        OSMDocumentParserCallback callback( *document );
149
150        cout << "Trying to parse data" << endl;
151
152        ret = parser.Parse( callback, file.c_str() );
153        if( ret!=0 ) return 1;
154       
155        cout << "Split ways" << endl;
156
157        document->SplitWays();
158        //############# Export2DB
159        {
160
161                if( clean )
162                {
163                        test.dropTables();
164                }
165               
166                test.createTables();
167               
168                std::map<std::string, Type*>& types= config->m_Types;
169                std::map<std::string, Type*>::iterator tIt(types.begin());
170                std::map<std::string, Type*>::iterator tLast(types.end());
171               
172                while(tIt!=tLast)
173                {
174                        Type* type = (*tIt++).second;
175                        test.exportType(type);
176
177                        std::map<std::string, Class*>& classes= type->m_Classes;
178                        std::map<std::string, Class*>::iterator cIt(classes.begin());
179                        std::map<std::string, Class*>::iterator cLast(classes.end());
180
181                        while(cIt!=cLast)
182                        {
183                                Class* clss = (*cIt++).second;
184                                test.exportClass(type, clss);
185                        }
186                }
187               
188
189                std::map<long long, Node*>& nodes= document->m_Nodes;
190                std::map<long long, Node*>::iterator it(nodes.begin());
191                std::map<long long, Node*>::iterator last(nodes.end());
192               
193                while(it!=last)
194                {
195                        Node* node = (*it++).second;
196                        test.exportNode(node->id,node->lon, node->lat, node->numsOfUse);
197                }
198               
199                std::vector<Way*>& ways= document->m_SplittedWays;
200                std::vector<Way*>::iterator it_way( ways.begin() );
201                std::vector<Way*>::iterator last_way( ways.end() );     
202                while( it_way!=last_way )
203                {
204                        Way* pWay = *it_way++;
205                        test.exportWay(pWay);
206                }
207                cout << "create topology" << endl;
208                test.createTopology();
209        }       
210       
211        //#############
212       
213        /*
214        std::vector<Way*>& ways= document.m_Ways;
215        std::vector<Way*>::iterator it( ways.begin() );
216        std::vector<Way*>::iterator last( ways.end() );
217        while( it!=last )
218        {
219                Way* pWay = *it;
220               
221                if( !pWay->name.empty() )
222                {
223                        if( pWay->m_NodeRefs.empty() )
224                        {
225                                std::cout << pWay->name.c_str() << endl;
226                        }
227                        else
228                        {
229                                Node* n0 = pWay->m_NodeRefs.front();
230                                Node* n1 = pWay->m_NodeRefs.back();
231                                //if(n1->numsOfUse==1)
232                                //cout << "way-id: " << pWay->id << " name: " << pWay->name <<endl;
233                                //std::cout << n0->lon << " "  << n0->lat << " " << n1->lon << " " << n1->lat << " " << pWay->name.c_str() << " highway: " << pWay->highway.c_str() << " Start numberOfUse: " << n0->numsOfUse << " End numberOfUse: " << n1->numsOfUse  << " ID: " << n1->id <<  endl;
234                        }
235                }
236                if( pWay->id == 20215432 ) // Pfaenderweg
237                {
238                        cout << pWay->name << endl;
239                        int a=4;
240                }
241                ++it;
242        }
243        */
244       
245        cout << "#########################" << endl;
246       
247        cout << "size of streets: " << document->m_Ways.size() <<       endl;
248        cout << "size of splitted ways : " << document->m_SplittedWays.size() <<        endl;
249
250        cout << "finished" << endl;
251
252        //string n;
253        //getline( cin, n );
254        return 0;
255}
256
Note: See TracBrowser for help on using the browser.