Wednesday, May 25, 2016

Using folium - 2: Use customized icon (Plot moment tensor)

In this script, we will try to use a customize icon for marker on the folium map. It is actulaly really simple to use a customized icon, you can either refer to a file on your local drive, or give it a url so that it will link the file. The following scripts are showing both, the first one is ploting iron man who visit Berkeley (link the file online), and the second one is plotting a moment tensor from a file we generated on our drive. You can find the script at Qingkai's Github.
In [1]:
import folium
In [2]:
url_base = 'http://vignette3.wikia.nocookie.net/'
url_folder = 'the-adventures-of-the-gladiators-of-cybertron/'
url_file = 'images/6/67/Iron_Man.png/revision/latest?cb=20140720074813'
logo_url = url_base + url_folder + url_file
UCB_lat = 37.8719
UCB_lon = -122.2585

map_1 = folium.Map(location=[UCB_lat, UCB_lon], zoom_start=12,\
                      control_scale = True, tiles='Stamen Terrain')

icon = folium.features.CustomIcon(logo_url,\
                                  icon_size=(200, 200))

folium.Marker([UCB_lat, UCB_lon],
          popup='Iron man visit Berkeley',
          icon=icon).add_to(map_1)

map_1
Out[2]:

Plot moment tensor on the map

The following is how we can plot the moment tensors using obspy, and place it on the folium map using custom icon. Note that you need Obspy as the dependences. Let's plot the moment tensor for the 2014 south Napa earthquake here at Bay area, you can find details of this event at USGS. I won't show the maps here, but save it as a html file you can see it.
In [3]:
from obspy.imaging.beachball import beachball

# latitude and longitude of the earthquake
evla = 38.215
evlo = -122.312

# South Napa EQ moment tensor
mt = [247, 82, 8]
beachball(mt, size=200, linewidth=2, facecolor='b', outfile= './beachball.png')

# Add the USGS style map
url_base = 'http://server.arcgisonline.com/'
tiles = 'ArcGIS/rest/services/NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}'
tileset = url_base + tiles
map_1 = folium.Map(location=[38, -122], zoom_start=8,\
                      control_scale = True, tiles=tileset, attr='USGS style')

# Add the Moment Tensor as an icon
icon_url = './beachball.png'
icon = folium.features.CustomIcon('/beachball.png',icon_size=(45, 45))

folium.Marker([evla, evlo],
          popup='Napa Earthquake',
          icon=icon).add_to(map_1)

map_1.save('moment_tensors.html')


1 comment:

  1. Hi, i liked too much this post.

    i tryied implement a png image in my project and recived this error:

    'AssertionError: You cannot render this Element if it's not in a Figure.'

    Do you know why i recived this error ?

    I'm triyng to use this image:

    http://ap.imagensbrasil.org/images/2017/06/30/oie_transparent.png

    ReplyDelete