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 "OSMDocumentParserCallback.h"
|
---|
23 | #include "OSMDocument.h"
|
---|
24 | #include "Way.h"
|
---|
25 | #include "Node.h"
|
---|
26 |
|
---|
27 | // define here, which streetstype you want to parse
|
---|
28 | // for applying this filter, compile with "DISTRICT" as flag (g++ -DRESTRICT)
|
---|
29 | //#define _FILTER if(m_pActWay->highway == "motorway" || m_pActWay->highway == "primary" || m_pActWay->highway == "secondary")
|
---|
30 |
|
---|
31 | namespace osm
|
---|
32 | {
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | Parser callback for OSMDocument files
|
---|
37 | */
|
---|
38 | void OSMDocumentParserCallback::StartElement( const char *name, const char** atts )
|
---|
39 | {
|
---|
40 | if( strcmp(name,"nd") == 0 )
|
---|
41 | {
|
---|
42 | if( m_pActWay && atts != NULL )
|
---|
43 | {
|
---|
44 | const char* name = *atts++;
|
---|
45 | const char* value = *atts++;
|
---|
46 | if( strcmp(name,"ref")==0 )
|
---|
47 | {
|
---|
48 | long long nodeRefId = atol( value );
|
---|
49 | m_pActWay->AddNodeRef( m_rDocument.FindNode( nodeRefId ) ); |
---|
50 | Node * node = m_rDocument.FindNode( nodeRefId ); |
---|
51 | if(node != 0 ){ |
---|
52 | node->numsOfUse+=1; |
---|
53 | }else { |
---|
54 | std::cout << "Reference nd=" << nodeRefId << " has no corresponding Node Entry (Maybe Node entry after Reference?)" << std::endl; |
---|
55 | } |
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | else if( strcmp(name,"node") == 0 )
|
---|
60 | {
|
---|
61 | if (atts != NULL)
|
---|
62 | {
|
---|
63 | long long id=-1;
|
---|
64 | double lat=-1;
|
---|
65 | double lon=-1;
|
---|
66 | const char** attribut = (const char**)atts;
|
---|
67 | while( *attribut != NULL )
|
---|
68 | {
|
---|
69 | const char* name = *attribut++;
|
---|
70 | const char* value = *attribut++;
|
---|
71 | if( strcmp( name, "id" ) == 0 )
|
---|
72 | {
|
---|
73 | id = atol( value);
|
---|
74 | }
|
---|
75 | else if( strcmp( name, "lat" ) == 0 )
|
---|
76 | {
|
---|
77 | lat = atof( value );
|
---|
78 | }
|
---|
79 | else if( strcmp( name, "lon" ) == 0 )
|
---|
80 | {
|
---|
81 | lon = atof( value );
|
---|
82 | }
|
---|
83 | }
|
---|
84 | if( id>0 ) m_rDocument.AddNode( new Node( id, lat, lon ) );
|
---|
85 | }
|
---|
86 | }
|
---|
87 | else if( strcmp(name,"tag") == 0 )
|
---|
88 | {
|
---|
89 | // <tag k="name" v="Pfänderweg"/>
|
---|
90 | if (atts != NULL)
|
---|
91 | {
|
---|
92 | std::string k;
|
---|
93 | std::string v;
|
---|
94 | const char** attribut = (const char**)atts;
|
---|
95 | while( *attribut != NULL )
|
---|
96 | {
|
---|
97 | const char* name = *attribut++;
|
---|
98 | const char* value = *attribut++;
|
---|
99 | if( strcmp( name, "k" ) == 0 )
|
---|
100 | {
|
---|
101 | k = value;
|
---|
102 | }
|
---|
103 | else if( strcmp( name, "v" ) == 0 )
|
---|
104 | {
|
---|
105 | v = value;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | if( !k.empty() )
|
---|
109 | {
|
---|
110 | if( m_pActWay && k.compare("name")==0 )
|
---|
111 | {
|
---|
112 | m_pActWay->name = v;
|
---|
113 | }
|
---|
114 | else if( m_pActWay && k.compare("oneway")==0 )
|
---|
115 | {
|
---|
116 | m_pActWay->oneway = true; |
---|
117 | std::cout<<"Edge "<<m_pActWay->id<<" is oneway"<<std::endl; |
---|
118 | |
---|
119 | }
|
---|
120 | //else if( m_pActWay && k.compare("highway")==0 )
|
---|
121 | else if( m_pActWay && m_rDocument.m_rConfig.m_Types.count(k) )
|
---|
122 | {
|
---|
123 | m_pActWay->type = k;
|
---|
124 | m_pActWay->clss = v;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else if( strcmp(name,"way") == 0 )
|
---|
130 | {
|
---|
131 | if (atts != NULL)
|
---|
132 | {
|
---|
133 | long long id=-1;
|
---|
134 | bool visibility = false;
|
---|
135 | const char** attribut = (const char**)atts;
|
---|
136 | while( *attribut != NULL )
|
---|
137 | {
|
---|
138 | const char* name = *attribut++;
|
---|
139 | const char* value = *attribut++;
|
---|
140 | if( strcmp( name, "id" ) == 0 )
|
---|
141 | {
|
---|
142 | id = atol( value);
|
---|
143 | }
|
---|
144 | else if( strcmp( name, "visible" ) == 0 )
|
---|
145 | {
|
---|
146 | visibility = strcmp(value,"true")==0;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | if( id>0 )
|
---|
150 | {
|
---|
151 | m_pActWay = new Way( id, visibility );
|
---|
152 |
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | else if( strcmp(name,"osm") == 0 )
|
---|
157 | {
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | void OSMDocumentParserCallback::EndElement( const char* name )
|
---|
164 | {
|
---|
165 | if( strcmp(name,"way") == 0 )
|
---|
166 | {
|
---|
167 | //#ifdef RESTRICT
|
---|
168 | //_FILTER
|
---|
169 | |
---|
170 | if( m_rDocument.m_rConfig.m_Types.count(m_pActWay->type) && m_rDocument.m_rConfig.m_Types[m_pActWay->type]->m_Classes.count(m_pActWay->clss) ) |
---|
171 | {
|
---|
172 | //#endif
|
---|
173 | std::cout<<"We need a way of type "<<m_pActWay->type<<" and class "<< m_pActWay->clss<<std::endl; |
---|
174 |
|
---|
175 | m_rDocument.AddWay( m_pActWay );
|
---|
176 | |
---|
177 | //#ifdef RESTRICT
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | std::cout<<"We DON'T need a way of type "<<m_pActWay->type<<" and class "<< m_pActWay->clss<<std::endl; |
---|
182 | delete m_pActWay;
|
---|
183 | }
|
---|
184 | //#endif
|
---|
185 |
|
---|
186 | m_pActWay = 0;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | }; // end namespace osm
|
---|