Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active April 5, 2023 07:37
Show Gist options
  • Save dannguyen/5f33be9b652f6d1e1ea8 to your computer and use it in GitHub Desktop.
Save dannguyen/5f33be9b652f6d1e1ea8 to your computer and use it in GitHub Desktop.
quickie demo of how to use the Google Static Maps API and Python to serialize and visualize USGS earthquakes data http://www.compciv.org/guides/python/how-tos/creating-proper-url-query-strings/
# slightly more Pythonic, cleaner version
from csv import DictReader
import requests
import webbrowser
USGS_URL = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv'
GMAPS_URL = 'https://maps.googleapis.com/maps/api/staticmap'
# get the USGS data, create a list of lines
lines = requests.get(USGS_URL).text.splitlines()
# get the latitude/longitude pairs
coordinate_pairs = ["%s,%s" % (q['latitude'], q['longitude']) for q in DictReader(lines)]
# this is another way of serializing the URL
preq = requests.PreparedRequest()
preq.prepare_url(GMAPS_URL, {'size':'800x500', 'markers': coordinate_pairs})
webbrowser.open(preq.url)
# quick and ugly version, esp. if you forgot how to do a list comprehension
import csv
import requests
from urllib.parse import urlencode
import webbrowser
# find a list of feeds at:
# http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
usgs_url = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv'
resp = requests.get(usgs_url)
lines = resp.text.splitlines()
earthquakes = csv.DictReader(lines)
coordinate_pairs = []
for quake in earthquakes:
coordinate_pairs.append(quake['latitude'] + ',' + quake['longitude'])
# create URL according to Google Static Maps API specs
endpoint_url = 'https://maps.googleapis.com/maps/api/staticmap'
query_string = urlencode(
{'size': '800x500', 'markers': coordinate_pairs},
doseq=True)
url = endpoint_url + '?' + query_string
webbrowser.open(url)

Here's what a sample result looks like:

  https://maps.googleapis.com/maps/api/staticmap?size=800x500&markers=-6.6214%2C154.7073&markers=22.9387%2C120.5928&markers=54.007%2C158.5064&markers=39.3%2C-74.3&markers=35.6519%2C-3.6755&markers=59.6047%2C-153.3405&markers=18.8414%2C-106.9536&markers=41.9519%2C142.7205&markers=3.8769%2C126.8708

img

Copy link

ghost commented Apr 5, 2023

Great work, used last year. Keep it up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment