ArcGIS Developer

ArcGIS API for Python

Download the samples Try it live

Safe Streets to Schools


Accidents near elementary schools, such as this one has drawn the attention of city employees and civic-minded individuals to the topic of pedestrian and bicycle safety.

In this notebook, we want to suggest policy actions to civic authorities of Pasadena, California that will reduce the likelihood of future accidents. We will map accident data regarding pedestrians and cyclists struck by vehicles. Then, we will determine the number of accidents that occurred within each school zone and identify the five most dangerous zones. We will present this analysis using a notebook that provides narrative context and helps stakeholders understand our methodology.

The sample uses ArcGIS API for Python to help city officials in improving pedestrian and bicycle safety near schools in the city. We will first uncover patterns in accident data using spatial analysis tool such as calculate_density, find_hot_spots and HeatmapRenderer. HeatmapRenderer highlights the capabilities of map widget to show areas where large number of accidents occurred. To visualize different category of accidents on the map, create_symbol tool plays a vital role along with functionality of renderers and arcade expressions.

As the visualizations are not enough to make policy decisions, we'll further use create_drive_time_areas and summarize_within tools to determine the number of accidents that have occurred within half mile walk distance from each school.

Since the city does not have enough funds that encompasses all school zones, we will limit our analysis to the five most vulnerable areas. So, we apply filter to identify the five most dangerous zones in which a pedestrian or cyclist was injured or killed. City officials could then suggest measures such as adding more bike lanes in these areas or change street signs to reduce accidents near schools and increase safety.


Necessary Imports

In [1]:
%matplotlib inline

import pandas as pd
from datetime import datetime as dt
import matplotlib.pyplot as plt
from IPython.display import display

from arcgis.gis import GIS
from arcgis.features.analyze_patterns import calculate_density, find_hot_spots
from arcgis.mapping.symbol import create_symbol
from arcgis.features.use_proximity import create_drive_time_areas
from arcgis.features.summarize_data import summarize_within

Connect to your ArcGIS Online organization

Connect to the GIS via an existing profile or creating a new connection by e.g. gis = GIS("", "arcgis_python", "P@ssword123")

In [2]:
gis = GIS('home')

Get the data for the analysis

Search for the Traffic Collisions layer. You can specify the owner's name as "api_data_owner" to get more specific results.

In [3]:
items ='title:Traffic Collisions, owner:api_data_owner', 'feature layer')
In [4]:
Traffic Collisions
Traffic collisions in Pasadena, California, since 2008.Feature Layer Collection by api_data_owner
Last Modified: April 26, 2019
0 comments, 24 views
In [5]:
item = items[0]

The item shows all accidents that occurred in Pasadena during the past decade.

We can print the names of the layers in this item and assign them to variables that will be used in our analysis

The code below prints name of layers.

In [6]:
for lyr in item.layers:

Since the item is a Feature Layer Collection, accessing the layers property gives us a list of FeatureLayer objects. The collisions layer is the only layer in this item.

In [7]:
collisions = item.layers[0]
In [8]:
m1 ='Pasadena, California', 13)