-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm_eliminate_non_intersection_street_joins.pl
274 lines (230 loc) · 9.33 KB
/
osm_eliminate_non_intersection_street_joins.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
#--------------------------------------------------
# osm_eliminate_non_intersection_street_joins.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.
#--------------------------------------------------
# Usage:
#
# perl osm_eliminate_non_intersection_street_joins.pl
#--------------------------------------------------
# Gets intersections from the file
# "output_merged_sorted_intersections_and_street_names.txt"
# and checks street names. If both street names
# are the same, the intersection is removed because
# the node is a join point for two segments of the
# same street. If either street name is missing
# from the supplied region-specific info here,
# that intersection is removed.
#--------------------------------------------------
# Input format:
#
# Notice that street ID pairs appear twice
# because that was needed for sorting, but now
# the version with the bigger ID number first
# will be removed.
# Input format:
# 0920_0916 w142307953 Union_Glacier_Camp_Road
# 0920_0916 w142307953 w142307954 2200226 6840922
# 0920_0916 w142307954 Elephant_Head_Road
# 0920_0916 w142307954 w142307953 2200226 6840922
# 0920_0917 w1115464624 Union_Glacier_Camp_Road
# 0920_0917 w1115464624 w142307953 2480943 2441468
# 0920_0917 w1115464624 w142307956 2315676 4200065
# 0920_0917 w142307953 Union_Glacier_Camp_Road
# 0920_0917 w142307953 w1115464624 2480943 2441468
# 0920_0917 w142307956 Track_to_Peak_942
# Sample input test lines:
# 1063_1070 w4973733 w4973735 4255414 4169558
# 1063_1070 w4973733 Street_Name
# 1063_1070 w4973735 Street_Name_Something
# 1063_1070 w4973733 w4998649 4255414 4169558
# 1063_1070 w4973735 w11937047 4255414 4169558
# 1063_1070 w4998649 Street_Name
# 1063_1070 w11937047 Street_Name_Different
# 1059_1010 w148010553 Street_Name_Other
# 1059_1010 w111269061 Street_Name_Two
# 1059_1010 w4982951 w4982993 9601275 7868320
# 1059_1010 w4982951 Some_Other_Name
# 1059_1010 w119374047 Yet_Another
# 1059_1010 w111269061 w119374047 9435372 8502535
# 1059_1010 w111269061 w148010553 9435372 8502535
# 1059_1010 w4973732 w4973734 4255414 4169558
# 1059_1010 w4973732 Maney_Hill_Road
# 1059_1010 w4973734 Maney_Hill_Road
# 1059_1010 w119374047 w148010553 9435372 8502535
# 1059_1010 w311606465 w881130716 9530363 7690581
# 1059_1010 w4606006 w135343990 9444494 7483975
# 1059_1010 w4606006 w135344010 9444494 7483975
# 1059_1020 w4606006 Another_Street
# 1059_1020 w4606006 w135343990 9444494 7483975
# 1059_1020 w4973734 Another_Street_Diffname
# 1059_1020 w4973732 w4973734 4255414 4169558
# 1059_1020 w46066 w1353490 9444494 7483975
# 1059_1020 w4973732 Maney_Hill_Road
#--------------------------------------------------
# Open the main output file, and a log file that
# lists information about the intersections that
# were removed.
$output_filename = 'output_uploadable_street_intersections.txt' ;
print "output filename: " . $output_filename . "\n" ;
open( OUT_FILE , '>' , $output_filename ) ;
$output_filename = 'output_log_street_joins.txt' ;
print "output filename: " . $output_filename . "\n" ;
open( OUT_LOG_FILE , '>' , $output_filename ) ;
#--------------------------------------------------
# Open the input file that contains the
# intersection info and region-specific street
# name info.
$input_filename = 'output_merged_sorted_intersections_and_street_names.txt' ;
print "input filename: " . $input_filename . "\n" ;
open( IN_FILE , '<' , $input_filename ) ;
#--------------------------------------------------
# Get the region ID from each line of the input
# file.
$previous_region_id = "" ;
$storage_line_number = 0 ;
while( $input_line = <IN_FILE> )
{
chomp( $input_line ) ;
if ( $input_line =~ /^([0-9]+[ _][0-9]+) / )
{
$next_region_id = $1 ;
} else
{
if ( $input_line =~ /[^ ]/ )
{
print OUT_LOG_FILE "line not recognized: " . $input_line . "\n" ;
}
next ;
}
#--------------------------------------------------
# If the region ID has changed, process the data
# from the recent region, then start handling the
# new region.
if ( $previous_region_id eq "" )
{
$previous_region_id = $next_region_id ;
}
if ( $next_region_id ne $previous_region_id )
{
&filter_out_joins( ) ;
$previous_region_id = $next_region_id ;
$storage_line_number = 0 ;
}
#--------------------------------------------------
# Store a street name that is within this region.
if ( $input_line =~ /^[0-9]+[ _][0-9]+ +([wr][0-9]+) +([^ ]+) *$/ )
{
$street_id = $1 ;
$street_name = $2 ;
$street_name_for_street_id{ $street_id } = $street_name ;
#--------------------------------------------------
# Store the intersection lines within this region.
} elsif ( $input_line =~ /^[0-9]+[ _][0-9]+ +[wr][0-9]+ +[wr][0-9]+ +[0-9]+ +[0-9]+/ )
{
$storage_line_number ++ ;
$line_numbered[ $storage_line_number ] = $input_line ;
#--------------------------------------------------
# Ignore anything not recognized.
} else
{
print OUT_LOG_FILE "info not recognized: " . $input_line . "\n" ;
next ;
}
#--------------------------------------------------
# Repeat the loop to handle the next input line.
}
#--------------------------------------------------
# Process the last region.
if ( $storage_line_number > 0 )
{
&filter_out_joins( ) ;
}
#--------------------------------------------------
# End of main code.
print OUT_LOG_FILE "removed " . $removal_count_join . " joins from intersections" . "\n" ;
print OUT_LOG_FILE "removed " . $removal_count_missing_name . " intersections with missing names" . "\n" ;
close( OUT_FILE ) ;
#--------------------------------------------------
# Subroutine filter_out_joins
#
# Only write the intersection lines in which the
# two street names are known but do not match.
# Also remove the lines in which the first street
# ID number is larger than the second street ID
# because those are duplications that need to be
# removed.
sub filter_out_joins
{
$number_of_lines_in_region = $storage_line_number ;
if ( $number_of_lines_in_region < 1 )
{
return ;
}
for ( $line_number = 1 ; $line_number <= $number_of_lines_in_region ; $line_number ++ )
{
if ( $line_numbered[ $line_number ] =~ /^[0-9]+[ _][0-9]+ +([wr])([0-9]+) +([wr])([0-9]+) +[ 0-9]+/ )
{
$first_way_or_relation_letter = $1 ;
$first_street_id_number_only = $2 ;
$second_way_or_relation_letter = $3 ;
$second_street_id_number_only = $4 ;
$first_street_id = $first_way_or_relation_letter . $first_street_id_number_only ;
$second_street_id = $second_way_or_relation_letter . $second_street_id_number_only ;
if ( ( $first_street_id_number_only + 0 ) > ( $second_street_id_number_only + 0 ) )
{
next ;
}
if ( ( exists( $street_name_for_street_id{ $first_street_id } ) ) && ( exists( $street_name_for_street_id{ $second_street_id } ) ) )
{
if ( $street_name_for_street_id{ $first_street_id } ne $street_name_for_street_id{ $second_street_id } )
{
print OUT_FILE $line_numbered[ $line_number ] . "\n" ;
} else
{
# print OUT_LOG_FILE $street_name_for_street_id{ $first_street_id } . " " . $first_street_id . "\n" . $street_name_for_street_id{ $second_street_id } . " " . $second_street_id . "\n\n" ;
$removal_count_join ++ ;
}
} else
{
if ( not( exists( $street_name_for_street_id{ $first_street_id } ) ) )
{
print OUT_LOG_FILE "missing name for street ID " . $first_street_id . "\n" ;
$removal_count_missing_name ++ ;
}
if ( ( $second_street_id ne $first_street_id ) && ( not( exists( $street_name_for_street_id{ $second_street_id } ) ) ) )
{
print OUT_LOG_FILE "missing name for street ID " . $second_street_id . "\n" ;
$removal_count_missing_name ++ ;
}
}
}
}
%street_name_for_street_id = ( ) ;
$storage_line_number = 0 ;
}
#--------------------------------------------------
# End of code.