1 #ifndef OSM2PGSQL_OUTPUT_GAZETTEER_HPP
2 #define OSM2PGSQL_OUTPUT_GAZETTEER_HPP
3 
4 /**
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  *
7  * This file is part of osm2pgsql (https://osm2pgsql.org/).
8  *
9  * Copyright (C) 2006-2021 by the osm2pgsql developer community.
10  * For a full list of authors see the git log.
11  */
12 
13 #include <memory>
14 #include <utility>
15 
16 #include <osmium/memory/buffer.hpp>
17 
18 #include "gazetteer-style.hpp"
19 #include "osmium-builder.hpp"
20 #include "osmtypes.hpp"
21 #include "output.hpp"
22 
23 class db_copy_thread_t;
24 class thread_pool_t;
25 
26 struct middle_query_t;
27 
28 class output_gazetteer_t : public output_t
29 {
output_gazetteer_t(output_gazetteer_t const * other,std::shared_ptr<middle_query_t> const & cloned_mid,std::shared_ptr<db_copy_thread_t> const & copy_thread)30     output_gazetteer_t(output_gazetteer_t const *other,
31                        std::shared_ptr<middle_query_t> const &cloned_mid,
32                        std::shared_ptr<db_copy_thread_t> const &copy_thread)
33     : output_t(cloned_mid, other->m_thread_pool, other->m_options),
34       m_copy(copy_thread), m_builder(other->m_options.projection),
35       m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
36     {}
37 
38 public:
output_gazetteer_t(std::shared_ptr<middle_query_t> const & mid,std::shared_ptr<thread_pool_t> thread_pool,options_t const & options,std::shared_ptr<db_copy_thread_t> const & copy_thread)39     output_gazetteer_t(std::shared_ptr<middle_query_t> const &mid,
40                        std::shared_ptr<thread_pool_t> thread_pool,
41                        options_t const &options,
42                        std::shared_ptr<db_copy_thread_t> const &copy_thread)
43     : output_t(mid, std::move(thread_pool), options), m_copy(copy_thread),
44       m_builder(options.projection),
45       m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
46     {
47         m_style.load_style(options.style);
48     }
49 
50     std::shared_ptr<output_t>
clone(std::shared_ptr<middle_query_t> const & mid,std::shared_ptr<db_copy_thread_t> const & copy_thread) const51     clone(std::shared_ptr<middle_query_t> const &mid,
52           std::shared_ptr<db_copy_thread_t> const &copy_thread) const override
53     {
54         return std::shared_ptr<output_t>(
55             new output_gazetteer_t{this, mid, copy_thread});
56     }
57 
58     void start() override;
stop()59     void stop() noexcept override {}
60     void sync() override;
61 
pending_way(osmid_t)62     void pending_way(osmid_t) noexcept override {}
pending_relation(osmid_t)63     void pending_relation(osmid_t) noexcept override {}
64 
65     void node_add(osmium::Node const &node) override;
66     void way_add(osmium::Way *way) override;
67     void relation_add(osmium::Relation const &rel) override;
68 
69     void node_modify(osmium::Node const &node) override;
70     void way_modify(osmium::Way *way) override;
71     void relation_modify(osmium::Relation const &rel) override;
72 
node_delete(osmid_t id)73     void node_delete(osmid_t id) override { delete_unused_full('N', id); }
way_delete(osmid_t id)74     void way_delete(osmid_t id) override { delete_unused_full('W', id); }
relation_delete(osmid_t id)75     void relation_delete(osmid_t id) override { delete_unused_full('R', id); }
76 
77 private:
78     enum
79     {
80         PLACE_BUFFER_SIZE = 4096
81     };
82 
83     /// Delete all places that are not covered by the current style results.
84     void delete_unused_classes(char osm_type, osmid_t osm_id);
85     /// Delete all places for the given OSM object.
86     void delete_unused_full(char osm_type, osmid_t osm_id);
87     bool process_node(osmium::Node const &node);
88     bool process_way(osmium::Way *way);
89     bool process_relation(osmium::Relation const &rel);
90 
91     gazetteer_copy_mgr_t m_copy;
92     gazetteer_style_t m_style;
93 
94     geom::osmium_builder_t m_builder;
95     osmium::memory::Buffer m_osmium_buffer;
96 };
97 
98 #endif // OSM2PGSQL_OUTPUT_GAZETTEER_HPP
99