How many people are entering / leaving the 1 Train?

Demin Hu
Data Mining the City
3 min readOct 20, 2017

In this Project, I want to visualize how many people are entering/ leaving the subway station at different date and different time.

I get the MTA data set of the turnstile gate of New York subway stations. So, generally the turnstile gates send the data of usage every 4 hours. In the project, I try to calculate the number of people during that 4 hours and generate the same number of red points moving towards the subway station. In this way, the density of points is showing how crowded the station is.

After this visualization, I try to find other elements to show that “city as a platform”. Now, I collected the data of some food carts owned by the NYC government. I try to make an assumption that food carts should move with the actual flow of subway traffic.

MTA data set

Food cart data

Code

import spatialpixel.mapping.slippymapper as slippymapper
import csv
def setup():
size(1200, 1000, P2D)

global times, starttime, endtime, timeindex
times = ['traffic000', 'traffic400', 'traffic800', 'traffic1200', 'traffic1600']
timeindex = 0
starttime = times[timeindex]
endtime = times[timeindex+1]
global stationsmarkers
stationsmarkers = []

global nyc
nyc = slippymapper.SlippyMapper(40.8079337, -73.9482598, 13, 'carto-dark', width, height)
with open('foodcart.csv') as f:
reader = csv.reader(f)
header = reader.next()
for row in reader:
lat = float(row[2])
lon = float(row[3])
data = {
'lat' :lat,
'lon' :lon,
}
nyc.addMarker(lat, lon, FoodMarker(data))
with open('1007entry.csv') as f:
reader = csv.reader(f)
header = reader.next()
for row in reader:
#this is where get the data of entrance
#the rownumber can vary from 3 to 7
latitude = float(row[2])
longitude = float(row[1])
data = {
'traffic000':float(row[3]),
'traffic400':float(row[4]),
'traffic800':float(row[5]),
'traffic1200':float(row[6]),
'traffic1600':float(row[7]),
'latitude':latitude,
'longitude':longitude,
'name':row[0]
}
m = StationMarker(data)
stationsmarkers.append(m)
nyc.addMarker(latitude, longitude, m)
nyc.addMarker(latitude, longitude, StationNumber(data))
nyc.render()
def draw():
background(255)
nyc.draw()
class FoodMarker(slippymapper.DataMarker):
def drawMarker(self, x, y, marker):
lat = self.data['lat']
lon = self.data['lon']
marker.noStroke()
marker.fill(255)
marker.ellipse(x, y, 8, 8)

class StationNumber(slippymapper.DataMarker):
def drawMarker(self, x, y, marker):

traffic1200 = self.data['traffic1200']
traffic1600 = self.data['traffic1600']

diamx = traffic1600 - traffic1200
marker.noStroke()
marker.noFill()

if dist(x, y, mouseX, mouseY) < diamx/15 :
marker.noStroke()
marker.fill(255,0,0)

marker.rect(x, y-4, diamx/2, 10)
if dist(x, y, mouseX, mouseY) < diamx/15 :
marker.fill(255,0,0)
marker.textSize(30)
marker.text(traffic1600-traffic1200, x+diamx/2, y)
marker.text("people/4hour", x+diamx/2, y+30)
def keyPressed():
global stationsmarkers
for m in stationsmarkers:
m.reset()

global times, starttime, endtime, timeindex
times = ['traffic000', 'traffic400', 'traffic800', 'traffic1200', 'traffic1600']
timeindex += 1
if timeindex >= len(times) - 1:
timeindex = 0
starttime = times[timeindex]
endtime = times[timeindex+1]
class StationMarker(slippymapper.DataMarker):
def reset(self):
self.points = []

def __init__(self, data):
super(StationMarker, self).__init__(data)

self.points = []
def drawMarker(self, x, y, marker):
global starttime, endtime

trafficstart = self.data[starttime]
trafficend = self.data[endtime]

diam = log(trafficstart)
marker.stroke(11,151,240)
marker.noFill()
marker.ellipse(x, y, diam/2, diam/2)
marker.textSize(20)
marker.text(starttime, 150, 100)
marker.text(endtime, 280, 100)
marker.fill(255,0,0)
marker.noStroke()
marker.ellipse(170,150,20,20)
marker.text("people-to-station", 200, 155)
marker.fill(255)
marker.ellipse(170,180,20,20)
marker.text("food-cart", 200, 185)
numPoints = trafficend - trafficstart
if len(self.points) == 0:
for i in xrange(numPoints):
self.points.append(MovingPoint(x, y, -1))

for pt in self.points:
pt.draw()

class MovingPoint(object):
def __init__(self, x, y, direction=1):
self.x = x
self.y = y
self.theta = random(0, TWO_PI)
self.radius = int(random(0, 20))
self.direction = direction

def draw(self):
stroke(255,0,0)
x = self.x + ((self.radius + self.direction * frameCount) % 20) * cos(self.theta)*1.2
y = self.y + ((self.radius + self.direction * frameCount) % 20) * sin(self.theta)*1.2
point(x, y)

--

--