-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_city_info_from_postal_into_osm.pl
186 lines (152 loc) · 6.77 KB
/
merge_city_info_from_postal_into_osm.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
#--------------------------------------------------
# merge_city_info_from_postal_into_osm.pl
#--------------------------------------------------
# (c) Copyright 2014-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 they also arrange 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.
#--------------------------------------------------
# Specify the file that contains the state abbreviations.
$input_file_name_state_abbreviations = 'output_state_country_abbreviations_for_each_city.txt' ;
#--------------------------------------------------
# Specify the log file.
$log_file_name = 'output_log_merge_city_info_from_postal_into_osm.txt' ;
#--------------------------------------------------
# Get the state abbreviations and country codes
# from the postal-code info file.
#
# Allow for name inconsistency between
# postal-code info and Open Street Map data:
# OSM: Saint Louis MO node=151786910
# Zipcode data: St. Louis MO
open( IN_STATE , "<" . $input_file_name_state_abbreviations ) ;
while ( $input_line = <IN_STATE> ) {
chomp ( $input_line ) ;
if ( $input_line =~ /^([^ ]+) +([^ ]+) +([^ ]+) +([0-9]+) +([0-9]+) *$/ )
{
$city_name_searchable = $1 ;
$state_abbreviation = $2 ;
$country_code = $3 ;
$latitude = $4 ;
$longitude = $5 ;
$state_abbreviation = lc( $state_abbreviation ) ;
$combo_city_latitude_longitude = $city_name_searchable . "_" . substr( $latitude , 0 , 4 ) . "_" . substr( $longitude , 0 , 4 ) ;
$state_abbreviation_for_combo{ $combo_city_latitude_longitude } = $state_abbreviation ;
$country_code_for_combo{ $combo_city_latitude_longitude } = $country_code ;
if ( $combo_city_latitude_longitude =~ /saint_/ )
{
$combo_city_latitude_longitude =~ s/saint_/st._/ ;
$state_abbreviation_for_combo{ $combo_city_latitude_longitude } = $state_abbreviation ;
$country_code_for_combo{ $combo_city_latitude_longitude } = $country_code ;
}
}
}
#--------------------------------------------------
# Begin a loop that handles each line of city info.
#
# Sample input line:
# london ?? gb 10515073219 09998723525 n107775 London
# portland ?? ?? 10455202471 98773258050 n1666626393 Portland
while ( $input_line = <STDIN> ) {
chomp ( $input_line ) ;
$state_abbreviation = "" ;
if ( $input_line =~ /^([^ ]+) ([^ ]+) ([^ ]+) ([0-9]+) ([0-9]+) ([^ ]+) ([^ ]+) *$/ )
{
$city_name_searchable = $1 ;
$state_or_province = $2 ;
$country_code = $3 ;
$latitude = $4 ;
$longitude = $5 ;
$city_id = $6 ;
$displayed_city_name = $7 ;
$state_or_province_raw = $state_or_province ;
#--------------------------------------------------
# If the OSM-source city matches a postal-code-source
# city that has the same location, then
# supply the state/province and country code.
# For debugging, keep track of which cities
# were matched.
$combo_city_latitude_longitude = $city_name_searchable . "_" . substr( $latitude , 0 , 4 ) . "_" . substr( $longitude , 0 , 4 ) ;
if ( exists( $state_abbreviation_for_combo{ $combo_city_latitude_longitude } ) )
{
$state_abbreviation = $state_abbreviation_for_combo{ $combo_city_latitude_longitude } ;
$country_code = $country_code_for_combo{ $combo_city_latitude_longitude } ;
$state_or_province = $state_abbreviation ;
$found_combo{ $combo_city_latitude_longitude } = "y" ;
# $city_name = $combo_city_latitude_longitude ;
# $city_name =~ s/[0-9]+_[0-9]+$// ;
# $found_city{ $city_name_searchable . "_" . $state_abbreviation } = "y" ;
} else
{
$state_abbreviation = "" ;
$not_found_combo{ $combo_city_latitude_longitude } = "y" ;
}
#--------------------------------------------------
# If the node for Washington DC has an unknown
# state code, ignore it because another entry has
# the correct information.
if ( ( $city_id eq "n158368533" ) && ( $state_or_province eq "??" ) )
{
next ;
}
#--------------------------------------------------
# If the country code is not a two-letter code,
# specify it as unknown.
if ( $country_code !~ /^[A-Z][A-Z]$/i )
{
$country_code = "??" ;
}
#--------------------------------------------------
# Reminder: In the Dashrep code:
#
# Add the state abbreviation to the end of the
# displayed version of the city name -- so that
# it's clear which same-name city was found.
# If the city is not in a recognized state or
# province and if the two-letter country code is known,
# append the country code to the end of the city
# name.
#--------------------------------------------------
# Write the orginal city-info line, possibly
# with the state/province and country code changed.
print $city_name_searchable . " " . uc( $state_or_province ) . " " . uc( $country_code ) . " " . $latitude . " " . $longitude . " " . $city_id . " " . $displayed_city_name . "\n" ;
if ( $state_abbreviation ne "" )
{
print $city_name_searchable . "_" . lc( $state_abbreviation ) . " " . uc( $state_or_province ) . " " . uc( $country_code ) . " " . $latitude . " " . $longitude . " " . $city_id . " " . $displayed_city_name . "\n" ;
}
#--------------------------------------------------
# Repeat the loop that handles each line of city info.
}
}
#--------------------------------------------------
# Indicate which postal-code-source cities were
# not matched.
open( OUTLOG , ">" . $log_file_name ) ;
foreach $combo_city_latitude_longitude ( keys( %not_found_combo ) )
{
print OUTLOG "city not found: " . $combo_city_latitude_longitude . "\n" ;
}
print OUTLOG "\n\n" ;
foreach $combo_city_latitude_longitude ( keys( %found_combo ) )
{
print OUTLOG ">> " . $combo_city_latitude_longitude . "\n" ;
}