Created
December 15, 2015 20:36
-
-
Save springmeyer/7f0f65a6ed629592cbaa to your computer and use it in GitHub Desktop.
C++ code sample using libosmium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
To build: | |
git clone [email protected]:osmcode/libosmium.git | |
clang++ -o get-trees get-trees.cpp -O3 -DNDEBUG -I./libosmium/include -std=c++11 -lz | |
./get-trees | |
*/ | |
// C++ API docs: http://docs.osmcode.org/libosmium-manual/ | |
#include <exception> | |
#include <vector> | |
#include <iostream> | |
#include <osmium/handler.hpp> | |
#include <osmium/io/pbf_input.hpp> | |
#include <osmium/visitor.hpp> | |
class TreeHandler : public osmium::handler::Handler { | |
public: | |
TreeHandler() { | |
} | |
void node(const osmium::Node& node) { | |
auto const& tags = node.tags(); | |
const char * natural_char = tags.get_value_by_key("natural"); | |
if (natural_char) { | |
std::string natural = std::string(natural_char); | |
if (!natural.empty() && natural == "tree") { | |
auto const& location = node.location(); | |
std::clog << location.lon() << "," << location.lat() << "\n"; | |
} | |
} | |
} | |
}; | |
int main(int argc, char** argv) | |
{ | |
std::vector<std::string> args; | |
for (int i=1;i<argc;++i) | |
{ | |
args.push_back(argv[i]); | |
} | |
if (args.empty()) | |
{ | |
std::clog << "please pass the path to an osm pbf file\n"; | |
return -1; | |
} | |
try | |
{ | |
std::string filename = args[0]; | |
osmium::io::Reader reader(filename); | |
TreeHandler handler; | |
osmium::apply(reader, handler); | |
reader.close(); | |
} | |
catch (std::exception const& ex) | |
{ | |
std::clog << "error: " << ex.what() << "\n"; | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment