-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckdata.cpp
More file actions
330 lines (287 loc) · 9.17 KB
/
checkdata.cpp
File metadata and controls
330 lines (287 loc) · 9.17 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "cppGzip/DecodeGzip.h"
#include "cppGzip/EncodeGzip.h"
#include "cppo5m/OsmData.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "dbjson.h"
#include "util.h"
#include "pgmap.h"
using namespace std;
class DataChecker : public IDataStreamHandler
{
private:
map<int64_t, vector<int64_t> > nodeIdVers, wayIdVers, relationIdVers;
std::shared_ptr<class PgTransaction> transaction;
void NodesExists(const std::set<int64_t> &objectIds,
std::set<int64_t> &exists, std::set<int64_t> ¬Exists);
void WaysExists(const std::set<int64_t> &objectIds,
std::set<int64_t> &exists, std::set<int64_t> ¬Exists);
void RelationsExists(const std::set<int64_t> &objectIds,
std::set<int64_t> &exists, std::set<int64_t> ¬Exists);
public:
DataChecker(std::shared_ptr<class PgTransaction> transactionIn);
virtual ~DataChecker();
virtual bool Sync() {return false;};
virtual bool Reset() {return false;};
virtual bool Finish();
virtual bool StoreIsDiff(bool) {return false;};
virtual bool StoreBounds(double x1, double y1, double x2, double y2);
virtual bool StoreNode(int64_t objId, const class MetaData &metaData,
const TagMap &tags, double lat, double lon);
virtual bool StoreWay(int64_t objId, const class MetaData &metaData,
const TagMap &tags, const std::vector<int64_t> &refs);
virtual bool StoreRelation(int64_t objId, const class MetaData &metaData, const TagMap &tags,
const std::vector<std::string> &refTypeStrs, const std::vector<int64_t> &refIds,
const std::vector<std::string> &refRoles);
};
DataChecker::DataChecker(std::shared_ptr<class PgTransaction> transactionIn):
transaction(transactionIn)
{
}
DataChecker::~DataChecker()
{
}
bool DataChecker::Finish()
{
for(map<int64_t, vector<int64_t> >::iterator it = nodeIdVers.begin(); it != nodeIdVers.end(); it++)
{
if(it->second.size() > 1)
cout << "Node " << it->first << " occurs " << it->second.size() << " times" << endl;
}
for(map<int64_t, vector<int64_t> >::iterator it = wayIdVers.begin(); it != wayIdVers.end(); it++)
{
if(it->second.size() > 1)
cout << "Way " << it->first << " occurs " << it->second.size() << " times" << endl;
}
for(map<int64_t, vector<int64_t> >::iterator it = relationIdVers.begin(); it != relationIdVers.end(); it++)
{
if(it->second.size() > 1)
cout << "Relation " << it->first << " occurs " << it->second.size() << " times" << endl;
}
return false;
}
bool DataChecker::StoreBounds(double x1, double y1, double x2, double y2)
{
cout << "bounds " << x1 <<","<< y1 <<","<< x2 <<","<< y2 << endl;
return false;
}
bool DataChecker::StoreNode(int64_t objId, const class MetaData &metaData,
const TagMap &tags, double lat, double lon)
{
if(!transaction)
{
map<int64_t, vector<int64_t> >::iterator it = nodeIdVers.find(objId);
if(it == nodeIdVers.end())
{
vector<int64_t> vers;
vers.push_back(metaData.version);
nodeIdVers[objId] = vers;
}
else
it->second.push_back(metaData.version);
}
if(lat < -90 or lat > 90)
cout << "Node " << objId << " lat out of range " << lat << endl;
if(lon < -180 or lon > 180)
cout << "Node " << objId << " lon out of range " << lon << endl;
return false;
}
bool DataChecker::StoreWay(int64_t objId, const class MetaData &metaData,
const TagMap &tags, const std::vector<int64_t> &refs)
{
if(!transaction)
{
map<int64_t, vector<int64_t> >::iterator it = wayIdVers.find(objId);
if(it == wayIdVers.end())
{
vector<int64_t> vers;
vers.push_back(metaData.version);
wayIdVers[objId] = vers;
}
else
{
it->second.push_back(metaData.version);
}
}
//Check for too few nodes
if (refs.size() < 2)
cout << "Too few nodes in way: " << objId <<endl;
//Check for missing nodes
std::set<int64_t> nodeIds(refs.begin(), refs.end()), nodeExists, nodeNotExists;
this->NodesExists(nodeIds, nodeExists, nodeNotExists);
for(auto it=nodeNotExists.begin(); it!=nodeNotExists.end(); it++)
{
cout << "Node " << (*it) << " does not exist, but referenced by way: ";
cout << objId << endl;
}
return false;
}
bool DataChecker::StoreRelation(int64_t objId, const class MetaData &metaData, const TagMap &tags,
const std::vector<std::string> &refTypeStrs, const std::vector<int64_t> &refIds,
const std::vector<std::string> &refRoles)
{
if(!transaction)
{
map<int64_t, vector<int64_t> >::iterator it = relationIdVers.find(objId);
if(it == relationIdVers.end())
{
vector<int64_t> vers;
vers.push_back(metaData.version);
relationIdVers[objId] = vers;
}
else
it->second.push_back(metaData.version);
}
if(refTypeStrs.size() != refIds.size() or refTypeStrs.size() != refRoles.size())
throw runtime_error("Relation refs have inconsistent lengths");
//Check for too few nodes
if (refTypeStrs.size() == 0)
cout << "Too few members in relation: " << objId <<endl;
//Check for missing members
std::set<int64_t> refNodeIds, refWayIds, refRelationIds, refExists, refNotExists;
for(size_t i=0; i<refTypeStrs.size(); i++)
{
if(refTypeStrs[i] == "node")
refNodeIds.insert(refIds[i]);
else if(refTypeStrs[i] == "way")
refWayIds.insert(refIds[i]);
else if(refTypeStrs[i] == "relation")
refRelationIds.insert(refIds[i]);
}
this->NodesExists(refNodeIds, refExists, refNotExists);
for(auto it=refNotExists.begin(); it!=refNotExists.end(); it++)
cout << "Node " << (*it) << " does not exist, but referenced by relation: " << objId << endl;
refExists.clear();
refNotExists.clear();
this->WaysExists(refWayIds, refExists, refNotExists);
for(auto it=refNotExists.begin(); it!=refNotExists.end(); it++)
cout << "Way " << (*it) << " does not exist, but referenced by relation: " << objId << endl;
refExists.clear();
refNotExists.clear();
this->RelationsExists(refRelationIds, refExists, refNotExists);
for(auto it=refNotExists.begin(); it!=refNotExists.end(); it++)
cout << "Relation " << (*it) << " does not exist, but referenced by relation: " << objId << endl;
return false;
}
void DataChecker::NodesExists(const std::set<int64_t> &objectIds,
std::set<int64_t> &exists, std::set<int64_t> ¬Exists)
{
if(objectIds.size()==0) return;
if(!transaction)
{
for(auto it=objectIds.begin(); it!=objectIds.end(); it++)
{
if(nodeIdVers.find(*it) != nodeIdVers.end())
exists.insert(*it);
else
notExists.insert(*it);
}
}
else
{
shared_ptr<class OsmData> osmData(new class OsmData());
transaction->GetObjectsById("node", objectIds, osmData);
std::set<int64_t> inDatabase;
for(size_t i=0; i<osmData->nodes.size(); i++)
inDatabase.insert(osmData->nodes[i].objId);
for(auto it=objectIds.begin(); it!=objectIds.end(); it++)
{
if(inDatabase.find(*it) != inDatabase.end())
exists.insert(*it);
else
notExists.insert(*it);
}
}
}
void DataChecker::WaysExists(const std::set<int64_t> &objectIds,
std::set<int64_t> &exists, std::set<int64_t> ¬Exists)
{
if(objectIds.size()==0) return;
if(!transaction)
{
for(auto it=objectIds.begin(); it!=objectIds.end(); it++)
{
if(wayIdVers.find(*it) != wayIdVers.end())
exists.insert(*it);
else
notExists.insert(*it);
}
}
else
{
shared_ptr<class OsmData> osmData(new class OsmData());
transaction->GetObjectsById("way", objectIds, osmData);
std::set<int64_t> inDatabase;
for(size_t i=0; i<osmData->ways.size(); i++)
inDatabase.insert(osmData->ways[i].objId);
for(auto it=objectIds.begin(); it!=objectIds.end(); it++)
{
if(inDatabase.find(*it) != inDatabase.end())
exists.insert(*it);
else
notExists.insert(*it);
}
}
}
void DataChecker::RelationsExists(const std::set<int64_t> &objectIds,
std::set<int64_t> &exists, std::set<int64_t> ¬Exists)
{
if(objectIds.size()==0) return;
if(!transaction)
{
for(auto it=objectIds.begin(); it!=objectIds.end(); it++)
{
if(relationIdVers.find(*it) != relationIdVers.end())
exists.insert(*it);
else
notExists.insert(*it);
}
}
else
{
shared_ptr<class OsmData> osmData(new class OsmData());
transaction->GetObjectsById("relation", objectIds, osmData);
std::set<int64_t> inDatabase;
for(size_t i=0; i<osmData->relations.size(); i++)
inDatabase.insert(osmData->relations[i].objId);
for(auto it=objectIds.begin(); it!=objectIds.end(); it++)
{
if(inDatabase.find(*it) != inDatabase.end())
exists.insert(*it);
else
notExists.insert(*it);
}
}
}
int main(int argc, char **argv)
{
std::shared_ptr<class PgTransaction> transaction;
if(argc > 1)
{
string argv1 = argv[1];
if (argv1!="db")
{
shared_ptr<class IDataStreamHandler> dataChecker(new class DataChecker(transaction));
LoadOsmFromFile(argv[1], dataChecker);
}
else
{
cout << "Reading settings from config.cfg" << endl;
std::map<string, string> config;
ReadSettingsFile("config.cfg", config);
string cstr = GeneratePgConnectionString(config);
class PgMap pgMap(cstr, config["dbtableprefix"], config["dbtablemodifyprefix"],
config["dbtablemodifyprefix"], config["dbtabletestprefix"]);
transaction = pgMap.GetTransaction("ACCESS SHARE");
shared_ptr<class IDataStreamHandler> dataChecker(new class DataChecker(transaction));
transaction->Dump(false, false, true, true, dataChecker);
}
}
else
{
cout << "Please specify filename as program argument or use 'db' to scan database" << endl;
}
cout << "All done!" << endl;
}