Mining Twitter Data with Python Part 7: Geolocation and Interactive Maps
The final part of this 7 part series explores using geolocation and interactive maps with Twitter data.
By Marco Bonzanini, Independent Data Science Consultant.
Geolocation is the process of identifying the geographic location of an object such as a mobile phone or a computer. Twitter allows its users to provide their location when they publish a tweet, in the form of latitude and longitude coordinates. With this information, we are ready to create some nice visualisation for our data, in the form of interactive maps.
GeoJSON is a format for encoding geographic data structures. The format supports a variety of geometric types that can be used to visualise the desired shapes onto a map. For our examples, we just need the simplest structure, a Point. A point is identified by its coordinates (latitude and longitude).
In GeoJSON, we can also represent objects such as a Feature or aFeatureCollection. The first one is basically a geometry with additional properties, while the second one is a list of features.
Our Twitter data set can be represented in GeoJSON as aFeatureCollection, where each tweet would be an individual Featurewith its one geometry (the aforementioned Point).
This is how the JSON structure looks like:
From Tweets to GeoJSON
Assuming the data are stored in a single file as described in the first chapter of this tutorial, we simply need to iterate all the tweets looking for the coordinates field, which may or may not be present. Keep in mind that you need to use coordinates, because the geo field is deprecated (seethe API).
This code will read the data set, looking for tweets where the coordinates are explicitely given. Once the GeoJSON data structure is created (in the form of a Python dictionary), then the data are dumped into a file called geo_data.json:
Interactive Maps with Leaflet.js
In order to prepare a web page that will host a map, you simply need to include the library and its CSS, by putting in the head section of your document the following lines:
Moreover, we have all our GeoJSON data in a separate file, so we want to load the data dynamically rather than manually put all the points in the map. For this purpose, we can easily play with jQuery, which we also need to include:
The map itself will be placed into a div element:
We’re now ready to create the map with Leaflet:
A screenshot of the results:
The above example uses OpenStreetMap for the tile images, but Leaflet lets you choose other services. For example, in the following screenshot the tiles are coming from MapBox.
You can see the interactive maps in action here:
Bio: Marco Bonzanini is a Data Scientist based in London, UK. Active in the PyData community, he enjoys working in text analytics and data mining applications. He's the author of "Mastering Social Media Mining with Python" (Packt Publishing, July 2016).
Original. Reposted with permission.
- Mining Twitter Data with Python Part 4: Rugby and Term Co-occurrences
- Mining Twitter Data with Python Part 5: Data Visualisation Basics
- Mining Twitter Data with Python Part 6: Sentiment Analysis Basics
- How to create an interactive 3D chart and share it easily with anyone
- Top KDnuggets tweets, Aug 26 - Sep 01: A realistic look at the time…
- Top KDnuggets tweets, Oct 14-20: An Introduction To Mathematics…
- Top KDnuggets tweets, Nov 06-12: 10 FREE must-read ebooks on AI.…
- Python, Selenium & Google for Geocoding Automation: Free and Paid
- Top KDnuggets tweets, Oct 09-15: #DeepLearning for Natural Language…