[osmium-tool] 15/97: Option to "osmium cat": Only write out specified object types.

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Tue Jul 21 20:15:29 UTC 2015


This is an automated email from the git hooks/post-receive script.

sebastic pushed a commit to tag v1.0.0
in repository osmium-tool.

commit f9a067ff9314e8e0a1a988041c17ca04574190d6
Author: Jochen Topf <jochen at topf.org>
Date:   Thu Jul 17 19:17:20 2014 +0200

    Option to "osmium cat": Only write out specified object types.
---
 doc/osmium-cat.md   |  8 ++++++++
 src/command_cat.cpp | 37 +++++++++++++++++++++++++++++++++++--
 src/command_cat.hpp |  2 ++
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/doc/osmium-cat.md b/doc/osmium-cat.md
index 688a25d..64261fa 100644
--- a/doc/osmium-cat.md
+++ b/doc/osmium-cat.md
@@ -49,6 +49,10 @@ can be used to convert OSM files from one format into another.
 :   Allow an existing output file to be overwritten. Normally **osmium** will
     refuse to write over an existing file.
 
+--object-type, -t TYPE
+:   Read only objects of given type (*node*, *way*, *relation*, *changeset*).
+    By default all types are read. This option can be given multiple times.
+
 --verbose, -v
 :   Set verbose mode. The program will output information about what it is
     doing to *stderr*.
@@ -71,6 +75,10 @@ Concatenate all change files in the 'changes' directory into one:
 
     osmium cat -o all-changes.osc.gz changes/*.osc.gz
 
+Copy nodes and ways from source to destination file:
+
+    osmium cat -o dest.osm.pbf source.osm.pbf -t node -t way
+
 
 # SEE ALSO
 
diff --git a/src/command_cat.cpp b/src/command_cat.cpp
index 1b82e98..3dc4569 100644
--- a/src/command_cat.cpp
+++ b/src/command_cat.cpp
@@ -43,6 +43,7 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
         ("generator", po::value<std::string>(), "Generator setting for file header")
         ("output-header", po::value<std::vector<std::string>>(), "Add output header")
         ("overwrite,O", "Allow existing output file to be overwritten")
+        ("object-type,t", po::value<std::vector<std::string>>(), "Read only objects of given type (node, way, relation, changeset)")
         ;
 
         po::options_description hidden("Hidden options");
@@ -93,6 +94,24 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
             m_output_overwrite = osmium::io::overwrite::allow;
         }
 
+        if (vm.count("object-type")) {
+            m_osm_entity_bits = osmium::osm_entity_bits::nothing;
+            for (const auto& t : vm["object-type"].as<std::vector<std::string>>()) {
+                if (t == "node") {
+                    m_osm_entity_bits |= osmium::osm_entity_bits::node;
+                } else if (t == "way") {
+                    m_osm_entity_bits |= osmium::osm_entity_bits::way;
+                } else if (t == "relation") {
+                    m_osm_entity_bits |= osmium::osm_entity_bits::relation;
+                } else if (t == "changeset") {
+                    m_osm_entity_bits |= osmium::osm_entity_bits::changeset;
+                } else {
+                    std::cerr << "Unknown object type '" << t << "' (Allowed are 'node', 'way', 'relation', and 'changeset').\n";
+                    return false;
+                }
+            }
+        }
+
     } catch (boost::program_options::error& e) {
         std::cerr << "Error parsing command line: " << e.what() << std::endl;
         return false;
@@ -113,6 +132,20 @@ bool CommandCat::setup(const std::vector<std::string>& arguments) {
     for (const auto& h : m_output_headers) {
         m_vout << "    " << h << "\n";
     }
+    m_vout << "  object types:";
+    if (m_osm_entity_bits & osmium::osm_entity_bits::node) {
+        m_vout << " node";
+    }
+    if (m_osm_entity_bits & osmium::osm_entity_bits::way) {
+        m_vout << " way";
+    }
+    if (m_osm_entity_bits & osmium::osm_entity_bits::relation) {
+        m_vout << " relation";
+    }
+    if (m_osm_entity_bits & osmium::osm_entity_bits::changeset) {
+        m_vout << " changeset";
+    }
+    m_vout << "\n";
 
     if ((m_output_filename == "-" || m_output_filename == "") && m_output_format.empty()) {
         std::cerr << "When writing to STDOUT you need to use the --output-format,f option to declare the file format.\n";
@@ -147,7 +180,7 @@ bool CommandCat::run() {
     try {
         if (m_input_files.size() == 1) { // single input file
             m_vout << "Copying input file '" << m_input_files[0].filename() << "'\n";
-            osmium::io::Reader reader(m_input_files[0]);
+            osmium::io::Reader reader(m_input_files[0], m_osm_entity_bits);
             osmium::io::Header header = reader.header();
             header.set("generator", m_generator);
             for (const auto& h : m_output_headers) {
@@ -166,7 +199,7 @@ bool CommandCat::run() {
             osmium::io::Writer writer(m_output_file, header, m_output_overwrite);
             for (const auto& input_file : m_input_files) {
                 m_vout << "Copying input file '" << input_file.filename() << "'\n";
-                osmium::io::Reader reader(input_file);
+                osmium::io::Reader reader(input_file, m_osm_entity_bits);
                 while (osmium::memory::Buffer buffer = reader.read()) {
                     writer(std::move(buffer));
                 }
diff --git a/src/command_cat.hpp b/src/command_cat.hpp
index 37da1ce..cb5f131 100644
--- a/src/command_cat.hpp
+++ b/src/command_cat.hpp
@@ -28,6 +28,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <osmium/io/file.hpp>
 #include <osmium/io/overwrite.hpp>
+#include <osmium/osm/entity_bits.hpp>
 
 #include "osmc.hpp"
 
@@ -41,6 +42,7 @@ class CommandCat : public Command {
     osmium::io::File m_output_file;
     std::vector<osmium::io::File> m_input_files;
     osmium::io::overwrite m_output_overwrite = osmium::io::overwrite::no;
+    osmium::osm_entity_bits::type m_osm_entity_bits = osmium::osm_entity_bits::all;
 
 public:
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/osmium-tool.git



More information about the Pkg-grass-devel mailing list