root/tools/osm2pgrouting/tags/release-0.1/src/OSMDocument.cpp

Revision 189, 4.8 KB (checked in by murray, 2 years ago)

osm2pgrouting: add a new tool to this project, which converts and imports osm data to a pgrouting
table

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 "OSMDocument.h"
23#include "Node.h"
24#include "Way.h"
25#include "math_functions.h"
26
27template< class Map >
28inline void ez_mapdelete( Map& c )
29{
30        typename Map::iterator it( c.begin() );
31        typename Map::iterator last( c.end() );
32        while( it!=last )
33        {
34                delete (*it++).second;
35        }
36}
37
38template< class Vector >
39inline void ez_vectordelete( Vector& c )
40{
41        typename Vector::iterator it( c.begin() );
42        typename Vector::iterator last( c.end() );
43        while( it!=last )
44        {
45                delete (*it++);
46        }
47}
48namespace osm
49{
50
51OSMDocument::OSMDocument()
52{
53}
54
55OSMDocument::~OSMDocument()
56{
57        ez_mapdelete( m_Nodes );
58        ez_vectordelete( m_Ways );             
59        ez_vectordelete( m_SplittedWays );
60}
61void OSMDocument::AddNode( Node* n )
62{
63        m_Nodes[n->id] = n;
64}
65
66void OSMDocument::AddWay( Way* w )
67{
68        m_Ways.push_back( w );
69}
70
71Node* OSMDocument::FindNode( long long nodeRefId )
72const
73{
74        std::map<long long, Node*>::const_iterator  it = m_Nodes.find( nodeRefId );
75        return (it!=m_Nodes.end() ) ? it->second : 0;
76}
77
78void OSMDocument::SplitWays()
79{
80       
81        std::vector<Way*>::const_iterator it(m_Ways.begin());
82        std::vector<Way*>::const_iterator last(m_Ways.end());
83
84        //splitted ways get a new ID
85        long long id=0;
86
87        while(it!=last)
88        {
89                Way* currentWay = *it++;
90               
91                std::vector<Node*>::const_iterator it_node( currentWay->m_NodeRefs.begin());   
92                std::vector<Node*>::const_iterator last_node( currentWay->m_NodeRefs.end());
93               
94                Node* backNode = currentWay->m_NodeRefs.back();
95
96
97
98                while(it_node!=last_node)
99                {
100                       
101                        Node* node = *it_node++;
102                        Node* secondNode=0;
103                        Node* lastNode=0;
104                       
105                        Way* splitted_way = new Way( id++, currentWay->visible );
106                        splitted_way->name=currentWay->name;
107                        splitted_way->highway=currentWay->highway;
108
109        //GeometryFromText('MULTILINESTRING(('||x1||' '||y1||','||x2||' '||y2||'))',4326);
110                       
111                        splitted_way->geom="MULTILINESTRING(("+ boost::lexical_cast<std::string>(node->lon) + " " + boost::lexical_cast<std::string>(node->lat) +",";
112                       
113                        splitted_way->AddNodeRef(node);
114                       
115                        bool found=false;
116                       
117                        if(it_node!=last_node)
118                        {
119                                while(it_node!=last_node && !found)
120                                {
121                                        splitted_way->AddNodeRef(*it_node);
122                                        if((*it_node)->numsOfUse>1)
123                                        {
124                                                found=true;
125                                                secondNode = *it_node;
126                                                splitted_way->AddNodeRef(secondNode);
127                                                double length = getLength(node,secondNode);
128                                                if(length<0)
129                                                        length*=-1;
130                                                splitted_way->length+=length;
131                                                splitted_way->geom+= boost::lexical_cast<std::string>(secondNode->lon) + " " + boost::lexical_cast<std::string>(secondNode->lat) + "))";
132                                               
133                                        }
134                                        else if(backNode==(*it_node))
135                                        {
136                                                lastNode=*it_node++;
137                                                splitted_way->AddNodeRef(lastNode);
138                                                double length = getLength(node,lastNode);
139                                                if(length<0)
140                                                        length*=-1;
141                                                splitted_way->length+=length;
142                                                splitted_way->geom+= boost::lexical_cast<std::string>(lastNode->lon) + " " + boost::lexical_cast<std::string>(lastNode->lat) + "))";
143                                        }
144                                        else
145                                        {
146                                                splitted_way->geom+= boost::lexical_cast<std::string>((*it_node)->lon) + " " + boost::lexical_cast<std::string>((*it_node)->lat) + ",";
147                                                *it_node++;
148                                        }
149                                }
150                        }
151                               
152                        if(splitted_way->m_NodeRefs.front()!=splitted_way->m_NodeRefs.back())
153                                m_SplittedWays.push_back(splitted_way);
154                        else
155                        {
156                                delete splitted_way;
157                                splitted_way=0;
158                        }
159                               
160                }
161
162        }
163
164} // end SplitWays
165
166} // end namespace osm
Note: See TracBrowser for help on using the browser.