-
Notifications
You must be signed in to change notification settings - Fork 5
/
twitter_geojson.py
43 lines (39 loc) · 1.43 KB
/
twitter_geojson.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
#read dataset of tweets in jsonl file and produce a geoson file associted with geographical data
import json
from argparse import ArgumentParser
def get_parser():
parser = ArgumentParser()
parser.add_argument('--tweets')
parser.add_argument('--geojson')
return parser
if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()
#Read tweet collection and build geo data structure.
with open(args.tweets, 'r') as f: #read dataset of tweets
geo_data = {
'type' : "FeatureCollection",
'features' : []
}
for line in f:
tweet = json.loads(line)
try:
if tweet['coordinates']:
geo_json_feature = {
"type" : "Feature",
"geometry": {
"type" : "Point",
"coordinates" : tweet['coordinates']['coordinates']
},
"properties": {
"text" : tweet['text'],
"created_at" : tweet['created_at']
}
}
geo_data['features'].append(geo_json_feature)
except KeyError:
#Skip if json doc is not a tweet (errors, etc)
continue
#Save geo data
with open(args.geojson, 'w') as fout:
fout.write(json.dumps(geo_data, indent=4))