Simple osm xml v0.6 parser.
Structure for the parsed data follows closely how OSM documents are formed: we have top level OSM struct containing bounds, nodes array, ways array and relations array. Each parsed element follows closely their corresponding osm-wiki entry.
References to the other elements in the document are left unresolved in parsed form. There is API to resolve individual references to their corresponding elements, but whole document cannot be made connected.
Tags for the element are stored in .tags
field as array of Tag {
key: String, val: String }
.
Add as dependency by adding this into Cargo.toml
:
[dependencies]
osm-xml = "0.5.0"
Then take crate into use with extern crate osm_xml as osm;
.
Below is simple example program which digs out some statistics from given osm-document. This includes parsing document, finding and using all the different kind of elements and resolving references (both resolvable and unresolvable).
```rust extern crate osm_xml as osm;
use std::fs::File;
fn main() { let f = File::open("/path/to/map.osm").unwrap(); let doc = osm::OSM::parse(f).unwrap(); let relinfo = relationreferencestatistics(&doc); let wayinfo = wayreferencestatistics(&doc);
println!("Node count {}", doc.nodes.len());
println!("Way count {}", doc.ways.len());
println!("Relation count {}", doc.relations.len());
println!("Tag count {}", tag_count(&doc));
println!("Way reference count: {}, invalid references: {}", way_info.0, way_info.1);
println!("Relation reference count: {}, resolved: {}, unresolved: {}", rel_info.0, rel_info.1, rel_info.2);
}
fn relationreferencestatistics(doc: &osm::OSM) -> (usize, usize, usize) { doc.relations.iter() .flatmap(|relation| relation.members.iter()) .fold((0, 0, 0), |acc, member| { let elref = match *member { osm::Member::Node(ref elref, _) => elref, osm::Member::Way(ref elref, _) => elref, osm::Member::Relation(ref elref, _) => elref, };
match doc.resolve_reference(&el_ref) {
osm::Reference::Unresolved => (acc.0 + 1, acc.1, acc.2 + 1),
osm::Reference::Node(_) |
osm::Reference::Way(_) |
osm::Reference::Relation(_) => (acc.0 + 1, acc.1 + 1, acc.2)
}
})
}
fn wayreferencestatistics(doc: &osm::OSM) -> (usize, usize) { doc.ways.iter() .flatmap(|way| way.nodes.iter()) .fold((0, 0), |acc, node| { match doc.resolvereference(&node) { osm::Reference::Node() => (acc.0 + 1, acc.1), osm::Reference::Unresolved | osm::Reference::Way() | osm::Reference::Relation(_) => (acc.0, acc.1 + 1) } }) }
fn tagcount(doc: &osm::OSM) -> usize { let nodetagcount = doc.nodes.iter() .map(|node| node.tags.len()) .fold(0, |acc, c| acc + c); let waytagcount = doc.ways.iter() .map(|way| way.tags.len()) .fold(0, |acc, c| acc + c); let relationtag_count = doc.relations.iter() .map(|relation| relation.tags.len()) .fold(0, |acc, c| acc + c);
node_tag_count + way_tag_count + relation_tag_count
} ```
Way
is a polygon2016-07-28
2016-07-22
osm-xml is licensed under MIT-license. See more in LICENSE.