Monday, January 27, 2020

Project #2 - Mapping Volcanos and Populations

My second project is a program that maps all of the volcanoes in the United States. The data is pulled from a .txt file that has the gps coordinates of each volcano. In addition, when the pin that denotes the location is clicked it will show the name and elevation of that volcano. The pins are also colored according to the elevation of the volcano. The name of the volcano is a hyperlink that when click opens a new tab with a Google search of that volcanoes name. I added another layer to the map that overlays each country with a different color based on population size. The data for the population size is pulled from a .json file.

import folium
import pandas

data = pandas.read_csv("Volcanoes.txt")
lat = list(data["LAT"])
lon = list(data["LON"])
elev = list(data["ELEV"])
name = list(data["NAME"])

html = """Volcano name:<br><a href="https://www.google.com/search?q=%%22%s%%22" 
target="_blank">%s</a><br>Height: %s feet"""

def color_producer(elevation):
    if elevation < 1000:
        return 'green'    elif 1000 <= elevation < 3000:
        return 'orange'    else:
        return 'red'

map = folium.Map(location=[38.58, -99.09], zoom_start=5, tiles="Stamen Terrain")

fgv = folium.FeatureGroup(name="Volcanoes")

for lt, ln, el, name in zip(lat, lon, elev, name):
    iframe = folium.IFrame(html=html % (name, name, (el*3.28084)), width=200, height=100)
    fgv.add_child(folium.Marker(location=[lt, ln], popup=folium.Popup(iframe), 
icon=folium.Icon(color=color_producer(el))))

fgp = folium.FeatureGroup(name="Population")

fgp.add_child(folium.GeoJson(data = (open("world.json", "r", encoding = "utf-8-sig").read()),
style_function = lambda x: {"fillColor":"green" if x["properties"]["POP2005"] < 10000000else "orange" 
if 10000000 <= x["properties"]["POP2005"] < 20000000 else "red"}))


map.add_child(fgv)
map.add_child(fgp)
map.add_child(folium.LayerControl())

map.save("Map_html_popup_advanced.html")

No comments:

Post a Comment

Project #8 - Stock Tracking Graph

For my 8th project, I made a program that pulls stock data from Yahoo Finance and displays it in a graph. For this example, I chose GOOG whi...