-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-coastlines.pl
executable file
·400 lines (365 loc) · 10.5 KB
/
merge-coastlines.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
#!/usr/bin/perl -w
use strict;
use warnings;
use Tree::R;
use Bit::Vector;
use Pod::Usage;
use constant EPSILON => 0.001;
use constant TRACE => 0;
# Grab the filename
my $xml = shift||'';
pod2usage(1) unless $xml;
# Check we can load the file
if($xml eq "-") {
die("Sorry, reading from stdin is not supported, as we have to make several passes\n");
}
unless( -f $xml) {
die("Osm file '$xml' could not be found\n");
}
unless( -s $xml ) {
die " $xml has 0 size\n";
}
# Sub to open xml
sub openXML {
if( $xml =~ /\.bz2$/ )
{
open(XML, "bzcat $xml |") or die($!);
}
elsif( $xml =~ /\.gz$/ )
{
open(XML, "zcat $xml |") or die($!);
}
else
{
open(XML, "<$xml") or die("$!");
}
#open(XML, "<:utf8","$xml") or die("$!");
}
# Sub to close xml
sub closeXML {
close XML;
}
sub processXML {
my ($nodeH, $wayH, $relH) = @_;
openXML();
# $pass++;
# Process the file, giving tags to the helpers that like them
# Hold the main line, tags and segs of the tag
my $main_line;
my $main_type;
my $wanted;
my @tags;
my @nodes;
my @rel_ways;
my @rel_nodes;
my $startNewTag = sub{
$wanted = 0;
@tags = ();
@nodes = ();
@rel_ways = ();
@rel_nodes = ();
};
while(my $line = <XML>) {
if($line =~ /^\s*<node/) {
$main_line = $line;
$main_type = "node";
&$startNewTag();
unless($line =~ /\/>\s*$/) { next; }
}
elsif($line =~ /^\s*\<way/) {
$main_line = $line;
$main_type = "way";
&$startNewTag();
unless($line =~ /\/>\s*$/) { next; }
}
elsif($line =~ /^\s*<relation/) {
$main_line = $line;
$main_type = "relation";
&$startNewTag();
unless($line =~ /\/>\s*$/) { next; }
}
if($line =~ /^\s*\<tag/) {
my ($name,$value) = ($line =~ /^\s*\<tag k=[\'\"](.*?)[\'\"] v=[\'\"](.*?)[\'\"]/);
unless($name) {
unless($line =~ /k="\s*" v="\s*"/) {
warn "Invalid line '$line'";
}
next;
}
my @tag = ($name,$value);
push @tags, \@tag;
}
elsif($line =~ /^\s*\<nd /) {
my ($ref) = ($line =~ /^\s*\<nd ref=[\'\"](\d+)[\'\"]/);
unless($main_type eq "way") { warn "Got nd when in $main_type\n"; next; }
unless($ref) { warn "Invalid line '$line'"; next; }
push @nodes, $ref;
}
elsif($line =~ /^\s*\<member /) {
my ($type,$ref,$role) = ($line =~ /^\s*\<member type=[\'\"](.*?)[\'\"] ref=[\'\"](\d+)[\'\"] role=[\'\"](.*)[\'\"]/);
unless($main_type eq "relation") { warn "Got member when in $main_type\n"; next; }
unless($type && $ref) { warn "Invalid line '$line'"; next; }
my %m;
$m{'type'} = $type;
$m{'ref'} = $ref;
$m{'role'} = $role;
if($type eq "node") {
push @rel_nodes, \%m;
} elsif($type eq "way") {
push @rel_ways, \%m;
} else {
warn("Got unknown member type '$type' in '$line'"); next;
}
}
# Do the decisions when closing tags - can be self closing
elsif($line =~ /^\s*<\/?node/) {
my ($id,$lat,$long) = ($main_line =~ /^\s*<node id=['"](\d+)['"].* lat=['"]?(\-?[\d\.]+)['"]? lon=['"]?(\-?[\d\.]+e?\-?\d*)['"]?/);
unless($id) { warn "Invalid node line '$main_line'"; next; }
unless($main_type eq "node") { warn "$main_type ended with $line"; next; }
if($nodeH) {
&$nodeH($id,$lat,$long,\@tags,$main_line,$line);
}
}
elsif($line =~ /^\s*\<\/?way/) {
my ($id) = ($main_line =~ /^\s*\<way id=[\'\"](\d+)[\'\"]/);
unless($id) { warn "Invalid way line '$main_line'"; next; }
unless($main_type eq "way") { warn "$main_type ended with $line"; next; }
if($wayH) {
&$wayH($id,\@tags,\@nodes,$main_line,$line);
}
}
elsif($line =~ /^\s*<\/?relation/) {
my ($id) = ($main_line =~ /^\s*\<relation id=[\'\"](\d+)[\'\"]/);
unless($id) { warn "Invalid relation line '$main_line'"; next; }
unless($main_type eq "relation") { warn "$main_type ended with $line"; next; }
if($relH) {
&$relH($id,\@tags,\@rel_nodes,\@rel_ways,$main_line,$line);
}
}
elsif($line =~ /^\s*\<\?xml/) {
# if($pass == 1) {
# print $line;
# }
}
elsif($line =~ /^\s*\<osm /) {
# if($pass == 1) {
# print $line;
# }
}
elsif($line =~ /^\s*\<\/osm\>/ ) {
# if($pass == 3) {
# print $line;
# }
}
else {
print STDERR "Unknown line $line\n";
exit 1;
};
}
# All done
closeXML();
}
sub MarkPoint
{
my $x = shift;
my $y = shift;
my $type = shift;
print "P$type $x $y\n";
}
my $wanted_nodes = Bit::Vector->new( 2000 * 1000 * 1000 );
my(%nodes,%ways);
my $totalways = 0;
my $closed = 0;
my $zero_length = 0;
sub nodeProcessor
{
my ($id,$lat,$long,$tagsRef,$main_line,$line) = @_;
if($wanted_nodes->contains($id)) {
$nodes{$id} = [$lat,$long];
}
}
sub wayProcessor
{
my ($id,$tagsRef,$nodesRef,$main_line,$line) = @_;
# return unless scalar(grep{defined $tags{natural} and $tags{natural} eq "coastline";
if( scalar(@$nodesRef) <= 1)
{ $zero_length++; return }
if( $nodesRef->[0] == $nodesRef->[-1] )
{
$closed++;
print "C1 $id\n";
}
else
{
$wanted_nodes->Bit_On( $nodesRef->[0] );
$wanted_nodes->Bit_On( $nodesRef->[-1] );
$ways{$id} = [$nodesRef->[0],$nodesRef->[-1]];
$totalways++;
if( $id == TRACE )
{ print STDERR "Found way $id [$nodesRef->[0],$nodesRef->[-1]]\n" }
}
}
print STDERR "Pass 1: Collecting ways\n";
# This assumes the ways come first, which may not always be the case
processXML( \&nodeProcessor, \&wayProcessor, undef );
print STDERR "$totalways collected, $closed closed, $zero_length zero-length\n";
my $pass = 2;
my $epsilon = EPSILON;
my $ways_remain = $totalways;
my $completed = 0;
my $ways_output = 0;
open TEMP, ">to-merge.txt" or die;
for my $pass (2..4)
{
print STDERR "Pass ${pass}: Starting with ",scalar(keys %ways)," ways (epsilon=$epsilon)\n";
print STDERR "Pass ${pass}a: Adding to R-Tree\n";
my $tree = new Tree::R();
# Add all begin points to the tree
for my $way (keys %ways)
{
if( not defined $nodes{$ways{$way}[0]} )
{
print STDERR "Missing node: $ways{$way}[0](way=$way)\n";
delete $ways{$way};
$ways_remain--;
next;
}
if( not defined $nodes{$ways{$way}[1]} )
{
print STDERR "Missing node: $ways{$way}[1](way=$way)\n";
delete $ways{$way};
$ways_remain--;
next;
}
my @coords = @{$nodes{$ways{$way}[0]}};
$tree->insert( $way, @coords, @coords );
if( $way == TRACE )
{ print STDERR "Inserted into r-tree: id $way ($coords[0],$coords[1])\n" }
}
my $ways_used = new Bit::Vector( 300_000_000 );
print STDERR "Pass ${pass}b: Joining ways\n";
WAY: for my $way (keys %ways)
{
if( $way == TRACE )
{ print STDERR "Joining $way used=", $ways_used->contains($way), "\n" }
next if $ways_used->contains($way);
for(;;)
{
my @coords = @{$nodes{$ways{$way}[1]}};
my @nearby;
$tree->query_completely_within_rect( $coords[0] - $epsilon, $coords[1] - $epsilon, $coords[0] + $epsilon, $coords[1] + $epsilon, \@nearby );
# This is a list of way_ids that start near the end of this way
my $match = undef;
my $mindist = 999;
# print "Searching near [$coords[0],$coords[1]]\n";
for my $w (@nearby)
{
# print "Way $way: testing '$w'\n";
if( $w == TRACE or $way == TRACE )
{ print STDERR "Found way $w used=",$ways_used->contains($w)," way=$way\n" }
next if $ways_used->contains($w);
if( $ways{$w}[0] == $ways{$way}[1] )
{ $match = $w; $mindist = 0; last }
my @coords2 = @{$nodes{$ways{$w}[0]}};
my $dist = ($coords[0]-$coords2[0])**2 + ($coords[1]-$coords2[1])**2;
if( $dist < $mindist )
{
$mindist = $dist;
$match = $w;
}
}
# If we loop around to ourselves and we link to no other nodes we bail. We want unclosed single ways to display
next WAY unless $mindist < 3 * $epsilon * $epsilon;
next WAY unless defined $match;
next WAY if $match == $way and scalar(@{$ways{$way}}) == 2 and $ways{$way}[0] != $ways{$way}[1];
if( $ways{$match}[0] != $ways{$way}[1] and $mindist < 1e-8 )
{
print TEMP "$ways{$way}[1] $ways{$match}[0]\n";
}
if( $mindist > 0 )
{
MarkPoint( @coords, 2 );
}
if( $mindist > 0.05 )
{
MarkPoint( @{$nodes{$ways{$mindist}[0]}}, 2 );
}
if( $match == $way )
{
if( $match == TRACE )
{ print STDERR "Closed way $way" }
OutputWay( $way, 1 );
$completed++;
$ways_remain--;
$ways_used->Bit_On($way);
last;
}
if( $way == TRACE or $match == TRACE )
{ print STDERR "Appending to $way: $match, now $ways{$way}[0] -> $ways{$match}[1]\n" }
$ways_used->Bit_On($match);
$ways_remain--;
push @{ $ways{$way} }, $match;
$ways{$way}[1] = $ways{$match}[1];
}
}
print STDERR "Remain: $ways_remain / $totalways (complete $completed)\n";
print STDERR "Pass ${pass}c: Consolidate remaining\n";
my %new_ways;
my $consolidated = 0;
for my $way (keys %ways)
{
if( $way == TRACE )
{ print STDERR "Consolidating $way: ",$ways_used->contains($way),"\n" }
next if $ways_used->contains($way);
my @list = Consolidate( \%ways, $way );
splice @list, 0, 1, $ways{$way}[0], $ways{$way}[1];
$new_ways{$way} = \@list;
# print "Consolidated $way: ($list[0] -> $list[1]) $way ",join(" ",@list[2..$#list]),"\n";
$consolidated++;
}
print STDERR "Consolidated: $consolidated\n";
%ways = %new_ways;
$epsilon *= 10;
}
print STDERR "Dumping remaining\n";
for my $way (keys %ways)
{
MarkPoint( @{$nodes{$ways{$way}[0]}}, 1 );
MarkPoint( @{$nodes{$ways{$way}[1]}}, 1 );
OutputWay($way, 0);
}
print STDERR "Total ways output: $ways_output\n";
exit 0;
sub Consolidate
{
my $ways = shift;
my $way = shift;
if( $way == TRACE )
{ print STDERR "Consolidate($way)\n" }
my @res = ($way);
if( not defined $ways{$way} )
{
if( $way == TRACE )
{ print STDERR "Skippping\n" }
# This can happen after the first pass, then we don't remember the full details of each way anymore
return @res;
}
my @tmp = @{ $ways{$way} };
if( $way == TRACE )
{ print STDERR "Subways: ", join(" ", @tmp[2..$#tmp]), "\n" }
for my $i (2..$#tmp)
{
push @res, Consolidate($ways, $tmp[$i]);
}
if( scalar(grep{$_ == TRACE} @res) )
{ print STDERR "Found ",TRACE," as subway of $way\n" }
return @res;
}
sub OutputWay
{
my $way = shift;
my $complete = shift;
my @list = Consolidate(\%ways, $way);
$ways_output += scalar(@list);
print "",($complete?"C":"I"),scalar(@list)," ",join(" ",@list),"\n" or die "Output error ($!)\n";
}