Skip to content

Instantly share code, notes, and snippets.

@mroberts444
Created September 28, 2020 06:03
Show Gist options
  • Save mroberts444/ce0f0dae44930b6d08aa463ce9a329ad to your computer and use it in GitHub Desktop.
Save mroberts444/ce0f0dae44930b6d08aa463ce9a329ad to your computer and use it in GitHub Desktop.
CA Fire Perimeters
import geopandas as gpd
import pandas as pd
import fiona
import sys
import numpy as np
import folium
from folium.plugins import Search
#data from https://frap.fire.ca.gov/mapping/gis-data/
#create a list of layers within a file geodatabase
mainpath = '~/Documents/Projects/CA_Fire_Perimeters'
fgdb = mainpath+'/fire19_1.gdb/'
#layerlist = fiona.listlayers(fgdb)
#print(layerlist)
print("\nCreating geodata frame...")
gdf1 = gpd.read_file(fgdb,layer=1)
print("\nFiltering data...")
# Replaces empty names with unk
gdf1['FIRE_NAME'] = gdf1['FIRE_NAME'].replace([""," "], 'UNKNOWN')
gdf1['FIRE_NAME'] = gdf1['FIRE_NAME'].str.lower()
gdf1['FIRE_NAME'] = gdf1['FIRE_NAME'].str.capitalize()
# Round acreage to whole numbers
gdf1['GIS_ACRES'] = gdf1['GIS_ACRES'].fillna(0).astype(np.int64)
# Remove fires less than 50 ac. to improve map performance
indexNames = gdf1[ gdf1['GIS_ACRES'] < 50 ].index
gdf1.drop(indexNames,inplace=True)
# Replace cause codes with strings
causes = ['Lightning','Equipment Use','Smoking','Campfire','Debris','Railroad',
'Arson','Playing with Fire','Other','Vehicle','Power Line','Firefighter Training',
'Non-Firefighter Training','Unknown','Structure','Aircraft','Volcanic',
'Escaped Prescribed Burn','Campfire']
causeidx = range(1,len(causes),1)
for i in causeidx:
gdf1['CAUSE'] = gdf1['CAUSE'].replace([i],causes[i-1])
# Filter by decade
decades = range(1900,2020,10)
# Colors for each decade
colors = ['magenta','navy','royalblue','steelblue','darkcyan','darkgreen','limegreen',
'olive','yellow','orange','firebrick','red']
def decadeFires(gdf,decade,color):
years = range(decade,decade+10,1)
years = [str(y) for y in years]
print(years)
gdf2 = gdf[gdf['YEAR_'].isin(years)]
p = folium.features.GeoJson(gdf2.to_crs(epsg='4326').to_json(),
name=str(decade)+'s',
style_function=lambda x: {"weight":1, 'color':color,'fillColor':color, 'fillOpacity':0.2},
highlight_function=lambda x: {'weight':3, 'color':color},
show=False,
tooltip=folium.features.GeoJsonTooltip(
fields=['YEAR_','FIRE_NAME','GIS_ACRES','CAUSE'],
aliases=['Year','Name','Acres Burned','Cause'],
labels=True,
localize=True,
sticky=True,
)
)
return p
print("\nBuilding map...")
token = ' ' # your mapbox token
tileurl_street = 'https://api.mapbox.com/styles/xxxxx/{z}/{x}/{y}?access_token=' + str(token)
tileurl_sat = 'https://api.mapbox.com/styles/xxxxx/{z}/{x}/{y}?access_token=' + str(token)
tileurl_hyb = 'https://api.mapbox.com/styles/xxxxx/{z}/{x}/{y}?access_token=' + str(token)
tileurl_topo = 'https://api.mapbox.com/styles/xxxxx/{z}/{x}/{y}?access_token=' + str(token)
m = folium.Map(location=[37.11,-120.16], zoom_start=6, tiles=None)
folium.TileLayer(tileurl_street, name='Streets', attr='Mapbox').add_to(m)
folium.TileLayer(tileurl_sat, name='Imagery', attr='Mapbox').add_to(m)
folium.TileLayer(tileurl_hyb, name='Hybrid', attr='Mapbox').add_to(m)
folium.TileLayer(tileurl_topo, name='Topographic', attr='Mapbox').add_to(m)
print('\nAdding decades to map...\n')
for i in range(len(decades)):
perim = decadeFires(gdf1,decades[i],colors[i])
m.add_child(perim)
folium.LayerControl(collapsed=False, position='topright').add_to(m)
print('\nSaving map...')
m.save(mainpath+'/perimeters.html')
print('\nDone.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment