This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_deletion_history.cpp
154 lines (131 loc) · 4.7 KB
/
user_deletion_history.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
#include <unordered_set>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <osmium/io/any_input.hpp>
#include <osmium/handler.hpp>
#include <osmium/visitor.hpp>
struct HistoryHandler : public osmium::handler::Handler {
protected:
std::unordered_set<osmium::user_id_type> m_filter_users;
std::ofstream &m_nodefile, &m_wayfile, &m_relfile;
bool matches_user_filter(const osmium::OSMObject& obj) {
if (obj.user_is_anonymous()) return false;
std::unordered_set<osmium::user_id_type>::const_iterator it = m_filter_users.find(obj.uid());
if (it == m_filter_users.end()) return false;
return true;
}
public:
uint64_t nodes = 0, unodes = 0;
uint64_t ways = 0, uways = 0;
uint64_t rels = 0, urels = 0;
HistoryHandler(std::unordered_set<osmium::user_id_type> &filter_users,
std::ofstream &nodefile, std::ofstream &wayfile, std::ofstream &relfile) : Handler(),
m_filter_users(filter_users), m_nodefile(nodefile), m_wayfile(wayfile), m_relfile(relfile) {
}
void node(const osmium::Node& node) {
++nodes;
if (!matches_user_filter(node)) return;
++unodes;
if (node.visible()==false) {
m_nodefile <<
node.id() << "\t" <<
node.version() << "\t" <<
node.changeset() << "\t" <<
node.timestamp().to_iso() << "\t" <<
node.uid() << std::endl;
}
}
void way(const osmium::Way& way) {
++ways;
if (!matches_user_filter(way)) return;
++uways;
if (way.visible()==false) {
m_wayfile <<
way.id() << "\t" <<
way.version() << "\t" <<
way.changeset() << "\t" <<
way.timestamp().to_iso() << "\t" <<
way.uid() << std::endl;
}
}
void relation(const osmium::Relation& rel) {
++rels;
if (!matches_user_filter(rel)) return;
++urels;
if (rel.visible()==false) {
m_relfile <<
rel.id() << "\t" <<
rel.version() << "\t" <<
rel.changeset() << "\t" <<
rel.timestamp().to_iso() << "\t" <<
rel.uid() << std::endl;
}
}
};
// http://insanecoding.blogspot.co.uk/2011/11/how-to-read-in-file-in-c.html
std::string get_file_contents(const std::string filename) {
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in) {
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}
throw(errno);
}
bool load_user_filter(const std::string filename, std::unordered_set<osmium::user_id_type> &filter_users) {
try {
std::string str = get_file_contents(filename); // throws int errno
std::istringstream ss(str);
ss.imbue(std::locale::classic()); // locale independent parsing
osmium::user_id_type uid;
while (ss >> uid) { // throws std::exception
filter_users.insert(uid);
if (ss.peek() == '\n') ss.ignore();
}
return true;
}
catch (int errno) {
std::cout << "Error: " << strerror(errno) << std::endl;
}
catch (std::exception &e) {
std::cout << "Exception: " << e.what() << std::endl;
}
return false;
}
int main(int argc, char* argv[]) {
if (argc != 6) {
std::cerr << "Usage: " << argv[0] << " OSM-HISTORY-FILE USERID_INFILE NODE_OUTFILE WAY_OUTFILE REL_OUTFILE" << std::endl;
return 1;
}
osmium::io::File infile(argv[1]);
osmium::io::Reader reader(infile);
if (reader.header().has_multiple_object_versions()) {
std::cout << "History file." << std::endl;
}
std::unordered_set<osmium::user_id_type> filter_users;
if (!load_user_filter(argv[2], filter_users)) {
return 1;
}
std::cout << "Loaded " << filter_users.size() << " user IDs to filter." << std::endl;
std::ofstream nodefile;
nodefile.open(argv[3]);
std::ofstream wayfile;
wayfile.open(argv[4]);
std::ofstream relfile;
relfile.open(argv[5]);
HistoryHandler handler(filter_users, nodefile, wayfile, relfile);
osmium::apply(reader, handler);
reader.close();
nodefile.close();
wayfile.close();
relfile.close();
std::cout << "Nodes: " << handler.unodes << " of " << handler.nodes << "\n";
std::cout << "Ways: " << handler.uways << " of " << handler.ways << "\n";
std::cout << "Relations: " << handler.urels << " of " << handler.rels << "\n";
google::protobuf::ShutdownProtobufLibrary();
}