~kosova/+junk/openstreetmapkosova

« back to all changes in this revision

Viewing changes to prepareupload.pl

  • Committer: Andreas Horst
  • Date: 2010-12-11 09:07:30 UTC
  • Revision ID: andreas@goetherad-20101211090730-p7ze29zmgd93bhv9
updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
use Data::Dumper;
4
4
use Carp;
5
5
 
 
6
=head1
 
7
    1. empty ways
 
8
    2. double points in way
 
9
    3. 
 
10
=cut
 
11
 
6
12
my $tolerance=20;
7
13
my %nodes;
8
14
my %replace; # replace these ids with these
13
19
my $newids = -3000000; # we give new objects ids starting here
14
20
my $current_way;
15
21
my $debug=1;
16
 
 
17
 
#my $current_parent_way; # when we split the way, keep track of where they came from
18
 
 
19
 
# reprocessing 
20
 
# we need to store all the ways after the first pass because we dont want to have to cut a way twice.
21
 
22
 
 
23
 
sub emitstring
24
 
{
25
 
    my $s=shift;
26
 
    # get the string
27
 
    # print it 
28
 
}
29
 
 
30
 
 
31
 
my $skip=0;
 
22
my %ways;
 
23
my %waystring;
 
24
# just store the last node seen an used that.
 
25
my $last_node_seen=0;
 
26
my %rels; # the relationships
 
27
#mapping of old ways onto new
 
28
my %waymapping;
 
29
 
 
30
##### we look for two in a row.. cannot have that
 
31
my %seenfilter;
 
32
my $QUOTE="[\\'\\\"]";
 
33
my %seen; # what have we emitted
32
34
 
33
35
sub begin_way
34
36
{
35
37
    my $id=shift;
36
38
    $current_way = $id;
37
 
    $skip=0;
 
39
    warn "setting current way:$current_way" if $debug;
 
40
    $last_node_seen=0;
38
41
}
39
42
 
40
43
sub end_way
41
44
{
 
45
    warn "closing current way:$current_way" if $debug;
 
46
    warn "Way $current_way contains" . join (",",@{$ways{$current_way}->{nodes}}) . "\n" if $debug;
 
47
 
42
48
    $current_way=undef;
 
49
    $last_node_seen=0;
43
50
}
44
51
 
45
52
sub checksum
50
57
    return int($checksum);
51
58
}
52
59
 
53
 
sub postprocess_node_in_way
54
 
{
55
 
    my $id=shift;
56
 
#    my $coords=$nodeids{$id};
57
 
#    warn Dumper($coords);
58
 
#    die "no coords for $id" unless $coords;
59
 
 
60
 
 
61
 
#    my ($lat,$lon) =@{$coords};
62
 
#    my $checksum = checksum($lat,$lon);
63
 
#    $current_way->{checksum} += $checksum;
64
 
#    $current_way->{checksum} += 
65
 
}
66
 
 
67
 
 
68
 
my %ways;
69
 
 
70
 
# # the way.
71
 
# sub split_way
72
 
# {
73
 
#     my $id=shift; # the splitting node
74
 
 
75
 
#     
76
 
#     $current_parent_way=$current_way;
77
 
#     $current_way = $newids;
78
 
 
79
 
#     #
80
 
#     push (@{$ways{$current_way}->{nodes}},$id);# add to way
81
 
 
82
 
#     $ways{$current_way}{parent}=$current_parent_way; # store the parent
83
 
#     push @{$ways{$current_parent_way}{children}},$current_way; # push the children
84
 
#     # we need to apply all relationships to the children as well. and tags if needed.
85
 
# }
86
 
 
87
 
 
88
 
# sub re_process_waynd
89
 
# {
90
 
#     my $id=shift;
91
 
 
92
 
#     ## now we look if we have to cut this way into new bits
93
 
#     if ($nodesways{$id}) # we have seen this node before
94
 
#     {
95
 
#       # we need to split the way here.
96
 
#       split_way($current_way);  # create a new way now starting at this point.       
97
 
#     }
98
 
 
99
 
#     # do we have to add the connection
100
 
#     push (@{$ways{$current_way}->{nodes}},$id);# add to way
101
 
 
102
 
#     # store the current way in the array, first one is 
103
 
#     push @{$nodesways{$id}},$current_way;
104
 
    
105
 
# }
106
 
 
107
 
my %waystring;
108
 
 
109
 
 
110
60
sub post_process_way
111
61
{
112
62
    my $rel  =shift;
113
 
    my $wayid =shift || carp "No way";
114
 
 
115
 
 
 
63
    my $wayid =shift || die carp "No way";
116
64
    if ($ways{$wayid}->{relationship})
117
65
    {
118
66
        die "way $wayid already in rel";
123
71
    }
124
72
}
125
73
 
126
 
 
127
 
 
128
 
##### we look for two in a row.. cannot have that
129
 
my %seenfilter;
130
74
sub checkpair
131
75
{
132
76
    my $first=shift;
133
77
    my $second=shift;
134
 
 
135
78
    if ($first > $second)
136
79
    {
137
80
        my $t=$second;
140
83
    }
141
84
    if ($seenfilter{"${first}|${second}"}++)
142
85
    {
 
86
        warn "seen pair: first:${first} second:${second} skipping" if $debug;
143
87
        # we have seen pair in reverse, bail
144
88
        #       return;
145
89
        return 0;
146
90
    }
147
91
    return 1;
148
92
}
149
 
sub checkfordups
150
 
{
151
 
    my @way=@_;
152
 
    my $last;
153
 
    my $fail=0;
154
 
    my $count=0;
155
 
    my $size=$#way + 1;
156
 
 
157
 
    ### now check 
158
 
    foreach my $nd (@way)
159
 
    {
160
 
        if ($last)
161
 
        {
162
 
            if (!checkpair($nd,$last))
163
 
            {
164
 
                # we have seen pair in reverse, bail
165
 
                #       return;
166
 
                $fail=1;
167
 
            }           
168
 
        }
169
 
        $last=$nd;      
170
 
        $count++;
171
 
    }
172
 
    return $fail;
173
 
}
174
 
 
175
 
#mapping of old ways onto new
176
 
my %waymapping;
177
93
 
178
94
=head2
179
95
    finish_way (way_id, [@runlist])
180
 
 
181
96
    this is called when we have a finished set of nodes in a run (contigious) along a way
182
 
    that all have the same set of attributes, they dont need any more cutting.
183
 
    
 
97
    that all have the same set of attributes, they dont need any more cutting.    
184
98
    returns the newid, so we can append the trailing bits
185
 
 
186
99
=cut
187
 
 
188
 
 
189
100
sub finish_way
190
101
{
191
102
    my $wayid=shift;
193
104
    my $tag =shift;
194
105
    #remove duplicates from the ways 
195
106
    my @newlist = @{$runlist};
196
 
 
 
107
    $last_node_seen=0; #reset
197
108
    my $runstring= join (",",sort {$a <=> $b} @newlist);
198
 
#    print "Last Run was connected to " . join (",",@last) . "\n";
 
109
    warn "Run for $wayid with tag $tag contains " . join (",",@newlist) . "\n" if $debug;
199
110
    # run string
200
111
    if ( $#newlist == 0)
201
112
    {
202
113
#       warn "only 1 object";
203
114
        return;
204
115
    }
205
 
 
206
116
    if ($waystring{$runstring})
207
117
    {
208
118
        # we have done this one.
214
124
        $newids--; # allocate a new id.
215
125
        push @{$waymapping{$wayid}},$newids; # map the old id onto the new
216
126
        $waystring{$runstring}=$newids; # give this new way an id
217
 
        warn "Run Finished (" . $runstring . ")\n" if $debug;
218
 
 
 
127
        warn "Run in way $wayid Finished (" . $runstring . ")\n" if $debug;
219
128
        # now put the nodes on the new way....
220
129
        push @{$ways{$newids}->{nodes}},@newlist;
221
130
    }
222
 
 
223
131
    return $newids;
224
132
}
225
133
 
232
140
    my @last;
233
141
    my @run; # a run of nodes in the same context
234
142
    my $tag="";
235
 
 
236
143
    my $segment=0;
237
 
 
238
144
    my $lastnode=0;
239
 
 
240
145
    foreach my $nd (@{$ways{$wayid}->{nodes}})
241
146
    {
242
147
        my @others = ();
243
 
 
244
148
        if (!$nd)
245
149
        {
246
150
            warn "no node";
250
154
            @others = @{$nodesways{$nd}}; # get a list of ways connecting to the node
251
155
            push @run,$nd;
252
156
        }
253
 
 
254
157
        if (!@last) # we are at the first in the list
255
158
        {
256
159
            @last=@others; # the first one
257
160
            $tag="first";
258
161
        }
259
 
 
260
 
 
261
162
        if (@last != @others) # skip over the first one
262
163
        {
263
 
 
264
164
#### EMIT THE LAST RUN
265
165
##TODO ----------
266
166
            finish_way ($wayid,\@run,$tag); # finish the run list
268
168
            $tag = "segment $segment";
269
169
#######################################
270
170
            ### new run starting
 
171
            warn "node $nd is connected to ways: " . join (",",@others) . "\n" if $debug;
271
172
            warn "Last Run:connected to " . join (",",@last) . "\n" if $debug;
272
173
            warn "Starting new run\n" if $debug;
273
174
            # get the last from the old run
274
175
            my $last = pop @run; #
275
176
            @run=();
 
177
            warn "adding into way:$wayid node:$last\n" if $debug;
276
178
            push @run,$last;
277
179
## reporting
278
180
            if ($#others > 0)
298
200
        else
299
201
        {
300
202
#           same as before, add to the run
 
203
            warn "duplicate in way:$wayid  node:$nd\n" if $debug;
301
204
        }
302
 
 
303
205
        ###
304
206
#       print "NODE $nd is connected to " . join (",",@others) . "\n";  
305
207
        @last=@others;
306
208
    }
307
 
 
308
 
 
309
209
    #########################
310
210
    warn "Finish up run\n" if $debug;
311
211
    finish_way ($wayid,\@run, "last");
312
 
   
 
212
    
313
213
    warn "way done $wayid\n" if $debug;
314
 
 
315
214
#####################################################################
316
215
    # emit the last element in the loop
317
 
    ############################################################
318
 
    
 
216
    ############################################################    
 
217
}
 
218
 
 
219
sub way_in_node
 
220
{
 
221
    my $way=shift;
 
222
    my $id=shift;
 
223
    my $isok=1;
 
224
    foreach my $chk (@{$nodesways{$id}})
 
225
    {
 
226
        if ($chk eq $way)
 
227
        {
 
228
            return 1;
 
229
        }
 
230
    }
 
231
    return 0;
319
232
}
320
233
 
321
234
sub process_waynd
322
235
{
323
 
#    $debug=1;
324
236
    my $id=shift;
 
237
    warn "process_waynd $id in way $current_way\n" if $debug;
325
238
    if ($replace{$id})
326
239
    {
327
240
        my $new=$replace{$id};
 
241
        warn "adding replacing $id with $new in way $current_way\n" if $debug;
328
242
        $id=$new;       
329
243
    }
330
 
 
331
244
    # dont add duplicates in array
332
245
    # look if the nodesways(what ways are in this node)
333
 
    if (!(grep {/$current_way/} @{$nodesways{$id}}))
 
246
    if (!way_in_node($current_way,$id))
334
247
    {
335
248
        # what whays is this node in
 
249
        warn "adding node:$id to way:$current_way\n" if $debug;
336
250
        push @{$nodesways{$id}},$current_way; # store the way in the node
337
251
    }    
338
 
 
339
252
    my $count = $#{$ways{$current_way}->{nodes}};
340
 
 
341
 
    if ($count < 1)
 
253
    if ($count < 0)
342
254
    {
343
 
        warn "Got count $count";
 
255
        warn "Got count $count of nodes in $current_way\n" if $debug;
344
256
    }
345
 
 
346
257
    # done add duplicates to end of way
347
258
    if ($ways{$current_way}) # look up the current way
348
259
    {
349
 
 
350
 
        my $lastinway=$skip || $ways{$current_way}->{nodes}[-1] || 0;
351
 
 
 
260
#       my $lastinway=$last_node_seen;
 
261
        #my $lastinway=$ways{$current_way}->{nodes}[-1] || 0;
 
262
        # the first is missing
 
263
        my $lastitem = $ways{$current_way}->{nodes}[-1]; # the last item in the way
 
264
        my $lastinway=$lastitem;
 
265
        if ($lastitem)
 
266
        {
 
267
            if ($lastinway)
 
268
            {
 
269
                if ($lastitem != $lastinway)
 
270
                {
 
271
                    warn "in way $current_way inconsistent data $lastitem != $lastinway and count $count \n" if $debug;
 
272
                }       
 
273
            }
 
274
            $lastinway =$lastitem;
 
275
        }
 
276
                
 
277
        warn "last in way node :$lastinway in way:$current_way\n" if $debug;
352
278
        if ($lastinway ne $id) # not the last in the way
353
279
        {
354
280
            my $other=0;
355
 
 
356
 
            if (checkpair($lastinway, $id)) # remove all duplicate ways
 
281
          #  if (checkpair($lastinway, $id)) # remove all duplicate ways
357
282
            {
358
 
                # the first is missing
359
 
                if ($count <= 0) 
360
 
                {
361
 
                    if ($lastinway)
362
 
                    {
363
 
                        warn "adding first $lastinway";
364
 
                        push (@{$ways{$current_way}->{nodes}},$lastinway);# store the first
365
 
                    }
366
 
                }
367
 
 
368
 
                warn "adding pair $lastinway, $id\n" if $debug;
369
 
                my $lastitem = $ways{$current_way}->{nodes}[-1];
370
 
                carp "inconsistent data $lastitem and count $count " unless $ways{$current_way}->{nodes}[-1]==$lastinway;
 
283
                warn "in way $current_way adding pair lastinway :$lastinway | lastitem:$lastitem, node:$id\n" if $debug;
 
284
                
371
285
                push (@{$ways{$current_way}->{nodes}},$id);# store the node     
372
 
                $skip=0;
373
 
            }
374
 
            else
375
 
            {
376
 
                warn "going to skip $id\n" if $debug;
377
 
                # remove this way.
378
 
                $skip=$id; # save this for usage.
379
 
                return; # break 
380
 
            }
 
286
 
 
287
 
 
288
                # only store the last node seen if it is not a duplicate
 
289
                $last_node_seen=$id;
 
290
 
 
291
            }# if check pair
 
292
            # else
 
293
            # {
 
294
            #   # now we use this for checking duplicates.
 
295
            #   #$last_node_seen=$id;
 
296
 
 
297
            #   warn "skipping this pair" if $debug;
 
298
            # }
 
299
=head2
 
300
=cut
381
301
        }
382
302
        else
383
303
        {
386
306
    }
387
307
    else
388
308
    {
 
309
        warn "if ways: $current_way" if $debug;
389
310
        #start a new way
390
311
        #push (@{$ways{$current_way}->{nodes}},$id);# store the node     
391
 
        $skip=$id; # add this id to the skip list, so we can add it.
392
312
#       warn "null :$current_way " . Dumper($ways{$current_way});
393
313
    }
394
314
 
395
315
#    $debug=0;
396
316
}
397
317
 
398
 
#my %checksum;
399
318
sub node
400
319
{
401
320
    my $id=shift;
403
322
    my $lon=shift;
404
323
    my $slat=sprintf("%0.${tolerance}f",$lat);
405
324
    my $slon=sprintf("%0.${tolerance}f",$lon);
406
 
 
407
325
    if ($nodes{$slat}{$slon})
408
326
    {
409
327
        my $old=$nodes{$slat}{$slon};   
414
332
    {
415
333
        $nodes{$slat}{$slon}=$id;
416
334
        $nodeids{$id}=[$lat,$lon]; # store the nodes values
417
 
 
418
335
        return $id;
419
336
    }
420
337
}
421
338
 
422
 
my $QUOTE="[\\'\\\"]";
423
339
sub consumeattrs
424
340
{
425
341
    if (s/timestamp=${QUOTE}[\d\-T:Z]+${QUOTE}//)
433
349
    }
434
350
    if (s/uid=${QUOTE}\d+${QUOTE}\s?//)
435
351
    {}
436
 
 
437
352
    if (s/user=${QUOTE}[\w\s]+${QUOTE}\s*//)
438
353
    {
439
354
    }
453
368
        #next; # skip this
454
369
        return 0;
455
370
    }
456
 
 
457
371
    if (s/version=${QUOTE}\d+${QUOTE}\s*//)
458
372
    {
459
373
        #remove version
460
374
    }
461
 
 
462
375
    return 1;
463
 
 
464
376
#    warn "done $_";
465
377
}
466
378
 
470
382
    open IN, $file or die;
471
383
    my $coordpattern = "-?[\\d\\.\\-Ee]+";
472
384
    my $QUOTE="[\\'\\\"]";
473
 
 
474
385
    my $lat=0;
475
386
    my $lon=0;
476
387
    my $current_rel=0;
479
390
        if (/<\?xml version=${QUOTE}1.0${QUOTE}/)  #encoding=${QUOTE}UTF-8${QUOTE}\?
480
391
        {
481
392
        }
482
 
        elsif (/<osm version=${QUOTE}0.\d${QUOTE} /) #generator=${QUOTE}[\w\s]+${QUOTE}>
483
 
        {
484
 
    }
485
 
    elsif (/<node/)
486
 
    {
487
 
        next unless consumeattrs;
488
 
        if (s/lon=${QUOTE}($coordpattern)${QUOTE} //)
489
 
        {
490
 
            $lon=$1;
 
393
        elsif (/<osm version=${QUOTE}0.\d${QUOTE}/) #generator=${QUOTE}[\w\s]+${QUOTE}>
 
394
        {
 
395
        }
 
396
        elsif (/<node/)
 
397
        {
 
398
            next unless consumeattrs;
 
399
            if (s/lon=${QUOTE}($coordpattern)${QUOTE} //)
 
400
            {
 
401
                $lon=$1;
491
402
#           warn "LON $1";
492
 
        }
493
 
        else
494
 
        {
495
 
            die "no lon $coordpattern $_";
496
 
        }
497
 
        
498
 
        if (s/lat=${QUOTE}($coordpattern)${QUOTE} //)
499
 
        {
500
 
            $lat=$1;
 
403
            }
 
404
            else
 
405
            {
 
406
                die "no lon $coordpattern $_";
 
407
            }
 
408
            
 
409
            if (s/lat=${QUOTE}($coordpattern)${QUOTE} //)
 
410
            {
 
411
                $lat=$1;
501
412
#           warn "LAT $1";
502
 
        }
503
 
        else
504
 
        {
505
 
            die "no lat $_";
506
 
        }
507
 
        
508
 
        if (/<node id=${QUOTE}(-?\d+)${QUOTE}\s*\/?>/)
509
 
        {
510
 
            node($1,$lat,$lon);
511
 
        }
512
 
        else
513
 
        {           
514
 
            die "Missing 2 $_";
515
 
        }
516
 
        
517
 
    }
518
 
    elsif (/<\/node/)
519
 
    {
520
 
        #end of node
521
 
    }
522
 
    elsif (/\s*<way/){
523
 
 
524
 
 
 
413
            }
 
414
            else
 
415
            {
 
416
                die "no lat $_";
 
417
            }
 
418
            
 
419
            if (/<node id=${QUOTE}(-?\d+)${QUOTE}\s*\/?>/)
 
420
            {
 
421
                node($1,$lat,$lon);
 
422
            }
 
423
            else
 
424
            {       
 
425
                die "Missing 2 $_";
 
426
            }
 
427
            
 
428
        }
 
429
        elsif (/<\/node/)
 
430
        {
 
431
            #end of node
 
432
        }
 
433
        elsif (/\s*<way/){
525
434
#       consumeattrs;
526
 
        next unless consumeattrs;
527
 
 
528
 
        if (/\s*<way id=${QUOTE}(\-?\d+)${QUOTE}\s*>/)
529
 
        {
530
 
            begin_way $1;           
531
 
        }
532
 
        else
533
 
        {
534
 
            die "missing way $_";
535
 
        }
536
 
 
537
 
    }
538
 
    elsif(/<nd ref=${QUOTE}(-?\d+)${QUOTE}\s*\/>/)
539
 
    {
540
 
        process_waynd($1);
541
 
    }
542
 
    elsif (/<\/way>/){
543
 
        # end of way
544
 
        end_way;
545
 
    }
 
435
            next unless consumeattrs;
 
436
            if (/\s*<way id=${QUOTE}(\-?\d+)${QUOTE}\s*>/)
 
437
            {
 
438
                begin_way $1;       
 
439
            }
 
440
            else
 
441
            {
 
442
                die "missing way $_";
 
443
            }
 
444
        }
 
445
        elsif(/<nd ref=${QUOTE}(-?\d+)${QUOTE}\s*\/>/)
 
446
        {
 
447
            process_waynd($1);
 
448
        }
 
449
        elsif (/<\/way>/){
 
450
            # end of way
 
451
            end_way;
 
452
        }
546
453
#    <way id=${QUOTE}-572620${QUOTE} action=${QUOTE}modify${QUOTE} timestamp=${QUOTE}2010-12-05T01:31:40Z${QUOTE} visible=${QUOTE}true${QUOTE}>
547
 
    elsif (/<nd ref=${QUOTE}(-_\d)${QUOTE} \/>/){}
 
454
        elsif (/<nd ref=${QUOTE}(-_\d)${QUOTE} \/>/){}
548
455
        elsif (/<relation/){
549
456
            
550
457
#       consumeattrs;
551
458
            next unless consumeattrs;
552
459
            
553
 
        if (/<relation id=${QUOTE}(\-?\d+)${QUOTE}\s*/)
554
 
        {
555
 
            $current_rel=$1;
556
 
        }
557
 
        else
558
 
        {
559
 
            die "Bad Relation $_";
560
 
        }
561
 
 
 
460
            if (/<relation id=${QUOTE}(\-?\d+)${QUOTE}\s*/)
 
461
            {
 
462
                $current_rel=$1;
 
463
            }
 
464
            else
 
465
            {
 
466
                die "Bad Relation $_";
 
467
            }
562
468
            # all on one line?
563
469
            while (s/<member type=${QUOTE}way${QUOTE} ref=${QUOTE}(-?\d+)${QUOTE} role=${QUOTE}outer${QUOTE}\s?\/>//){
564
470
                
568
474
#               warn "member $_";
569
475
                post_process_way($current_rel,$1); 
570
476
            }
571
 
 
572
 
    }
 
477
        }
573
478
        
574
479
        elsif (/<\/relation>/){
575
480
            $current_rel=0;
576
481
        }
577
482
        elsif (/<member type=${QUOTE}way${QUOTE} ref=${QUOTE}(-?\d+)${QUOTE} role=${QUOTE}outer${QUOTE}\s?\/>/){
578
 
 
579
 
        # now we want to post process this way
580
 
        # cut the way on all the intersections with all other relations
581
 
        # remove other ways that are duplicate, match it 100%
 
483
            # now we want to post process this way
 
484
            # cut the way on all the intersections with all other relations
 
485
            # remove other ways that are duplicate, match it 100%
582
486
#       warn "member $_";
583
 
        post_process_way($current_rel,$1); 
584
 
    }
585
 
    elsif (/<member type=${QUOTE}node${QUOTE} ref=${QUOTE}(-?\d+)${QUOTE} role=${QUOTE}admin_centre${QUOTE}\s?\/>/){}
586
 
    elsif (/<tag k=${QUOTE}(.+)${QUOTE} v=${QUOTE}(.+)${QUOTE}\s*\/>/)
587
 
    {
588
 
        while (s/<tag k=${QUOTE}([^\/\${QUOTE}]+)${QUOTE} v=${QUOTE}([^\/\${QUOTE}]+)${QUOTE}\s*\/>//)
 
487
            post_process_way($current_rel,$1); 
 
488
        }
 
489
        elsif (/<member type=${QUOTE}node${QUOTE} ref=${QUOTE}(-?\d+)${QUOTE} role=${QUOTE}admin_centre${QUOTE}\s?\/>/){}
 
490
        elsif (/<tag k=${QUOTE}(.+)${QUOTE} v=${QUOTE}(.+)${QUOTE}\s*\/>/)
589
491
        {
 
492
            while (s/<tag k=${QUOTE}([^\/\${QUOTE}]+)${QUOTE} v=${QUOTE}([^\/\${QUOTE}]+)${QUOTE}\s*\/>//)
 
493
            {
590
494
#           warn "$2";
591
 
            if ($current_rel)
592
 
            {
593
 
                $tags{$current_rel}{$1}=$2;
594
 
            }
595
 
            if ($current_way)
596
 
            {
597
 
                $tags{$current_way}{$1}=$2;
598
 
            }
599
 
        }
600
 
    }
601
 
    elsif (/<\/osm>/)
602
 
    {
603
 
    }
604
 
    elsif (/^\s+$/)
605
 
    {
606
 
    }
607
 
    else
608
 
    {
609
 
        die "Missing anything $_";
610
 
    }
611
 
}
612
 
close IN;
613
 
}
614
 
 
615
 
my %rels; # the relationships
 
495
                if ($current_rel)
 
496
                {
 
497
                    $tags{$current_rel}{$1}=$2;
 
498
                }
 
499
                if ($current_way)
 
500
                {
 
501
                    $tags{$current_way}{$1}=$2;
 
502
                }
 
503
            }
 
504
        }
 
505
        elsif (/<\/osm>/)
 
506
        {
 
507
        }
 
508
        elsif (/^\s+$/)
 
509
        {
 
510
        }
 
511
        else
 
512
        {
 
513
            die "Missing anything $_";
 
514
        }
 
515
    }
 
516
    close IN;
 
517
}
 
518
 
 
519
=head2
 
520
    duplicate way algorithm
 
521
 
 
522
    1. each node has a pointer to a set of arc, the next node.
 
523
    2. if the next node points back to this node, it is duplicate, we remove it.
 
524
=cut
616
525
 
617
526
sub post_process_ways
618
527
{
619
528
    foreach my $wayid (sort keys %ways)
620
529
    {
621
530
        my $rel =$ways{$wayid}->{relationship};
622
 
        post_process_way_end $rel, $wayid;
 
531
        #post_process_way_end $rel, $wayid; # for each way, lets process it.
 
532
        push @{$waymapping{$wayid}},$wayid; # map onto self for now
623
533
    }
624
534
}
625
535
 
626
536
 
 
537
##################### MAIN ROUTINE TO CLEAN
 
538
 
627
539
foreach my $file (@ARGV)
628
540
{
629
541
    parse $file;
630
542
}
631
 
 
632
543
post_process_ways;
633
 
 
634
544
foreach my $wayid (sort keys %ways)
635
545
{   
636
546
    my $rel =$ways{$wayid}->{relationship};
647
557
}       
648
558
#warn Dumper(\%waymapping); # dump out the ways 
649
559
# now we can emit the relationships with the new ways instead of the old ones.
650
 
 
651
 
my %seen; # what have we emitted
652
560
#####################################################
653
561
### now output the doc 
654
562
print "<osm version='0.6'>\n";
655
 
 
656
563
foreach my $rel (keys %{rels})
657
564
{
658
565
#    warn "emit $rel\n";
659
566
#    warn Dumper($tags{$rel});
660
 
 #   warn Dumper($rels{$rel});
 
567
    #   warn Dumper($rels{$rel});
661
568
    my @ways;
662
569
    foreach my $oldway ( @{$rels{$rel}})
663
570
    {
664
571
        warn "found old way $oldway" if $debug;
665
 
 
 
572
=head2
 
573
    here we map all old ways onto new ways.
 
574
    the old ways are not used, only the new ones, recreated from the old
 
575
    the waymapping hash manages that
 
576
=cut
666
577
        foreach my $newway (@{$waymapping{$oldway}}     )
667
578
        {
668
579
            # add the new ways
669
580
            push @ways,$newway;
670
 
 
671
581
            if (!$seen{$newway}++) 
672
582
            {
673
 
                warn "found new way $newway" if $debug;
 
583
                warn "found new way $newway from old way $oldway" if $debug;
674
584
#               if (!$fail)
675
585
                {
676
 
 
677
 
                foreach my $nd (@{$ways{$newway}->{nodes}})
678
 
                {
679
 
                    # have we emitted the node yet?
680
 
                    print "$nd in $newway\n" if $debug;
681
 
                    
682
 
                    if (!$seen{$nd}++) 
683
 
                    {               
684
 
#                       emitnode($nd, @{$nodeids{$nd}});
685
 
                        my ($lat,$lon)=@{$nodeids{$nd}};
686
 
                        print "<node id=\'$nd\' lat='$lat' lon='$lon'>\n";
687
 
                        print "<tag k='_ID' v='$nd' />\n";
688
 
                        print "</node>\n";
689
 
                    }
690
 
                    
691
 
                }
692
 
                # emit new way------------------------ 
693
 
                print "<way id='$newway'>\n";
694
 
                foreach my $nd (@{$ways{$newway}->{nodes}})
695
 
                {
696
 
                    print "<nd ref='$nd'/>\t";
697
 
                }
698
 
                print "<tag k='is_in:country' v='Colombia'/>\n";
699
 
                print "<tag k='_ID' v='$newway' />\n";
700
 
 
701
 
                print "</way>\n";
702
 
                }
703
 
 
 
586
                    foreach my $nd (@{$ways{$newway}->{nodes}})
 
587
                    {
 
588
                        # have we emitted the node yet?
 
589
                        warn "$nd in $newway\n" if $debug;
 
590
                        
 
591
                        if (!$seen{$nd}++) 
 
592
                        {                   
 
593
                            my ($lat,$lon)=@{$nodeids{$nd}};
 
594
                            print "<node id=\'$nd\' lat='$lat' lon='$lon'>\n";
 
595
                            print "<tag k='_ID' v='$nd' />\n";
 
596
                            print "</node>\n";
 
597
                        }
 
598
                        
 
599
                    }
 
600
                    # emit new way------------------------ 
 
601
                    print "<way id='$newway'>\n";
 
602
                    foreach my $nd (@{$ways{$newway}->{nodes}})
 
603
                    {
 
604
                        print "<nd ref='$nd'/>\t";
 
605
                    }
 
606
                    print "<tag k='is_in:country' v='Colombia'/>\n";
 
607
                    print "<tag k='_ID' v='$newway' />\n";
 
608
                    print "</way>\n";
 
609
                }
704
610
            }
705
611
            else
706
612
            {
708
614
            }
709
615
        }## after all new ways
710
616
        
711
 
 
712
617
    }# after old ways
713
 
 
714
 
 
715
618
    # relationships
716
619
    if (!$seen{$rel}++) 
717
620
    {
728
631
            my $v=$tags{$rel}{$k};
729
632
            print "<tag k='$k' v='$v' />\n";
730
633
        }
731
 
 
732
634
        print "<tag k='_ID' v='$rel' />\n";
733
635
        print "</relation>\n";
734
636
    }
735
637
    # now emit the relationship
736
638
    #warn Dumper($tags{$rel});
737
 
 
738
639
}# end of relationship
739
640
print "</osm>\n";