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_collector.hpp>
 
  11 #include <osmium/area/problem_reporter_exception.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/index/map/sparse_mem_array.hpp>
 
  19 typedef osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location> index_type;
 
  21 typedef osmium::handler::NodeLocationsForWays<index_type, index_type> location_handler_type;
 
  24 class ExportToWKTHandler : public osmium::handler::Handler {
 
  26     osmium::geom::WKTFactory<> m_factory;
 
  27     std::unordered_map<std::string, std::ofstream>  m_files;
 
  31     void node(const osmium::Node& node) {
 
  32         print_geometry(node.tags(), m_factory.create_point(node));
 
  35     void way(const osmium::Way& way) {
 
  36         if (!way.is_closed() || !way.tags().get_value_by_key("area"))
 
  37             print_geometry(way.tags(), m_factory.create_linestring(way));
 
  40     void area(const osmium::Area& area) {
 
  41         if (!area.from_way() || area.tags().get_value_by_key("area"))
 
  42             print_geometry(area.tags(), m_factory.create_multipolygon(area));
 
  46         for (auto& fd : m_files)
 
  52     void print_geometry(const osmium::TagList& tags, const std::string& wkt) {
 
  53         const char* scenario = tags.get_value_by_key("test:section");
 
  54         const char* id = tags.get_value_by_key("test:id");
 
  56             auto& fd = m_files[std::string(scenario)];
 
  58                 fd.open(std::string(scenario) + ".wkt");
 
  59             fd << id << " |  " << wkt << "\n";
 
  63 }; // class ExportToWKTHandler
 
  65 int main(int argc, char* argv[]) {
 
  67         std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
 
  71     std::string input_filename {argv[1]};
 
  73     osmium::area::ProblemReporterException problem_reporter;
 
  74     osmium::area::Assembler::config_type assembler_config(&problem_reporter);
 
  75     osmium::area::MultipolygonCollector<osmium::area::Assembler> collector(assembler_config);
 
  77     std::cerr << "Pass 1...\n";
 
  78     osmium::io::Reader reader1(input_filename, osmium::osm_entity_bits::relation);
 
  79     collector.read_relations(reader1);
 
  80     std::cerr << "Pass 1 done\n";
 
  84     location_handler_type location_handler(index_pos, index_neg);
 
  86     std::cerr << "Pass 2...\n";
 
  87     ExportToWKTHandler export_handler;
 
  88     osmium::io::Reader reader2(input_filename);
 
  89     osmium::apply(reader2, location_handler, export_handler, collector.handler([&export_handler](osmium::memory::Buffer&& buffer) {
 
  90         osmium::apply(buffer, export_handler);
 
  93     export_handler.close();
 
  94     std::cerr << "Pass 2 done\n";
 
  97     google::protobuf::ShutdownProtobufLibrary();