<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> import warnings <EOL> from geopy import Point <EOL> from geopy . distance import distance as geopy_distance <EOL> from . . location import EARTH <EOL> from . . resolver import AbstractResolver , register <EOL> @ register ( '<STR_LIT>' ) <EOL> class GeocodeResolver ( AbstractResolver ) : <EOL> """<STR_LIT>""" <EOL> cell_size = <NUM_LIT> <EOL> def __init__ ( self , max_distance = <NUM_LIT> ) : <EOL> self . max_distance = float ( max_distance ) <EOL> self . location_map = defaultdict ( list ) <EOL> def _cells_for ( self , latitude , longitude ) : <EOL> """<STR_LIT>""" <EOL> latitude = latitude * self . cell_size <EOL> longitude = longitude * self . cell_size <EOL> shift_size = self . cell_size / <NUM_LIT:2> <EOL> for latitude_cell in ( latitude - shift_size , <EOL> latitude , latitude + shift_size ) : <EOL> for longitude_cell in ( longitude - shift_size , <EOL> longitude , longitude + shift_size ) : <EOL> yield ( int ( latitude_cell / self . cell_size ) , <EOL> int ( longitude_cell / self . cell_size ) ) <EOL> def add_location ( self , location ) : <EOL> if not location . latitude and location . longitude : <EOL> return <EOL> for cell in self . _cells_for ( location . latitude , location . longitude ) : <EOL> self . location_map [ cell ] . append ( location ) <EOL> def resolve_tweet ( self , tweet ) : <EOL> tweet_coordinates = ( tweet . get ( '<STR_LIT>' ) or { } ) . get ( '<STR_LIT>' ) <EOL> if not tweet_coordinates : <EOL> return None <EOL> tweet_coordinates = Point ( longitude = tweet_coordinates [ <NUM_LIT:0> ] , <EOL> latitude = tweet_coordinates [ <NUM_LIT:1> ] ) <EOL> closest_candidate = None <EOL> closest_distance = float ( '<STR_LIT>' ) <EOL> for cell in self . _cells_for ( tweet_coordinates . latitude , <EOL> tweet_coordinates . longitude ) : <EOL> for candidate in self . location_map [ cell ] : <EOL> candidate_coordinates = Point ( <EOL> candidate . latitude , candidate . longitude ) <EOL> distance = geopy_distance ( <EOL> tweet_coordinates , candidate_coordinates ) . miles <EOL> if closest_distance > distance : <EOL> closest_candidate = candidate <EOL> closest_distance = distance <EOL> if closest_distance < self . max_distance : <EOL> return ( False , closest_candidate ) <EOL> return None </s>
