]> git.openstreetmap.org Git - nominatim.git/blob - tests/scenes/bin/osm2wkt.cc
add functional tests
[nominatim.git] / tests / scenes / bin / osm2wkt.cc
1
2 // The code in this file is released into the Public Domain.
3
4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 #include <unordered_map>
8
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>
18
19 typedef osmium::index::map::StlMap<osmium::unsigned_object_id_type, osmium::Location> index_type;
20
21 typedef osmium::handler::NodeLocationsForWays<index_type, index_type> location_handler_type;
22
23
24 class ExportToWKTHandler : public osmium::handler::Handler {
25
26     osmium::geom::WKTFactory m_factory;
27     std::unordered_map<std::string, std::ofstream>  m_files;
28
29 public:
30
31     void node(const osmium::Node& node) {
32         print_geometry(node.tags(), m_factory.create_point(node));
33     }
34
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));
38     }
39
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));
43     }
44
45     void close() {
46         for (auto& fd : m_files)
47             fd.second.close();
48     }
49
50 private:
51
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");
55         if (scenario && id) {
56             auto& fd = m_files[std::string(scenario)];
57             if (!fd.is_open())
58                 fd.open(std::string(scenario) + ".wkt");
59             fd << id << " |  " << wkt << "\n";
60         }
61     }
62
63 }; // class ExportToWKTHandler
64
65 int main(int argc, char* argv[]) {
66     if (argc != 2) {
67         std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
68         exit(1);
69     }
70
71     std::string input_filename {argv[1]};
72
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);
77
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";
82
83     index_type index_pos;
84     index_type index_neg;
85     location_handler_type location_handler(index_pos, index_neg);
86
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());
91     reader2.close();
92     osmium::apply(collector, export_handler);
93     export_handler.close();
94     std::cerr << "Pass 2 done\n";
95
96
97     google::protobuf::ShutdownProtobufLibrary();
98
99 }
100
101