forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-gazetteer.cpp
290 lines (230 loc) · 7.88 KB
/
output-gazetteer.cpp
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <libpq-fe.h>
#include <boost/format.hpp>
#include "middle.hpp"
#include "options.hpp"
#include "osmtypes.hpp"
#include "output-gazetteer.hpp"
#include "pgsql.hpp"
#include "util.hpp"
#include "wkb.hpp"
#include <cstring>
#include <iostream>
#include <memory>
void output_gazetteer_t::stop_copy(void)
{
/* Do we have a copy active? */
if (!copy_active) return;
if (buffer.length() > 0)
{
pgsql_CopyData("place", Connection, buffer);
buffer.clear();
}
/* Terminate the copy */
if (PQputCopyEnd(Connection, nullptr) != 1)
{
std::cerr << "COPY_END for place failed: " << PQerrorMessage(Connection) << "\n";
util::exit_nicely();
}
/* Check the result */
pg_result_t res(PQgetResult(Connection));
if (PQresultStatus(res.get()) != PGRES_COMMAND_OK) {
std::cerr << "COPY_END for place failed: " << PQerrorMessage(Connection) << "\n";
util::exit_nicely();
}
/* We no longer have an active copy */
copy_active = false;
}
void output_gazetteer_t::delete_unused_classes(char osm_type, osmid_t osm_id) {
char tmp2[2];
tmp2[0] = osm_type; tmp2[1] = '\0';
char const *paramValues[2];
paramValues[0] = tmp2;
paramValues[1] = (single_fmt % osm_id).str().c_str();
auto res = pgsql_execPrepared(ConnectionDelete, "get_classes", 2,
paramValues, PGRES_TUPLES_OK);
int sz = PQntuples(res.get());
if (sz > 0 && !m_style.has_data()) {
/* unconditional delete of all places */
delete_place(osm_type, osm_id);
} else {
std::string clslist;
for (int i = 0; i < sz; i++) {
std::string cls(PQgetvalue(res.get(), i, 0));
if (!m_style.has_place(cls)) {
clslist.reserve(clslist.length() + cls.length() + 3);
if (!clslist.empty())
clslist += ',';
clslist += '\'';
clslist += cls;
clslist += '\'';
}
}
if (!clslist.empty()) {
/* Stop any active copy */
stop_copy();
/* Delete all places for this object */
pgsql_exec(Connection, PGRES_COMMAND_OK,
"DELETE FROM place WHERE osm_type = '%c' AND osm_id = %"
PRIdOSMID " and class = any(ARRAY[%s])",
osm_type, osm_id, clslist.c_str());
}
}
}
void output_gazetteer_t::delete_place(char osm_type, osmid_t osm_id)
{
/* Stop any active copy */
stop_copy();
/* Delete all places for this object */
pgsql_exec(Connection, PGRES_COMMAND_OK, "DELETE FROM place WHERE osm_type = '%c' AND osm_id = %" PRIdOSMID, osm_type, osm_id);
return;
}
int output_gazetteer_t::connect() {
/* Connection to the database */
Connection = PQconnectdb(m_options.database_options.conninfo().c_str());
/* Check to see that the backend connection was successfully made */
if (PQstatus(Connection) != CONNECTION_OK) {
std::cerr << "Connection to database failed: " << PQerrorMessage(Connection) << "\n";
return 1;
}
if (m_options.append) {
ConnectionDelete = PQconnectdb(m_options.database_options.conninfo().c_str());
if (PQstatus(ConnectionDelete) != CONNECTION_OK)
{
std::cerr << "Connection to database failed: " << PQerrorMessage(ConnectionDelete) << "\n";
return 1;
}
pgsql_exec(ConnectionDelete, PGRES_COMMAND_OK, "PREPARE get_classes (CHAR(1), " POSTGRES_OSMID_TYPE ") AS SELECT class FROM place WHERE osm_type = $1 and osm_id = $2");
}
return 0;
}
int output_gazetteer_t::start()
{
int srid = m_options.projection->target_srs();
if (connect()) {
util::exit_nicely();
}
/* Start a transaction */
pgsql_exec(Connection, PGRES_COMMAND_OK, "BEGIN");
/* (Re)create the table unless we are appending */
if (!m_options.append) {
/* Drop any existing table */
pgsql_exec(Connection, PGRES_COMMAND_OK, "DROP TABLE IF EXISTS place CASCADE");
/* Create the new table */
std::string sql =
"CREATE TABLE place ("
" osm_id " POSTGRES_OSMID_TYPE " NOT NULL,"
" osm_type char(1) NOT NULL,"
" class TEXT NOT NULL,"
" type TEXT NOT NULL,"
" name HSTORE,"
" admin_level SMALLINT,"
" address HSTORE,"
" extratags HSTORE," +
(boost::format(" geometry Geometry(Geometry,%1%) NOT NULL") % srid)
.str() +
")";
if (m_options.tblsmain_data) {
sql += " TABLESPACE " + m_options.tblsmain_data.get();
}
pgsql_exec_simple(Connection, PGRES_COMMAND_OK, sql);
std::string index_sql =
"CREATE INDEX place_id_idx ON place USING BTREE (osm_type, osm_id)";
if (m_options.tblsmain_index) {
index_sql += " TABLESPACE " + m_options.tblsmain_index.get();
}
pgsql_exec_simple(Connection, PGRES_COMMAND_OK, index_sql);
}
return 0;
}
void output_gazetteer_t::stop(osmium::thread::Pool *)
{
/* Stop any active copy */
stop_copy();
/* Commit transaction */
pgsql_exec(Connection, PGRES_COMMAND_OK, "COMMIT");
PQfinish(Connection);
if (ConnectionDelete)
PQfinish(ConnectionDelete);
if (ConnectionError)
PQfinish(ConnectionError);
return;
}
int output_gazetteer_t::process_node(osmium::Node const &node)
{
m_style.process_tags(node);
if (m_options.append)
delete_unused_classes('N', node.id());
/* Are we interested in this item? */
if (m_style.has_data()) {
auto wkb = m_builder.get_wkb_node(node.location());
m_style.copy_out(node, wkb, buffer);
flush_place_buffer();
}
return 0;
}
int output_gazetteer_t::process_way(osmium::Way *way)
{
m_style.process_tags(*way);
if (m_options.append)
delete_unused_classes('W', way->id());
/* Are we interested in this item? */
if (m_style.has_data()) {
/* Fetch the node details */
m_mid->nodes_get_list(&(way->nodes()));
/* Get the geometry of the object */
geom::osmium_builder_t::wkb_t geom;
if (way->is_closed()) {
geom = m_builder.get_wkb_polygon(*way);
}
if (geom.empty()) {
auto wkbs = m_builder.get_wkb_line(way->nodes(), 0.0);
if (wkbs.empty()) {
return 0;
}
geom = wkbs[0];
}
m_style.copy_out(*way, geom, buffer);
flush_place_buffer();
}
return 0;
}
int output_gazetteer_t::process_relation(osmium::Relation const &rel)
{
auto const &tags = rel.tags();
char const *type = tags["type"];
if (!type) {
delete_unused_full('R', rel.id());
return 0;
}
bool is_waterway = strcmp(type, "waterway") == 0;
if (strcmp(type, "associatedStreet") == 0
|| !(strcmp(type, "boundary") == 0
|| strcmp(type, "multipolygon") == 0 || is_waterway)) {
delete_unused_full('R', rel.id());
return 0;
}
m_style.process_tags(rel);
if (m_options.append)
delete_unused_classes('R', rel.id());
/* Are we interested in this item? */
if (!m_style.has_data())
return 0;
/* get the boundary path (ways) */
osmium_buffer.clear();
auto num_ways = m_mid->rel_way_members_get(rel, nullptr, osmium_buffer);
if (num_ways == 0) {
delete_unused_full('R', rel.id());
return 0;
}
for (auto &w : osmium_buffer.select<osmium::Way>()) {
m_mid->nodes_get_list(&(w.nodes()));
}
auto geoms = is_waterway
? m_builder.get_wkb_multiline(osmium_buffer, 0.0)
: m_builder.get_wkb_multipolygon(rel, osmium_buffer);
if (!geoms.empty()) {
m_style.copy_out(rel, geoms[0], buffer);
flush_place_buffer();
}
return 0;
}