[GIS] How to geocode location using twittersearch for leaflet map of tweets

leafletpythontwitter

I'm using twittersearch to iterate over user input topics and location of interest that will then be visualized in a leaflet map. So using 'place' I can get a map, no problem. I'm trying to increase my odds of getting points by using 'location' in another for loop. Do I need to geocode the location data, if so what am I missing?

Geocode piece..

from geopy import geocoders

def geo(location):
    g = geocoders.Nominatim()
    loc = g.geocode(location)
    return loc.latitude, loc.longitude

original 'place' piece that produces results..

for tweet in ts.search_tweets_iterable(tso):
    tweetText = ( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode("ascii", "ignore")
    print tweetText
    if tweet['place'] is not None:
        (lat, lng) = geo(tweet['place']['full_name'])
        tw = tweet['text']
        user= tweet['user']['screen_name']
        #map has markers placed at the location of the tweet, the pop up text is the contenet of the the tweet and the poster/user
        #this is in the form of username said tweet content
        folium.Marker([lat, lng], popup= user + " said " + tw,icon=folium.Icon(color='red',icon='info-sign')).add_to(mapLeafletPython)
        print( '@%s tweeted: %s' % (user, tw) + 'And they said it from (' + str(lat) +', ' +str(lng)+')').encode("ascii", "ignore")

this is the location piece that produces this error…AttributeError: 'NoneType' object has no attribute 'latitude'
which occurs on the (lat1, lng1) line.

for tweet in ts.search_tweets_iterable(tso):
    tweetText = ( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode("ascii", "ignore")
    if tweet['user'] is not None:
        (lat1, lng1) = geo(tweet['user']['location'])
        tw = tweet['text']
        user= tweet['user']['screen_name']
        #map has markers placed at the location of the tweet, the pop up text is the contenet of the the tweet and the poster/user
        #this is in the form of username said tweet content
        folium.Marker([lat1, lng1], popup= user + " said " + tw,icon=folium.Icon(color='blue',icon='info-sign')).add_to(mapLeafletPython)
        print( '@%s tweeted: %s' % (user, tw) + 'And they said it from (' + str(lat) +', ' +str(lng)+')').encode("ascii", "ignore")

"tweetSearchUser" is the input from user as well as "where"

last piece that I think you need for testing besides variable ts which is the token..

tso = TwitterSearchOrder() # create a TwitterSearchOrder object
tso.set_keywords([tweetSearchUser]) # let's define all words we would like to have a look for
#tso.set_count(200) #limit return records to a number
tso.set_include_entities(False) # and don't give us all those entity information

Using this as a reference for twitter api and json format

http://socialmedia-class.org/twittertutorial.html

https://github.com/MichaelMartinez/blog/blob/master/content/iPython-Notebooks/Python-Twitter-Tools.md

I'm baffled here…

Updated with error…

Best Answer

The geopy geocoder will return a None object if a location can't be geocoded. For example:

>>> g = geocoders.Nominatim()
>>> loc = g.geocode("This place doesn't exist")
>>> type(loc)
NoneType

You'll need to take a look at the locations coming in from the user location, and probably through a try...except block around getting those results.

Related Question