def report(lng, lat): """ Prints to screen interesting information about the 5 earthquakes nearest to a given geocoordinate point What it expects: ---------------- lng (number) - a longitude coordinate lat (number) - a latitude coordinate What it does: ------------- Calls filtrate(lng, lat) to get 5 closest records (as a list of dictionaries) Then it prints some kind of human-readable output. Note that this is different from filtrate() itself, which does not print anything but instead returns a list of dictionaries What it returns: ---------------- Nothing. This is just for printing to screen for human readability as a warmup to producing the same kind of data in more attractive HTML format. """ user_pt = (lng, lat) quakes = filtrate(lng, lat) for q in quakes: quake_pt = (float(q['longitude']), float(q['latitude'])) distance = round(haversine(user_pt, quake_pt), 1) quake_url = USGS_QUAKE_EVENT_BASEURL + q['id'] infotxt = NARRATIVE_INFO_TEMPLATE.format(date=q['time'], place=q['place'], magnitude=q['mag'], distance=distance, quake_url=quake_url) print(infotxt)
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
# but since the user is using our simple text interface # we can just serialize it as text and # print it to screen serialized_results = json.dumps(georesult, indent=4) print(serialized_results) elif command == 'filtrate': # because input() ALWAYS takes what it is given and turns it # into text, we have to typecast it to float before sending it # to filtrate() print("Enter longitude: ") lng = float(input()) print("Enter latitude: ") lat = float(input()) results = filtrate(lng, lat) # turn it into proper text serialized_results = json.dumps(results, indent=2) # print to screen...nothing else to do print(serialized_results) elif command == 'report': user_location = input(bolden("What is your current location/address? ")) georesult = geocode(user_location) # if there is a valid, 'status' is "OK"; else it is None if georesult['status']: lng = georesult['longitude'] lat = georesult['latitude'] report(lng, lat) else: bprint("Your location", location, "did not return any results.")