1- use osmpbfreader;
1+ use osmpbfreader:: objects :: { Node , NodeId , OsmId , Ref , Relation , RelationId , Tags , Way , WayId } ;
22use std:: fs:: File ;
33
44#[ derive( Debug , Default , PartialEq ) ]
@@ -29,30 +29,204 @@ const PBF_STATS_HIGHWAY: Stats = Stats {
2929 sum_relations_ids : 0 ,
3030} ;
3131
32+ fn get_expected_objects ( ) -> ( Vec < Node > , Vec < Way > , Vec < Relation > ) {
33+ let nodes: Vec < Node > = vec ! [
34+ Node {
35+ id: NodeId ( 1724610081 ) ,
36+ tags: Tags :: new( ) ,
37+ decimicro_lat: ( 10.2871538 * 1e7 ) as i32 ,
38+ decimicro_lon: ( -109.2143679 * 1e7 ) as i32 ,
39+ } ,
40+ Node {
41+ id: NodeId ( 1724610082 ) ,
42+ tags: Tags :: new( ) ,
43+ decimicro_lat: ( 10.2872043 * 1e7 ) as i32 ,
44+ decimicro_lon: ( -109.2157633 * 1e7 ) as i32 ,
45+ } ,
46+ Node {
47+ id: NodeId ( 2710971331 ) ,
48+ tags: [
49+ ( "historic" . into( ) , "wreck" . into( ) ) ,
50+ ( "seamark:type" . into( ) , "wreck" . into( ) ) ,
51+ ]
52+ . into_iter( )
53+ . collect( ) ,
54+ decimicro_lat: ( 10.2873955 * 1e7 ) as i32 ,
55+ decimicro_lon: ( -109.2123449 * 1e7 ) as i32 ,
56+ } ,
57+ Node {
58+ id: NodeId ( 12710481252 ) ,
59+ tags: Tags :: new( ) ,
60+ decimicro_lat: ( 10.2925456 * 1e7 ) as i32 ,
61+ decimicro_lon: ( -109.2212479 * 1e7 ) as i32 ,
62+ } ,
63+ ] ;
64+ let ways: Vec < Way > = vec ! [
65+ Way {
66+ id: WayId ( 633118718 ) ,
67+ tags: [
68+ ( "natural" . into( ) , "wood" . into( ) ) ,
69+ ( "trees" . into( ) , "coconut_palms" . into( ) ) ,
70+ ]
71+ . into_iter( )
72+ . collect( ) ,
73+ nodes: vec![
74+ NodeId ( 5976744399 ) ,
75+ NodeId ( 5976744400 ) ,
76+ NodeId ( 5976744401 ) ,
77+ NodeId ( 5976744402 ) ,
78+ NodeId ( 5976744399 ) ,
79+ ] ,
80+ } ,
81+ Way {
82+ id: WayId ( 1253640658 ) ,
83+ tags: [ ( "natural" . into( ) , "wood" . into( ) ) ] . into_iter( ) . collect( ) ,
84+ nodes: vec![
85+ NodeId ( 11653090668 ) ,
86+ NodeId ( 11653090669 ) ,
87+ NodeId ( 11653090670 ) ,
88+ NodeId ( 11653090668 ) ,
89+ ] ,
90+ } ,
91+ Way {
92+ id: WayId ( 1372754726 ) ,
93+ tags: {
94+ let mut tags = Tags :: new( ) ;
95+ tags. insert( "aeroway" . into( ) , "taxiway" . into( ) ) ;
96+ tags
97+ } ,
98+ nodes: vec![
99+ NodeId ( 12710481236 ) ,
100+ NodeId ( 12710481237 ) ,
101+ NodeId ( 12710481238 ) ,
102+ NodeId ( 12710481239 ) ,
103+ ] ,
104+ } ,
105+ ] ;
106+ let relations: Vec < Relation > = vec ! [
107+ Relation {
108+ id: RelationId ( 5883537 ) ,
109+ tags: [
110+ ( "natural" . into( ) , "beach" . into( ) ) ,
111+ ( "type" . into( ) , "multipolygon" . into( ) ) ,
112+ ]
113+ . into_iter( )
114+ . collect( ) ,
115+ refs: vec![
116+ Ref {
117+ member: OsmId :: Way ( WayId ( 392464137 ) ) ,
118+ role: "inner" . into( ) ,
119+ } ,
120+ Ref {
121+ member: OsmId :: Way ( WayId ( 160415456 ) ) ,
122+ role: "outer" . into( ) ,
123+ } ,
124+ ] ,
125+ } ,
126+ Relation {
127+ id: RelationId ( 8803113 ) ,
128+ tags: [
129+ ( "place" . into( ) , "islet" . into( ) ) ,
130+ ( "type" . into( ) , "multipolygon" . into( ) ) ,
131+ ]
132+ . into_iter( )
133+ . collect( ) ,
134+ refs: vec![ Ref {
135+ member: OsmId :: Way ( WayId ( 633118720 ) ) ,
136+ role: "outer" . into( ) ,
137+ } ] ,
138+ } ,
139+ ] ;
140+
141+ ( nodes, ways, relations)
142+ }
143+
144+ fn get_expected_objects_highway ( ) -> ( Vec < Node > , Vec < Way > , Vec < Relation > ) {
145+ let nodes: Vec < Node > = vec ! [
146+ Node {
147+ id: NodeId ( 12710481223 ) ,
148+ tags: Tags :: new( ) ,
149+ decimicro_lat: ( 10.2901053 * 1e7 ) as i32 ,
150+ decimicro_lon: ( -109.2103612 * 1e7 ) as i32 ,
151+ } ,
152+ Node {
153+ id: NodeId ( 12710481226 ) ,
154+ tags: Tags :: new( ) ,
155+ decimicro_lat: ( 10.2897482 * 1e7 ) as i32 ,
156+ decimicro_lon: ( -109.2101919 * 1e7 ) as i32 ,
157+ } ,
158+ ] ;
159+ let ways: Vec < Way > = vec ! [ Way {
160+ id: WayId ( 1372754721 ) ,
161+ tags: {
162+ let mut tags = Tags :: new( ) ;
163+ tags. insert( "highway" . into( ) , "residential" . into( ) ) ;
164+ tags. insert( "name" . into( ) , "Clipperton St" . into( ) ) ;
165+ tags
166+ } ,
167+ nodes: vec![
168+ NodeId ( 12710481223 ) ,
169+ NodeId ( 12710481226 ) ,
170+ NodeId ( 12710481233 ) ,
171+ NodeId ( 12710481224 ) ,
172+ NodeId ( 12710481225 ) ,
173+ ] ,
174+ } ] ;
175+ let relations: Vec < Relation > = vec ! [ ] ;
176+
177+ ( nodes, ways, relations)
178+ }
179+
32180#[ test]
33181fn read_pbf ( ) {
34182 let file = File :: open ( PBF ) . unwrap ( ) ;
35183 let mut pbf = osmpbfreader:: OsmPbfReader :: new ( file) ;
36184 let mut stats = Stats :: default ( ) ;
37185
186+ let ( exp_nodes, exp_ways, exp_relations) = get_expected_objects ( ) ;
187+ let mut num_found_nodes = 0 ;
188+ let mut num_found_ways = 0 ;
189+ let mut num_found_relations = 0 ;
190+
38191 for obj in pbf. iter ( ) {
39192 let obj = obj. unwrap ( ) ;
40193 match obj {
41194 osmpbfreader:: OsmObj :: Node ( n) => {
42195 stats. num_nodes += 1 ;
43196 stats. sum_nodes_ids += n. id . 0 ;
197+ for exp_node in & exp_nodes {
198+ if exp_node. id . 0 == n. id . 0 {
199+ num_found_nodes += 1 ;
200+ assert_eq ! ( & n, exp_node) ;
201+ }
202+ }
44203 }
45204 osmpbfreader:: OsmObj :: Way ( w) => {
46205 stats. num_ways += 1 ;
47206 stats. sum_ways_ids += w. id . 0 ;
207+ for exp_way in & exp_ways {
208+ if exp_way. id . 0 == w. id . 0 {
209+ num_found_ways += 1 ;
210+ assert_eq ! ( & w, exp_way) ;
211+ }
212+ }
48213 }
49214 osmpbfreader:: OsmObj :: Relation ( r) => {
50215 stats. num_relations += 1 ;
51216 stats. sum_relations_ids += r. id . 0 ;
217+ for exp_relation in & exp_relations {
218+ if exp_relation. id . 0 == r. id . 0 {
219+ num_found_relations += 1 ;
220+ assert_eq ! ( & r, exp_relation) ;
221+ }
222+ }
52223 }
53224 }
54225 }
55226 assert_eq ! ( stats, PBF_STATS_ALL ) ;
227+ assert_eq ! ( num_found_nodes, exp_nodes. len( ) ) ;
228+ assert_eq ! ( num_found_ways, exp_ways. len( ) ) ;
229+ assert_eq ! ( num_found_relations, exp_relations. len( ) ) ;
56230}
57231
58232#[ test]
@@ -61,24 +235,50 @@ fn read_pbf_par_iter() {
61235 let mut pbf = osmpbfreader:: OsmPbfReader :: new ( file) ;
62236 let mut stats = Stats :: default ( ) ;
63237
238+ let ( exp_nodes, exp_ways, exp_relations) = get_expected_objects ( ) ;
239+ let mut num_found_nodes = 0 ;
240+ let mut num_found_ways = 0 ;
241+ let mut num_found_relations = 0 ;
242+
64243 for obj in pbf. par_iter ( ) {
65244 let obj = obj. unwrap ( ) ;
66245 match obj {
67246 osmpbfreader:: OsmObj :: Node ( n) => {
68247 stats. num_nodes += 1 ;
69248 stats. sum_nodes_ids += n. id . 0 ;
249+ for exp_node in & exp_nodes {
250+ if exp_node. id . 0 == n. id . 0 {
251+ num_found_nodes += 1 ;
252+ assert_eq ! ( & n, exp_node) ;
253+ }
254+ }
70255 }
71256 osmpbfreader:: OsmObj :: Way ( w) => {
72257 stats. num_ways += 1 ;
73258 stats. sum_ways_ids += w. id . 0 ;
259+ for exp_way in & exp_ways {
260+ if exp_way. id . 0 == w. id . 0 {
261+ num_found_ways += 1 ;
262+ assert_eq ! ( & w, exp_way) ;
263+ }
264+ }
74265 }
75266 osmpbfreader:: OsmObj :: Relation ( r) => {
76267 stats. num_relations += 1 ;
77268 stats. sum_relations_ids += r. id . 0 ;
269+ for exp_relation in & exp_relations {
270+ if exp_relation. id . 0 == r. id . 0 {
271+ num_found_relations += 1 ;
272+ assert_eq ! ( & r, exp_relation) ;
273+ }
274+ }
78275 }
79276 }
80277 }
81278 assert_eq ! ( stats, PBF_STATS_ALL ) ;
279+ assert_eq ! ( num_found_nodes, exp_nodes. len( ) ) ;
280+ assert_eq ! ( num_found_ways, exp_ways. len( ) ) ;
281+ assert_eq ! ( num_found_relations, exp_relations. len( ) ) ;
82282}
83283
84284#[ test]
@@ -88,6 +288,11 @@ fn read_pbf_highway() {
88288 let mut stats = Stats :: default ( ) ;
89289 let mut sum_ids = 0 ;
90290
291+ let ( exp_nodes, exp_ways, exp_relations) = get_expected_objects_highway ( ) ;
292+ let mut num_found_nodes = 0 ;
293+ let mut num_found_ways = 0 ;
294+ let mut num_found_relations = 0 ;
295+
91296 let objs = pbf
92297 . get_objs_and_deps ( |obj| obj. is_way ( ) && obj. tags ( ) . contains_key ( "highway" ) )
93298 . unwrap ( ) ;
@@ -97,14 +302,34 @@ fn read_pbf_highway() {
97302 osmpbfreader:: OsmObj :: Node ( n) => {
98303 stats. num_nodes += 1 ;
99304 stats. sum_nodes_ids += n. id . 0 ;
305+ for exp_node in & exp_nodes {
306+ if exp_node. id . 0 == n. id . 0 {
307+ num_found_nodes += 1 ;
308+ assert_eq ! ( n, exp_node) ;
309+ }
310+ }
100311 }
101312 osmpbfreader:: OsmObj :: Way ( w) => {
102313 stats. num_ways += 1 ;
103314 stats. sum_ways_ids += w. id . 0 ;
315+ assert ! ( w. tags. contains_key( "highway" ) ) ;
316+ for exp_way in & exp_ways {
317+ if exp_way. id . 0 == w. id . 0 {
318+ num_found_ways += 1 ;
319+ assert_eq ! ( w, exp_way) ;
320+ }
321+ }
104322 }
105323 osmpbfreader:: OsmObj :: Relation ( r) => {
106324 stats. num_relations += 1 ;
107325 stats. sum_relations_ids += r. id . 0 ;
326+ assert ! ( r. tags. contains_key( "highway" ) ) ;
327+ for exp_relation in & exp_relations {
328+ if exp_relation. id . 0 == r. id . 0 {
329+ num_found_relations += 1 ;
330+ assert_eq ! ( r, exp_relation) ;
331+ }
332+ }
108333 }
109334 }
110335 }
@@ -113,4 +338,7 @@ fn read_pbf_highway() {
113338 + PBF_STATS_HIGHWAY . sum_ways_ids
114339 + PBF_STATS_HIGHWAY . sum_relations_ids ;
115340 assert_eq ! ( sum_ids, exp_sum_ids) ;
341+ assert_eq ! ( num_found_nodes, exp_nodes. len( ) ) ;
342+ assert_eq ! ( num_found_ways, exp_ways. len( ) ) ;
343+ assert_eq ! ( num_found_relations, exp_relations. len( ) ) ;
116344}
0 commit comments