osm-xml

Build Status Crates.io version Crates.io license

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 }.

Usage

Add as dependency by adding this into Cargo.toml:

[dependencies] osm-xml = "0.5.1"

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); let polycount = doc.ways.iter().fold(0, |acc, way| { if way.ispolygon() { return acc + 1 }

    acc
});

println!("Node count {}", doc.nodes.len());
println!("Way count {}", doc.ways.len());
println!("Polygon count {}", poly_count);
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

} ```

Features missing for 1.0

Features which would be nice to have

Changelog

0.5.1

2017-04-25

0.5.0

2016-07-28

0.4.0

2016-07-22

License

osm-xml is licensed under MIT-license. See more in LICENSE.