def get_geocodes(tweet_list): """Get the latitude and longitude for each tweet's location. Returns the number of tweets with invalid location data.""" print('Getting coordinates for tweet locations...') geo = OpenMapQuest(api_key=keys.mapquest_key) # geocoder bad_locations = 0 for tweet in tweet_list: processed = False delay = .1 # used if OpenMapQuest times out to delay next call while not processed: try: # get coordinates for tweet['location'] geo_location = geo.geocode(tweet['location']) processed = True except: # timed out, so wait before trying again print('OpenMapQuest service timed out. Waiting.') time.sleep(delay) delay += .1 if geo_location: tweet['latitude'] = geo_location.latitude tweet['longitude'] = geo_location.longitude else: bad_locations += 1 # tweet['location'] was invalid print('Done geocoding') return bad_locations
def get_geocodes(tweet_list): """Get the latitude and longitude for each tweet's location. Returns the number of tweets with invalid location data.""" print('Getting coordinates for tweet locations...') geo = OpenMapQuest(api_key=key.mapquest_key) # geocoder bad_locations = 0 for tweet in tweet_list: processed = False delay = .1 # used if OpenMapQuest times out to delay next call while not processed: try: # get coordinates for tweet['location'] geo_location = geo.geocode(tweet['location']) processed = True except: # timed out, so wait before trying again print('OpenMapQuest service timed out. Waiting.') time.sleep(delay) delay += .1 if geo_location: tweet['latitude'] = geo_location.latitude tweet['longitude'] = geo_location.longitude else: bad_locations += 1 # tweet['location'] was invalid print('Done geocoding') return bad_locations ########################################################################## # (C) Copyright 2019 by Deitel & Associates, Inc. and # # Pearson Education, Inc. All Rights Reserved. # # # # DISCLAIMER: The authors and publisher of this book have used their # # best efforts in preparing the book. These efforts include the # # development, research, and testing of the theories and programs # # to determine their effectiveness. The authors and publisher make # # no warranty of any kind, expressed or implied, with regard to these # # programs or to the documentation contained in these books. The authors # # and publisher shall not be liable in any event for incidental or # # consequential damages in connection with, or arising out of, the # # furnishing, performance, or use of these programs. # ##########################################################################
def GetGeocodes(ListOfTweets): """ A Function to customize the list of dictionaries which represents tweets, and add in every dictionary the GeoLocation.latitude and the GeoLocation.longitude attributes for the location of the tweet. """ print("Getting Coords...") # Connect mapquest with my personal developer API key. Geo = OpenMapQuest(api_key=Keys.MapAPIKey) # number of tweets without locations. BadLocations = 0 # traverse the tweet list. for Tweet in ListOfTweets: ProcessedFlag = False Delay = .1 GeoLocation = "" # try to see if a tweet is processed and handle a connection failure or timeout(if we trigger one). while not ProcessedFlag: try: GeoLocation = Geo.geocode(Tweet["Location"]) ProcessedFlag = True except: print("Time out, trying again") time.sleep(Delay) Delay += .1 # Add {"Latitude" : GeoLocation.latitude} and {"Longitude" : GeoLocation.longitude} # to our Tweet Dictionary. if GeoLocation: Tweet["Latitude"] = GeoLocation.latitude Tweet["Longitude"] = GeoLocation.longitude else: BadLocations += 1 print("GeoCoding Done") # return the number of bad locations. return BadLocations
def get_geocodes(tweet_list): """Get the latitude and longitude for each tweet's location. Returns the number of tweets with invalid location data.""" print('Getting tweet locations...') geo = OpenMapQuest(api_key=mykeys.mapquest_key) invalid = 0 for tweet in tweet_list: processed = False delay = .1 while not processed: try: geo_location = geo.geocode(tweet['location']) processed = True except: print('OpenMapQuest service timed out, trying again...') time.sleep(delay) delay += .1 if geo_location: tweet['latitude'] = geo_location.latitude tweet['longitude'] = geo_location.longitude else: invalid += 1 # tweet['location'] was invalid print('Geocoding successful') return invalid
# Section 15.2 Self Check snippets # Exercise 1 import keys from geopy import OpenMapQuest geo = OpenMapQuest(api_key=keys.mapquest_key) geo.geocode('Chicago, IL') ########################################################################## # (C) Copyright 2019 by Deitel & Associates, Inc. and # # Pearson Education, Inc. All Rights Reserved. # # # # DISCLAIMER: The authors and publisher of this book have used their # # best efforts in preparing the book. These efforts include the # # development, research, and testing of the theories and programs # # to determine their effectiveness. The authors and publisher make # # no warranty of any kind, expressed or implied, with regard to these # # programs or to the documentation contained in these books. The authors # # and publisher shall not be liable in any event for incidental or # # consequential damages in connection with, or arising out of, the # # furnishing, performance, or use of these programs. # ##########################################################################
from state_codes import state_codes geo = OpenMapQuest(api_key=keys.mapquest_key) states = tweet_counts_df.State.unique() states.sort() locations = [] for state in states: processed = False delay = .1 while not processed: try: locations.append(geo.geocode(state_codes[state] + ', USA')) print(locations[-1]) processed = True except: # timed out, so wait before trying again print('OpenMapQuest service timed out. Waiting.') time.sleep(delay) delay += .1 # Grouping the Tweet Counts by State tweets_counts_by_state = tweet_counts_df.groupby('State', as_index=False).sum() tweets_counts_by_state.head() # Creating the Map import folium