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
|
#ifndef OUTPUT_GAZETTEER_H
#define OUTPUT_GAZETTEER_H
#include <memory>
#include <string>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/format.hpp>
#include "geometry-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(const taglist_t &tags);
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(char osm_type, osmid_t osm_id, const std::string &geom,
std::string &buffer);
void clear();
private:
void copy_opt_string(const std::string *val, std::string &buffer)
{
if (val) {
escape(*val, buffer);
buffer += "\t";
} else {
buffer += "\\N\t";
}
}
std::string domain_name(const std::string &cls)
{
std::string ret;
bool hasname = false;
std::string prefix(cls + ":name");
for (const auto& item: *src) {
if (boost::starts_with(item.key, prefix) &&
(item.key.length() == prefix.length()
|| item.key[prefix.length()] == ':')) {
if (!hasname) {
ret.reserve(item.key.length() + item.value.length() + 10);
hasname = true;
} else
ret += ",";
ret += "\"";
escape_array_record(std::string(item.key, 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<const tag_t *> names;
std::vector<const tag_t *> extratags;
std::vector<const tag_t *> address;
const taglist_t *src;
int admin_level;
const std::string *countrycode;
std::string housenumber;
const std::string *street;
const std::string *addr_place;
const std::string *postcode;
boost::format single_fmt;
public:
std::string srid_str;
};
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),
single_fmt("%1%"),
point_fmt("POINT(%.15g %.15g)")
{
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),
reproj(other.reproj),
single_fmt(other.single_fmt),
point_fmt(other.point_fmt)
{
buffer.reserve(PLACE_BUFFER_SIZE);
builder.set_exclude_broken_polygon(m_options.excludepoly);
connect();
}
virtual ~output_gazetteer_t() {}
virtual std::shared_ptr<output_t> clone(const middle_query_t* cloned_middle) const
{
output_gazetteer_t *clone = new output_gazetteer_t(*this);
clone->m_mid = cloned_middle;
return std::shared_ptr<output_t>(clone);
}
int start();
void stop();
void commit() {}
void enqueue_ways(pending_queue_t &job_queue, osmid_t id, size_t output_id, size_t& added) {}
int pending_way(osmid_t id, int exists) { return 0; }
void enqueue_relations(pending_queue_t &job_queue, osmid_t id, size_t output_id, size_t& added) {}
int pending_relation(osmid_t id, int exists) { return 0; }
int node_add(osmid_t id, double lat, double lon, const taglist_t &tags)
{
return process_node(id, lat, lon, tags);
}
int way_add(osmid_t id, const idlist_t &nodes, const taglist_t &tags)
{
return process_way(id, nodes, tags);
}
int relation_add(osmid_t id, const memberlist_t &members, const taglist_t &tags)
{
return process_relation(id, members, tags);
}
int node_modify(osmid_t id, double lat, double lon, const taglist_t &tags)
{
return process_node(id, lat, lon, tags);
}
int way_modify(osmid_t id, const idlist_t &nodes, const taglist_t &tags)
{
return process_way(id, nodes, tags);
}
int relation_modify(osmid_t id, const memberlist_t &members, const taglist_t &tags)
{
return process_relation(id, members, tags);
}
int node_delete(osmid_t id)
{
delete_place('N', id);
return 0;
}
int way_delete(osmid_t id)
{
delete_place('W', id);
return 0;
}
int relation_delete(osmid_t id)
{
delete_place('R', id);
return 0;
}
private:
enum { PLACE_BUFFER_SIZE = 4092 };
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(osmid_t id, double lat, double lon, const taglist_t &tags);
int process_way(osmid_t id, const idlist_t &nodes, const taglist_t &tags);
int process_relation(osmid_t id, const memberlist_t &members, const taglist_t &tags);
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, housenumber, street, addr_place, isin, postcode, country_code, 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;
geometry_builder builder;
std::shared_ptr<reprojection> reproj;
// string formatters
// Need to be part of the class, so we have one per thread.
boost::format single_fmt;
boost::format point_fmt;
};
extern output_gazetteer_t out_gazetteer;
#endif
|