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/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/stl_map.hpp>
19 typedef osmium::index::map::StlMap<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 typedef osmium::area::Assembler area_assembler_type;
74 osmium::area::ProblemReporterException problem_reporter;
75 area_assembler_type assembler(&problem_reporter);
76 osmium::area::Collector<area_assembler_type> collector(assembler);
78 std::cerr << "Pass 1...\n";
79 osmium::io::Reader reader1(input_filename);
80 collector.read_relations(reader1);
81 std::cerr << "Pass 1 done\n";
85 location_handler_type location_handler(index_pos, index_neg);
87 std::cerr << "Pass 2...\n";
88 ExportToWKTHandler export_handler;
89 osmium::io::Reader reader2(input_filename);
90 osmium::apply(reader2, location_handler, export_handler, collector.handler());
92 osmium::apply(collector, export_handler);
93 export_handler.close();
94 std::cerr << "Pass 2 done\n";
97 google::protobuf::ShutdownProtobufLibrary();