- For our last example, we're going to introduce
a combination of geocoding and mapping
and use that information to help us take
a live tweet stream and plot the resulting
tweets that we get on an interactive map.
And as you'll see, the tweets will be
plotted as map markers which the user
of the map can click on to pop open the map marker
and to look at the sender of the tweet
and the tweet's text as well.
So, we'll once again be collecting
streaming tweets using the same techniques
that we used in the last two examples.
Now, as far as location information in tweets is concerned
by default, for privacy reasons, twitter disables
precise location information that would give
latitude and longitude coordinates
from which a tweet was sent.
If you want, as a user of twitter, you can opt in
to allowing twitter to track your locations
and some people do choose to do that.
However, even though most tweets don't
contain precise location information
they do typically include the user's home location
however, that may be a fake location
or a fictitious location, it may be invalid
depending on what they typed in
when they set up their user account.
So, what we're going to do is grab those locations
the user's home locations, and we're going
to take those and translate them into
actual latitudes and longitudes if we can
for any valid locations
and then we'll use those to plot our map markers.
Now to do part of this work, we're going to be
taking advantage of the geopy library
which we had you install in early on in this lesson
at the same time that you were installing Tweepy
and in the geopy library, it provides geocoding
capabilities which is the ability to
take a location and get the corresponding
latitude and longitude in the world.
Geopy has support for dozens of different
geocoding services, many of those geocoding services
have either free tiers or lite tiers
with rate limits on them
and for our purposes, we chose to use
the OpenMapQuest geocoding service via the geopy library.
Now, as you'll see, we're going to be able to
take a location such as Boston, MA
turn it into a latitude and longitude
which are numeric values that we will use
for plotting on the maps.
The OpenMapQuest geocoding API gives you the ability
to make up to 15 thousand transactions per month
on their free tier, and you can sign up
for OpenMapQuest at this location.
So, once you go to that website
in the upper right corner of the page at the moment, anyway
is where you'll see the sign up link
so you'll wanna go ahead and sign up
and you'll have to fill out your information
and once you're signed up, it will log you in
and it will take you to this web page
and If it doesn't you can enter
this web page on your own and on that web page
you'll be able to create new keys for accessing this API.
So I'm gonna go ahead and pull up my logged in account
and you can see I've already created
a DeitelTest app, a similar concept to
what we did with creating a Twitter app
I won't click on this because then it will
reveal my private key, but what you're going to
do here is create a new key
and so I'll pull up that page briefly
all you need to specify is the app name
we don't use this concept of a callback URL
in any of our apps.
So, you'll specify the name and click
create app, once you do that, you'll be taken
to a page where you can manage
the app's keys and in our case
I could click on this to show you what that looks like
but, basically, what you're going to need to do
is grab the consumer key value
and you're going to replace in our keys.py file
the YourKeyHere value for the mapquest_key variable.
So you'll wanna go ahead and open keys.py
like you did earlier today or earlier
whenever you were watching the videos
and you'll replace your key in the keys.py file
or you'll put your key in the keys.py file
make sure you save the file before you import it
into the IPython session or into any code that requires it.
Now, in addition, we're going to be using another
library which you have not installed yet.
It's the Folium library, let me pull up its
GitHub page here, so on this page
you'll find the GitHub information
and installation instructions
but basically the folium library is
a python library that takes advantage of Leaflet.js
which is a JavaScript mapping library
for creating maps in the context of webpages
so we'll be using this to create our maps.
As you'll see we will output an HTML file
that actually contains the mapping code
and when you open that HTML file in your web browser
you'll be presented with the interactive
map that we're going to create.
So, before you execute any of the code in this example
you will want to make sure that you install
the folium library and it will also install
as part of that, the support for Leaflet.js as well.
Now, Leaflet.js uses open source maps by default
specifically, it uses maps from the OpenStreetMap.org
website, and they do require us to display
the copyright notice that you see here on the screen
so all of the maps are copyright to the contributors
that created those maps so, we're just making it-
making you - excuse me
aware of the fact that these maps are under copyright
so please be careful when using
them in your own applications.
So with that said, the next thing that
we're going to do in the next video
is start the setup for creating this application
that's going to stream a bunch of tweets
and translate their locations into latitudes
and longitudes so that we can plot them on a map.