]> git.openstreetmap.org Git - nominatim.git/blob - test/scenes/bin/osm2wkt.cc
Merge pull request #600 from lonvia/behave-tests-rebased
[nominatim.git] / test / 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/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>
18
19 typedef osmium::index::map::SparseMemArray<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     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);
76
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";
81
82     index_type index_pos;
83     index_type index_neg;
84     location_handler_type location_handler(index_pos, index_neg);
85
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);
91     }));
92     reader2.close();
93     export_handler.close();
94     std::cerr << "Pass 2 done\n";
95 }
96
97