Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS API for Python

Download the samples Try it live

Fighting California forest fires using spatial analysis

The state of California has had a dangerous fire season in 2015 and 2016. A standard procedure while fighting these fires is identifying facilities that are at risk and evacuating them. Tasks such as this can be accomplished easily using spatial analysis tools available on your GIS. Spatial analysis tools allow overlaying the extent of fire and the locations of the facilities on a map and identifying the ones that fall within the fire's extent.

Thus, this sample demonstrates the application of spatial analysis tools such as buffer and overlay.

This notebook describes a scenario wherein an analyst automates the process of identifying facilities at risk from forest fires and sharing this information with other departments such as the fire department, etc.

Note: To run this sample, you need the pandas library in your conda environment. If you don't have the library, install it by running the following command from cmd.exe or your shell

conda install pandas
In [1]:
import datetime
import arcgis
from arcgis.gis import GIS
from IPython.display import display
import pandas as pd

# create a Web GIS object
gis = GIS("https://pythonapi.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")

Using groups to share items and collaborate

A group effort spanning several professionals and teams is required to meet challenges such as forest fires. Members of a web GIS can collaborate with each other by sharing web maps, layers, applications, etc. with each other. This sharing is accomplished by creating groups in the GIS and items shared to a group can be used by members of the group.

The code below lists the items shared with the 'LA County Emergency Management' group. This group contains a collection of maps, apps and layers that are published as the authoritative common map for the county of Los Angeles.

In [49]:
# get our group
group = gis.groups.search('LA County Emergency Management')[0]
group
Out[49]:
LA County Emergency Management

Summary: Group providing data relevant to emergencies in Los Angeles, California, USA.
Description: None
Owner: api_data_owner
Created: August 08, 2019
In [50]:
# list items in the group
items = group.content()
for item in items: 
    display(item)
Active California Fires
Fires in Southern California active during 2016-2017.Feature Layer Collection by api_data_owner
Last Modified: August 08, 2019
0 comments, 5 views
Critical Infrastructure in Southern California
Essential facilities to public health and safety in Southern California.Feature Layer Collection by api_data_owner
Last Modified: August 08, 2019
0 comments, 4 views
LA County Infrastructure at Risk
Web Map by arcgis_python
Last Modified: August 08, 2019
0 comments, 16 views

Visualize the fire data

Let us create a map widget to see the fire related information in it's geographic context:

In [10]:
# create a map of our area of interest
m = gis.map('Los Angeles', 9)
m
In [7]:
# add the active fires layer
fires = items[0]
m.add_layer(fires)
In [8]:
# add our critical infrastructure layer
infra = items[1]
m.add_layer(infra)
In [51]:
#we shall use this webmap item in the last section of the notebook.
webmapitem = items[2]

The fires feature layer, that we just added to the map, contains the boundaries of the active forest fires in the region. For this demo scenario, this layer is being constantly updated by the Fire Department with inputs from fire fighters in the field and remote sensing data obtained from satellites.

The infra feature layer contains the locations of critical infrastructure facilities in the region. The objective of this script is to identify critical infrastructure facilities that are at risk due to the fires, and alert firefighters, county officials and others for allocating firefighting resources, planning evacuations, etc.

Create a buffer of 4 miles around fire boundaries

To identify the critical infrastructure resources that are the risk, let us create a buffer of 4 miles around the fire boundaries and consider all facilities that fall within this area at risk. The process of buffer expands the feature's boundaries in all directions.

To perform buffers, we use the create_buffers function available in the arcgis.features.use_proximity module. As an input to the tool, we provide the fires layer as the layer to be buffered.

Feature analysis tools can return (in memory) feature collections as output for immediate consumption, or create a new feature service if the output_name parameter is specified.

We specify an output name (with a timestamp) for the service as we may want the buffered fire perimeters to be persisted for bookkeeping purposes, or be shared with others as feature layers or in web maps:

In [17]:
# create a map of our area of interest
m1 = gis.map('Los Angeles', 9)
m1
In [12]:
#add the active fires layer to the map
m1.add_layer(fires)
In [13]:
#add the critical infrastructure layer to the map
m1.add_layer(infra)
In [14]:
from arcgis.features.use_proximity import create_buffers

# buffer the active fire boundaries and add as new content

timestamp = '{:%Y_%m_%d_%H_%M_%S}'.format(datetime.datetime.now())
firebuffers = create_buffers(fires, [4], None, 'Miles', output_name="Fire_Buffers_" + timestamp )

The output layer is private by default. To share it with particular groups, members within the org, or everyone, we can use the Item.share() method. The code below shares it to everyone (public):

In [15]:
# share the layer with public
firebuffers.share(True)
Out[15]:
{'results': [{'itemId': '986ebff2fe7943f887ec44251f60095f',
   'success': True,
   'notSharedWith': []}]}

Since the output_name parameter was specified, the tool created a new feature layer item as output. We can visualize this by adding it to the map above. Now we can observe more facilities falling within the buffered area.

In [16]:
# add risk areas to map
m1.add_layer(firebuffers)

Perform overlay analysis to extract facilities that fall within the fire buffers

To programmatically extract the locations that fall within the fire buffers, we use overlay_layers tool under FeatureAnalysisTools class just like we did for create_buffers tool earlier. The overlay_layers tool supports a few overlay types, here we use Intersect as we need to perform a spatial intersection to identify the facilities that are located within the fire boundaries. To learn more about this operation, refer to the documentation.

We specify an output name (with a timestamp) for the service as we want to keep a record of the critical infrastructure within the risk boundaries and share it with others as feature layers or in web maps:

In [18]:
from arcgis.features.manage_data import overlay_layers

# run analysis to determine critical infrastructure within the risk boundaries
riskinfra = overlay_layers(firebuffers, infra, 
                           
                        overlay_type="Intersect",
                        output_name="At_Risk_Infrastructure_" + timestamp)
In [19]:
# set sharing on new analysis layer
riskinfra.share(True)
Out[19]:
{'results': [{'itemId': '5dd153f7e87d4d4d9901c784ef7bf342',
   'success': True,
   'notSharedWith': []}]}

The output of the overlay analysis is a Feature Layer Item. We can access the layers and tables in an Item represing GIS services using the layers and tables attributes.

In [20]:
riskinfra.layers
Out[20]:
[<FeatureLayer url:"https://pythonapi.playground.esri.com/server/rest/services/Hosted/At_Risk_Infrastructure_2019_08_08_15_45_02/FeatureServer/0">]

Read analysis results as a pandas dataframe for analysis

Let us read this results of the overlay analysis by querying it's layer for attribute data, and display the attribute table to get a sense of the results. Layers can be queried for attributes and geometry using the query() method.

The query we ran in the previous step returned a list of dictionaries representing the features. For further analysis or for visualizing the output data as a table, let us convert it into a pandas dataframe.

In [22]:
at_risk_facilities  = riskinfra.layers[0]
df = at_risk_facilities.query(as_df=True) # read the returned features as a Pandas dataframe
df
Out[22]:
objectid_1 join_count target_fid objectid post_id name descriptio cat1 cat2 cat3 ... perimeter_ perimeter1 acres status containmen shape_leng buff_dist orig_fid analysisarea SHAPE
0 1 1 15 40242905 24626.0 Azusa Powerplant Conduit Physical Features Power Plants ... 2016-06-26 1300 10850 Active 0.50 24948.867757 4.0 5 112.459596 {"x": -117.87199392276113, "y": 34.17666797642...
1 2 2 17 40244777 26620.0 Motion Picture And Television Fund Hospital Health and Mental Health Hospitals and Medical Centers ... 2016-06-26 800 1080 Active 0.65 3787.741971 4.0 2 57.433119 {"x": -118.63618579426031, "y": 34.15750062135...
2 3 1 48 40280838 27055.0 Pacoima Memorial Hospital Health and Mental Health Hospitals and Medical Centers ... 2016-06-26 1300 8940 Active 0.80 6262.263940 4.0 4 60.135537 {"x": -118.38329528379319, "y": 34.28092898174...
3 4 1 57 40281293 13559.0 Whiteman Airport Transportation Airports ... 2016-06-26 1300 8940 Active 0.80 6262.263940 4.0 4 60.135537 {"x": -118.41329310645722, "y": 34.25918012113...
4 5 2 59 40281296 730.0 Kaiser Permanente - Woodland Hills Medical Center The Medical Centers Provides 24 Hour Emergency... Health and Mental Health Hospitals and Medical Centers 911 Receiving ... 2016-06-26 800 1080 Active 0.65 3787.741971 4.0 2 57.433119 {"x": -118.58972710110294, "y": 34.17098795728...
5 6 1 166 40283163 637.0 Pacifica Hospital Of The Valley The facility provides hospital services, inpat... Health and Mental Health Hospitals and Medical Centers 911 Receiving ... 2016-06-26 1300 8940 Active 0.80 6262.263940 4.0 4 60.135537 {"x": -118.39619134564362, "y": 34.24014857730...
6 7 1 218 40284438 70599.0 Northeast Valley Health Corporation - Pacoima ... The agency provides health services primarily ... Health and Mental Health Hospitals and Medical Centers ... 2016-06-26 1300 8940 Active 0.80 6262.263940 4.0 4 60.135537 {"x": -118.41070642294858, "y": 34.27416334584...

7 rows × 43 columns

From the data frame, we can see there are 7 features which fell within the fire buffers. To make this result usable, let us export a CSV with only the facility name and other critical details. This CSV can be considered as a valuable information product as a result of this analysis and can be shared with the teams assisting in organizing the firefighting efforts, evacuations, etc.

In [23]:
# view simplified risk facilities table
df1_simplified = df[['name', 'cat1', 'post_id']]
df1_simplified
Out[23]:
name cat1 post_id
0 Azusa Powerplant Conduit Physical Features 24626.0
1 Motion Picture And Television Fund Hospital Health and Mental Health 26620.0
2 Pacoima Memorial Hospital Health and Mental Health 27055.0
3 Whiteman Airport Transportation 13559.0
4 Kaiser Permanente - Woodland Hills Medical Center Health and Mental Health 730.0
5 Pacifica Hospital Of The Valley Health and Mental Health 637.0
6 Northeast Valley Health Corporation - Pacoima ... Health and Mental Health 70599.0
In [24]:
# Export this to a csv file. This CSV can then be shared with fire fighters.
csv_file = r'at_risk_facilities.csv'
df1_simplified.to_csv(csv_file)

Mapping the infrastructure at risk

Tables and charts aren't enough to convey the information gathered from this analysis. By plotting the locations of the critical infrastructure at risk from the forest fires, we are able to identify patterns that can be of great value in fighting a catastrophe of this scale and prevent or minimize human, infrastructural and environmental loss:

In [28]:
# create new map over aoi
m2 = gis.map('Los Angeles', 9)
m2