-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet2geoJSON.py
109 lines (92 loc) · 3.46 KB
/
tweet2geoJSON.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
SCHEME:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.09,37.64 ]
},
"properties": {
"name": "@BrianLehman",
"tweet_body": "I freaking love maps!",
}
}
http://geojson.org/geojson-spec.html#bounding-boxes
"""
def place_lookup(tweet):
#TODO https://twittercommunity.com/t/schema-of-boundingbox-in-places-section/8663
boundingBox = tweet["place"]["bounding_box"]["coordinates"][0]
lat = float((boundingBox[0][0] + boundingBox[1][0]) / 2.0)
lng = float((boundingBox[1][1] + boundingBox[2][1]) / 2.0)
return [lat, lng]
def format2geoJSON(tweet):
try:
if tweet["coordinates"] != None:
# get lat,lng and create geoJSON object:
tweet_geoJSON = {"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": tweet["coordinates"]["coordinates"], #[lat,lng]
},
"properties": {
"name": tweet["user"]["screen_name"],
"user_description": tweet["user"]["description"],
"place": tweet["place"],
"default_profile": tweet["user"]["default_profile"],
"followers_count": tweet["user"]["followers_count"],
"verified": tweet["user"]["verified"],
"lang": tweet["user"]["lang"],
"tweet_body": tweet["text"],
"time": tweet["created_at"],
"favorite_count": tweet["favorite_count"],
"retweeted": tweet["retweeted"],
"in_reply_to_user_id_str": tweet["in_reply_to_user_id_str"],
"in_reply_to_status_id_str": tweet["in_reply_to_status_id_str"],
"possibly_sensitive": tweet["possibly_sensitive"],
"hashtags": tweet["entities"]["hashtags"],
"symbols": tweet["entities"]["symbols"],
"user_mentions": tweet["entities"]["user_mentions"],
"urls": tweet["entities"]["urls"],
"img": tweet["user"]["profile_image_url"]
}
}
return tweet_geoJSON
elif tweet["place"] != None:
# convert place to lat,lng and create geoJSON object:
coords = place_lookup(tweet)
tweet_geoJSON = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": coords,
"img": tweet["user"]["profile_image_url"]
},
"properties": {
"name": tweet["user"]["screen_name"],
"user_description": tweet["user"]["description"],
"place": tweet["place"],
"default_profile": tweet["user"]["default_profile"],
"followers_count": tweet["user"]["followers_count"],
"verified": tweet["user"]["verified"],
"lang": tweet["user"]["lang"],
"tweet_body": tweet["text"],
"time": tweet["created_at"],
"favorite_count": tweet["favorite_count"],
"retweeted": tweet["retweeted"],
"in_reply_to_user_id_str": tweet["in_reply_to_user_id_str"],
"in_reply_to_status_id_str": tweet["in_reply_to_status_id_str"],
"possibly_sensitive": tweet["possibly_sensitive"],
"hashtags": tweet["entities"]["hashtags"],
"symbols": tweet["entities"]["symbols"],
"user_mentions": tweet["entities"]["user_mentions"],
"urls": tweet["entities"]["urls"],
}
}
return tweet_geoJSON
else:
# TODO Add a search in the text to look for mentions of locations!
# text = tweet["text"]
return None
except: return None