-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcities.py
378 lines (354 loc) · 10.4 KB
/
cities.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
import os
import urllib2
import zipfile
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Text, Integer, Float
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from utils import is_number
ROOT = os.path.abspath(os.path.dirname(__file__))
path = lambda *x: os.path.normpath(os.path.join(ROOT, *x))
CITIES_BASE = 'cities5000'
CITIES_URL = 'http://download.geonames.org/export/dump/%s.zip' % CITIES_BASE
CITIES_ZIP_PATH = path('%s.zip' % CITIES_BASE)
CITIES_TXT_PATH = path('%s.txt' % CITIES_BASE)
CITIES_DB_PATH = path('%s.db' % CITIES_BASE)
CITIES_DB_URL = 'sqlite:///%s' % CITIES_DB_PATH
SCHEMA_VERSION = 5
# https://gist.github.com/alexex/4073388
COUNTRIES = dict([
('AF', u'Afghanistan'),
('AX', u'\xc5land Islands'),
('AL', u'Albania'),
('DZ', u'Algeria'),
('AS', u'American Samoa'),
('AD', u'Andorra'),
('AO', u'Angola'),
('AI', u'Anguilla'),
('AQ', u'Antarctica'),
('AG', u'Antigua and Barbuda'),
('AR', u'Argentina'),
('AM', u'Armenia'),
('AW', u'Aruba'),
('AU', u'Australia'),
('AT', u'Austria'),
('AZ', u'Azerbaijan'),
('BS', u'Bahamas'),
('BH', u'Bahrain'),
('BD', u'Bangladesh'),
('BB', u'Barbados'),
('BY', u'Belarus'),
('BE', u'Belgium'),
('BZ', u'Belize'),
('BJ', u'Benin'),
('BM', u'Bermuda'),
('BT', u'Bhutan'),
('BO', u'Bolivia, Plurinational State of'),
('BQ', u'Bonaire, Sint Eustatius and Saba'),
('BA', u'Bosnia and Herzegovina'),
('BW', u'Botswana'),
('BV', u'Bouvet Island'),
('BR', u'Brazil'),
('IO', u'British Indian Ocean Territory'),
('BN', u'Brunei Darussalam'),
('BG', u'Bulgaria'),
('BF', u'Burkina Faso'),
('BI', u'Burundi'),
('KH', u'Cambodia'),
('CM', u'Cameroon'),
('CA', u'Canada'),
('CV', u'Cape Verde'),
('KY', u'Cayman Islands'),
('CF', u'Central African Republic'),
('TD', u'Chad'),
('CL', u'Chile'),
('CN', u'China'),
('CX', u'Christmas Island'),
('CC', u'Cocos (Keeling Islands)'),
('CO', u'Colombia'),
('KM', u'Comoros'),
('CG', u'Congo'),
('CD', u'Congo, The Democratic Republic of the'),
('CK', u'Cook Islands'),
('CR', u'Costa Rica'),
('CI', u"C\xf4te D'ivoire"),
('HR', u'Croatia'),
('CU', u'Cuba'),
('CW', u'Cura\xe7ao'),
('CY', u'Cyprus'),
('CZ', u'Czech Republic'),
('DK', u'Denmark'),
('DJ', u'Djibouti'),
('DM', u'Dominica'),
('DO', u'Dominican Republic'),
('EC', u'Ecuador'),
('EG', u'Egypt'),
('SV', u'El Salvador'),
('GQ', u'Equatorial Guinea'),
('ER', u'Eritrea'),
('EE', u'Estonia'),
('ET', u'Ethiopia'),
('FK', u'Falkland Islands (Malvinas)'),
('FO', u'Faroe Islands'),
('FJ', u'Fiji'),
('FI', u'Finland'),
('FR', u'France'),
('GF', u'French Guiana'),
('PF', u'French Polynesia'),
('TF', u'French Southern Territories'),
('GA', u'Gabon'),
('GM', u'Gambia'),
('GE', u'Georgia'),
('DE', u'Germany'),
('GH', u'Ghana'),
('GI', u'Gibraltar'),
('GR', u'Greece'),
('GL', u'Greenland'),
('GD', u'Grenada'),
('GP', u'Guadeloupe'),
('GU', u'Guam'),
('GT', u'Guatemala'),
('GG', u'Guernsey'),
('GN', u'Guinea'),
('GW', u'Guinea-bissau'),
('GY', u'Guyana'),
('HT', u'Haiti'),
('HM', u'Heard Island and McDonald Islands'),
('VA', u'Holy See (Vatican City State)'),
('HN', u'Honduras'),
('HK', u'Hong Kong'),
('HU', u'Hungary'),
('IS', u'Iceland'),
('IN', u'India'),
('ID', u'Indonesia'),
('IR', u'Iran, Islamic Republic of'),
('IQ', u'Iraq'),
('IE', u'Ireland'),
('IM', u'Isle of Man'),
('IL', u'Israel'),
('IT', u'Italy'),
('JM', u'Jamaica'),
('JP', u'Japan'),
('JE', u'Jersey'),
('JO', u'Jordan'),
('KZ', u'Kazakhstan'),
('KE', u'Kenya'),
('KI', u'Kiribati'),
('KP', u"Korea, Democratic People's Republic of"),
('KR', u'Korea, Republic of'),
('KW', u'Kuwait'),
('KG', u'Kyrgyzstan'),
('LA', u"Lao People's Democratic Republic"),
('LV', u'Latvia'),
('LB', u'Lebanon'),
('LS', u'Lesotho'),
('LR', u'Liberia'),
('LY', u'Libya'),
('LI', u'Liechtenstein'),
('LT', u'Lithuania'),
('LU', u'Luxembourg'),
('MO', u'Macao'),
('MK', u'Macedonia, The Former Yugoslav Republic of'),
('MG', u'Madagascar'),
('MW', u'Malawi'),
('MY', u'Malaysia'),
('MV', u'Maldives'),
('ML', u'Mali'),
('MT', u'Malta'),
('MH', u'Marshall Islands'),
('MQ', u'Martinique'),
('MR', u'Mauritania'),
('MU', u'Mauritius'),
('YT', u'Mayotte'),
('MX', u'Mexico'),
('FM', u'Micronesia, Federated States of'),
('MD', u'Moldova, Republic of'),
('MC', u'Monaco'),
('MN', u'Mongolia'),
('ME', u'Montenegro'),
('MS', u'Montserrat'),
('MA', u'Morocco'),
('MZ', u'Mozambique'),
('MM', u'Myanmar'),
('NA', u'Namibia'),
('NR', u'Nauru'),
('NP', u'Nepal'),
('NL', u'Netherlands'),
('NC', u'New Caledonia'),
('NZ', u'New Zealand'),
('NI', u'Nicaragua'),
('NE', u'Niger'),
('NG', u'Nigeria'),
('NU', u'Niue'),
('NF', u'Norfolk Island'),
('MP', u'Northern Mariana Islands'),
('NO', u'Norway'),
('OM', u'Oman'),
('PK', u'Pakistan'),
('PW', u'Palau'),
('PS', u'Palestinian Territory, Occupied'),
('PA', u'Panama'),
('PG', u'Papua New Guinea'),
('PY', u'Paraguay'),
('PE', u'Peru'),
('PH', u'Philippines'),
('PN', u'Pitcairn'),
('PL', u'Poland'),
('PT', u'Portugal'),
('PR', u'Puerto Rico'),
('QA', u'Qatar'),
('RE', u'R\xe9union'),
('RO', u'Romania'),
('RU', u'Russian Federation'),
('RW', u'Rwanda'),
('BL', u'Saint Barth\xe9lemy'),
('SH', u'Saint Helena, Ascension and Tristan Da Cunha'),
('KN', u'Saint Kitts and Nevis'),
('LC', u'Saint Lucia'),
('MF', u'Saint Martin (French Part)'),
('PM', u'Saint Pierre and Miquelon'),
('VC', u'Saint Vincent and the Grenadines'),
('WS', u'Samoa'),
('SM', u'San Marino'),
('ST', u'Sao Tome and Principe'),
('SA', u'Saudi Arabia'),
('SN', u'Senegal'),
('RS', u'Serbia'),
('SC', u'Seychelles'),
('SL', u'Sierra Leone'),
('SG', u'Singapore'),
('SX', u'Sint Maarten (Dutch Part)'),
('SK', u'Slovakia'),
('SI', u'Slovenia'),
('SB', u'Solomon Islands'),
('SO', u'Somalia'),
('ZA', u'South Africa'),
('GS', u'South Georgia and the South Sandwich Islands'),
('SS', u'South Sudan'),
('ES', u'Spain'),
('LK', u'Sri Lanka'),
('SD', u'Sudan'),
('SR', u'Suriname'),
('SJ', u'Svalbard and Jan Mayen'),
('SZ', u'Swaziland'),
('SE', u'Sweden'),
('CH', u'Switzerland'),
('SY', u'Syrian Arab Republic'),
('TW', u'Taiwan, Province of China'),
('TJ', u'Tajikistan'),
('TZ', u'Tanzania, United Republic of'),
('TH', u'Thailand'),
('TL', u'Timor-leste'),
('TG', u'Togo'),
('TK', u'Tokelau'),
('TO', u'Tonga'),
('TT', u'Trinidad and Tobago'),
('TN', u'Tunisia'),
('TR', u'Turkey'),
('TM', u'Turkmenistan'),
('TC', u'Turks and Caicos Islands'),
('TV', u'Tuvalu'),
('UG', u'Uganda'),
('UA', u'Ukraine'),
('AE', u'United Arab Emirates'),
('GB', u'United Kingdom'),
('US', u'United States'),
('UM', u'United States Minor Outlying Islands'),
('UY', u'Uruguay'),
('UZ', u'Uzbekistan'),
('VU', u'Vanuatu'),
('VE', u'Venezuela, Bolivarian Republic of'),
('VN', u'Viet Nam'),
('VG', u'Virgin Islands, British'),
('VI', u'Virgin Islands, U.S.'),
('WF', u'Wallis and Futuna'),
('EH', u'Western Sahara'),
('YE', u'Yemen'),
('ZM', u'Zambia'),
('ZW', u'Zimbabwe')
])
Base = declarative_base()
class Meta(Base):
__tablename__ = 'Meta'
version = Column(Integer, nullable=False, primary_key=True)
class City(Base):
__tablename__ = 'City'
id = Column(Integer, nullable=False, primary_key=True)
name = Column(Text, nullable=False)
country = Column(Text, nullable=False)
state = Column(Text)
latitude = Column(Float, nullable=False)
longitude = Column(Float, nullable=False)
population = Column(Integer, nullable=False)
@property
def full_name(self):
country = self.country
if country in COUNTRIES: country = COUNTRIES[country]
if self.state:
return '%s, %s (%s)' % (self.name, self.state, country)
return '%s (%s)' % (self.name, country)
_engine = None
_Session = None
def get_engine():
global _engine
if _engine is None:
_engine = create_engine(CITIES_DB_URL)
return _engine
def get_session():
global _Session
if _Session is None:
_Session = sessionmaker(bind=get_engine())
return _Session()
def find(name):
return get_session().query(City).filter(City.name.like('%s%%' % name)).\
order_by(City.population.desc())
def destroy_db():
global _engine
global _Session
_engine = None
_Session = None
os.unlink(CITIES_DB_PATH)
def create_db():
if os.path.exists(CITIES_DB_PATH):
session = get_session()
try:
if session.query(Meta)[0].version != SCHEMA_VERSION:
raise Exception('schema mismatch, rebuild database')
return
except Exception, e:
session.close()
destroy_db()
print "Creating %s.db." % CITIES_BASE
get_cities_txt()
engine = get_engine()
Base.metadata.create_all(engine)
session = get_session()
for line in open(CITIES_TXT_PATH, 'r'):
parts = line.split('\t')
state = parts[10]
if is_number(state): state = None
city = City(
id=parts[0],
name=parts[1].decode('utf-8'),
latitude=parts[4],
longitude=parts[5],
country=parts[8],
state=state,
population=parts[14]
)
session.add(city)
session.add(Meta(version=SCHEMA_VERSION))
session.commit()
def get_cities_txt():
if os.path.exists(CITIES_TXT_PATH): return
get_cities_zip()
print "Extracting %s.txt" % CITIES_BASE
with zipfile.ZipFile(CITIES_ZIP_PATH) as zf:
zf.extract('%s.txt' % CITIES_BASE, path())
def get_cities_zip():
if os.path.exists(CITIES_ZIP_PATH): return
print "Downloading %s." % CITIES_URL
f = urllib2.urlopen(CITIES_URL)
open(CITIES_ZIP_PATH, 'wb').write(f.read())
if __name__ == '__main__':
create_db()