forked from avar/osm2sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm2sqlite.pl
482 lines (451 loc) · 11.2 KB
/
osm2sqlite.pl
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/perl
use strict;
use XML::Parser;
use Data::Dumper;
use DBI;
use Date::Parse qw(str2time);
my $dbname=$ARGV[0] || "osm.sqlite";
unlink $dbname;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname"
,""
,""
,{ PrintError => 1, AutoCommit => 0 });
# We need just very few of these tokens actually as global values.
my ($node_id);
my ($nd_last_way_id);
my $nd_ordering;
my($way_id, $way_changeset, $way_visible, $way_userstring_id, $way_timestamp);
#my($node_id, $node_lat, $node_lon, $node_changeset, $node_visible, $node_userstring_id, $node_version, $node_uid, $node_timestamp);
my($relation_id, $relation_changeset, $relation_visible, $relation_userstring_id, $relation_timestamp);
my($member_type_id, $member_ref, $member_role, $member_relation_id, $member_ordering, $member_last_relation_id);
my($tag_k, $tag_v, $tag_parenttype_id, $tag_parent_id, $tag_ordering);
my($changeset_id, $changeset_closed_at, $changeset_max_lat, $changeset_uid, $changeset_max_lon, $changeset_open, $changeset_created_at, $changeset_min_lat, $changeset_min_lon, $changeset_userstring_id);
&init_db();
my %sql = (
add_node => qq[INSERT INTO node (id, lat, lon, changeset, visible, userstring_id, timestamp, uid, version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)],
add_way => qq[INSERT INTO way (id, changeset, visible, userstring_id, timestamp) VALUES (?, ?, ?, ?, ?)],
add_nd => qq[INSERT INTO nd (ref, way_id, ordering) VALUES (?, ?, ?)],
add_rel => qq[INSERT INTO relation (id, changeset, visible, userstring_id, timestamp) VALUES (?, ?, ?, ?, ?)],
add_rel_member => qq[INSERT INTO member (membertype, ref, role, relation_id, ordering) VALUES (?, ?, ?, ?, ?)],
add_tag => qq[INSERT INTO tag (k, v, tagparenttype_id, tagparent_id, ordering) VALUES (?, ?, ?, ?, ?)],
add_changeset => qq[INSERT INTO changeset (id, closed_at, max_lat, uid, max_lon, open, created_at, min_lat, min_lon, userstring_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)],
ustr_count => qq[SELECT count(id) as c, id FROM userstring WHERE name = ?],
ustr_insert => qq[INSERT INTO userstring (name) VALUES (?)],
);
my %sth;
$sth{$_} = $dbh->prepare($sql{$_}) for keys %sql;
my %count;
my @tagpath;
my $callcounter;
my $p = new XML::Parser(
Handlers=>
{
Start=> \&start_handler,
End=> \&end_handler,
Default=>\&default_handler,
Char => \&char_handler
},
ErrorContext => 2);
if (not -t STDIN) {
$p->parse(*STDIN);
} else {
$p->parsefile($ARGV[0]);
}
$dbh->commit();
print Dumper(\%count);
sub default_handler()
{
my ($p, $data) =@_;
my($line);
$line=$p->current_line;
#print "DEFAULT_HANDLER: ".$data."\n";
}
sub start_handler()
{
my ($p, $data, %attr_vals) =@_;
my($line, $key);
my($tag_last_parenttype_id, $tag_last_parent_id);
$callcounter++;
$line=$p->current_line;
#print "START: ".$data." "."\nattr-vals: ".join(" ",keys %attr_vals)."\n";
#print Dumper(\%attr_vals)."\n";
foreach $key (keys %attr_vals)
{
$count{'attr'}{$data}{$key}++;
}
# The XML tag path (without counters) as an array is updated
push @tagpath, $data;
if ($data eq "node")
{
# Preprocessing XML data
$node_id = $attr_vals{'id'};
my $node_lat = $attr_vals{'lat'};
my $node_lon = $attr_vals{'lon'};
my $node_changeset = $attr_vals{'changeset'};
my $node_visible;
if($attr_vals{'visible'} eq "true")
{
$node_visible=1;
}
elsif($attr_vals{'visible'} eq "false")
{
$node_visible=0;
}
else
{
## print("ERROR of OSM compliance: node visibility is not defined!\n");
$node_visible=2;
}
my $node_userstring_id = autouserid($attr_vals{'user'});
my $node_timestamp = str2time($attr_vals{'timestamp'});
# These attributes I found in planet.osm in 2010-02.
my $node_uid=$attr_vals{'uid'};
if($node_uid eq "")
{
$node_uid=0;
}
my $node_version=$attr_vals{'version'};
$sth{add_node}->execute(
$node_id,
$node_lat,
$node_lon,
$node_changeset,
$node_visible,
$node_userstring_id,
$node_timestamp,
$node_uid,
$node_version
) or print $dbh->err;
$tag_parent_id=$node_id;
}
elsif ($data eq "way")
{
$way_id=$attr_vals{'id'};
$way_changeset=$attr_vals{'changeset'};
if($attr_vals{'visible'} eq "true")
{
$way_visible=1;
}
elsif($attr_vals{'visible'} eq "false")
{
$way_visible=0;
}
else
{
$way_visible=2;
}
$way_userstring_id=&autouserid($attr_vals{'user'});
$way_timestamp=str2time($attr_vals{'timestamp'});
$sth{add_way}->execute(
$way_id,
$way_changeset,
$way_visible,
$way_userstring_id,
$way_timestamp
) or print $dbh->err;
$tag_parent_id=$way_id;
}
elsif($data eq "nd")
{
# Check: is this <nd> a child of a way?
if ($tagpath[$#tagpath-1] eq "way")
{
# Ok, let's go ahead reading in way node references.
my $nd_ref=$attr_vals{'ref'};
my $nd_way_id=$way_id;
if($nd_last_way_id == $way_id)
{
$nd_ordering++;
}
else
{
$nd_ordering=1;
}
$nd_last_way_id=$way_id;
$sth{add_nd}->execute(
$nd_ref,
$nd_way_id,
$nd_ordering
) or print $dbh->err;
}
else
{
print("ERROR: <nd> should be XML child of a way.\n");
}
}
elsif($data eq "relation")
{
$relation_id=$attr_vals{'id'};
if(defined($attr_vals{'changeset'}))
{
$relation_changeset=$attr_vals{'changeset'};
}
else
{
$relation_changeset=0;
}
if($attr_vals{'visible'} eq "true")
{
$relation_visible=1;
}
elsif($attr_vals{'visible'} eq "false")
{
$relation_visible=0;
}
else
{
$relation_visible=2;
}
$relation_userstring_id=&autouserid($attr_vals{'user'});
$relation_timestamp=str2time($attr_vals{'timestamp'});
$sth{add_rel}->execute(
$relation_id,
$relation_changeset,
$relation_visible,
$relation_userstring_id,
$relation_timestamp
) or print $dbh->err;
$tag_parent_id=$relation_id;
}
elsif($data eq "member")
{
# Check: is this <member> a child of a relation?
if ($tagpath[$#tagpath-1] eq "relation")
{
# Creating the SQL sub-select string
$member_type_id=$attr_vals{'type'};
$member_ref=$attr_vals{'ref'};
$member_role=$attr_vals{'role'};
$member_relation_id=$relation_id;
if($member_last_relation_id == $relation_id)
{
$member_ordering++;
}
else
{
$member_ordering=1;
}
$member_last_relation_id=$relation_id;
$sth{add_rel_member}->execute(
$member_type_id,
$member_ref,
$member_role,
$member_relation_id,
$member_ordering
) or print $dbh->err;
}
else
{
print("ERROR: <member> should be XML child of a relation.\n");
}
}
elsif($data eq "tag")
{
my ($parent);
$parent=$tagpath[$#tagpath-1];
$tag_k=$attr_vals{'k'};
$tag_v=$attr_vals{'v'};
$tag_parenttype_id=$parent;
#$tag_parent_id; This is the respective ID set in preferences changeset node way relation
if ($parent eq "preferences") # I could do this with eval(), but this should be faster
{
# $tag_parent_id=$preferences_id;
}
elsif($parent eq "changeset")
{
$tag_parent_id=$changeset_id;
}
elsif($parent eq "node")
{
$tag_parent_id=$node_id;
}
elsif($parent eq "way")
{
$tag_parent_id=$way_id;
}
elsif($parent eq "relation")
{
$tag_parent_id=$relation_id;
}
if($tag_last_parent_id == $tag_parent_id and $tag_last_parenttype_id=$tag_parenttype_id)
{
$tag_ordering++;
}
else
{
$tag_ordering=1;
}
$tag_last_parent_id=$tag_parent_id;
$tag_last_parenttype_id=$tag_parenttype_id;
$sth{add_tag}->execute(
$tag_k,
$tag_v,
$tag_parenttype_id,
$tag_parent_id,
$tag_ordering
) or print $dbh->err;
}
elsif($data eq "changeset")
{
$changeset_id=$attr_vals{'id'};
$changeset_closed_at=str2time($attr_vals{'closed_at'});
$changeset_max_lat=$attr_vals{'max_lat'};
$changeset_uid=$attr_vals{'uid'};
$changeset_max_lon=$attr_vals{'max_lon'};
if($attr_vals{'open'} eq "true")
{
$changeset_open=1;
}
else
{
$changeset_open=0;
}
$changeset_created_at=str2time($attr_vals{'created_at'});
$changeset_min_lat=$attr_vals{'min_lat'};
$changeset_min_lon=$attr_vals{'min_lon'};
$changeset_userstring_id=&autouserid($attr_vals{'user'});
$tag_parent_id=$changeset_id;
$sth{add_changeset}->execute(
$changeset_id,
$changeset_closed_at,
$changeset_max_lat,
$changeset_uid,
$changeset_max_lon,
$changeset_open,
$changeset_created_at,
$changeset_min_lat,
$changeset_min_lon,
$changeset_userstring_id
) or print $dbh->err;
}
if($#tagpath >=1)
{
$count{'isparent'}{$tagpath[$#tagpath-1]}{$tagpath[$#tagpath-0]}++
}
if($callcounter%100000 == 0)
{
open(RESULT, ">", "osm-tmp-result.txt") || die "$!";
print RESULT (Dumper(\%count));
close(RESULT);
#$dbh->commit();
}
}
sub end_handler()
{
my ($p, $data) =@_;
my($line);
$line=$p->current_line;
#print "END: ".$data."\n";
pop @tagpath;
}
sub char_handler()
{
my ($p, $data) =@_;
my($line);
#print "CHAR: ".$data."\n";
}
sub init_db()
{
# Schema for OSM API v0.6
my ($sth, $query, @updates);
@updates=
("CREATE TABLE IF NOT EXISTS node
(
id INTEGER PRIMARY KEY,
lat REAL NOT NULL,
lon REAL NOT NULL,
changeset TEXT,
visible BOOLEAN NOT NULL,
userstring_id INTEGER,
timestamp DATETIME,
uid INTEGER,
version INTEGER
);",
"CREATE TABLE IF NOT EXISTS way
(
id INTEGER PRIMARY KEY,
changeset TEXT,
visible BOOLEAN,
userstring_id INTEGER,
timestamp DATETIME
);",
"CREATE TABLE IF NOT EXISTS relation
(
id INTEGER PRIMARY KEY,
changeset TEXT,
visible BOOLEAN,
userstring_id INTEGER,
timestamp DATETIME
);",
"CREATE TABLE IF NOT EXISTS nd
(
ref INTEGER NOT NULL,
way_id INTEGER NOT NULL,
ordering INTEGER NOT NULL,
PRIMARY KEY(ref,way_id,ordering)
);",
"CREATE TABLE IF NOT EXISTS member
(
membertype TEXT NOT NULL,
ref INTEGER NOT NULL,
role TEXT,
relation_id INTEGER NOT NULL,
ordering INTEGER NOT NULL,
PRIMARY KEY(relation_id, membertype, ref, role, ordering)
);",
"CREATE TABLE IF NOT EXISTS tag
(
k TEXT NOT NULL,
v TEXT NOT NULL,
tagparenttype_id INTEGER NOT NULL,
tagparent_id INTEGER NOT NULL,
ordering INTEGER NOT NULL,
PRIMARY KEY(k, v, tagparenttype_id, tagparent_id, ordering)
);",
"CREATE TABLE IF NOT EXISTS changeset
(
id INTEGER PRIMARY KEY,
closed_at DATETIME,
max_lat REAL,
uid INTEGER,
max_lon REAL,
open BOOLEAN,
created_at DATETIME,
min_lat REAL,
min_lon REAL,
userstring_id INTEGER
);",
"CREATE TABLE IF NOT EXISTS userstring
(
id INTEGER PRIMARY KEY,
name TEXT
);"
)
;
foreach $query (@updates)
{
$sth=$dbh->do($query);
}
$dbh->commit();
}
sub autouserid() # Creates a new user ID if necessary; returns in any case the user ID
{
my $userstring=shift;
my ($query, $sth, $hashref, $uid);
$sth{ustr_count}->execute(
$userstring,
);
$hashref=$sth{ustr_count}->fetchrow_hashref;
if($$hashref{'c'} == 0)
{
$sth{ustr_insert}->execute(
$userstring,
);
$dbh->commit;
$sth{ustr_count}->execute(
$userstring,
);
$hashref = $sth{ustr_count}->fetchrow_hashref;
}
return $$hashref{'id'};
}