[osmium-tool] 31/44: Add getid subcommand.

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


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

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

commit f4a140ba44bf2a7f4f9d3f0bba995c8895408efa
Author: Jochen Topf <jochen at topf.org>
Date:   Fri Jul 3 13:23:39 2015 +0200

    Add getid subcommand.
---
 CMakeLists.txt            |   1 +
 man/osmium-getid.md       |  65 +++++++++++++++
 src/command_getid.cpp     | 198 ++++++++++++++++++++++++++++++++++++++++++++++
 src/command_getid.hpp     |  53 +++++++++++++
 test/CMakeLists.txt       |   1 +
 test/getid/CMakeLists.txt |  15 ++++
 test/getid/input.osm      |  22 ++++++
 test/getid/output.osm     |  10 +++
 8 files changed, 365 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b37d26d..957d36a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -119,6 +119,7 @@ if(PANDOC)
     add_man_page(1 osmium-cat)
     add_man_page(1 osmium-check-refs)
     add_man_page(1 osmium-fileinfo)
+    add_man_page(1 osmium-getid)
     add_man_page(1 osmium-merge-changes)
     add_man_page(1 osmium-renumber)
     add_man_page(1 osmium-time-filter)
diff --git a/man/osmium-getid.md b/man/osmium-getid.md
new file mode 100644
index 0000000..a9b9b63
--- /dev/null
+++ b/man/osmium-getid.md
@@ -0,0 +1,65 @@
+
+# NAME
+
+osmium-getid - get objects from OSM file with given IDs
+
+
+# SYNOPSIS
+
+**osmium getid** \[*OPTIONS*\] *INPUT-FILE* *ID*...
+
+
+# DESCRIPTION
+
+Get objects with the given IDs from the input and write them to the output
+
+
+# OPTIONS
+
+-f, --output-format=FORMAT
+:   The format of the output file. Can be used to set the output file format
+    if it can't be autodetected from the output file name.
+    See **osmium-file-formats**(5) or the libosmium manual for details.
+
+-F, --input-format=FORMAT
+:   The format of the input files. Can be used to set the input format if it
+    can't be autodetected from the file names. This will set the format for
+    all input files, there is no way to set the format for some input files
+    only. See **osmium-file-formats**(5) or the libosmium manual for details.
+
+--generator=NAME
+:   The name and version of the program generating the output file. It will be
+    added to the header of the output file. Default is "*osmium/*" and the version
+    of osmium.
+
+-o, --output=FILE
+:   Name of the output file. Default is '-' (*stdout*).
+
+-O, --overwrite
+:   Allow an existing output file to be overwritten. Normally **osmium** will
+    refuse to write over an existing file.
+
+-v, --verbose
+:   Set verbose mode. The program will output information about what it is
+    doing to *stderr*.
+
+
+# DIAGNOSTICS
+
+**osmium getid** exits with code 0 if everything went alright and all IDs
+were found, it exits with code 2 if there was a problem with the command
+line arguments, and with exit code 1 if not all IDs were found.
+
+
+# EXAMPLES
+
+Output nodes 17 and 1234, way 42, and relation 111 to *stdout* in OPL format:
+
+    osmium getid -f opl planet.osm.pbf n1234 w42 n17 r111
+
+
+# SEE ALSO
+
+* [Osmium website](http://osmcode.org/osmium)
+* [Libosmium manual](http://osmcode.org/libosmium/manual/libosmium-manual.html)
+
diff --git a/src/command_getid.cpp b/src/command_getid.cpp
new file mode 100644
index 0000000..27c6300
--- /dev/null
+++ b/src/command_getid.cpp
@@ -0,0 +1,198 @@
+/*
+
+Osmium -- OpenStreetMap data manipulation command line tool
+http://osmcode.org/osmium
+
+Copyright (C) 2013-2015  Jochen Topf <jochen at topf.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include <numeric>
+#include <string>
+#include <vector>
+
+#include <boost/program_options.hpp>
+
+#include <osmium/io/any_input.hpp>
+#include <osmium/io/any_output.hpp>
+#include <osmium/osm/entity_bits.hpp>
+#include <osmium/osm/types_from_string.hpp>
+#include <osmium/util/string.hpp>
+
+#include "command_getid.hpp"
+
+std::vector<osmium::object_id_type>& CommandGetId::ids(osmium::item_type type) noexcept {
+    return m_ids[osmium::item_type_to_nwr_index(type)];
+}
+
+void CommandGetId::sort_unique(osmium::item_type type) {
+    std::sort(ids(type).begin(), ids(type).end());
+    auto last = std::unique(ids(type).begin(), ids(type).end());
+    ids(type).erase(last, ids(type).end());
+}
+
+bool CommandGetId::setup(const std::vector<std::string>& arguments) {
+    namespace po = boost::program_options;
+    po::variables_map vm;
+
+    po::options_description cmdline("Allowed options");
+    cmdline.add_options()
+    ("verbose,v", "Set verbose mode")
+    ("output,o", po::value<std::string>(), "Output file")
+    ("output-format,f", po::value<std::string>(), "Format of output file")
+    ("input-format,F", po::value<std::string>(), "Format of input file")
+    ("generator", po::value<std::string>(), "Generator setting for file header")
+    ("overwrite,O", "Allow existing output file to be overwritten")
+    ;
+
+    po::options_description hidden("Hidden options");
+    hidden.add_options()
+    ("input-filename", po::value<std::string>(), "OSM input file")
+    ("ids", po::value<std::vector<std::string>>(), "OSM ids")
+    ;
+
+    po::options_description desc("Allowed options");
+    desc.add(cmdline).add(hidden);
+
+    po::positional_options_description positional;
+    positional.add("input-filename", 1);
+    positional.add("ids", -1);
+
+    po::store(po::command_line_parser(arguments).options(desc).positional(positional).run(), vm);
+    po::notify(vm);
+
+    if (vm.count("ids")) {
+        std::string sids;
+        for (const auto& s : vm["ids"].as<std::vector<std::string>>()) {
+            sids += s + " ";
+        }
+        auto vids = osmium::split_string(sids, "\t ;,/|", true);
+        for (const auto& s : vids) {
+            auto p = osmium::string_to_object_id(s.c_str(), osmium::osm_entity_bits::nwr);
+            if (p.first == osmium::item_type::undefined) {
+                p.first = osmium::item_type::node;
+            }
+            ids(p.first).push_back(p.second);
+        }
+
+        sort_unique(osmium::item_type::node);
+        sort_unique(osmium::item_type::way);
+        sort_unique(osmium::item_type::relation);
+    } else {
+        throw argument_error("Need at least one id to look for...");
+    }
+
+    if (vm.count("generator")) {
+        m_generator = vm["generator"].as<std::string>();
+    }
+
+    if (vm.count("verbose")) {
+        m_vout.verbose(true);
+    }
+
+    setup_input_file(vm);
+    setup_output_file(vm);
+
+    m_vout << "Started osmium apply-changes\n";
+
+    m_vout << "Command line options and default settings:\n";
+    m_vout << "  generator: " << m_generator << "\n";
+    m_vout << "  input data file name: " << m_input_filename << "\n";
+    m_vout << "  output filename: " << m_output_filename << "\n";
+    m_vout << "  input format: " << m_input_format << "\n";
+    m_vout << "  output format: " << m_output_format << "\n";
+    m_vout << "  looking for these ids:\n";
+    m_vout << "    nodes:";
+    for (osmium::object_id_type id : ids(osmium::item_type::node)) {
+        m_vout << " " << id;
+    }
+    m_vout << "\n";
+    m_vout << "    ways:";
+    for (osmium::object_id_type id : ids(osmium::item_type::way)) {
+        m_vout << " " << id;
+    }
+    m_vout << "\n";
+    m_vout << "    relations:";
+    for (osmium::object_id_type id : ids(osmium::item_type::relation)) {
+        m_vout << " " << id;
+    }
+    m_vout << "\n";
+
+    return true;
+}
+
+bool CommandGetId::run() {
+    osmium::osm_entity_bits::type types = osmium::osm_entity_bits::nothing;
+
+    if (! ids(osmium::item_type::node).empty()) {
+        types |= osmium::osm_entity_bits::node;
+    }
+    if (! ids(osmium::item_type::way).empty()) {
+        types |= osmium::osm_entity_bits::way;
+    }
+    if (! ids(osmium::item_type::relation).empty()) {
+        types |= osmium::osm_entity_bits::relation;
+    }
+
+    m_vout << "Reading input file...\n";
+    osmium::io::Reader reader(m_input_file, types);
+
+    osmium::io::Header header;
+    header.set("generator", m_generator);
+
+    typedef osmium::io::InputIterator<osmium::io::Reader, osmium::OSMObject> object_iterator;
+
+    object_iterator it(reader);
+    object_iterator end;
+    osmium::memory::Buffer output_buffer(10240);
+
+    size_t num_ids = ids(osmium::item_type::node).size() +
+                     ids(osmium::item_type::way).size() +
+                     ids(osmium::item_type::relation).size();
+
+    for (; it != end; ++it) {
+        auto& index = ids(it->type());
+        auto result = std::equal_range(index.begin(), index.end(), it->id());
+        if (result.first != result.second) {
+            output_buffer.add_item(*it);
+            output_buffer.commit();
+            --num_ids;
+        }
+    }
+
+    m_vout << "Writing out results...\n";
+    osmium::io::Writer writer(m_output_file, header, m_output_overwrite);
+    writer(std::move(output_buffer));
+    writer.close();
+
+    if (num_ids == 0) {
+        m_vout << "Found all objects.\n";
+    } else {
+        m_vout << "Did not find " << num_ids << " objects.\n";
+    }
+    m_vout << "Done.\n";
+
+    return num_ids == 0;
+}
+
+namespace {
+
+    const bool register_get_id_command = CommandFactory::add("getid", "Get objects with given ID from OSM file", []() {
+        return new CommandGetId();
+    });
+
+}
+
diff --git a/src/command_getid.hpp b/src/command_getid.hpp
new file mode 100644
index 0000000..7a6a309
--- /dev/null
+++ b/src/command_getid.hpp
@@ -0,0 +1,53 @@
+#ifndef COMMAND_GETID_HPP
+#define COMMAND_GETID_HPP
+
+/*
+
+Osmium -- OpenStreetMap data manipulation command line tool
+http://osmcode.org/osmium
+
+Copyright (C) 2013-2015  Jochen Topf <jochen at topf.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include <string>
+#include <vector>
+
+#include <osmium/osm/item_type.hpp>
+#include <osmium/osm/types.hpp>
+
+#include "osmc.hpp"
+
+class CommandGetId : public Command, with_single_osm_input, with_osm_output {
+
+    std::vector<osmium::object_id_type> m_ids[3];
+
+public:
+
+    CommandGetId() = default;
+
+    std::vector<osmium::object_id_type>& ids(osmium::item_type type) noexcept;
+
+    void sort_unique(osmium::item_type type);
+
+    bool setup(const std::vector<std::string>& arguments) override final;
+
+    bool run() override final;
+
+}; // class CommandGetId
+
+
+#endif // COMMAND_GETID_HPP
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1c3b531..671754c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -47,6 +47,7 @@ endfunction()
 
 add_subdirectory(cat)
 add_subdirectory(check-refs)
+add_subdirectory(getid)
 add_subdirectory(fileinfo)
 add_subdirectory(help)
 add_subdirectory(renumber)
diff --git a/test/getid/CMakeLists.txt b/test/getid/CMakeLists.txt
new file mode 100644
index 0000000..ead84fd
--- /dev/null
+++ b/test/getid/CMakeLists.txt
@@ -0,0 +1,15 @@
+#-----------------------------------------------------------------------------
+#
+#  CMake Config
+#
+#  Osmium Tool Tests - getid
+#
+#-----------------------------------------------------------------------------
+
+function(check_getid _name _input _output)
+    check_output(getid-${_name} "getid --generator=test -f osm getid/${_input} n11,n12 w21" "getid/${_output}")
+endfunction()
+
+check_getid(n input.osm output.osm)
+
+#-----------------------------------------------------------------------------
diff --git a/test/getid/input.osm b/test/getid/input.osm
new file mode 100644
index 0000000..2f7500b
--- /dev/null
+++ b/test/getid/input.osm
@@ -0,0 +1,22 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version="0.6" upload="false" generator="testdata">
+  <node id="10" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1" lat="1" lon="1"/>
+  <node id="11" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1" lat="2" lon="1"/>
+  <node id="12" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1" lat="3" lon="1"/>
+  <node id="13" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1" lat="4" lon="1"/>
+  <way id="20" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1">
+    <nd ref="10"/>
+    <nd ref="11"/>
+    <nd ref="12"/>
+    <tag k="foo" v="bar"/>
+  </way>
+  <way id="21" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1">
+    <nd ref="12"/>
+    <nd ref="13"/>
+    <tag k="xyz" v="abc"/>
+  </way>
+  <relation id="30" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1">
+    <member type="node" ref="12" role="m1"/>
+    <member type="way" ref="20" role="m2"/>
+  </relation>
+</osm>
diff --git a/test/getid/output.osm b/test/getid/output.osm
new file mode 100644
index 0000000..5dc3623
--- /dev/null
+++ b/test/getid/output.osm
@@ -0,0 +1,10 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version="0.6" generator="test">
+  <node id="11" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1" lat="2" lon="1"/>
+  <node id="12" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1" lat="3" lon="1"/>
+  <way id="21" version="1" timestamp="2015-01-01T01:00:00Z" uid="1" user="test" changeset="1">
+    <nd ref="12"/>
+    <nd ref="13"/>
+    <tag k="xyz" v="abc"/>
+  </way>
+</osm>

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