]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/scenes/bin/osm2wkt.cc
Reformatting of country keys
[nominatim.git] / test / scenes / bin / osm2wkt.cc
index d13a4632fcb4abdc771730551194ee66c2081a96..22e74b4dbb5e8f49da451f415a7f5fb3bb72a592 100644 (file)
@@ -7,19 +7,49 @@
 #include <unordered_map>
 
 #include <osmium/area/assembler.hpp>
-#include <osmium/area/multipolygon_collector.hpp>
-#include <osmium/area/problem_reporter_exception.hpp>
+#include <osmium/area/multipolygon_manager.hpp>
+
 #include <osmium/geom/wkt.hpp>
 #include <osmium/handler.hpp>
 #include <osmium/handler/node_locations_for_ways.hpp>
 #include <osmium/io/any_input.hpp>
 #include <osmium/visitor.hpp>
+#include <osmium/object_pointer_collection.hpp>
 #include <osmium/index/map/sparse_mem_array.hpp>
+#include <osmium/osm/object_comparisons.hpp>
 
 typedef osmium::index::map::SparseMemArray<osmium::unsigned_object_id_type, osmium::Location> index_type;
 
 typedef osmium::handler::NodeLocationsForWays<index_type, index_type> location_handler_type;
 
+struct AbsoluteIdHandler : public osmium::handler::Handler {
+
+    enum { BASE = 100000000 };
+
+    void node(osmium::Node& o) {
+        if (o.id() < 0)
+            o.set_id(BASE-o.id());
+    }
+
+    void way(osmium::Way& o) {
+        if (o.id() < 0)
+            o.set_id(BASE-o.id());
+
+        for (osmium::NodeRef &n: o.nodes())
+            if (n.ref() < 0)
+                n.set_ref(BASE-n.ref());
+    }
+
+    void relation(osmium::Relation& o) {
+        if (o.id() < 0)
+            o.set_id(BASE-o.id());
+
+        for (auto &m : o.members())
+            if (m.ref() < 0)
+                m.set_ref(BASE-m.ref());
+    }
+};
+
 
 class ExportToWKTHandler : public osmium::handler::Handler {
 
@@ -33,7 +63,8 @@ public:
     }
 
     void way(const osmium::Way& way) {
-        if (!way.is_closed() || !way.tags().get_value_by_key("area"))
+        if (!way.nodes().empty()
+            && (!way.is_closed() || !way.tags().get_value_by_key("area")))
             print_geometry(way.tags(), m_factory.create_linestring(way));
     }
 
@@ -48,7 +79,6 @@ public:
     }
 
 private:
-
     void print_geometry(const osmium::TagList& tags, const std::string& wkt) {
         const char* scenario = tags.get_value_by_key("test:section");
         const char* id = tags.get_value_by_key("test:id");
@@ -68,28 +98,42 @@ int main(int argc, char* argv[]) {
         exit(1);
     }
 
-    std::string input_filename {argv[1]};
+    osmium::io::File input_file{argv[1]};
+
+    // need to sort the data first and make ids absolute
+    std::cerr << "Read file...\n";
+    osmium::io::Reader reader{input_file};
+    std::vector<osmium::memory::Buffer> changes;
+    osmium::ObjectPointerCollection objects;
+    AbsoluteIdHandler abshandler;
+    while (osmium::memory::Buffer buffer = reader.read()) {
+            osmium::apply(buffer, abshandler, objects);
+            changes.push_back(std::move(buffer));
+    }
+    reader.close();
 
-    osmium::area::ProblemReporterException problem_reporter;
-    osmium::area::Assembler::config_type assembler_config(&problem_reporter);
-    osmium::area::MultipolygonCollector<osmium::area::Assembler> collector(assembler_config);
+    std::cerr << "Sort file...\n";
+    objects.sort(osmium::object_order_type_id_version());
 
-    std::cerr << "Pass 1...\n";
-    osmium::io::Reader reader1(input_filename, osmium::osm_entity_bits::relation);
-    collector.read_relations(reader1);
-    std::cerr << "Pass 1 done\n";
+    osmium::area::Assembler::config_type assembler_config;
+    osmium::area::MultipolygonManager<osmium::area::Assembler> mp_manager{assembler_config};
 
+    std::cerr << "Pass 1...\n";
     index_type index_pos;
     index_type index_neg;
     location_handler_type location_handler(index_pos, index_neg);
+    ExportToWKTHandler export_handler;
+    osmium::apply(objects.begin(), objects.end(), location_handler,
+                  export_handler, mp_manager);
+    mp_manager.prepare_for_lookup();
+    std::cerr << "Pass 1 done\n";
+
 
     std::cerr << "Pass 2...\n";
-    ExportToWKTHandler export_handler;
-    osmium::io::Reader reader2(input_filename);
-    osmium::apply(reader2, location_handler, export_handler, collector.handler([&export_handler](osmium::memory::Buffer&& buffer) {
+    osmium::apply(objects.cbegin(), objects.cend(), mp_manager.handler([&export_handler](osmium::memory::Buffer&& buffer) {
         osmium::apply(buffer, export_handler);
     }));
-    reader2.close();
+
     export_handler.close();
     std::cerr << "Pass 2 done\n";
 }