Giter Club home page Giter Club logo

osm-xml's Introduction

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

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

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 rel_info = relation_reference_statistics(&doc);
    let way_info = way_reference_statistics(&doc);
    let poly_count = doc.ways.values().fold(0, |acc, way| {
        if way.is_polygon() {
            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 relation_reference_statistics(doc: &osm::OSM) -> (usize, usize, usize) {
    doc.relations.values()
        .flat_map(|relation| relation.members.iter())
        .fold((0, 0, 0), |acc, member| {
            let el_ref = match *member {
                 osm::Member::Node(ref el_ref, _) => el_ref,
                 osm::Member::Way(ref el_ref, _) => el_ref,
                 osm::Member::Relation(ref el_ref, _) => el_ref,
            };

            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 way_reference_statistics(doc: &osm::OSM) -> (usize, usize) {
    doc.ways.values()
        .flat_map(|way| way.nodes.iter())
        .fold((0, 0), |acc, node| {
            match doc.resolve_reference(&node) {
                osm::Reference::Node(_) => (acc.0 + 1, acc.1),
                osm::Reference::Unresolved  |
                osm::Reference::Way(_)      |
                osm::Reference::Relation(_) => (acc.0, acc.1 + 1)
            }
        })
}

fn tag_count(doc: &osm::OSM) -> usize {
    let node_tag_count = doc.nodes.values()
        .map(|node| node.tags.len())
        .fold(0, |acc, c| acc + c);
    let way_tag_count = doc.ways.values()
        .map(|way| way.tags.len())
        .fold(0, |acc, c| acc + c);
    let relation_tag_count = doc.relations.values()
        .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

  • combining OSM-structs (something simple, make it easier to update existing elements inside map bounds)
  • writing out OSM documents
  • common element attribute parsing (visible, author, changeset, etc)
  • customizing parsing behaviour (short circuit on errors, optional fields, etc)
  • nicer error reporting: position in the osm-document of the offending element

Features which would be nice to have

  • tag "database": would make finding elements with tags faster / saves memory on parsed structure as tags are just references to actual strings

Changelog

0.6.0

2018-02-03

  • Change core data structures from vector to hashmap (this is incompatible change)
  • Upgrade dependencies

0.5.1

2017-04-25

  • Relax OSM::parse to take Read instead of File

0.5.0

2016-07-28

  • Way::is_polygon

0.4.0

2016-07-22

  • Initial release

License

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

osm-xml's People

Contributors

orva avatar fschutt avatar tyoverby avatar

Watchers

James Cloos avatar Matthieu Viry avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.