Last active
December 17, 2015 02:32
-
-
Save mourner/226d7f8c491f6855eea0 to your computer and use it in GitHub Desktop.
Get all OSM trees into JSON using libosmium
This file contains 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
// g++ or clang++ -o get-trees get-trees.cpp -O3 -DNDEBUG -I../libosmium/include -std=c++11 -lz -lpthread | |
#include <exception> | |
#include <vector> | |
#include <iostream> | |
#include <iomanip> | |
#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(); | |
if (!first) std::cout << ","; | |
std::cout << std::setprecision(8) << "[" << location.lon() << "," << location.lat() << "]"; | |
first = false; | |
trees++; | |
std::cerr << trees << "\r"; | |
} | |
} | |
} | |
private: | |
bool first = true; | |
uint trees = 0; | |
}; | |
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, osmium::osm_entity_bits::node); | |
TreeHandler handler; | |
std::cout << "["; | |
osmium::apply(reader, handler); | |
std::cout << "]"; | |
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