]> git.openstreetmap.org Git - nominatim.git/blob - test/scenes/bin/osm2wkt.cc
Updated documentation for PyICU support
[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_manager.hpp>
11
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>
20
21 typedef osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location> index_type;
22
23 typedef osmium::handler::NodeLocationsForWays<index_type, index_type> location_handler_type;
24
25 struct AbsoluteIdHandler : public osmium::handler::Handler {
26
27     enum { BASE = 100000000 };
28
29     void node(osmium::Node& o) {
30         if (o.id() < 0)
31             o.set_id(BASE-o.id());
32     }
33
34     void way(osmium::Way& o) {
35         if (o.id() < 0)
36             o.set_id(BASE-o.id());
37
38         for (osmium::NodeRef &n: o.nodes())
39             if (n.ref() < 0)
40                 n.set_ref(BASE-n.ref());
41     }
42
43     void relation(osmium::Relation& o) {
44         if (o.id() < 0)
45             o.set_id(BASE-o.id());
46
47         for (auto &m : o.members())
48             if (m.ref() < 0)
49                 m.set_ref(BASE-m.ref());
50     }
51 };
52
53
54 class ExportToWKTHandler : public osmium::handler::Handler {
55
56     osmium::geom::WKTFactory<> m_factory;
57     std::unordered_map<std::string, std::ofstream>  m_files;
58
59 public:
60
61     void node(const osmium::Node& node) {
62         print_geometry(node.tags(), m_factory.create_point(node));
63     }
64
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));
69     }
70
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));
74     }
75
76     void close() {
77         for (auto& fd : m_files)
78             fd.second.close();
79     }
80
81 private:
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");
85         if (scenario && id) {
86             auto& fd = m_files[std::string(scenario)];
87             if (!fd.is_open())
88                 fd.open(std::string(scenario) + ".wkt");
89             fd << id << " |  " << wkt << "\n";
90         }
91     }
92
93 }; // class ExportToWKTHandler
94
95 int main(int argc, char* argv[]) {
96     if (argc != 2) {
97         std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
98         exit(1);
99     }
100
101     osmium::io::File input_file{argv[1]};
102
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));
112     }
113     reader.close();
114
115     std::cerr << "Sort file...\n";
116     objects.sort(osmium::object_order_type_id_version());
117
118     osmium::area::Assembler::config_type assembler_config;
119     osmium::area::MultipolygonManager<osmium::area::Assembler> mp_manager{assembler_config};
120
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";
130
131
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);
135     }));
136
137     export_handler.close();
138     std::cerr << "Pass 2 done\n";
139 }
140
141