-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosm2csv.cpp
More file actions
356 lines (311 loc) · 12.6 KB
/
osm2csv.cpp
File metadata and controls
356 lines (311 loc) · 12.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "cppGzip/DecodeGzip.h"
#include "cppGzip/EncodeGzip.h"
#include "cppo5m/OsmData.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include "dbjson.h"
#include "util.h"
using namespace std;
/*
select * from pg_stat_activity;
SELECT *, ST_X(geom) as lon, ST_Y(geom) AS lat FROM andorra_nodes WHERE geom && ST_MakeEnvelope(1.5020099, 42.5228903, 1.540173, 42.555443, 4326);
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO microcosm;
CREATE INDEX nodes_gin ON nodes USING GIN (tags);
SELECT * FROM nodes WHERE tags ? 'amenity' LIMIT 10;
Import from fosm.org 2015 dump
table_name | row_estimate | toast_bytes | table_bytes | total | index | toast | table
-------------------------+--------------+-------------+--------------+------------+------------+------------+------------
planet_nodes | 1.36598e+09 | 131072 | 166346784768 | 252 GB | 98 GB | 128 kB | 155 GB
planet_ways | 1.23554e+08 | 548945920 | 48414597120 | 48 GB | 2647 MB | 524 MB | 45 GB
planet_relations | 1.39281e+06 | 14221312 | 653623296 | 1082 MB | 445 MB | 14 MB | 623 MB
planet_way_mems | 1.6108e+09 | | 84069392384 | 112 GB | 34 GB | | 78 GB
planet_relation_mems_w | 1.2219e+07 | | 637747200 | 870 MB | 262 MB | | 608 MB
planet_relation_mems_r | 112434 | | 5898240 | 8256 kB | 2496 kB | | 5760 kB
planet_relation_mems_n | 1.78653e+06 | | 93265920 | 127 MB | 38 MB | | 89 MB
COPY planet_relations TO PROGRAM 'gzip > /home/postgres/dumprelations.gz' WITH (FORMAT 'csv', DELIMITER ',', NULL 'NULL');
*/
inline string Int64ToStr(int64_t val)
{
stringstream ss;
ss << val;
return ss.str();
}
class CsvStore : public IDataStreamHandler
{
private:
std::filebuf livenodeFile, livewayFile, liverelationFile, oldnodeFile, oldwayFile, oldrelationFile;
std::filebuf nodeIdsFile, wayIdsFile, relationIdsFile;
std::filebuf wayMembersFile, relationMemNodesFile, relationMemWaysFile, relationMemRelsFile;
std::shared_ptr<class EncodeGzip> livenodeFileGzip, livewayFileGzip, liverelationFileGzip, oldnodeFileGzip, oldwayFileGzip, oldrelationFileGzip;
std::shared_ptr<class EncodeGzip> nodeIdsFileGzip, wayIdsFileGzip, relationIdsFileGzip;
std::shared_ptr<class EncodeGzip> wayMembersFileGzip, relationMemNodesFileGzip, relationMemWaysFileGzip, relationMemRelsFileGzip;
public:
CsvStore(const std::string &outPrefix);
virtual ~CsvStore();
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) {return false;};
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);
};
CsvStore::CsvStore(const std::string &outPrefix)
{
cout << outPrefix+"livenodes.csv.gz" << endl;
livenodeFile.open(outPrefix+"livenodes.csv.gz", std::ios::out | std::ios::binary);
if(!livenodeFile.is_open()) throw runtime_error("Error opening output");
livenodeFileGzip.reset(new class EncodeGzip(livenodeFile));
livewayFile.open(outPrefix+"liveways.csv.gz", std::ios::out | std::ios::binary);
livewayFileGzip.reset(new class EncodeGzip(livewayFile));
liverelationFile.open(outPrefix+"liverelations.csv.gz", std::ios::out | std::ios::binary);
liverelationFileGzip.reset(new class EncodeGzip(liverelationFile));
oldnodeFile.open(outPrefix+"oldnodes.csv.gz", std::ios::out | std::ios::binary);
oldnodeFileGzip.reset(new class EncodeGzip(oldnodeFile));
oldwayFile.open(outPrefix+"oldways.csv.gz", std::ios::out | std::ios::binary);
oldwayFileGzip.reset(new class EncodeGzip(oldwayFile));
oldrelationFile.open(outPrefix+"oldrelations.csv.gz", std::ios::out | std::ios::binary);
oldrelationFileGzip.reset(new class EncodeGzip(oldrelationFile));
nodeIdsFile.open(outPrefix+"nodeids.csv.gz", std::ios::out | std::ios::binary);
nodeIdsFileGzip.reset(new class EncodeGzip(nodeIdsFile));
wayIdsFile.open(outPrefix+"wayids.csv.gz", std::ios::out | std::ios::binary);
wayIdsFileGzip.reset(new class EncodeGzip(wayIdsFile));
relationIdsFile.open(outPrefix+"relationids.csv.gz", std::ios::out | std::ios::binary);
relationIdsFileGzip.reset(new class EncodeGzip(relationIdsFile));
wayMembersFile.open(outPrefix+"waymems.csv.gz", std::ios::out | std::ios::binary);
wayMembersFileGzip.reset(new class EncodeGzip(wayMembersFile));
relationMemNodesFile.open(outPrefix+"relationmems-n.csv.gz", std::ios::out | std::ios::binary);
relationMemNodesFileGzip.reset(new class EncodeGzip(relationMemNodesFile));
relationMemWaysFile.open(outPrefix+"relationmems-w.csv.gz", std::ios::out | std::ios::binary);
relationMemWaysFileGzip.reset(new class EncodeGzip(relationMemWaysFile));
relationMemRelsFile.open(outPrefix+"relationmems-r.csv.gz", std::ios::out | std::ios::binary);
relationMemRelsFileGzip.reset(new class EncodeGzip(relationMemRelsFile));
}
CsvStore::~CsvStore()
{
livenodeFileGzip.reset();
livenodeFile.close();
livewayFileGzip.reset();
livewayFile.close();
liverelationFileGzip.reset();
liverelationFile.close();
oldnodeFileGzip.reset();
oldnodeFile.close();
oldwayFileGzip.reset();
oldwayFile.close();
oldrelationFileGzip.reset();
oldrelationFile.close();
nodeIdsFileGzip.reset();
nodeIdsFile.close();
wayIdsFileGzip.reset();
wayIdsFile.close();
relationIdsFileGzip.reset();
relationIdsFile.close();
wayMembersFileGzip.reset();
wayMembersFile.close();
relationMemNodesFileGzip.reset();
relationMemNodesFile.close();
relationMemWaysFileGzip.reset();
relationMemWaysFile.close();
relationMemRelsFileGzip.reset();
relationMemRelsFile.close();
}
bool CsvStore::Finish()
{
return false;
}
bool CsvStore::StoreNode(int64_t objId, const class MetaData &metaData,
const TagMap &tags, double lat, double lon)
{
string tagsJson;
EncodeTags(tags, tagsJson);
StrReplaceAll(tagsJson, "\"", "\"\"");
string usernameStr = metaData.username;
if(metaData.username.size() > 0)
{
StrReplaceAll(usernameStr, "\"", "\"\"");
usernameStr = "\""+usernameStr+"\"";
}
else
usernameStr = "NULL";
string uidStr;
if(metaData.uid!=0)
uidStr = Int64ToStr(metaData.uid);
else
uidStr = "NULL";
string changesetStr = Int64ToStr(metaData.changeset);
if(metaData.changeset==0)
changesetStr="NULL";
string timestampStr = Int64ToStr(metaData.timestamp);
if(metaData.timestamp==0)
timestampStr="NULL";
string visibleStr = metaData.visible ? "true" : "false";
std::string changesetIndex="NULL";
stringstream ss;
ss.precision(9);
if(metaData.current and metaData.visible)
{
ss << objId <<","<< changesetStr <<","<< changesetIndex <<","<< usernameStr <<","<< uidStr <<","<< \
timestampStr <<","<< metaData.version <<",\"" << tagsJson << "\",SRID=4326;POINT("<< fixed << lon<<" "<<lat<<")\n";
string row(ss.str());
this->livenodeFileGzip->sputn(row.c_str(), row.size());
}
else
{
ss << objId <<","<< changesetStr <<","<< changesetIndex <<","<< usernameStr <<","<< uidStr <<","<< visibleStr <<","<<\
timestampStr <<","<< metaData.version <<",\"" << tagsJson << "\",SRID=4326;POINT("<< fixed << lon<<" "<<lat<<")\n";
string row(ss.str());
this->oldnodeFileGzip->sputn(row.c_str(), row.size());
}
string objIdStr = Int64ToStr(objId);
objIdStr += "\n";
this->nodeIdsFileGzip->sputn(objIdStr.c_str(), objIdStr.size());
return false;
}
bool CsvStore::StoreWay(int64_t objId, const class MetaData &metaData,
const TagMap &tags, const std::vector<int64_t> &refs)
{
string tagsJson;
EncodeTags(tags, tagsJson);
StrReplaceAll(tagsJson, "\"", "\"\"");
string refsJson;
EncodeInt64Vec(refs, refsJson);
StrReplaceAll(refsJson, "\"", "\"\"");
string usernameStr = metaData.username;
if(metaData.username.size() > 0)
{
StrReplaceAll(usernameStr, "\"", "\"\"");
usernameStr = "\""+usernameStr+"\"";
}
else
usernameStr = "NULL";
string uidStr;
if(metaData.uid!=0)
uidStr = Int64ToStr(metaData.uid);
else
uidStr = "NULL";
string changesetStr = Int64ToStr(metaData.changeset);
if(metaData.changeset==0)
changesetStr="NULL";
string timestampStr = Int64ToStr(metaData.timestamp);
if(metaData.timestamp==0)
timestampStr="NULL";
string visibleStr = metaData.visible ? "true" : "false";
std::string changesetIndex="NULL";
stringstream ss;
if(metaData.current and metaData.visible)
{
ss << objId <<","<< changesetStr <<","<< changesetIndex <<","<< usernameStr <<","<< uidStr <<","<< \
timestampStr <<","<< metaData.version <<",\"" << tagsJson << "\",\""<<refsJson<<"\",NULL\n";
string row(ss.str());
this->livewayFileGzip->sputn(row.c_str(), row.size());
}
else
{
ss << objId <<","<< changesetStr <<","<< changesetIndex <<","<< usernameStr <<","<< uidStr <<","<< visibleStr <<","<<\
timestampStr <<","<< metaData.version <<",\"" << tagsJson << "\",\""<<refsJson<<"\",NULL\n";
string row(ss.str());
this->oldwayFileGzip->sputn(row.c_str(), row.size());
}
string objIdStr = Int64ToStr(objId);
objIdStr += "\n";
this->wayIdsFileGzip->sputn(objIdStr.c_str(), objIdStr.size());
for(size_t i=0; i<refs.size(); i++)
{
stringstream ss2;
ss2 << objId <<","<< metaData.version <<","<< i <<","<< refs[i] << "\n";
string memStr(ss2.str());
this->wayMembersFileGzip->sputn(memStr.c_str(), memStr.size());
}
return false;
}
bool CsvStore::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)
{
string tagsJson;
EncodeTags(tags, tagsJson);
StrReplaceAll(tagsJson, "\"", "\"\"");
string refsJson;
EncodeRelationMems(refTypeStrs, refIds, refsJson);
StrReplaceAll(refsJson, "\"", "\"\"");
string refRolesJson;
EncodeStringVec(refRoles, refRolesJson);
StrReplaceAll(refRolesJson, "\"", "\"\"");
string usernameStr = metaData.username;
if(metaData.username.size() > 0)
{
StrReplaceAll(usernameStr, "\"", "\"\"");
usernameStr = "\""+usernameStr+"\"";
}
else
usernameStr = "NULL";
string uidStr;
if(metaData.uid!=0)
uidStr = Int64ToStr(metaData.uid);
else
uidStr = "NULL";
string changesetStr = Int64ToStr(metaData.changeset);
if(metaData.changeset==0)
changesetStr="NULL";
string timestampStr = Int64ToStr(metaData.timestamp);
if(metaData.timestamp==0)
timestampStr="NULL";
string visibleStr = metaData.visible ? "true" : "false";
std::string changesetIndex="NULL";
stringstream ss;
if(metaData.current and metaData.visible)
{
ss << objId <<","<< changesetStr <<","<< changesetIndex <<","<< usernameStr <<","<< uidStr <<","<< \
timestampStr <<","<< metaData.version <<",\"" << tagsJson << "\",\""<<refsJson<<"\",\""<<refRolesJson<<"\",NULL\n";
string row(ss.str());
this->liverelationFileGzip->sputn(row.c_str(), row.size());
}
else
{
ss << objId <<","<< changesetStr <<","<< changesetIndex <<","<< usernameStr <<","<< uidStr <<","<< visibleStr <<","<<\
timestampStr <<","<< metaData.version <<",\"" << tagsJson << "\",\""<<refsJson<<"\",\""<<refRolesJson<<"\",NULL\n";
string row(ss.str());
this->oldrelationFileGzip->sputn(row.c_str(), row.size());
}
string objIdStr = Int64ToStr(objId);
objIdStr += "\n";
this->relationIdsFileGzip->sputn(objIdStr.c_str(), objIdStr.size());
for(size_t i=0; i<refIds.size(); i++)
{
const std::string &refTypeStr = refTypeStrs[i];
stringstream ss2;
ss2 << objId <<","<< metaData.version <<","<< i <<","<< refIds[i] << "\n";
string memStr(ss2.str());
if(refTypeStr == "node")
{
this->relationMemNodesFileGzip->sputn(memStr.c_str(), memStr.size());
}
else if(refTypeStr == "way")
{
this->relationMemWaysFileGzip->sputn(memStr.c_str(), memStr.size());
}
else if(refTypeStr == "relation")
{
this->relationMemRelsFileGzip->sputn(memStr.c_str(), memStr.size());
}
}
return false;
}
int main(int argc, char **argv)
{
cout << "Reading settings from config.cfg" << endl;
std::map<string, string> config;
ReadSettingsFile("config.cfg", config);
cout << "Writing output to " << config["csv_absolute_path"] << endl;
shared_ptr<class IDataStreamHandler> csvStore(new class CsvStore(config["csv_absolute_path"]));
LoadOsmFromFile(config["dump_path"], csvStore);
csvStore.reset();
cout << "All done!" << endl;
}