-
Notifications
You must be signed in to change notification settings - Fork 184
/
populate_database.rb
executable file
·423 lines (334 loc) · 10.7 KB
/
populate_database.rb
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
#!/usr/bin/env ruby
require 'rubygems'
require 'postgres'
require 'csv'
# Some hackiness to include the library script, even if invoked from another directory
require File.join(File.expand_path(File.dirname(__FILE__)), 'geodict_lib')
def load_cities(conn)
begin
conn.exec('DROP TABLE cities')
rescue
# ignore errors here
end
conn.exec('CREATE TABLE cities (
city VARCHAR(80),
country CHAR(2),
region_code CHAR(2),
population INT,
lat FLOAT,
lon FLOAT,
last_word VARCHAR(32),
PRIMARY KEY (city, country, region_code));')
conn.exec('CREATE INDEX city_last_word_index ON cities(last_word)')
file_name = DSTKConfig::SOURCE_FOLDER+'worldcitiespop.csv'
File.foreach(file_name) do |line|
begin
row = CSV.parse_line(line)
country = row[0]
city = row[1]
region_code = row[3]
population = row[4]
lat = row[5]
lon = row[6]
rescue
printf(STDERR, 'Problem with '+line)
next
end
if !population or population == ''
population = 0
end
begin
city.strip!
last_word, index, skipped = pull_word_from_end(city, city.length-1, false)
conn.exec("INSERT INTO cities (city, country, region_code, population, lat, lon, last_word)
values ("+
"'"+PGconn.escape(city)+"'"+
", '"+PGconn.escape(country)+"'"+
", '"+PGconn.escape(region_code)+"'"+
", '"+PGconn.escape(population.to_s)+"'"+
", '"+PGconn.escape(lat.to_s)+"'"+
", '"+PGconn.escape(lon.to_s)+"'"+
", '"+PGconn.escape(last_word)+"')"
);
rescue
# do nothing
end
end
end
def load_countries(conn)
begin
conn.exec('DROP TABLE countries')
rescue
# ignore errors here
end
conn.exec('CREATE TABLE countries (
country VARCHAR(64),
PRIMARY KEY(country),
country_code CHAR(2),
lat FLOAT,
lon FLOAT,
last_word VARCHAR(32))')
conn.exec('CREATE INDEX country_last_word_index ON countries(last_word)')
country_positions = {}
file_name = DSTKConfig::SOURCE_FOLDER+'countrypositions.csv'
File.foreach(file_name) do |line|
begin
row = CSV.parse_line(line)
country_code = row[0]
lat = row[1]
lon = row[2]
rescue
printf(STDERR, 'Problem with '+line)
next
end
country_positions[country_code] = { :lat => lat, :lon => lon }
end
file_name = DSTKConfig::SOURCE_FOLDER+'countrynames.csv'
File.foreach(file_name) do |line|
begin
row = CSV.parse_line(line)
country_code = row[0]
country_names = row[1]
rescue
printf(STDERR, 'Problem with '+line)
next
end
country_names_list = country_names.split(' | ')
lat = country_positions[country_code][:lat]
lon = country_positions[country_code][:lon]
country_names_list.each do |country_name|
begin
country_name.strip!
last_word, index, skipped = pull_word_from_end(country_name, country_name.length-1, false)
conn.exec("INSERT INTO countries (country, country_code, lat, lon, last_word)
values ("+
"'"+PGconn.escape(country_name)+"'"+
", '"+PGconn.escape(country_code)+"'"+
", '"+PGconn.escape(lat.to_s)+"'"+
", '"+PGconn.escape(lon.to_s)+"'"+
", '"+PGconn.escape(last_word)+"')"
);
rescue
printf(STDERR, 'Problem with country '+country_name)
end
end
end
end
def load_regions(conn)
begin
conn.exec('DROP TABLE regions')
rescue
# ignore errors here
end
conn.exec('CREATE TABLE regions (
region VARCHAR(64),
region_code CHAR(4),
country_code CHAR(2),
lat FLOAT,
lon FLOAT,
last_word VARCHAR(32))')
load_us_regions(conn)
load_non_us_regions(conn)
end
def load_us_regions(conn)
us_state_positions = {}
file_name = DSTKConfig::SOURCE_FOLDER+'us_statepositions.csv'
File.foreach(file_name) do |line|
begin
row = CSV.parse_line(line)
region_code = row[0]
lat = row[1]
lon = row[2]
rescue
printf(STDERR, 'Problem with '+line)
next
end
us_state_positions[region_code] = { :lat => lat, :lon => lon }
end
country_code = 'US'
file_name = DSTKConfig::SOURCE_FOLDER+'us_statenames.csv'
File.foreach(file_name) do |line|
begin
row = CSV.parse_line(line)
region_code = row[0]
state_names = row[2]
rescue
printf(STDERR, 'Problem with '+line)
next
end
state_names_list = state_names.split('|')
lat = us_state_positions[region_code][:lat]
lon = us_state_positions[region_code][:lon]
state_names_list.each do |state_name|
begin
state_name.strip!
last_word, index, skipped = pull_word_from_end(state_name, state_name.length-1, false)
conn.exec("INSERT INTO regions (region, region_code, country_code, lat, lon, last_word)
values ("+
"'"+PGconn.escape(state_name)+"'"+
", '"+PGconn.escape(region_code)+"'"+
", '"+PGconn.escape(country_code)+"'"+
", '"+PGconn.escape(lat.to_s)+"'"+
", '"+PGconn.escape(lon.to_s)+"'"+
", '"+PGconn.escape(last_word)+"')"
);
rescue
printf(STDERR, 'Problem with region '+state_name+"\n")
# do nothing
end
end
end
end
def load_non_us_regions(conn)
have_loaded_region = {}
file_name = DSTKConfig::SOURCE_FOLDER+'geonames_postalcodes.tsv'
File.foreach(file_name) do |line|
row = line.split("\t")
country_code = row[0]
postal_code = row[1]
place_name = row[2]
admin_name1 = row[3]
admin_code1 = row[4]
admin_name2 = row[5]
admin_code2 = row[6]
admin_name3 = row[7]
admin_code3 = row[8]
lat = row[9]
lon = row[10]
accuracy = row[11]
if country_code == 'US' then next end
if !admin_name1 or admin_name1 == '' then next end
if !admin_code1 or admin_code1 == '' then next end
if !lat or lat == '' then next end
if !lon or lon == '' then next end
key = admin_name1 + '_' + country_code
if have_loaded_region[key]
next
end
have_loaded_region[key] = true
state_names_list = [admin_name1, admin_code1]
state_names_list.each do |state_name|
state_name.strip!
last_word, index, skipped = pull_word_from_end(state_name, state_name.length-1, false)
sql = "INSERT INTO regions (region, region_code, country_code, lat, lon, last_word)
values ("+
"'"+PGconn.escape(state_name)+"'"+
", '"+PGconn.escape(admin_code1)+"'"+
", '"+PGconn.escape(country_code)+"'"+
", '"+PGconn.escape(lat.to_s)+"'"+
", '"+PGconn.escape(lon.to_s)+"'"+
", '"+PGconn.escape(last_word)+"')"
conn.exec(sql)
end
end
end
def load_postal_codes(conn)
begin
conn.exec('DROP TABLE postal_codes')
rescue
# ignore errors here
end
conn.exec('CREATE TABLE postal_codes (
postal_code VARCHAR(64),
region_code VARCHAR(64),
country_code CHAR(2),
lat FLOAT,
lon FLOAT,
last_word VARCHAR(32))')
postal_codes = {}
file_name = DSTKConfig::SOURCE_FOLDER+'geonames_postalcodes.tsv'
$stderr.puts "Starting load of '#{file_name}'"
File.foreach(file_name) do |line|
row = line.split("\t")
country_code = row[0]
postal_code = row[1]
place_name = row[2]
admin_name1 = row[3]
admin_code1 = row[4]
admin_name2 = row[5]
admin_code2 = row[6]
admin_name3 = row[7]
admin_code3 = row[8]
lat_string = row[9]
lon_string = row[10]
accuracy = row[11]
if !postal_code or postal_code == '' then next end
if !admin_code1 or admin_code1 == '' then next end
if !lat_string or lat_string == '' then next end
if !lon_string or lon_string == '' then next end
key = country_code + '*' + postal_code
if !postal_codes[key]
postal_codes[key] = {
'postal_code' => postal_code,
'region_code' => admin_code1,
'country_code' => country_code,
'coordinates' => []
}
end
postal_codes[key]['coordinates'] << {'lat' => lat_string.to_f, 'lon' => lon_string.to_f}
end
$stderr.puts "Starting to add postal codes to the database"
postal_codes.each do |key, info|
postal_code = info['postal_code']
region_code = info['region_code']
country_code = info['country_code']
coordinates = info['coordinates']
last_word, index, skipped = pull_word_from_end(postal_code, postal_code.length-1, false)
lat = 0.0
lon = 0.0
coordinates.each do |position|
lat += position['lat']
lon += position['lon']
end
lat = (lat / coordinates.length)
lon = (lon / coordinates.length)
sql = "INSERT INTO postal_codes (postal_code, region_code, country_code, lat, lon, last_word)
values ("+
"'"+PGconn.escape(postal_code)+"'"+
", '"+PGconn.escape(region_code)+"'"+
", '"+PGconn.escape(country_code)+"'"+
", '"+PGconn.escape(lat.to_s)+"'"+
", '"+PGconn.escape(lon.to_s)+"'"+
", '"+PGconn.escape(last_word)+"')"
conn.exec(sql)
end
file_name = DSTKConfig::SOURCE_FOLDER + 'crawled_postalcodes.tsv'
$stderr.puts "Starting load of '#{file_name}'"
File.foreach(file_name) do |line|
row = line.split("\t")
country_code = row[0]
postal_code = row[1]
place_name = row[2]
admin_name1 = row[3]
admin_code1 = row[4]
admin_name2 = row[5]
admin_code2 = row[6]
admin_name3 = row[7]
admin_code3 = row[8]
lat_string = row[9]
lon_string = row[10]
accuracy = row[11]
if !postal_code or postal_code == '' then next end
if !admin_code1 or admin_code1 == '' then next end
if !lat_string or lat_string == '' then next end
if !lon_string or lon_string == '' then next end
last_word, index, skipped = pull_word_from_end(postal_code, postal_code.length-1, false)
sql = "INSERT INTO postal_codes (postal_code, region_code, country_code, lat, lon, last_word)
values ("+
"'"+PGconn.escape(postal_code)+"'"+
", '"+PGconn.escape(admin_code1)+"'"+
", '"+PGconn.escape(country_code)+"'"+
", '"+PGconn.escape(lat_string)+"'"+
", '"+PGconn.escape(lon_string)+"'"+
", '"+PGconn.escape(last_word)+"')"
conn.exec(sql)
end
$stderr.puts "Finished adding postal codes to the database"
conn.exec('CREATE INDEX postal_codes_last_word_index ON postal_codes(last_word)')
$stderr.puts "Finished adding postal codes to the database"
end
conn = get_database_connection(DSTKConfig::DATABASE)
load_cities(conn)
load_countries(conn)
load_regions(conn)
load_postal_codes(conn)