g0blin Bringing together art & technology

28Jul/101

Twitter location filtering

During my daily visit to NewScientist, I came across an article detailing a researchers efforts to map the mood of the USA, based on data available through the Twitter Stream API. The method of determining the mood I will go into in another article - however for those who are interested you should look into Affective Norms for English Words - the aim of this article is to make available to us an easy to way to determine the bounding boxes for the locations filtering parameters required to filter the Twitter Stream by location, instead of keywords.

My goal was to stream Tweets from the whole of the UK, so first of all I needed to create a bounding box that would cover the UK in its entirety. This involved creating squares from two sets of Lng/Lat coordinates that could ecompass the UK. Now, this was rather easy, but it may be worth pointing out that Twitter use the reverse pairing order to what Google Maps do. Google Maps order their coordinate pairs Lat/Lng, Twitter, Lng/Lat.

Now that I had my bounding box, I plugged it into the Twitter Stream API. Unfortunately, unless you run a site that makes use of Tweets with a userbase in the thousands, Twitter restrict you to 10 bounding boxes of 1° by 1°, and as such I was unable to stream all of the Tweets coming from the UK using this method - bummer. I did however end up with a small piece of Javascript that works on the Google Maps API that allowed me to place these bounding boxes on a Map, and get the required location filtering string that I needed to pass to the Twitter Stream API.

Below is the example source code, and a link to a demo. To use it, simply click on the map. A 1° by 1° box will appear which you can then position on the area that you wish to monitor for Tweets. Click, and it will be locked to that position. The text box at the bottom of the screen will display the string that you must pass to the Twitter Stream API in order to limit Tweet results to the areas shown on the map. By default, bounding boxes snap to the nearest °, however you can turn off that option with the button in the top left. You can also clear the current coordinate set with another button in the top left.

In order to make use of the resulting string, paste it into a text file - lets call it 'locations'. The content should be as below.

locations=

Once you have done this, run the below command using your Twitter username/password and you will begin to see Tweets streaming in - assuming people are Tweeting in the area(s) you have specified!

curl -d @locations http://stream.twitter.com/1/statuses/filter.json -uAnyTwitterUser:Password.

And now, as promised the HTML/Javascript source code for the bounding box plotter and a link to the demo.

Demo

That's all for now - I'll be following this up with a few more articles on gathering information from Twitter which will include topics such as PHP, node.js and the Affective Norms for English Words.

Comments (1) Trackbacks (0)
  1. Hey Bud… just an FYI that you need HTTPS when making the CURl call 😉

    https://stream.twitter.com/1/statuses/filter.json


Leave a comment

No trackbacks yet.