-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm_merge_lats_lons_with_intersection_pairs.pl
473 lines (357 loc) · 16.3 KB
/
osm_merge_lats_lons_with_intersection_pairs.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
#--------------------------------------------------
# osm_merge_lats_lons_with_intersection_pairs.pl
#--------------------------------------------------
# (c) Copyright 2022-2023 by Richard Fobes at SolutionsCreative.com
# Permission to copy and use and modify this
# software is hereby given to individuals and to
# businesses with ten or fewer employees if this
# copyright notice is included in all copies
# and modified copies.
# All other rights are reserved.
# Businesses with more than ten employees are
# encouraged to contract with small businesses
# to supply the service of running this software
# if there are arrangements for either business
# to make donations to support the Open Street
# Map project.
# Disclaimer of Warranty: THERE IS NO WARRANTY
# FOR THIS SOFTWARE. THE COPYRIGHT HOLDER PROVIDES
# THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY
# KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
# BUT NOT LIMITED TO, THE FITNESS FOR A
# PARTICULAR PURPOSE.
# Limitation of Liability: IN NO EVENT WILL THE
# COPYRIGHT HOLDER BE LIABLE TO ANYONE FOR
# DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
# INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OR INABILITY TO USE THE SOFTWARE.
#--------------------------------------------------
# Merges the latitude and longitude locations of
# intersection nodes with the pair of way IDs (or
# relation IDs) that will provide a name for the
# intersection.
#
# Usage:
#
# Delete files named:
# output_intersections_with_lats_lons_group_0.txt
# output_intersections_with_lats_lons_group_1.txt
# etc.
#
# perl osm_merge_lats_lons_with_intersection_pairs.pl
#--------------------------------------------------
# Intput files and format:
# street_node_way_pairs/node_way_pairs_in_group_00.txt
#
# n1105988000 r8715
# n809204700 r20605
# n7757174800 r178385
# n533792800 r291250
# n1568452400 r335422
# n7884603500 r398208
# n10695965600 w1149774142
# n140923700 w1149774191
# n10695963500 w1149774291
#--------------------------------------------------
# Output format:
#
# 0922_1166 w133795298 w191725117 1547107 6695210
#--------------------------------------------------
# Specify Linux or Windows path style.
# $slash_or_backslash = "\\" ; # windows
$slash_or_backslash = "/" ; # linux
#--------------------------------------------------
# Loop through the data based on the last two
# digits of the node ID number.
# for testing:
# for ( $ending_two_digits_as_integer = 1 ; $ending_two_digits_as_integer <= 2 ; $ending_two_digits_as_integer ++ )
for ( $ending_two_digits_as_integer = 0 ; $ending_two_digits_as_integer <= 99 ; $ending_two_digits_as_integer ++ )
{
#--------------------------------------------------
# Create text versions of variations of the
# ending digits.
$ending_two_digits_as_text = sprintf( "%02d" , $ending_two_digits_as_integer ) ;
$first_of_ending_two_digits_as_text = substr( $ending_two_digits_as_text , 0 , 1 ) ;
$second_of_ending_two_digits_as_text = substr( $ending_two_digits_as_text , 1 , 1 ) ;
print "\n" . "ending digits " . $ending_two_digits_as_text . "\n" ;
#--------------------------------------------------
# Create integers that represent the ending two
# digits in bcd -- binary coded decimal -- format.
# The decimal number 48 is the ascii code
# for the digit zero (0).
$first_of_ending_two_digits_in_bcd_format = ord( $first_of_ending_two_digits_as_text ) - 48 ;
$second_of_ending_two_digits_in_bcd_format = ord( $second_of_ending_two_digits_as_text ) - 48 ;
$ending_two_digits_in_bcd_format = ( $first_of_ending_two_digits_in_bcd_format * 16 ) + $second_of_ending_two_digits_in_bcd_format ;
#--------------------------------------------------
# Clear the info that was collected for the
# previous pair of ending digits.
%ways_and_relations_at_truncated_node = ( ) ;
#--------------------------------------------------
# Do initialization for this pair of ending
# digits.
$line_count = 0 ;
$node_match_count = 0 ;
$count_of_lines_not_recognized = 0 ;
#--------------------------------------------------
# Open the current output file.
$output_filename = 'output_intersections_with_lats_lons_group_' . $first_of_ending_two_digits_as_text . '.txt' ;
print "output filename: " . $output_filename . "\n" ;
open( OUT_FILE, '>>' , $output_filename ) or die $! ;
#--------------------------------------------------
# Open the input file that contains the node and
# way pairs and node and relation pairs.
$input_filename = 'street_node_way_pairs' . $slash_or_backslash . 'node_way_pairs_in_group_' . $ending_two_digits_as_text . '.txt' ;
open( IN_FILE_PAIRS , '<' , $input_filename ) ;
#--------------------------------------------------
# Begin a loop that reads the list of node and
# way pairs and node and relation pairs.
while ( $input_line = <IN_FILE_PAIRS> )
{
chomp( $input_line ) ;
$line_count ++ ;
#--------------------------------------------------
# Get the node ID and the way or relation ID,
# which is one "pair" of items.
if ( $input_line =~ /n([0-9]+) +([wr][0-9]+)/ )
{
$node_number_as_text = $1 ;
$way_or_relation_number_as_text = $2 ;
} else
{
# print "unrecognized: [" . $input_line . "]" . "\n" ;
$count_of_lines_not_recognized ++ ;
next ;
}
#--------------------------------------------------
# If the node number is short, pad it with leading
# zeros.
$character_length = length( ( $node_number_as_text . "" ) ) ;
if ( $character_length < 4 )
{
$node_number_as_text = substr( "0000" , 0 , ( 4 - $character_length ) ) . $node_number_as_text ;
}
#--------------------------------------------------
# If the ending two digits of the node number do
# not match the ones being handled, repeat the
# loop.
if ( substr( $node_number_as_text , -2 , 2 ) ne $ending_two_digits_as_text )
{
print "found node ID with unexpected ending digits: [" . $input_line . "]" . "\n" ;
next ;
}
#--------------------------------------------------
# Append the way ID number or relation ID number
# to a sub-list that is associated with this node
# ID. The sub-list lists the street ways and
# relations that include this node in their list
# of points (nodes).
# The index to the the sub-lists uses a
# truncated node ID because the last two digits
# are always the same. Omitting those last two
# unchanging digits increases processing speed.
$node_number_truncated = substr( $node_number_as_text , 0 , ( length( $node_number_as_text . "" ) - 2 ) ) ;
$ways_and_relations_at_truncated_node{ $node_number_truncated } = $ways_and_relations_at_truncated_node{ $node_number_truncated } . $way_or_relation_number_as_text . " " ;
# print OUT_FILE "[" . $node_number_as_text . "][" . $node_number_truncated . "][" . $way_or_relation_number_as_text . "][" . $ways_and_relations_at_truncated_node{ $node_number_truncated } . "]" . "\n" ;
$node_match_count ++ ;
#--------------------------------------------------
# Repeat the loop to handle the next line in the
# input file that lists the node and way and
# relation pairs.
}
#--------------------------------------------------
# Close the input file and write some log info.
close( IN_FILE_PAIRS ) ;
if ( $count_of_lines_not_recognized > 0 )
{
print "skipped " . $count_of_lines_not_recognized . " lines not recognized" . "\n" ;
}
print "got " . $node_match_count . " nodes from " . $line_count . " node street pairs in file " . $input_filename . "\n" ;
#--------------------------------------------------
# If testing and no nodes of interest were found,
# restart the loop to handle the next ending
# digits.
if ( $node_match_count < 1 )
{
print "test does not have nodes for these ending digits" . "\n" ;
next ;
}
#--------------------------------------------------
# Copy the items in the just-created list that
# are associated with more than one street way
# (or street relation). These nodes are street
# intersections. After making this copy,
# delete the associative array that was copied.
$intersection_count = 0 ;
%street_ids_for_truncated_node = ( ) ;
foreach $node_number_truncated ( keys ( %ways_and_relations_at_truncated_node ) )
{
$ways_and_relations_for_one_node = $ways_and_relations_at_truncated_node{ $node_number_truncated } ;
if ( index( $ways_and_relations_for_one_node , ' ' ) < ( length( $ways_and_relations_for_one_node ) - 1 ) )
{
$street_ids_for_truncated_node{ $node_number_truncated } = substr( $ways_and_relations_for_one_node , 0 , ( length( $ways_and_relations_for_one_node ) - 1 ) ) ;
# print OUT_FILE "[" . $node_number_truncated . "][" . $street_ids_for_truncated_node{ $node_number_truncated } . "]" . "\n" ;
$intersection_count ++ ;
}
}
%ways_and_relations_at_truncated_node = ( ) ;
print "found " . $intersection_count . " intersections" . "\n" ;
#--------------------------------------------------
# If no intersections were found, restart the
# loop to get node and way or relation pairs from
# a different input file.
if ( $intersection_count < 1 )
{
print "current ending digits skipped because no intersections found" . "\n" ;
next ;
}
#--------------------------------------------------
# Open the input file that contains the nodes,
# latitudes, and longitudes in packed binary
# format. It is specific to the current ending
# two digits of node numbers.
$input_filename = 'lats_lons_in_groups' . $slash_or_backslash . 'output_packed_lats_lons_in_group_' . $ending_two_digits_as_text . '.bin' ;
open( IN_FILE_BINARY, '<:raw' , $input_filename ) or die $! ;
print "binary in: " . $input_filename . "\n" ;
#--------------------------------------------------
# Begin a loop that handles each group of nine
# packed integers from the input file. These
# integers hold the node ID and latitude and
# longitude for one node. The nine packed
# integers are stored in 18 bytes, and each
# byte holds two digits. Hexadecimal conversion
# is used because in this case it matches
# binary coded decimal format.
$intersection_count = 0 ;
$progress_counter = 0 ;
$log_line_counter = 0 ;
while( read( IN_FILE_BINARY , $bytes , 18 ) )
{
$digits_node_lat_lon = unpack( "h36" , $bytes ) ;
#--------------------------------------------------
# Get the node number. The final
# two digits are already known to match the
# ending digits being handled.
# The leading zeros must be removed before
# checking the truncated version with a match.
$node_number_full = substr( $digits_node_lat_lon , 0 , 12 ) ;
$node_number_truncated = substr( $node_number_full , 0 , 10 ) ;
$node_number_truncated =~ s/^0+// ;
if ( length( $node_number_truncated ) == 0 )
{
$node_number_truncated = "0" ;
}
# print OUT_LOG "-[" . $node_number_full . "][" . $node_number_truncated . "]" . "\n" ;
#--------------------------------------------------
# Display progress.
$progress_counter ++ ;
if ( $progress_counter > 10000000 )
{
$progress_counter = 0 ;
$log_line_counter ++ ;
print "handled next 10,000,000 nodes (" . $log_line_counter . ")" . "\n" ;
}
#--------------------------------------------------
# If this node is not an intersection (and
# associated with at least one pair of way ID
# numbers), restart the node loop to handle the
# next node.
if ( not( exists( $street_ids_for_truncated_node{ $node_number_truncated } ) ) )
{
next ;
}
#--------------------------------------------------
# Get the latitude and longitude integers as text.
# They are 11 digits in length. Ignore the first
# digit in each group of 12 digits because that
# should always be zero.
$latitude = substr( $digits_node_lat_lon , 13 , 11 ) ;
$longitude = substr( $digits_node_lat_lon , 25 , 11 ) ;
# print OUT_LOG "latitude and longitude: " . $latitude . " " . $longitude . "\n" ;
#--------------------------------------------------
# The latitude and longitude are for a node that
# is an intersection. So get the first four
# digits of the latitude and longitude integers
# and combine them with an underscore to
# specify a region.
$region_latitude = substr( $digits_node_lat_lon , 13 , 4 ) ;
$region_longitude = substr( $digits_node_lat_lon , 25 , 4 ) ;
$latitude_truncated = substr( $digits_node_lat_lon , 17 , 7 ) ;
$longitude_truncated = substr( $digits_node_lat_lon , 29 , 7 ) ;
#--------------------------------------------------
# Begin a loop that pairs each street ID with each
# of the other street IDs that also share the same
# node ID.
# A relation ID and way ID can refer to the same
# street name, or the relation ID can provide a
# second name for the street.
# If both street IDs are the same, ignore this
# pair of street IDs.
@list_of_street_ids = split( / / , $street_ids_for_truncated_node{ $node_number_truncated } ) ;
for ( $which_street_first = 0 ; $which_street_first < $#list_of_street_ids ; $which_street_first ++ )
{
$street_id_first = $list_of_street_ids[ $which_street_first ] ;
for ( $which_street_second = $which_street_first + 1 ; $which_street_second <= $#list_of_street_ids ; $which_street_second ++ )
{
$street_id_second = $list_of_street_ids[ $which_street_second ] ;
if ( $street_id_first eq $street_id_second )
{
next ;
}
#--------------------------------------------------
# Write the info for one pair of street IDs.
# The latitude and longitude are split into a
# region -- which is identified by the first four
# digits in the latitude and the first four digits
# in the longitude -- and the remaining digits.
# Write the smaller street ID first and the
# larger street ID second.
#
# Output format:
# 0922_1166 w133795298 w191725117 1547107 6695210
if ( length( $street_id_first ) < length( $street_id_second ) )
{
$both_street_ids = $street_id_first . " " . $street_id_second ;
} elsif ( length( $street_id_second ) < length( $street_id_first ) )
{
$both_street_ids = $street_id_second . " " . $street_id_first ;
} else
{
$street_id_first_as_number = 0 + substr( $street_id_first , 1 ) ;
$street_id_second_as_number = 0 + substr( $street_id_second , 1 ) ;
if ( $street_id_first_as_number < $street_id_second_as_number )
{
$both_street_ids = $street_id_first . " " . $street_id_second ;
} else
{
$both_street_ids = $street_id_second . " " . $street_id_first ;
}
}
print OUT_FILE $region_latitude . '_' . $region_longitude . ' ' . $both_street_ids . ' ' . $latitude_truncated . ' ' . $longitude_truncated . "\n" ;
$intersection_count ++ ;
#--------------------------------------------------
# Repeat the loops for the next pair of street IDs
# at the same intersection.
}
}
#--------------------------------------------------
# Repeat the loop to consider the next node in
# the binary file that contains the node and
# latitude and longitude numbers.
}
#--------------------------------------------------
# Close the files for the current choice of last
# two digits in the node ID number.
close( OUT_FILE ) ;
close( IN_FILE_BINARY ) ;
#--------------------------------------------------
# Repeat the loop for the next group of nodes
# that have the next pair of two ending digits.
print "found " . $intersection_count . " matching intersections in this group" . "\n" ;
$total_intersection_count += $intersection_count ;
}
#--------------------------------------------------
# All done.
print "\n" ;
print "done, found " . $total_intersection_count . " intersections total" . "\n" ;
#--------------------------------------------------
# End of code.