Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS API for Python

Download the samples Try it live

Analyzing violent crime

Many of the poorest neighborhoods in the City of Chicago face violent crimes. Since some studies have linked alcohol to different crimes, there is pressure on the city officials to close down liquor establishments. The local business owners, on the other hand, want to block such restrictions as it could negatively impact business and the social fabric of the City. In this sample, we will perform an illustrative analysis to find a find a possible relation between violent crimes and liquor establishments. This will also help us figure out if poverty and unemployment rate are factors which contribute to more crimes in a specific area of the city. We will be using the Crime Analysis data from ArcGIS Living Atlas of the World in order to perform this analysis. The data was orginally obtained from the 2014 Violent Crime Data in the City of Chicago data portal (https://data.cityofchicago.org).

Through this sample, we will demonstrate the utility of a number of spatial analysis methods including hot spot analysis, feature overlay, data enrichment and spatial selection using ArGIS API for Python.

Further, based on the results of the analysis, this sample will try to assist with an effective solution to this problem.

Outline of Steps

We will use the following workflow for the analysis:

  1. Retrieve Crime Analysis data.
  2. Plot the data on map for visualzation
  3. Perform spatial analysis:
    • We will first create hotspots for the crime densities and liquor vendor layers. This will help is in visualizing the significant areas of crime incidences and liquor vendors.
    • Enrich the crime layer with poverty and unemployment rate data using the enrich_layer tool.
    • Visualize hotspots on the enriched layers.
    • Use overlay tool to find relationship between crimes and liquor establishments. This will also help to know whether or not poverty and unemployment rate are factors contributing to more crimes in any specific area of the city.
  4. Suggest ways for reducing crime based on the results of our analysis

Necessary Imports

In [1]:
from datetime import datetime
from IPython.display import display

from arcgis import GIS
from arcgis.features.analyze_patterns import find_hot_spots
from arcgis.features.enrich_data import enrich_layer
from arcgis.features.manage_data import overlay_layers
from arcgis.features.find_locations import find_existing_locations 

Connect to your GIS

In [2]:
gis = GIS(profile="your_online_profile")

Get the data for the analysis

Search for CrimeAnalysisData layer in ArcGIS Online. We can search for content shared by users outside our organization by setting outside_org to True.

In [3]:
items = gis.content.search('title:CrimeAnalysisData owner:api_data_owner', 'feature layer')
In [4]:
for item in items:
    display(item)
CrimeAnalysisData
This service has all of the data needed to work through the Analyzing Violent Crime case study. Much of the data was obtained from www.cityofchicago.orgFeature Layer Collection by api_data_owner
Last Modified: May 28, 2019
0 comments, 1 views

We will use the first item for our analysis. Since the item is a Feature Layer Collection, accessing the layers property will give us a list of FeatureLayer objects.

In [5]:
crime_item = items[0]
In [6]:
lyrs = crime_item.layers

The code below cycles through the layers and prints their names.

In [7]:
for lyr in lyrs:
    print(lyr.properties.name)
Public High Schools
Liquor Vendors
Violent Crime 2014
Analysis Boundary

We'll get the second layer and assign it to the violent_crimes variable. Similarly, get the analysis_boundary layer.

In [8]:
violent_crimes = lyrs[2] # Violent Crime 2014
In [9]:
analysis_boundary = lyrs[3] # Analysis Boundary

Let's visualize the crime incidents on a map of Chicago.

In [10]:
crime_map = gis.map('Chicago')
crime_map
Out[10]:
In [11]:
crime_map.add_layer(violent_crimes)

It is difficult to discern spatial patterns with so many points on the map. To make sense of the more than 22,000 crime points, and over 1,500 business points, we will map them using hot spot analysis.

We can add a number of different layer objects such as FeatureLayer, FeatureCollection, ImageryLayer, MapImageLayer to the map by calling the add_layer() method.

Create a hot spot map of violent crime densities

ArcGIS has a set of tools to help us identify, quantify and visualize satial patterns in our data by identifying areas of statistically significant clusters.

The find_hot_spots tool allows us to visualize areas having such clusters.

In [12]:
crime_hot_spots = find_hot_spots(violent_crimes,
                                 output_name='ViolentCrimeHotSpots' + str(datetime.now().microsecond),
                                 bounding_polygon_layer=analysis_boundary)
In [13]:
crime_hot_spots
Out[13]:
ViolentCrimeHotSpots901831
Feature Layer Collection by arcgis_python
Last Modified: June 03, 2019
0 comments, 0 views
In [14]:
crime_spots_map = gis.map('Chicago')
crime_spots_map
Out[14]: