forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse-osmium.cpp
167 lines (146 loc) · 5.34 KB
/
parse-osmium.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
/*
#-----------------------------------------------------------------------------
# osm2pgsql - converts planet.osm file into PostgreSQL
# compatible output suitable to be rendered by mapnik
#-----------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-----------------------------------------------------------------------------
*/
#include <boost/format.hpp>
#include "parse-osmium.hpp"
#include "reprojection.hpp"
#include "osmdata.hpp"
#include <osmium/io/any_input.hpp>
#include <osmium/handler.hpp>
#include <osmium/visitor.hpp>
#include <osmium/osm.hpp>
parse_osmium_t::parse_osmium_t(const std::string &fmt, int extra_attrs,
const bbox_t &bbox, const reprojection *proj,
bool do_append)
: parse_t(extra_attrs, bbox, proj), data(nullptr),
format(fmt == "auto"?"":fmt), append(do_append)
{}
void
parse_osmium_t::stream_file(const std::string &filename, osmdata_t *osmdata)
{
data = osmdata;
osmium::io::File infile(filename, format);
if (infile.format() == osmium::io::file_format::unknown)
throw std::runtime_error(format.empty()
?"Cannot detect file format. Try using -r."
: ((boost::format("Unknown file format '%1%'.")
% format).str()));
fprintf(stderr, "Using %s parser.\n", osmium::io::as_string(infile.format()));
osmium::io::Reader reader(infile);
osmium::apply(reader, *this);
reader.close();
data = nullptr;
}
void parse_osmium_t::node(osmium::Node& node)
{
// if the node is not valid, then node.location.lat/lon() can throw.
// we probably ought to treat invalid locations as if they were
// deleted and ignore them.
if (!node.location().valid()) {
fprintf(stderr, "WARNING: Node %" PRIdOSMID " (version %ud) has an invalid "
"location and has been ignored. This is not expected to happen with "
"recent planet files, so please check that your input is correct.\n",
node.id(), node.version());
return;
}
double lat = node.location().lat_without_check();
double lon = node.location().lon_without_check();
if (bbox.inside(lat, lon)) {
proj->reproject(&lat, &lon);
if (node.deleted()) {
data->node_delete(node.id());
} else {
convert_tags(node);
if (append) {
data->node_modify(node.id(), lat, lon, tags);
} else {
data->node_add(node.id(), lat, lon, tags);
}
}
stats.add_node(node.id());
}
}
void parse_osmium_t::way(osmium::Way& way)
{
if (way.deleted()) {
data->way_delete(way.id());
} else {
convert_tags(way);
convert_nodes(way.nodes());
if (append) {
data->way_modify(way.id(), nds, tags);
} else {
data->way_add(way.id(), nds, tags);
}
}
stats.add_way(way.id());
}
void parse_osmium_t::relation(osmium::Relation& rel)
{
if (rel.deleted()) {
data->relation_delete(rel.id());
} else {
convert_tags(rel);
convert_members(rel.members());
if (append) {
data->relation_modify(rel.id(), members, tags);
} else {
data->relation_add(rel.id(), members, tags);
}
}
stats.add_rel(rel.id());
}
void parse_osmium_t::convert_tags(const osmium::OSMObject &obj)
{
tags.clear();
for (auto const &t : obj.tags()) {
tags.emplace_back(t.key(), t.value());
}
if (extra_attributes) {
tags.emplace_back("osm_user", obj.user());
tags.emplace_back("osm_uid", std::to_string(obj.uid()));
tags.emplace_back("osm_version", std::to_string(obj.version()));
tags.emplace_back("osm_timestamp", obj.timestamp().to_iso());
tags.emplace_back("osm_changeset", std::to_string(obj.changeset()));
}
}
void parse_osmium_t::convert_nodes(const osmium::NodeRefList &in_nodes)
{
nds.clear();
for (auto const &n : in_nodes) {
nds.push_back(n.ref());
}
}
void parse_osmium_t::convert_members(const osmium::RelationMemberList &in_rels)
{
members.clear();
for (auto const &m: in_rels) {
OsmType type;
switch (m.type()) {
case osmium::item_type::node: type = OSMTYPE_NODE; break;
case osmium::item_type::way: type = OSMTYPE_WAY; break;
case osmium::item_type::relation: type = OSMTYPE_RELATION; break;
default:
fprintf(stderr, "Unsupported type: %u""\n", unsigned(m.type()));
}
members.emplace_back(type, m.ref(), m.role());
}
}