<jupyter_start><jupyter_text>Initial set-up<jupyter_code>import folium
import pandas as pd
import numpy as np
import re<jupyter_output><empty_output><jupyter_text>How folium worksThe workflow is basically you create a map object. You import your data and organise them with a feature group that you insert into the map.<jupyter_code>%cd "F:\Google Drive\JupyterNotebooks\PMega\Section 11 - Webmaps"
import os
os.listdir()<jupyter_output>F:\Google Drive\JupyterNotebooks\PMega\Section 11 - Webmaps<jupyter_text>Creating the map<jupyter_code>help(folium.Map)<jupyter_output>Help on class Map in module folium.folium:

class Map(folium.map.LegacyMap)
 |  Create a Map with Folium and Leaflet.js
 |  
 |  Generate a base map of given width and height with either default
 |  tilesets or a custom tileset URL. The following tilesets are built-in
 |  to Folium. Pass any of the following to the "tiles" keyword:
 |  
 |      - "OpenStreetMap"
 |      - "Mapbox Bright" (Limited levels of zoom for free tiles)
 |      - "Mapbox Control Room" (Limited levels of zoom for free tiles)
 |      - "Stamen" (Terrain, Toner, and Watercolor)
 |      - "Cloudmade" (Must pass API key)
 |      - "Mapbox" (Must pass API key)
 |      - "CartoDB" (positron and dark_matter)
 |  
 |  You can pass a custom tileset to Folium by passing a Leaflet-style
 |  URL to the tiles parameter: ``http://{s}.yourtiles.com/{z}/{x}/{y}.png``
 |  
 |  Parameters
 |  ----------
 |  location: tuple or list, default None
 |      Latitude and Longitude of Map (Northing, Easting).
 |  width: pixel int or perc[...]<jupyter_text>Creating a feature group<jupyter_code>help(folium.FeatureGroup)
all_volcs = folium.FeatureGroup(name="All")<jupyter_output><empty_output><jupyter_text>Importing and cleaning the data<jupyter_code>data_raw = pd.read_excel("GVP_Volcano_List.xlsx",header=1)
print("data_raw: "+str(data_raw.shape))
data = data_raw.dropna()
print("data: "+str(data.shape))
data.replace(to_replace="'",value=" ",regex= True, inplace=True)<jupyter_output><empty_output><jupyter_text>data["Volcano Name"].replace(regex=True,inplace=True,to_replacheldata.reset_index(inplace = True)note: reindex is where you have your own index to insert.<jupyter_code># This will do for now

data.replace(to_replace="'",value=" ",regex= True, inplace=True)
#debugging
with open("debug.txt","w+") as file:
    file.seek(0)
    for x in data["Volcano Name"]:
        file.write(str(x)+"\n")
    
lat = data["Latitude"]
lon = data["Longitude"]
elev = data["Elevation"]
name = data["Volcano Name"]
erupt = data["Last Eruption Year"]
erupt.head(5)
# checking that it has indeed removed the '
name.iloc[244:247]<jupyter_output><empty_output><jupyter_text>Setting up the rest of the map<jupyter_code>volcsmap = folium.Map(tiles="Mapbox Bright")
all_volcs = folium.FeatureGroup(name="All Volcanoes")
test = [1,"unknown"]
for i in test:
    try:
        print(float(i))
    except ValueError:
        print("gray")
def color_gen(lastErupt):
    try:
        x = float(lastErupt)
    except ValueError:
        return("gray")
    
    if x > 1900:
        return("red")
    elif 1400<= x < 1900:
        return("lightred")
    elif 1100 <= x <1400:
        return("orange")
    elif 600 <= x < 1100:
        return("pink")
    elif 100 <= x < 600:
        return("green")
    elif -1000 <= x < 100:
        return("darkgreen")
    elif x < -1000:
        return("darkpurple")

# now we add that feature group to the map

#volcsmap.add_child(all_volcs)
# folium.LayerControl().add_to(volcsmap)
#volcsmap.save("volcsmap.html")

# gathering data on lat, lon, elev and name
for i,j,k,l,m in zip(lat,lon,elev,name,erupt):
    #add markers to the featuregroup: all_volcs. Note that Marker is an object, not a function.
    #all_volcs.add_child(folium.CircleMarker(location=[i,j],popup=l+", "+str(k),icon=folium.Icon(color=color_gen(m))))
    
    #circle markers
    all_volcs.add_child(folium.CircleMarker(location=[i,j],radius=6, popup=l+", "+str(k),colour="gray",fill = True, fill_color=color_gen(m), fill_opacity=0.7))
help(folium.CircleMarker)
dir(folium)
help(folium.LayerControl)<jupyter_output>Help on class LayerControl in module folium.map:

class LayerControl(branca.element.MacroElement)
 |  Creates a LayerControl object to be added on a folium map.
 |  
 |  Parameters
 |  ----------
 |  position : str
 |        The position of the control (one of the map corners), can be
 |        'topleft', 'topright', 'bottomleft' or 'bottomright'
 |        default: 'topright'
 |  collapsed : boolean
 |        If true the control will be collapsed into an icon and expanded on
 |        mouse hover or touch.
 |        default: True
 |  autoZIndex : boolean
 |        If true the control assigns zIndexes in increasing order to all of
 |        its layers so that the order is preserved when switching them on/off.
 |        default: True
 |  
 |  Method resolution order:
 |      LayerControl
 |      branca.element.MacroElement
 |      branca.element.Element
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, position='topright', collapsed=True, autoZIndex=True)
 | [...]<jupyter_text>Adding another layer<jupyter_code>subVolcs = folium.Map(tiles="Mapbox Bright")
# prep the data. I'm grabbing all the data points with an elevation of less than zero
sub_volcs = data.loc[data["Elevation"]<0]
sub_lat = sub_volcs["Latitude"]
sub_lon = sub_volcs["Longitude"]
sub_elev = sub_volcs["Elevation"]
sub_name = sub_volcs["Volcano Name"]
# create a new FeatureGroup object
sub_volcs = folium.FeatureGroup(name="Submerged Volcanoes")

for i,j,k,l in zip(sub_lat,sub_lon,sub_name,sub_elev):
    sub_volcs.add_child(folium.Marker(location=[i,j],popup=k+", "+str(l),icon=folium.Icon(color="darkpurple")))
# now we add the feature group to the map volcsmap
subVolcs.add_child(sub_volcs)
folium.LayerControl().add_to(subVolcs)


volcsmap.add_child(sub_volcs)
volcsmap.add_child(all_volcs)

folium.LayerControl().add_to(volcsmap)
volcsmap.save("Testvolcsmap.html")
data.columns
import seaborn as sns
data["Last Eruption Year"].head(5)
time_data = pd.Series(data["Last Eruption Year"].convert_objects(convert_numeric=True).dropna())
time_data.head(5)
import matplotlib
%matplotlib inline
sns.distplot(time_data, kde=True, rug=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12511878365536902, 'fraction_numerical': 0.010136205258156478, 'mean_word_length': 4.293378038558257, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '519090', 'license': 'MIT', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2160, 'n_words': 553, 'path': 'PMega/Section 11 - Webmaps/GeoMap Project Polished.ipynb', 'repo_name': 'peternewman22/Python_Courses', 'size': '6314'}