-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakeatlas.py
600 lines (572 loc) · 12.9 KB
/
makeatlas.py
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Script used to build the geonames atlas into an sqlite database.
Works best on linuxes, for now.
"""
# CONFIGURATION
# where everything happens (empty writable directory)
_workdir = '/tmp'
# minimum population of cities
#
# some stats as of 2020-05-23:
# 1000+ => ~15M ~114'345 cities
# 500+ => ~19M ~156'260 cities
# 250+ => ~23M ~203'733 cities
# 100+ => ~27M ~262'684 cities
# 50+ => ~30M ~298'839 cities
# 1+ => ~40M ~423'935 cities
# 0+ => ~379M ~4'769'603 cities
_minpop = 1
# country codes
# this list must be up to date with geonames ftp
allcodes = [
'AD',
'AE',
'AF',
'AG',
'AI',
'AL',
'AM',
'AN',
'AO',
'AQ',
'AR',
'AS',
'AT',
'AU',
'AW',
'AZ',
'BA',
'BB',
'BD',
'BE',
'BF',
'BG',
'BH',
'BI',
'BJ',
'BL',
'BM',
'BN',
'BO',
'BQ',
'BR',
'BS',
'BT',
'BV',
'BW',
'BY',
'BZ',
'CA',
'CC',
'CD',
'CF',
'CG',
'CH',
'CI',
'CK',
'CL',
'CM',
'CN',
'CO',
'CR',
'CS',
'CU',
'CV',
'CW',
'CX',
'CY',
'CZ',
'DE',
'DJ',
'DK',
'DM',
'DO',
'DZ',
'EC',
'EE',
'EG',
'EH',
'ER',
'ES',
'ET',
'FI',
'FJ',
'FK',
'FM',
'FO',
'FR',
'GA',
'GB',
'GD',
'GE',
'GF',
'GG',
'GH',
'GI',
'GL',
'GM',
'GN',
'GP',
'GQ',
'GR',
'GS',
'GT',
'GU',
'GW',
'GY',
'HK',
'HM',
'HN',
'HR',
'HT',
'HU',
'ID',
'IE',
'IL',
'IM',
'IN',
'IO',
'IQ',
'IR',
'IS',
'IT',
'JE',
'JM',
'JO',
'JP',
'KE',
'KG',
'KH',
'KI',
'KM',
'KN',
'KP',
'KR',
'KW',
'KY',
'KZ',
'LA',
'LB',
'LC',
'LI',
'LK',
'LR',
'LS',
'LT',
'LU',
'LV',
'LY',
'MA',
'MC',
'MD',
'ME',
'MF',
'MG',
'MH',
'MK',
'ML',
'MM',
'MN',
'MO',
'MP',
'MQ',
'MR',
'MS',
'MT',
'MU',
'MV',
'MW',
'MX',
'MY',
'MZ',
'NA',
'NC',
'NE',
'NF',
'NG',
'NI',
'NL',
'NO',
'NP',
'NR',
'NU',
'NZ',
'OM',
'PA',
'PE',
'PF',
'PG',
'PH',
'PK',
'PL',
'PM',
'PN',
'PR',
'PS',
'PT',
'PW',
'PY',
'QA',
'RE',
'RO',
'RS',
'RU',
'RW',
'SA',
'SB',
'SC',
'SD',
'SE',
'SG',
'SH',
'SI',
'SJ',
'SK',
'SL',
'SM',
'SN',
'SO',
'SR',
'ST',
'SV',
'SX',
'SY',
'SZ',
'TC',
'TD',
'TF',
'TG',
'TH',
'TJ',
'TK',
'TL',
'TM',
'TN',
'TO',
'TR',
'TT',
'TV',
'TW',
'TZ',
'UA',
'UG',
'UM',
'US',
'UY',
'UZ',
'VA',
'VC',
'VE',
'VG',
'VI',
'VN',
'VU',
'WF',
'WS',
'XK',
'YE',
'YT',
'ZA',
'ZM',
'ZW' ]
# END CONFIG
import sys
import os.path
from sqlite3 import dbapi2 as sqlite
# Timezones
tzschema = """
CREATE TABLE Timezones
(
_idx integer primary key,
timezoneid varchar not null unique,
gmtoffset real not null,
dstoffset real not null,
rawoffset real not null
);
"""
class TimeZone(object):
def __init__(self, line):
line = line.split('\t')
#print(line)
self.countrycode = line[0]
self.timezoneid = line[1]
self.gmtoffset = line[2]
self.dstoffset = line[3]
self.rawoffset = line[4]
def insert(self, cur):
print("... timezone [%s]" % self.timezoneid)
sql = """INSERT INTO Timezones (timezoneid, gmtoffset, dstoffset,
rawoffset) VALUES ( ?,?,?,? );"""
try:
cur.execute(sql, (self.timezoneid, self.gmtoffset,
self.dstoffset, self.rawoffset))
except:
print('ERR='+self.timezoneid)
raise
@staticmethod
def insertDummy(cur):
print('... insert empty timezone')
sql = """INSERT INTO Timezones (timezoneid, gmtoffset, dstoffset,
rawoffset) VALUES ('?', 0, 0, 0);"""
cur.execute(sql)
@staticmethod
def downloadFile():
url = 'http://download.geonames.org/export/dump/timeZones.txt'
if not os.path.exists('in/timeZones.txt'):
os.system('cd in && wget %s && rm -f _timeZones.txt' % url)
# sort file
if not os.path.exists('in/_timeZones.txt'):
os.system('cd in && tail -n +2 timeZones.txt | sort -uk 2 > _timeZones.txt')
@staticmethod
def parseFile():
f = open('in/_timeZones.txt', 'r')
lines = f.read()
f.close()
lines = lines.split('\n')
ret = []
for line in lines:
if line != '':
ret.append(TimeZone(line))
return ret
# countries
ctyschema = """
CREATE TABLE CountryInfo
(
_idx integer primary key,
iso varchar not null unique,
iso3 varchar not null,
iso_numeric integer not null,
fips varchar not null,
country varchar not null,
capital varchar not null,
area integer not null,
population integer not null,
continent varchar not null,
tld varchar not null,
currencycode varchar not null,
currencyname varchar not null,
phone varchar not null,
postalcodeformat varchar not null,
postalcoderegex varchar not null,
languages varchar not null,
geonameid integer not null,
neighbours varchar not null,
equivalentfipscode varchar not null
);
"""
class CountryInfo(object):
def __init__(self, line):
words = line.split('\t')
self.iso = words[0]
self.iso3 = words[1]
self.iso_numeric = int(words[2])
self.fips = words[3]
self.country = words[4]
self.capital = words[5]
self.area = words[6]
self.population = words[7]
self.continent = words[8]
self.tld = words[9]
self.currencycode = words[10]
self.currencyname = words[11]
self.phone = words[12]
self.postalcodeformat = words[13]
self.postalcoderegex = words[14]
self.languages = words[15]
self.geonameid = int(words[16])
self.neighbours = words[17]
self.equivalentfipscode = words[18]
def insert(self, cur):
print("... country [%s]" % self.iso)
sql = '''INSERT INTO CountryInfo (iso, iso3, iso_numeric, fips,
country, capital, area, population, continent, tld, currencycode,
currencyname, phone, postalcodeformat, postalcoderegex, languages,
geonameid, neighbours, equivalentfipscode)
VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? );'''
cur.execute(sql, (self.iso, self.iso3, self.iso_numeric, self.fips,
self.country, self.capital, self.area, self.population,
self.continent, self.tld, self.currencycode, self.currencyname,
self.phone, self.postalcodeformat, self.postalcoderegex,
self.languages, self.geonameid, self.neighbours,
self.equivalentfipscode))
@staticmethod
def downloadFile():
url = 'http://download.geonames.org/export/dump/countryInfo.txt'
if not os.path.exists('in/countryInfo.txt'):
os.system('cd in && wget %s' % url)
@staticmethod
def parseFile():
f = open('in/countryInfo.txt', 'r')
lines = f.read()
f.close()
lines = lines.split('\n')
lines = lines[:-1]
i = 0;
ret = []
while True:
try:
line = lines[i]
except IndexError:
return ret
if not line.startswith('#'):
ret.append(CountryInfo(line))
i += 1
#
# cities
citiesschema = """
CREATE TABLE GeoNames
(
_idx integer primary key,
geonameid integer default null,
name varchar not null,
asciiname varchar not null,
alternatenames varchar not null,
latitude real not null,
longitude real not null,
--feature_class varchar,
--feature_code varchar,
country integer not null,
--cc2 varchar,
--admin1_code varchar,
--admin2_code varchar,
--admin3_code varchar,
--admin4_code varchar,
--population integer,
elevation integer not null default 0,
--dem integer,
timezone integer not null,
--modification_date varchar
foreign key (country) references CountryInfo(_idx),
foreign key (timezone) references Timezones(_idx)
);
"""
class GeoName(object):
def __init__(self, line):
line.strip()
words = line.split('\t')
self.geonameid = words[0]
self.name = words[1]
self.asciiname = words[2]
self.alternatenames = words[3]
self.latitude = words[4]
self.longitude = words[5]
self.feature_class = words[6]
self.feature_code = words[7]
self.country_code = words[8]
self.cc2 = words[9]
self.admin1_code = words[10]
self.admin2_code = words[11]
self.admin3_code = words[12]
self.admin4_code = words[13]
self.population = words[14]
self.elevation = words[15]
self.dem = words[16]
self.timezone = words[17]
if self.timezone == '': self.timezone = '?'
self.modification_date = words[18]
def insert(self, cur):
#print(self.name)
sql = """INSERT INTO GeoNames (geonameid, name, asciiname,
alternatenames, latitude, longitude, country, elevation, timezone)
VALUES ( ?,?,?,?,?,?,(SELECT _idx FROM CountryInfo WHERE iso = ?),
?,(SELECT _idx FROM Timezones WHERE timezoneid = ?));"""
try: cur.execute(sql, (self.geonameid, self.name, self.asciiname,
self.alternatenames, self.latitude, self.longitude,
self.country_code, self.elevation, self.timezone))
except sqlite.IntegrityError:
#print('ERR=(%s)' % self.timezone)
raise
@staticmethod
def downloadFile(ctycode):
if not os.path.exists('in/%s.txt' % ctycode):
if not os.path.exists('in/%s.zip' % ctycode):
os.system('cd in && wget http://download.geonames.org/export/dump/%s.zip' % ctycode)
os.system('cd in && unzip -o %s.zip' % ctycode)
@staticmethod
def parseFile(ctycode):
path = 'in/%s.txt' % ctycode
f = open(path, 'r')
lines = f.read()
f.close()
lines = lines.split('\n')
lines = lines[:-1]
ret = []
if ctycode not in ['AN','BV','CS','HM']: # those have no P
for l in lines:
name = GeoName(l)
if name.feature_class == 'P':
if name.population and int(name.population) >= _minpop:
ret.append(name)
else:
for l in lines:
ret.append(GeoName(l))
return ret
@staticmethod
def count(cur, code=None):
if code:
sql = """SELECT count(A._idx) FROM GeoNames as A, CountryInfo as B
WHERE B.iso = ? AND A.country = B._idx;"""
cur.execute(sql, (code,))
else:
sql = "SELECT count(_idx) FROM GeoNames;"
cur.execute(sql)
return int(cur.fetchone()[0])
def connectdb():
cnx = sqlite.connect('out/atlas.db', isolation_level=None)
cur = cnx.cursor()
return cur
def makeTimeZones(cur):
print('... making timezones table')
TimeZone.downloadFile()
cur.execute(tzschema)
TimeZone.insertDummy(cur)
zones = TimeZone.parseFile()
cur.execute('begin;')
for z in zones:
z.insert(cur)
cur.execute('end;')
def makeCountries(cur):
print('... making countries table')
CountryInfo.downloadFile()
cur.execute(ctyschema)
cties = CountryInfo.parseFile()
cur.execute('begin;')
for c in cties:
c.insert(cur)
cur.execute('end;')
def makeCountry(cur, ctycode):
print('... adding locations [%s]' % ctycode)
GeoName.downloadFile(ctycode)
loc = GeoName.parseFile(ctycode)
cur.execute('begin;')
for l in loc:
l.insert(cur)
cur.execute('end;')
def main():
os.chdir(_workdir)
if not os.path.exists('in'):
os.mkdir('in')
if not os.path.exists('out'):
os.mkdir('out')
if os.path.exists('out/atlas.db'):
print('error: file %s/out/atlas.db exists' % _workdir)
sys.exit(1)
cur = connectdb()
makeTimeZones(cur)
makeCountries(cur)
print('... making cities table')
cur.execute(citiesschema)
for code in allcodes:
makeCountry(cur, code)
#os.system('rm -f in/%s.txt' % code)
#tot = 0
#print('... counting:')
#for code in allcodes:
# cnt = GeoName.count(cur, code)
# print("++ [%s]: %d" % (code, cnt))
# tot += cnt
#print('# Total count = %d' % tot)
if __name__ == '__main__':
main()
# vi: set sw=4 ts=4 et