-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLoc.py
More file actions
66 lines (43 loc) · 1.79 KB
/
GetLoc.py
File metadata and controls
66 lines (43 loc) · 1.79 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
import json
import fileinput
from geopy.geocoders import Nominatim
geolocator = Nominatim()
outfile = file("balti2.json","w")
listy = ['Maryland', 'District of Columbia']
def json_dump(file_obj,tweet):
twit = json.dumps(tweet)
file_obj.write(twit + '\n')
def local_tweets(tweet,file_obj,state_pri,state_sec,country):
global U_count
if country == "United States of America":
for word in listy:
if state_pri == word or state_sec == word:
print state_sec,state_pri,country
json_dump(file_obj,tweet)
U_count = U_count + 1
for line in fileinput.input("baltimore.json"):
T_count = T_count + 1
tweet = json.loads(line)
if tweet["coordinates"] is not None:
longitude = str(tweet["coordinates"]["coordinates"][0])
latitude = str(tweet["coordinates"]["coordinates"][1])
location_pair =latitude + " , " + longitude
L_count = L_count + 1
try:
address, (latitude, longitude) = geolocator.reverse(location_pair, language='en')
address = address.split(",")
local_tweets(tweet,outfile,address[-2].lstrip(),address[-3].lstrip(),address[-1].lstrip())
except:
pass
elif tweet["user"]["location"]:
L_count = L_count + 1
try:
address, (latitude, longitude) = geolocator.geocode(tweet["user"]["location"],timeout=None)
address = address.split(",")
local_tweets(tweet,outfile,address[-2].lstrip(),address[-3].lstrip(),address[-1].lstrip())
except:
pass
else:
N_count = N_count + 1
json_dump(outfile,tweet)
fileinput.close()