-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1map.py
executable file
·55 lines (48 loc) · 1.98 KB
/
q1map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import sys
sys.path.append('.')
import matplotlib
matplotlib.use('Agg')
from matplotlib.path import Path
from rtree import index as rtree
import numpy, shapefile, time
def findNeighborhood(location, index, neighborhoods):
match = index.intersection((location[0], location[1], location[0], location[1]))
for a in match:
if any(map(lambda x: x.contains_point(location), neighborhoods[a][1])):
return a
return -1
def readNeighborhood(shapeFilename, index, neighborhoods):
sf = shapefile.Reader(shapeFilename)
for sr in sf.shapeRecords():
if sr.record[1] not in ['New York', 'Kings', 'Queens', 'Bronx']: continue
paths = map(Path, numpy.split(sr.shape.points, sr.shape.parts[1:]))
bbox = paths[0].get_extents()
map(bbox.update_from_path, paths[1:])
index.insert(len(neighborhoods), list(bbox.get_points()[0])+list(bbox.get_points()[1]))
neighborhoods.append((sr.record[3], paths))
neighborhoods.append(('UNKNOWN', None))
def parseInput():
for line in sys.stdin:
line = line.strip()
values = line.split(',')
if len(values)>1 and values[0]!='medallion':
yield values
def mapper():
index = rtree.Index()
neighborhoods = []
readNeighborhood('ZillowNeighborhoods-NY.shp', index, neighborhoods)
# read taxi trip and fare data
for values in parseInput():
pickup_neighborhood=-1 # default as first
total = -1 # default as first
drv_lcn = values[1]
if len(values) == 14: # trip_data
pickup_location = (float(values[10]), float(values[11]))
pickup_neighborhood = findNeighborhood(pickup_location, index, neighborhoods)
print '%s\t%s\t%s' % (drv_lcn, neighborhoods[pickup_neighborhood][0], total)
else: # trip_fare
total = float(values[10])
print '%s\t%s\t%s' % (drv_lcn, pickup_neighborhood, total)
if __name__=='__main__':
mapper()