## Wednesday, June 1, 2016

### Using folium - 3: Heatmap (Heatmap of earthquakes)

We talked in the previous blog how to query the USGS earthquake, and plot on the map. Using folium, it is very simple to plot a quick heatmap, in the following we will show how to do that, and you can checkout the script at Qingkai's Github. There are things to add, like the colorbar for the heatmap, specify radius in meters/km instead of pixels, but for now, it is a quick dirty heatmap plot.

In [1]:
from bs4 import BeautifulSoup
import urllib
import folium
from folium import plugins
from functions import *

In [2]:
# let's get the earthquake larger than M5 globally from 2010-01-01 to 2016-01-01.
url = build_query(outFormat = 'text', starttime = '2010-01-01', endtime = '2016-06-01', minmagnitude = 5.0)

# get the earthquake data from USGS and parse them into a numpy array
soup = BeautifulSoup(r, "lxml")
events_mat = parse_result(soup.text)

In [3]:
# extract lat, lon, and magnitude for the folium heatmap
lats = [float(item[2]) for item in events_mat]
lons = [float(item[3]) for item in events_mat]
mag = [float(item[4]) for item in events_mat]


## Heatmap¶

In [4]:
# Using USGS style tile
url_base = 'http://server.arcgisonline.com/ArcGIS/rest/services/'
service = 'NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}'
tileset = url_base + service

m = folium.Map(location=[37.8716, -122.2727], zoom_start=2,\
control_scale = True, tiles=tileset, attr='USGS style')

# I am using the magnitude as the weight for the heatmap