Twitter Geolocation – How to Convert Twitter Tweets into Points?

pointpythontwitter

I've been doing some research on using R to datamine twitter, but I haven't really found an answer or a decent tutorial for my question.

I'm interested in pulling tweets from twitter with a certain hashtag, within a certain timeframe, and plotting the location of those tweets on a map in either QGIS or ArcMap.

I know that tweets can have geolocation tied to them, but how do I extract this information in the first place?

Best Answer

I found a way using purely Python to get the coordinates for tweets using a word filter. It doesn't seem like many people include location with their tweets.

This might not be what you're after either because this is live streaming data. You can test it by putting a unique filter word and then tweeting that word from your Twitter account. You will see your tweet show up in Python almost instantly. This would be pretty cool to use for some huge event.

You'll need to install Tweepy.

pip install tweepy

And get a Twitter API Key.

Then you can use this script as a template:

import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

#Enter Twitter API Key information
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''

file = open("C:\\Output.csv", "w")
file.write("X,Y\n")

data_list = []
count = 0

class listener(StreamListener):

    def on_data(self, data):
        global count

        #How many tweets you want to find, could change to time based
        if count <= 2000:
            json_data = json.loads(data)

            coords = json_data["coordinates"]
            if coords is not None:
               print coords["coordinates"]
               lon = coords["coordinates"][0]
               lat = coords["coordinates"][1]

               data_list.append(json_data)

               file.write(str(lon) + ",")
               file.write(str(lat) + "\n")

               count += 1
            return True
        else:
            file.close()
            return False

    def on_error(self, status):
        print status

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
twitterStream = Stream(auth, listener())
#What you want to search for here
twitterStream.filter(track=["Halloween"])

Check out this documentation from Twitter too, it shows what you can put in the filter.

Here's the result of putting the filter as "Halloween" for a few minutes:

enter image description here

And for the hell of it, here's the first 2000 tweets that mentioned Halloween!

http://i.stack.imgur.com/bwdoP.png enter image description here

Happy Halloween!