/
geotagging_irma.py
109 lines (67 loc) · 1.78 KB
/
geotagging_irma.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
#!/usr/bin/env python
# coding: utf-8
# In[19]:
import pandas as pd
import numpy as np
import json
import glob
import re
from collections import Counter
from geopy.geocoders import Nominatim
from geopy import geocoders
import csv
import codecs
import time
# In[4]:
fi = open('irma_oneday_06.csv', 'r')
data = fi.read()
fi.close()
fo = open('irma_oneday_06_new.csv', 'w')
fo.write(data.replace('\x00', ''))
fo.close()
# In[8]:
temp_country = []
csvReader = csv.reader(codecs.open('irma_oneday_06_new.csv', 'rU', 'utf-8'))
for i,row in enumerate(csvReader):
location = ""
if i>0:
if len(row) > 10:
temp_country.append(row[-1])
# In[11]:
unique_locations = set(temp_country)
# In[13]:
unique_list = list(unique_locations)
# In[15]:
country = dict()
# In[17]:
gn = Nominatim(timeout=30)
# In[36]:
for i,row in enumerate(unique_list):
try:
location = ""
temp_country = []
if unique_list[i] not in country.keys():
location = ""
location = unique_list[i]
if str(location) == '':
country[location] = 'None'
else:
loc = gn.geocode(location,language='en',timeout=30)
temp_country=str(loc).split(",")
#print(temp_country)
country[location] = temp_country[-1]
#country.append(temp_country[-1])
time.sleep(1)
print(i)
else:
print(str(i) + "Passed")
pass
except:
country[unique_list[i]] = 'None'
print("Exception" + str(i))
# In[38]:
with open('country_dict.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in country.items():
writer.writerow([key, value])
# In[ ]: