osm_io/
lib.rs

1//! This crate provides tools used to manipulate the [Open Street Map](https://wiki.openstreetmap.org/wiki/Main_Page)
2//! data.
3//!
4//! The basic OSM data model is broadly defined by [OSM XML](https://wiki.openstreetmap.org/wiki/OSM_XML)
5//! with Node and Way objects defining geometry and Relation and Tag objects providing unstructured
6//! extension mechanisms. Due to different uses of the OSM data, variety of
7//! [data formats](https://wiki.openstreetmap.org/wiki/Databases_and_data_access_APIs) emerged to
8//! store and transmit it.
9//! Our main focus here are the following formats:
10//! * [apidb](https://wiki.openstreetmap.org/wiki/Openstreetmap-website/Database_schema) - database
11//! schema backing [OSM](https://www.openstreetmap.org/) website
12//! * [*.osm.pbf](https://wiki.openstreetmap.org/wiki/PBF_Format) - a very efficient data format
13//! used to transmit OSM data that can be downloaded from http://download.geofabrik.de/ or from
14//! https://planet.openstreetmap.org/pbf/.
15//!
16//! The goal at this stage is to be able to load large *.osm.pbf files, such as planet.osm.pbf into a
17//! Postgresql OSM database (apidb schema) and to dump an entire Postgres OSM database into a
18//! planet.osm.pbf file. See [osm-admin](https://github.com/navigatorsguild/osm-admin)
19//!
20//! Because the data sets are very large, a special attention is given to maintaining control over
21//! memory size and utilizing multiple CPU cores whenever is possible.
22//!
23//! # Roadmap
24//! * implement *.osm.pbf reader and writer - Done
25//! * implement *.osm.pbf parallel reader and parallel writer - Done
26//! * implement apidb reader and writer - Done
27//! * provide basic filtering (see example below) - Done
28//! * convert between *.osm.pbf and apidb and vice versa - Done see examples.
29//! * [S2](http://s2geometry.io/) indexing - index the entire OSM dataset by S2 cells for farther
30//! processing
31//! * context indexing - index the entire OSM dataset by relations between its objects. So, for
32//! example, it would be possible to efficiently discard all Nodes that belong to a deleted Way.
33//!
34//! # Issues
35//! Issues are welcome and appreciated. Please submit to https://github.com/navigatorsguild/osm-io/issues
36//!
37//! # Examples
38//! Example for filtering out nodes from *.osm.pbf extract
39//! ```
40//! use std::path::PathBuf;
41//!
42//! use anyhow;
43//! use benchmark_rs::stopwatch::StopWatch;
44//! use simple_logger::SimpleLogger;
45//!
46//! use osm_io::osm::model::element::Element;
47//! use osm_io::osm::pbf;
48//! use osm_io::osm::pbf::compression_type::CompressionType;
49//! use osm_io::osm::pbf::file_info::FileInfo;
50//!
51//! pub fn main() -> Result<(), anyhow::Error> {
52//!     SimpleLogger::new().init()?;
53//!     log::info!("Started pbf io pipeline");
54//!     let mut stopwatch = StopWatch::new();
55//!     stopwatch.start();
56//!     let input_path = PathBuf::from("./tests/fixtures/niue-230109.osm.pbf");
57//!     let output_path = PathBuf::from("./target/results/niue-230109.osm.pbf");
58//!     let reader = pbf::reader::Reader::new(&input_path)?;
59//!     let mut file_info = FileInfo::default();
60//!     file_info.with_writingprogram_str("pbf-io-example");
61//!     let mut writer = pbf::writer::Writer::from_file_info(
62//!         output_path,
63//!         file_info,
64//!         CompressionType::Zlib,
65//!     )?;
66//!
67//!     writer.write_header()?;
68//!
69//!     for element in reader.elements()? {
70//!         let mut filter_out = false;
71//!         match &element {
72//!             Element::Node { node } => {
73//!                 for tag in node.tags() {
74//!                     if tag.k() == "natural" && tag.v() == "tree" {
75//!                         filter_out = true;
76//!                         break;
77//!                     }
78//!                 }
79//!             }
80//!             Element::Way { way: _ } => {}
81//!             Element::Relation { relation: _ } => {}
82//!             Element::Sentinel => {
83//!                 filter_out = true;
84//!             }
85//!         }
86//!         if !filter_out {
87//!             writer.write_element(element)?;
88//!         }
89//!     }
90//!
91//!     writer.close()?;
92//!
93//!     log::info!("Finished pbf io pipeline, time: {}", stopwatch);
94//!     Ok(())
95//! }
96//! ```
97//!
98//! # Similar Software
99//! * [libosmium](https://osmcode.org/libosmium/) - very fast and very mature with a Python wrapper.
100//! * [osmosis](https://wiki.openstreetmap.org/wiki/Osmosis) - reference implementation for most if
101//! not all features.
102//! * [osmpbf](https://crates.io/crates/osmpbf) - very efficient *.osm.pbf reader written in Rust
103//!
104extern crate core;
105
106#[allow(clippy::enum_variant_names)]
107pub(crate) mod osmpbf {
108    include!(concat!(env!("OUT_DIR"), "/osmpbf.rs"));
109}
110
111pub mod osm;