<jupyter_start><jupyter_text>Automating Spatial Analysis with the ArcGIS API for Python   Import packages and login to ArcGIS Online<jupyter_code># import packages
import arcgis
import datetime
from arcgis.gis import GIS
from getpass import getpass
from IPython.display import display
from arcgis.features.use_proximity import create_buffers
from arcgis.features.manage_data import overlay_layers
from arcgis.features import FeatureLayerCollection
# Login to ArcGIS Online
password=getpass()
gis = GIS("https://arcgis.com", "bhammersley_tech", password)<jupyter_output>········<jupyter_text>Get our puffin group and display content<jupyter_code># get our group
Puffin_Group = gis.groups.search('Puffin Conservation')[0]
Puffin_Group
Wmap = gis.content.get('08ce9f4f24424e2887c9939c0d82ca1d')
Wmap.share(groups=Puffin_Group.id)
# list items in Puffin Conservation group
items = Puffin_Group.content()
for item in items:
    display(item)<jupyter_output><empty_output><jupyter_text>Find puffin habitat at risk of exposure to the oil spill<jupyter_code># create a map of our area of interest
m = gis.map()
m
# Specify the view
m.zoom = 7
m.center = [59.617161, -1.668248]
# Add the puffin habitat polygon layer
habitat = items[1]
m.add_layer(habitat)
# add the active oil_spill layer
oil_spill = items[0]
m.add_layer(oil_spill)
# buffer the active oil_spill boundaries and add as new content
timestamp = '{:%Y_%m_%d_%H_%M_%S}'.format(datetime.datetime.now())
oilbuffers = create_buffers(oil_spill, [30], None, 'Miles', output_name="Oil_Buffers_" + timestamp)
# add risk areas to map
m.add_layer(oilbuffers)
# run overlay analysis to determine habitat within the risk boundaries and add as new content
timestamp = '{:%Y_%m_%d_%H_%M_%S}'.format(datetime.datetime.now())
risk_habitat = overlay_layers(oilbuffers, habitat,                            
                        overlay_type="Intersect",
                        output_name="At_Risk_Habitat_" + timestamp)
# Update symbology of risk_habitat layer from an existing item used as a template

flc=FeatureLayerCollection.fromitem(risk_habitat)
template_layer = gis.content.get("61552a637bfb4e928d24fc022226cc2c").layers[0]
flc.layers[0].manager.update_definition({"drawingInfo": template_layer.properties['drawingInfo']})<jupyter_output><empty_output><jupyter_text>Update Webmap<jupyter_code># Get web map item from list of items we made earlier
webmapitem = items[3]

# create a webmap object from the item
webmap = arcgis.mapping.WebMap(webmapitem)
webmap
# remove the old layer from the web map
for lyr in webmap.layers:
    if lyr['id'].startswith('At_Risk_Habitat'):
        webmap.remove_layer(lyr)

# add our puffin habitat at risk of oil exposure we just generated in the analysis
webmap.add_layer(risk_habitat)

# Save changes to the webmap back to the GIS
webmap.update()
webmap
timestamp = '{:%Y%m%d%H%M%S}'.format(datetime.datetime.now())
timestamp
timestamp2 = '{:%Y%m%d%H%M%S}'.format(datetime.datetime.now())
timestamp2
int(timestamp2) - int(timestamp)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10039370078740158, 'fraction_numerical': 0.022637795275590553, 'mean_word_length': 5.68640350877193, 'pattern_counts': {'":': 1, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '405325', 'license': 'Apache-2.0', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1020, 'n_words': 270, 'path': 'working-with-the-arcgis-api-for-python-ac-19/notebooks/Spatial Analysis.ipynb', 'repo_name': 'EsriUK/presentations', 'size': '3048'}