2 // The code in this file is released into the Public Domain.
 
   7 #include <unordered_map>
 
   9 #include <osmium/area/assembler.hpp>
 
  10 #include <osmium/area/multipolygon_manager.hpp>
 
  12 #include <osmium/geom/wkt.hpp>
 
  13 #include <osmium/handler.hpp>
 
  14 #include <osmium/handler/node_locations_for_ways.hpp>
 
  15 #include <osmium/io/any_input.hpp>
 
  16 #include <osmium/visitor.hpp>
 
  17 #include <osmium/object_pointer_collection.hpp>
 
  18 #include <osmium/index/map/sparse_mem_array.hpp>
 
  19 #include <osmium/osm/object_comparisons.hpp>
 
  21 typedef osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location> index_type;
 
  23 typedef osmium::handler::NodeLocationsForWays<index_type, index_type> location_handler_type;
 
  25 struct AbsoluteIdHandler : public osmium::handler::Handler {
 
  27     enum { BASE = 100000000 };
 
  29     void node(osmium::Node& o) {
 
  31             o.set_id(BASE-o.id());
 
  34     void way(osmium::Way& o) {
 
  36             o.set_id(BASE-o.id());
 
  38         for (osmium::NodeRef &n: o.nodes())
 
  40                 n.set_ref(BASE-n.ref());
 
  43     void relation(osmium::Relation& o) {
 
  45             o.set_id(BASE-o.id());
 
  47         for (auto &m : o.members())
 
  49                 m.set_ref(BASE-m.ref());
 
  54 class ExportToWKTHandler : public osmium::handler::Handler {
 
  56     osmium::geom::WKTFactory<> m_factory;
 
  57     std::unordered_map<std::string, std::ofstream>  m_files;
 
  61     void node(const osmium::Node& node) {
 
  62         print_geometry(node.tags(), m_factory.create_point(node));
 
  65     void way(const osmium::Way& way) {
 
  66         if (!way.nodes().empty()
 
  67             && (!way.is_closed() || !way.tags().get_value_by_key("area")))
 
  68             print_geometry(way.tags(), m_factory.create_linestring(way));
 
  71     void area(const osmium::Area& area) {
 
  72         if (!area.from_way() || area.tags().get_value_by_key("area"))
 
  73             print_geometry(area.tags(), m_factory.create_multipolygon(area));
 
  77         for (auto& fd : m_files)
 
  82     void print_geometry(const osmium::TagList& tags, const std::string& wkt) {
 
  83         const char* scenario = tags.get_value_by_key("test:section");
 
  84         const char* id = tags.get_value_by_key("test:id");
 
  86             auto& fd = m_files[std::string(scenario)];
 
  88                 fd.open(std::string(scenario) + ".wkt");
 
  89             fd << id << " |  " << wkt << "\n";
 
  93 }; // class ExportToWKTHandler
 
  95 int main(int argc, char* argv[]) {
 
  97         std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
 
 101     osmium::io::File input_file{argv[1]};
 
 103     // need to sort the data first and make ids absolute
 
 104     std::cerr << "Read file...\n";
 
 105     osmium::io::Reader reader{input_file};
 
 106     std::vector<osmium::memory::Buffer> changes;
 
 107     osmium::ObjectPointerCollection objects;
 
 108     AbsoluteIdHandler abshandler;
 
 109     while (osmium::memory::Buffer buffer = reader.read()) {
 
 110             osmium::apply(buffer, abshandler, objects);
 
 111             changes.push_back(std::move(buffer));
 
 115     std::cerr << "Sort file...\n";
 
 116     objects.sort(osmium::object_order_type_id_version());
 
 118     osmium::area::Assembler::config_type assembler_config;
 
 119     osmium::area::MultipolygonManager<osmium::area::Assembler> mp_manager{assembler_config};
 
 121     std::cerr << "Pass 1...\n";
 
 122     index_type index_pos;
 
 123     index_type index_neg;
 
 124     location_handler_type location_handler(index_pos, index_neg);
 
 125     ExportToWKTHandler export_handler;
 
 126     osmium::apply(objects.begin(), objects.end(), location_handler,
 
 127                   export_handler, mp_manager);
 
 128     mp_manager.prepare_for_lookup();
 
 129     std::cerr << "Pass 1 done\n";
 
 132     std::cerr << "Pass 2...\n";
 
 133     osmium::apply(objects.cbegin(), objects.cend(), mp_manager.handler([&export_handler](osmium::memory::Buffer&& buffer) {
 
 134         osmium::apply(buffer, export_handler);
 
 137     export_handler.close();
 
 138     std::cerr << "Pass 2 done\n";