/
zipdecodes.py
57 lines (48 loc) · 1.62 KB
/
zipdecodes.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
56
57
from pyprocessing import *
# Data consists of four columns: zip, orthoX, orthoY, place name in tab separated file
data = (l.strip("\n").split("\t") for l in open("./zipcodes.csv", "r"))
# Go ahead and convert orthographic projections to floats
data = [(l[0], float(l[1]), float(l[2]), l[3]) for l in data]
cols = zip(*data)
min_x = min(cols[1])
max_x = max(cols[1])
min_y = min(cols[2])
max_y = max(cols[2])
prefix = ''
font = None
def setup():
global font
size(800,600) # create a window
font = createFont("Times New Roman", 24)
textFont(font)
smooth() # turn on anti-aliasing
noLoop() # don't call the draw function repeatedly
def keyPressed():
"""Event handler for keypresses, auto called by processing.
Only redraw the screen when there's been valid data input."""
global prefix
if key.char and key.char.isdigit():
prefix = prefix + key.char
redraw()
elif key.code == BACKSPACE:
prefix = prefix[:-1]
redraw()
def draw():
background(0) # Paint the whole screen black
yellow = color(240, 240, 100) # add some colors
red = color(240, 0, 0)
for zipcode in data:
if prefix and not zipcode[0].startswith(prefix):
pnt_color = red # zipcodes not in our partial zip
else:
pnt_color = yellow
coords = (int(map(zipcode[1], min_x, max_x, 50, 750)),
int(map(zipcode[2], max_y, min_y, 50, 550)))
stroke(pnt_color)
point(*coords)
# Show the zipcode prefix if any
if prefix:
fill(yellow)
text(prefix, 15, 30)
if __name__ == '__main__':
run()