def filtrate(lng, lat): """ Filters the earthquakes data and returns the 5 records that are closest to a given pair of geocoordinates What it expects: ---------------- lng (number) - a longitude coordinate lat (number) - a latitude coordinate That WRANGLED_DATA_FILENAME points to a valid CSV file What it does: ------------- - Reads the file at WRANGLED_DATA_FILENAME and parses it as CSV, i.e. into a list of dictionaries - Creates new list of dicts, in which the records are sorted by the haversine function, which calculates the distance between the provided lng,lat and the 'longitude', 'latitude' of each record What it returns: ---------------- A list of 5 dictionaries, each dictionary representing one of the 5 closest earthquakes """ # put lng and lat into a tuple the_pt = (lng, lat) # Read the data file # being suuuuper verbose here in reading the file # and deserializing the CSV...just in case you forgot the # step by step procedure... # feel free to just do: # # with open(WRANGLED_DATA_FILENAME, 'r') as f: # quakes = list(csv.DictReader(f.read().splitlines())) f = open(WRANGLED_DATA_FILENAME, 'r') txt = f.read() f.close() lines = txt.splitlines() csvthingy = csv.DictReader(lines) quakes = list(csvthingy) # I...hate...how Python does lambdas.... lambdakey = lambda q: haversine( the_pt, (float(q['longitude']), float(q['latitude']))) nearest_quakes = sorted(quakes, key=lambdakey) return nearest_quakes[0:5]
def publish(location): """ What it expects: ---------------- `location` is a string, representing some kind of human-readable geographical location `published_path` is a string representing a URL or filepath that the webbrowser can open. What it does: ------------- Geocodes the location string. Then uses filtrate() to get the most relevant earthquakes Then produces HTML data that displays that data What it returns: ---------------- A giant HTML string that can be written to file and opened as a webpage. """ user_location = geocode(location) quakes = filtrate(user_location['longitude'], user_location['latitude']) #### let's make a big string of HTML giant_html_string = "" ##### Make the top of the page ##### Let's make a map, starting with the user gmarkers = [] user_latlngstr = str(user_location['latitude']) + ',' + str(user_location['longitude']) user_marker = google_map_marker(user_latlngstr, color="0x00FF44", label='U') gmarkers.append(user_marker) for xid, quake in enumerate(quakes): latlngstr = str(quake['latitude']) + ',' + str(quake['longitude']) marker = google_map_marker(latlngstr, color="red", label=str(xid + 1)) gmarkers.append(marker) bigmap_url = google_static_map_url(markers=gmarkers, center=user_latlngstr, size="800x400") giant_html_string += html_head(quake_count=len(quakes), location=user_location['label'], map_url=bigmap_url) ################################### ##### Now add each quake as a "row" for q in quakes: latlngstr = "%s,%s" % (q['latitude'], q['longitude']) # each row has a bit of narrative text qdistance = haversine((q['longitude'], q['latitude']), (user_location['longitude'], user_location['latitude'])) qdistance = round(qdistance, 1) # make it prettier narrative_html = html_narrative(time=friendly_timestamp(q['time']), place=q['place'], magnitude=q['mag'], distance=qdistance) # give each quake its own little map gmarkers = [user_marker, latlngstr] gmap_url = google_static_map_url(markers=gmarkers, size="500x300" ) # For each quake/row, just throw it onto the giant_html_string giant_html_string += html_row( map_url=gmap_url, narrative=narrative_html, quake_id=q['id']) ##### Now slap on the tail of the HTML giant_html_string += html_tail() ##### ...and we're done return giant_html_string