Visualizing Mexican wildfires during 2017 with folium
By: Isaac Arroyo, Unsupervised Isaac
Tags: Wildfires, Data Visualization, Python, Data Journalism, Editors Pick

Authors:

Arroyo-Velázquez Isaac; Camacho-Perez Enrique

Introduction

The incidence of fires in size, frequency, and intensity has increased significantly around the world. Mexico, a mega-diverse country, has recurring forest fires, especially in the El Niño years. However, in Mexico, fires are caused mainly by humans, and factors like road accessibility and distance to towns are commonly associated with their frequency and distribution [1].

The general objective of this tutorial is to show how to use data science visualization tools, but in a particular way, the folium module, to understand the fires in Mexico during 2017.

CONAFOR database

In Mexico, prevention and control of forest fires are carried out by the National Forest Fire Protection Program (CONAFOR, by its Spanish acronym), and we will use its database obtained for the year 2017 through the following links:[2][3]

Data Exploration

First of all, a basic exploration of the data from CONAFOR will be carried out.

Number of rows: 8896

Number of columns : 53

Statistical summary of the data set:

Conversion of Units

Suspicious data

Before any transformation, we must make sure that the data is numeric type:

we have to change 19° to a numerical data

Conversion function

Using the columns of “Degrees”, “Minutes” and “Seconds” we find the decimal degrees for latitude and longitude, through the following function:

Having the longitude and latitude data we eliminate the columns “Degrees”, “Minutes” and “Seconds”

Visualizing Wildfires with Folium

When we have data involving positions, zones, or geographic regions, it is vital to visualize them clearly and user-friendly.

Python’s library, Folium [4], is the infallible and ideal tool for this situation. In this notebook, we will explain how to visualize the wildfires recorded during 2017 in Mexico. Each map shown will have its detailed procedure for recreation with other data.

import folium

First of all we are going to create an array with our data’s longitude and latitude information.

Every map made in Folium should be crated on top of a base map. This base is created by the Map() function:

location (tuple) : Center's map location as a tuple (latitude, longitude)

(tuple) : Center's map location as a tuple (latitude, longitude) zoom_start (int) : The amount of zoom you want when the map is displayed

Wildfire proximity

When we talk about some natural disasters (although wildfires are largely human-caused), everyone wants to know how close it is to where you live, and if it is close, how many are there or have happened? This circumstance is where Heat Maps are helpful to show the proximity of each point (data). Folium, fortunately, has the tool:

from folium.plugins import HeatMap

The relevant parameters (and the ones we’re going to use) are:

data (list or array) : Geographical locations of data to be plotted on the map.

(list or array) : Geographical locations of data to be plotted on the map. name (string) : Layer's name. This will appear in LayerControls . [default: None ]

(string) : Layer's name. This will appear in . [default: ] radius (int) : Radius of each data point on the map. [default: 15 ]

(int) : Radius of each data point on the map. [default: ] max_val (float) : Maximum intensity value of a point. [default: 1 ]

(float) : Maximum intensity value of a point. [default: ] min_opacity (float) : Minimum opacity value at which the "heat" of the heat map will begin. [default: 1 ]

(float) : Minimum opacity value at which the "heat" of the heat map will begin. [default: ] blur (int) : Amount of blur in the points. The bigger the number the blurrier the point. [default: 15 ]

Important note: If we want to modify any map parameter, it is important to re-run the code from creating the map ( Map() ) because if we just run the HeatMap() function, it will overlay a new layer on the map. The best way to deal with this problem is that once the code is ready, put everything in the same cell so that can re-run it all if it is necessary to change some parameters:

Wildfire duration (days)

We can see that our dataset contains valuable information, one of them is the fire’s duration. Moreover, although we have the start and end date, we have the variable called Duration time (days) , which has already categorized the time.

For a better visualization we can categorise the periods with colours.

Colour representation of duration time of wildfires. Image by: Isaac Arroyo

As a next step we will add the colour code to our dataset and translate the data from spanish to english

What are we going to do with this information? We are going to draw small circles whose centre is in Latitude and Longitude and whose colour is given by Duration time (colours). This option will be achieved with the function Circle():

location (tuple or list) : Latitude and Longitude (pair).

(tuple or list) : Latitude and Longitude (pair). popup (string o folium.Popup ) : Input text or visualization for object displayed when clicking.

(string o ) : Input text or visualization for object displayed when clicking. radius (float) : Radius of the circle, in meters.

(float) : Radius of the circle, in meters. color (string) : Colour of the circle outline, in HEX code.

(string) : Colour of the circle outline, in HEX code. fill (string) : If fill = True then the function ask for a fill_color

(string) : If then the function ask for a fill_color (bool) : Colour of the fill of the circle.

To show all the fires in the country we will use Circle() inside the for loop:

Combination: Duration time + Affected area

Continuing with the circle map, we can give the radius parameter a better understanding of fire dimensions.

The units of Affected area (ha) are hectares, so we can use it to determine the radius of our circle to have a first visual approach (and approximation) of the extent of these natural disasters.

Where 𝑅 is in hectometers (1 hm = 100 m) and translated to Python is:

radius = np.sqrt( df[‘Affected area (ha)’].values[i]/np.pi )*1000

The *1000 at the end is for scaling the circle 10 times (1 meter in real data = 10 meters in the visualization)

Total area affected (in hectares)

Circles alone are not enough to get a good picture of the situation, we also have information on the affected area (in hectares) under the variable name Affected area (ha):.

We can group them by state and obtain the sum of the affected area in a data frame

To obtain a clear and striking map of the loss of green areas due to wildfires we will use extra resources:

ID code of the Mexican states

JSON file for visualization

Mexican states ID code

El data frame of affected_area_states is somewhat incomplete for the map we want to do, so we need to complete with information found in the following csv file:

We can notice that two states do not match in name, so doing a merge would not work in our favor, so that we will change the names:

affected_area_states.iloc[6,0] = ‘CDMX’

affected_area_states.iloc[16,0] = ‘Estado de México

JSON file

With the IDNAMES we only have to "draw and paint" over the states of Mexico whose geometry will be given by the following JSON file: states_mx.json

Let’s look at the distribution of our data in order to categorise it by affected area: