<jupyter_start><jupyter_text>Fighting California forest fires using spatial analysisThe 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```<jupyter_code>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://python.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")<jupyter_output><empty_output><jupyter_text>Using groups to share items and collaborateA 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.<jupyter_code># get our group
group = gis.groups.search('LA County Emergency Management')[0]
group
# list items in the group
items = group.content()
for item in items: 
    display(item)<jupyter_output><empty_output><jupyter_text>Visualize the fire dataLet us create a map widget to see the fire related information in it's geographic context:<jupyter_code># create a map of our area of interest
m = gis.map('Los Angeles', 9)
m<jupyter_output><empty_output><jupyter_text>![m](http://esri.github.io/arcgis-python-api/notebooks/nbimages/04_CA_fires_01.PNG)<jupyter_code># add the active fires layer
fires = items[0]
m.add_layer(fires)
# add our critical infrastructure layer
infra = items[1]
m.add_layer(infra)
#we shall use this webmap item in the last section of the notebook.
webmapitem = items[2]<jupyter_output><empty_output><jupyter_text>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 boundariesTo 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:<jupyter_code># create a map of our area of interest
m1 = gis.map('Los Angeles', 9)
m1<jupyter_output><empty_output><jupyter_text>![m1](http://esri.github.io/arcgis-python-api/notebooks/nbimages/04_CA_fires_02.PNG)<jupyter_code>#add the active fires layer to the map
m1.add_layer(fires)
#add the critical infrastructure layer to the map
m1.add_layer(infra)
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 )<jupyter_output><empty_output><jupyter_text>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):<jupyter_code># share the layer with public
firebuffers.share(True)<jupyter_output><empty_output><jupyter_text>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.<jupyter_code># add risk areas to map
m1.add_layer(firebuffers)<jupyter_output><empty_output><jupyter_text>Perform overlay analysis to extract facilities that fall within the fire buffersTo 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](https://developers.arcgis.com/rest/analysis/api-reference/overlay-layers.htm).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:<jupyter_code>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)
# set sharing on new analysis layer
riskinfra.share(True)<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>riskinfra.layers<jupyter_output><empty_output><jupyter_text>Read analysis results as a pandas dataframe for analysisLet 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](http://pandas.pydata.org/) [dataframe](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html).<jupyter_code>at_risk_facilities  = riskinfra.layers[0]
features = at_risk_facilities.query()

df = features.sdf # read the returned features as a Pandas dataframe
df<jupyter_output><empty_output><jupyter_text>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.<jupyter_code># view simplified risk facilities table
df1_simplified = df[['name', 'cat1', 'post_id']]
df1_simplified
# 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)<jupyter_output><empty_output><jupyter_text>Mapping the infrastructure at riskTables 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:<jupyter_code># create new map over aoi
m2 = gis.map('Los Angeles', 9)
m2<jupyter_output><empty_output><jupyter_text>![m2](http://esri.github.io/arcgis-python-api/notebooks/nbimages/04_CA_fires_04.PNG)<jupyter_code>#add the critical infrastructure layer to the map
m2.add_layer(infra)
# add at risk facilities layer to fresh map
m2.add_layer(riskinfra)<jupyter_output><empty_output><jupyter_text>Sharing the result of the analysis as a web mapThe different teams working on the firefighting, relief and rescue efforts need a current and updated web map of the critical infrastruture at risk from the fires. The code below fetches the web-map from the GIS and updates it's operational layer to the one from the analysis above. This enables all interested parties to refer to one common, constantly updated web-map for planning firefighting, rescue and relief efforts.<jupyter_code>#display the webmap item that we had obtained as part of the 'LA County Emergency Management' group's content in the first section of this notebook.
webmapitem
# create a webmap from the item
webmap = arcgis.mapping.WebMap(webmapitem)
# read the operational layers in our webmap
for lyr in webmap['operationalLayers']:
    if lyr['id'].startswith('At_Risk_Infrastructure'):
        print(lyr['url'])
new_url = at_risk_facilities.url
new_url
# update the url of the 'At Risk Infrastructure' layer in the web map

for lyr in webmap['operationalLayers']:
    if lyr['id'].startswith('At_Risk_Infrastructure'):
        lyr['url'] = new_url
# save the updates back to the GIS
webmap.update()<jupyter_output><empty_output><jupyter_text>Web map items can also be visualized in the Jupyter Notebook as shown below. This enables use of web-maps for analysis and visualization within the notebook environment.<jupyter_code>#zoom in to the extents of Los Angeles in the map below, to view the webmapitem 
m3 = gis.map(webmapitem)
m3<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.061252268602540835, 'fraction_numerical': 0.004718693284936479, 'mean_word_length': 5.498231132075472, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 2, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '291768', 'license': 'Apache-2.0', 'n_tokens_mistral': 3022, 'n_tokens_neox': 2869, 'n_words': 1385, 'path': 'samples/04_gis_analysts_data_scientists/fighting_california_forest_fires_using_spatial_analysis.ipynb', 'repo_name': 'nitz21/arcpy', 'size': '11020'}