forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output-gazetteer.hpp
264 lines (215 loc) · 6.82 KB
/
output-gazetteer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifndef OUTPUT_GAZETTEER_H
#define OUTPUT_GAZETTEER_H
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/format.hpp>
#include <osmium/memory/buffer.hpp>
#include "osmium-builder.hpp"
#include "osmtypes.hpp"
#include "output.hpp"
#include "pgsql.hpp"
#include "util.hpp"
/**
* A private class to convert tags.
*/
class place_tag_processor
{
public:
place_tag_processor()
: single_fmt("%1%\t")
{
places.reserve(4);
extratags.reserve(15);
address.reserve(10);
}
~place_tag_processor() {}
void process_tags(osmium::OSMObject const &o);
bool has_data() const { return !places.empty(); }
bool has_place(const std::string &cls)
{
for (const auto& item: places) {
if (cls == item.key)
return true;
}
return false;
}
void copy_out(osmium::OSMObject const &o, const std::string &geom,
std::string &buffer);
void clear();
private:
void copy_opt_string(char const *val, std::string &buffer)
{
if (val) {
escape(val, buffer);
buffer += "\t";
} else {
buffer += "\\N\t";
}
}
std::string domain_name(const std::string &cls, osmium::TagList const &tags)
{
std::string ret;
bool hasname = false;
std::string prefix(cls + ":name");
for (const auto& item: tags) {
char const *k = item.key();
if (boost::starts_with(k, prefix)
&& (k[prefix.length()] == '\0' || k[prefix.length()] == ':')) {
if (!hasname) {
hasname = true;
} else {
ret += ",";
}
ret += "\"";
escape_array_record(k + cls.length() + 1, ret);
ret += "\"=>\"";
escape_array_record(item.value(), ret);
ret += "\"";
}
}
return ret;
}
void escape_array_record(const std::string &in, std::string &out)
{
for (const char c: in) {
switch(c) {
case '\\':
// Tripple escaping required: string escaping leaves us
// with 4 backslashes, COPY then reduces it to two, which
// are then interpreted as a single backslash by the hash
// parsing code.
out += "\\\\\\\\";
break;
case '\n':
case '\r':
case '\t':
case '"':
/* This is a bit naughty - we know that nominatim ignored these characters so just drop them now for simplicity */
out += ' '; break;
default: out += c; break;
}
}
}
std::vector<tag_t> places;
std::vector<osmium::Tag const *> names;
std::vector<osmium::Tag const *> extratags;
std::unordered_map<std::string, char const *> address;
int admin_level;
boost::format single_fmt;
};
class output_gazetteer_t : public output_t {
public:
output_gazetteer_t(const middle_query_t *mid_, const options_t &options_)
: output_t(mid_, options_), Connection(NULL), ConnectionDelete(NULL),
ConnectionError(NULL), copy_active(false),
m_builder(options_.projection, true), single_fmt("%1%"),
osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
{
buffer.reserve(PLACE_BUFFER_SIZE);
}
output_gazetteer_t(const output_gazetteer_t &other)
: output_t(other.m_mid, other.m_options), Connection(NULL),
ConnectionDelete(NULL), ConnectionError(NULL), copy_active(false),
m_builder(other.m_options.projection, true), single_fmt(other.single_fmt),
osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
{
buffer.reserve(PLACE_BUFFER_SIZE);
connect();
}
virtual ~output_gazetteer_t() {}
std::shared_ptr<output_t> clone(const middle_query_t* cloned_middle) const override
{
output_gazetteer_t *clone = new output_gazetteer_t(*this);
clone->m_mid = cloned_middle;
return std::shared_ptr<output_t>(clone);
}
int start() override;
void stop() override;
void commit() override {}
void enqueue_ways(pending_queue_t &, osmid_t, size_t, size_t&) override {}
int pending_way(osmid_t, int) override { return 0; }
void enqueue_relations(pending_queue_t &, osmid_t, size_t, size_t&) override {}
int pending_relation(osmid_t, int) override { return 0; }
int node_add(osmium::Node const &node) override
{
return process_node(node);
}
int way_add(osmium::Way *way) override
{
return process_way(way);
}
int relation_add(osmium::Relation const &rel) override
{
return process_relation(rel);
}
int node_modify(osmium::Node const &node) override
{
return process_node(node);
}
int way_modify(osmium::Way *way) override
{
return process_way(way);
}
int relation_modify(osmium::Relation const &rel) override
{
return process_relation(rel);
}
int node_delete(osmid_t id) override
{
delete_place('N', id);
return 0;
}
int way_delete(osmid_t id) override
{
delete_place('W', id);
return 0;
}
int relation_delete(osmid_t id) override
{
delete_place('R', id);
return 0;
}
private:
enum { PLACE_BUFFER_SIZE = 4096 };
void stop_copy(void);
void delete_unused_classes(char osm_type, osmid_t osm_id);
void delete_place(char osm_type, osmid_t osm_id);
int process_node(osmium::Node const &node);
int process_way(osmium::Way *way);
int process_relation(osmium::Relation const &rel);
int connect();
void flush_place_buffer()
{
if (!copy_active)
{
pgsql_exec(Connection, PGRES_COPY_IN,
"COPY place (osm_type, osm_id, class, type, name, "
"admin_level, address, extratags, geometry) FROM STDIN");
copy_active = true;
}
pgsql_CopyData("place", Connection, buffer);
buffer.clear();
}
void delete_unused_full(char osm_type, osmid_t osm_id)
{
if (m_options.append) {
places.clear();
delete_place(osm_type, osm_id);
}
}
struct pg_conn *Connection;
struct pg_conn *ConnectionDelete;
struct pg_conn *ConnectionError;
bool copy_active;
std::string buffer;
place_tag_processor places;
geom::osmium_builder_t m_builder;
// string formatters
// Need to be part of the class, so we have one per thread.
boost::format single_fmt;
osmium::memory::Buffer osmium_buffer;
};
#endif