Manhattan Crime Incidents in a Day

Shaochun Zhou
Data Mining the City
2 min readOct 20, 2017

Description:

Do you remember what you were doing on Thursday, June 1st, 2017? For me it was only a typical workday of my summer internship. Across New York City a million stories emerge over the course of a single day. This project explores crime incidents across New York City over the course of a single day.

354 crime incidents of all categories!

I’ve created a crime incident map to visualize where, when and what kind of crime that happened on this single day, to help you protect yourselves from appearing in some place in a certain time of a day.

Data Source:

NYPD Complaint Data Current YTD, provided by NYPD, available at: https://data.cityofnewyork.us/Public-Safety/NYPD-Complaint-Data-Current-YTD/5uac-w243

Code:

import spatialpixel.mapping.slippymapper as slippymapper
import csv
import datetime
def setup():
size(1000,800,P2D)
global nyc, latitude, longitude, reader, header, time1, infoList, cacheList, delta, threshold, crimeCat, font1, font2, font3, font4
global cacheInputCounter, cacheOutputCounter, locationCounter, listInitialCounter, infoListOutput
cacheOutputCounter = 0
locationCounter=0
cacheInputCounter = 0
listInitialCounter = 0
infoListOutput = 0
infoList = []
cacheList = []

font1 = createFont("Arial Black",20)
font2 = createFont("Calibri",20)
font3 = createFont("Arial Black",30)
font4 = createFont("Arial Black",20)

time1 = datetime.datetime(2017,6,1,0,0,0)
threshold = time1 + datetime.timedelta(minutes=60)
defaultday = datetime.datetime(1900,1,1,0,0,0)
delta = time1 - defaultday

nyc = slippymapper.SlippyMapper(40.775070, -73.970252, 12, "carto-dark",width,height)

with open('data.csv') as f:
reader = csv.reader(f)
header = reader.next()

for row in reader:
infoList.append([])
cacheList.append([])

latitude = float(row[6])
longitude = float(row[7])
crimeCat = row[2]
tm = datetime.datetime.strptime(row[1],"%X")
tm = tm + delta
infoList[listInitialCounter] = [latitude,longitude,tm,crimeCat]
cacheList[listInitialCounter] = [1,1,1]
listInitialCounter += 1

infoList.append([])
infoList[listInitialCounter] = [1,1,1,1]

nyc.render()
def draw():
global nyc, time1, threshold, infoList, infoListOutput, cacheInputCounter,cacheOutputCounter, marker
global cacheOutputMax
nyc.draw()


while infoList[infoListOutput][2] <> 1 and infoList[infoListOutput][2] >= time1 and infoList[infoListOutput][2] < threshold :

cacheList[cacheInputCounter][0] = infoList[infoListOutput][0]
cacheList[cacheInputCounter][1] = infoList[infoListOutput][1]
cacheList[cacheInputCounter][2] = infoList[infoListOutput][3]
cacheInputCounter +=1
infoListOutput += 1


for cacheOutputCounter in range(len(cacheList)):
if cacheList[cacheOutputCounter][0] <> 1:
fill(255)
ellipse (nyc.lonToX(cacheList[cacheOutputCounter][1]),nyc.latToY(cacheList[cacheOutputCounter][0]),15,15)
cacheOutputMax = cacheOutputCounter

for cacheMarker in range(cacheOutputMax):
if dist(mouseX,mouseY,nyc.lonToX(cacheList[cacheMarker][1]),nyc.latToY(cacheList[cacheMarker][0])) < 7.5:
textAlign(LEFT)
fill(255,0,0)
text(cacheList[cacheMarker][2],nyc.lonToX(cacheList[cacheMarker][1])+5,nyc.latToY(cacheList[cacheMarker][0]))
marker = Marker(locationCounter, str(time1.strftime("%H:%M")),str(threshold.strftime("%H:%M")))
marker.timeBar()

marker.title()
fill(255,0,0)
textAlign(LEFT)
text("Total Incidents:"+str(infoListOutput),80,180)
def mouseClicked():
global time1, threshold, locationCounter, cacheInputCounter, cacheList, infoListOutput

for row in xrange(listInitialCounter):
for col in xrange (2):
cacheList[row] = [1,1,1]

if threshold == datetime.datetime(2017,6,2,0,0,0):
time1 = datetime.datetime(2017,6,1,0,0,0)
threshold = time1 + datetime.timedelta(minutes=60)

locationCounter = 0
infoListOutput = 0

else:
time1 = time1 + datetime.timedelta(minutes=60)
threshold = time1 + datetime.timedelta(minutes=60)

locationCounter += 1
cacheInputCounter = 0


class Marker(object):
global font1,font2,font3,font4
def __init__(self, location, content1, content2):
self.location = location
self.content1 = content1
self.content2 = content2
textAlign(CENTER,CENTER)


def timeBar(self):
fill(255,0,0)
textSize(10)
stroke(255,0,0)
strokeWeight(5)
textFont(font1)
text("Time Bar",140,710)
line(140,700,830,700)
line(140+30*self.location,690,140+30*self.location,700)
textFont(font2)
text(self.content1+"--"+self.content2,140+30*self.location,670)

def title(self):
noStroke()
fill(255)
textFont(font3)
text("Manhattan Crime Incidents in a Day",width/2,50)
textFont(font4)
text("--Interval: One Hour",width/2,80)

--

--