-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocode_erasmus.py
More file actions
553 lines (409 loc) · 16.2 KB
/
geocode_erasmus.py
File metadata and controls
553 lines (409 loc) · 16.2 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 11:46:26 2023
# INFO
Run the script by opening a terminal in the directory of the script and typing:
python geocode_erasmus.py -i /path/to/pickle/ -o /path/to/output/
"""
import pandas as pd
from geopy.geocoders import Photon, Nominatim
from geopy.extra.rate_limiter import RateLimiter
import geopandas as gpd
import time
import random
import argparse
import dictionaries
# initialize argument parser
ap = argparse.ArgumentParser()
# set up arguments
ap.add_argument("-i", "--input", required=True,
help="Path to the Erasmus+ mobility pickled dataframe.")
ap.add_argument("-o", "--output", required=True,
help="Path to directory where resulting CSV and pickled dataframe are saved.")
# parse arguments
args = vars(ap.parse_args())
# symbol dictionary
sym_dict = {' - ': ', ',
' / ': ', ',
'/': ', ',
' | ': ', ',
'\t':''}
# read pickle in
df = pd.read_pickle(args["input"])
# problem list
prob_cols = ['Sending Country', 'Sending City', 'Sending Organization',
'Receiving Country', 'Receiving City', 'Receiving Organization']
probs = []
# list of placenames to switch for institution names
problist = ['Google', 'CheckURF', 'Check the PIC in URF For City',
'?', '??', '-', 'Desconocido', 'desconocido',
'DESCONOCIDO', 'XXXXX', 'xxxxx', 'XXX', 'xxx', 'Undefined',
'Unknown', 'See Era Id', 'Enschede', 'Nepcity',
'Dkzz', 'Otros', 'Oth', 'Nocity', 'nocity', 'NoCity', 'NOCITY']
# counters for problems
probsend = []
probrece = []
# set up organization dictionary
org_add = dictionaries.org_add
# check if sending or receiving city is "???" and replace with organization
print('[INFO] - Fixing problematic city names with institution names...')
for i, row in df.iterrows():
# force values to string
row['Sending City'] = str(row['Sending City'])
row['Receiving City'] = str(row['Receiving City'])
# check for question marks in sending
if ('???' in row['Sending City']) or (row['Sending City'].title() in problist):
probs.append(row[prob_cols])
probsend.append(row)
try:
df.at[i, 'Sending City'] = org_add[row['Sending Organization'].title()]
except:
df.at[i, 'Sending City'] = row['Sending Organization'].title()
# check for question marks in receiving city
if ('???' in row['Receiving City']) or (row['Receiving City'].title() in problist):
probs.append(row[prob_cols])
probrece.append(row)
try:
df.at[i, 'Receiving City'] = org_add[row['Receiving Organization'].title()]
except:
df.at[i, 'Receiving City'] = row['Receiving Organization'].title()
# Harmonize city name capitalization
df['Sending City'] = df['Sending City'].apply(lambda x: str(x).title())
df['Receiving City'] = df['Receiving City'].apply(lambda x: str(x).title())
# set up umlaut dictionary
umlaut_dict = dictionaries.umlaut_dict
# replace the weird names
for old, new in umlaut_dict.items():
# replace
df['Sending City'] = df['Sending City'].str.replace(old, new, regex=False)
df['Receiving City'] = df['Receiving City'].str.replace(old, new, regex=False)
# fix weird symbol use
for old, new in sym_dict.items():
# replace
df['Sending City'] = df['Sending City'].str.replace(old, new, regex=False)
df['Receiving City'] = df['Receiving City'].str.replace(old, new, regex=False)
# Clean country
print('[INFO] - Cleaning up columns...')
df['o_country'] = df['Sending Country'].apply(lambda x: x.split(' - ')[1])
df['d_country'] = df['Receiving Country'].apply(lambda x: x.split(' - ')[1])
# strip leading and trailing whitespace
df['Sending City'] = df['Sending City'].replace(r"^ +| +$", r"", regex=True)
df['Receiving City'] = df['Receiving City'].replace(r"^ +| +$", r"", regex=True)
# Get a geocodable location
df['origin'] = df['Sending City'] + ', ' + df['o_country']
df['destination'] = df['Receiving City'] + ', ' + df['d_country']
# set up place dictionary
placedict = dictionaries.placedict
# loop over data
for i, row in df.iterrows():
# drop remaining question marks
df.at[i, 'origin'] = row['origin'].replace('?', '')
df.at[i, 'destination'] = row['destination'].replace('?', '')
# drop placenames with "Cedex" as it refers to french nomenclature for postal code area
df.at[i, 'origin'] = row['origin'].replace('Cedex', '')
df.at[i, 'destination'] = row['destination'].replace('Cedex', '')
# replace some place names with geocodable place names
try:
df.at[i, 'origin'] = placedict[row['origin']]
except:
pass
try:
df.at[i, 'destination'] = placedict[row['destination']]
except:
pass
# save dataframe so geocoded locations can be eventually connected to the mobility
print('[INFO] - Saving mobility data before geocoding...')
df.to_pickle(args['output'] + 'erasmus_fixed_toponyms_pre-geocoding.pkl')
# get unique origins and destinations
origins = df['origin'].value_counts().reset_index()
destinations = df['destination'].value_counts().reset_index()
# rename column
destinations = destinations.rename(columns={'destination': 'origin'})
# concatenate into a dataframe
places = pd.concat([origins, destinations], ignore_index=True)
# group by and sum
places = places.groupby(['origin'])['count'].sum(
).reset_index(
).sort_values(by=['count'], ascending=False).reset_index(drop=True)
# drop duplicates
places = places.drop_duplicates(subset='origin')
# add to location list
loclist = []
loclist.append(places)
# concatenate location list to a proper dataframe
locations = pd.concat(loclist, ignore_index=True)
probs = pd.concat(probs, ignore_index=True)
print('[INFO] - Gathering locations is done!')
# sort by counts
locations = locations.sort_values(['count'], ascending=False)
# drop duplicates
locations = locations.drop_duplicates(subset=['origin']).reset_index(drop=True)
# save locations
print('[INFO] - Saving unique toponyms before geocoding...')
locations.to_pickle(args['output'] + 'erasmus_pre-geocoded_locations_only.pkl')
locations.to_csv(args['output'] + 'erasmus_pre-geocoded_locations_only.csv',
encoding='utf-8', sep=';')
# initialize geocoder
geolocator = Photon(user_agent='ErasmusGeocoder')
# set up rate limiter to not abuse the free API
geocoder = RateLimiter(geolocator.geocode, min_delay_seconds=1)
# get initial value for iterations
n_iter = 1
print('[INFO] - Starting to geocode with Photon...')
# keep tabs on unsuccesfull geocodes
unsuc = []
# geocode
for i, row in locations.iterrows():
if n_iter != 20:
# geocode location
geocoded = geocoder(row['origin'], timeout=120)
try:
# get geocoded address
locations.at[i, 'gc_address'] = geocoded.address
# save latitude and longitude
locations.at[i, 'y'] = geocoded.latitude
locations.at[i, 'x'] = geocoded.longitude
# update
n_iter += 1
except:
# append list
unsuc.append(row)
# fill in empty
locations.at[i, 'gc_address'] = 'UNSUCCESSFUL'
# save latitude and longitude
locations.at[i, 'y'] = None
locations.at[i, 'x'] = None
# update
n_iter += 1
elif n_iter == 20:
# geocode location
geocoded = geolocator.geocode(row['origin'], timeout=120)
try:
# get geocoded address
locations.at[i, 'gc_address'] = geocoded.address
# save latitude and longitude
locations.at[i, 'y'] = geocoded.latitude
locations.at[i, 'x'] = geocoded.longitude
# update
n_iter += 1
except:
# append list
unsuc.append(row)
# fill in empty
locations.at[i, 'gc_address'] = 'UNSUCCESSFUL'
# save latitude and longitude
locations.at[i, 'y'] = None
locations.at[i, 'x'] = None
# update
n_iter += 1
# 1-indexed iterator
current_i = i + 1
# print progress
print('[INFO] - Geocoding progress ' +
str(current_i) + '/' + str(len(locations)))
# save progress
locations.to_pickle(args['output'] +
'geocoded_locations_intermediate.pkl')
# wait
time.sleep(random.randint(40, 60))
# reset n_iter
n_iter = 1
# save locations
locations.to_pickle(args['output'] + 'geocoded_locations_Photon_first_run.pkl')
# get locations with unsuccessful and successful geocoding
unloc = locations[locations['gc_address'].isin(['UNSUCCESSFUL',
'nan'])
].reset_index(drop=True)
success = locations[~locations['gc_address'].isin(['UNSUCCESSFUL',
'nan'])
].reset_index(drop=True)
# duplicate origins for unlocs for fixing
unloc['origin2'] = unloc['origin']
# set up second unmlaut dictionary
udict = dictionaries.udict
# fix place names for mobilities with 2 or more flows
for old, new in udict.items():
# replace
unloc['origin2'] = unloc['origin2'].str.replace(old, new, regex=False)
# get initial value for iterations
n_iter = 1
print('[INFO] - Starting to geocode manually fixed toponyms...')
# keep tabs on unsuccesfull geocodes
unsuc = []
# geocode
for i, row in unloc.iterrows():
if n_iter != 25:
# geocode location
geocoded = geolocator.geocode(row['origin2'], timeout=120)
try:
# get geocoded address
unloc.at[i, 'gc_address'] = geocoded.address
# save latitude and longitude
unloc.at[i, 'y'] = geocoded.latitude
unloc.at[i, 'x'] = geocoded.longitude
# update
n_iter += 1
except:
# append list
unsuc.append(row)
# fill in empty
unloc.at[i, 'gc_address'] = 'UNSUCCESSFUL'
# save latitude and longitude
unloc.at[i, 'y'] = None
unloc.at[i, 'x'] = None
# update
n_iter += 1
elif n_iter == 25:
# geocode location
geocoded = geolocator.geocode(row['origin2'], timeout=120)
try:
# get geocoded address
unloc.at[i, 'gc_address'] = geocoded.address
# save latitude and longitude
unloc.at[i, 'y'] = geocoded.latitude
unloc.at[i, 'x'] = geocoded.longitude
# pdate
n_iter += 1
except:
# append list
unsuc.append(row)
# fill in empty
unloc.at[i, 'gc_address'] = 'UNSUCCESSFUL'
# save latitude and longitude
unloc.at[i, 'y'] = None
unloc.at[i, 'x'] = None
# update
n_iter += 1
# 1-indexed iterator
current_i = i + 1
# print progress
print('[INFO] - Geocoding progress ' +
str(current_i) + '/' + str(len(unloc)))
# save progress
unloc.to_pickle(args['output'] +
'geocoded_fixed_locations_intermediate.pkl')
# wait
time.sleep(random.randint(40, 60))
# reset n_iter
n_iter = 1
# drop origin2
unloc_res = unloc[['origin', 'count', 'gc_address', 'y', 'x']]
# save better fixed locations
unloc_res.to_pickle(args['output'] + 'geocoded_fixed_locations_second_run.pkl')
# separate unsuccesfuls
unloc_res_u = unloc_res[unloc_res['x'].isna()]
unloc_res_s = unloc_res[~unloc_res['x'].isna()]
# read in manual corrections
mancor = pd.read_excel("data/Unsuccessfuls_geocoded_ALL_DONE.xlsx",
converters={'origin': str,
'count': int,
'gc_address': str,
'y': float,
'x': float})
# simplify data
mancor = mancor[['origin', 'count', 'gc_address', 'y', 'x']]
# update unsuccesfully corrected geocodes with manually checked geocodes
up_unsuc = pd.merge(unloc_res_u[['origin']], mancor, on='origin', how='left')
up_unsuc = up_unsuc[['origin', 'count', 'gc_address', 'y', 'x']]
# get error rows
up_unsuc_errors = up_unsuc[up_unsuc['origin2'].isna()]
# initialize geocoder
geolocator = Nominatim(user_agent='ErasmusGeocoding')
# set up rate limiter
geocoder = RateLimiter(geolocator.geocode, min_delay_seconds=1)
# duplicate origins for unlocs for fixing
up_unsuc_errors['origin2'] = up_unsuc_errors['origin']
# set up final corrections dictionary
fincor = dictionaries.fincor
# fix place names for mobilities with 2 or more flows
for old, new in fincor.items():
# replace
up_unsuc_errors['origin2'] = up_unsuc_errors['origin2'].str.replace(old,
new,
regex=False)
# get initial value for iterations
n_iter = 1
print('[INFO] - Starting to geocode with Nominatim to finalize the data...')
# keep tabs on unsuccesfull geocodes
unsuc2 = []
# geocode
for i, row in up_unsuc_errors.iterrows():
if n_iter != 25:
# geocode location
geocoded = geolocator.geocode(row['origin2'], timeout=120)
try:
# get geocoded address
up_unsuc_errors.at[i, 'gc_address'] = geocoded.address
# save latitude and longitude
up_unsuc_errors.at[i, 'y'] = geocoded.latitude
up_unsuc_errors.at[i, 'x'] = geocoded.longitude
# update
n_iter += 1
except:
# append list
unsuc2.append(row)
# fill in empty
up_unsuc_errors.at[i, 'gc_address'] = 'UNSUCCESSFUL'
# save latitude and longitude
up_unsuc_errors.at[i, 'y'] = None
up_unsuc_errors.at[i, 'x'] = None
# update
n_iter += 1
elif n_iter == 25:
# geocode location
geocoded = geolocator.geocode(row['origin2'], timeout=120)
try:
# get geocoded address
up_unsuc_errors.at[i, 'gc_address'] = geocoded.address
# save latitude and longitude
up_unsuc_errors.at[i, 'y'] = geocoded.latitude
up_unsuc_errors.at[i, 'x'] = geocoded.longitude
# update
n_iter += 1
except:
# append list
unsuc2.append(row)
# fill in empty
up_unsuc_errors.at[i, 'gc_address'] = 'UNSUCCESSFUL'
# save latitude and longitude
up_unsuc_errors.at[i, 'y'] = None
up_unsuc_errors.at[i, 'x'] = None
# update
n_iter += 1
# 1-indexed iterator
current_i = i + 1
# print progress
print('[INFO] - Geocoding progress ' + str(current_i) +
'/' + str(len(up_unsuc_errors)))
# save progress
up_unsuc_errors.to_pickle(args['output'] +
'geocoded_fixed_nominatims_locations_intermediate.pkl')
# wait
time.sleep(random.randint(40, 60))
# reset n_iter
n_iter = 1
# columns to use in result
rescols = ['origin', 'count', 'gc_address', 'y', 'x']
# join together
result = pd.concat([success[rescols], unloc_res_s[rescols],
up_unsuc_errors[rescols]], ignore_index=True).sort_values(
['count'], ascending=False).reset_index(drop=True)
# save
print("[INFO] - Saving final results...")
result.to_pickle(args['output'] + 'geocoded_erasmus_placenames_final.pkl')
# drop unsuccessful results
result = result.dropna(subset=['x']).reset_index(drop=True)
# force datatypes to float for x and y
result['x'] = pd.to_numeric(result['x'])
result['y'] = pd.to_numeric(result['y'])
# convert to geodataframe
print('[INFO] - Turning geocoded locations into a GeoDataFrame..')
result = gpd.GeoDataFrame(result,
geometry=gpd.points_from_xy(result['x'],
result['y']),
crs='EPSG:4326')
result.to_file(args['output'] +
'geocoded_erasmus_placenames_finla.gpkg', driver='GPKG')
print('[INFO] - Saved result to geopackage..')
print("[INFO] - ... done!")