-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeohashingcomic.py
More file actions
executable file
·260 lines (216 loc) · 7.16 KB
/
geohashingcomic.py
File metadata and controls
executable file
·260 lines (216 loc) · 7.16 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Module to create Geohashing Comics for any date.
"""
import urllib
import sys
import os
import datetime
from PIL import Image
from geohashing import Geohashing
class GeohashingComic(object):
"""
Class to create Geohashing Comics for any date.
"""
# read the digits (and the -), the dots don't move
digits = {
c: Image.open(c.replace("-", "m") + ".png")
for c in "0123456789abcdef-"
}
def __init__(self,
date=datetime.date(2005, 5, 26),
dowjones=10458.68,
location=(37.421542, -122.085589),
):
self.gh = Geohashing(date, dowjones, location)
# Geohashing instance to calculate the hash and new latitude and longitude.
self.im = None
# The final image.
def draw_date(self):
"""
Draw the year, month and day.
"""
for i, c in enumerate("{:04d}".format(self.gh.date.year)):
self.im.paste(self.digits[c], (24 + 12 * i, 78))
for i, c in enumerate("{:02d}".format(self.gh.date.month)):
self.im.paste(self.digits[c], (88 + 11 * i, 78))
for i, c in enumerate("{:02d}".format(self.gh.date.day)):
self.im.paste(self.digits[c], (120 + 12 * i, 78))
def draw_dowjones(self):
"""
Draw the dow jones index.
"""
hofs = 165
for i, c in enumerate("{:8.2f}".format(self.gh.dowjones)):
checks_raw = [
(i == 1, -3), # does this work properly when the DJ is above 20000?
(i == 5, -3), # after the dot
]
offset_change = sum(change for check, change in checks_raw if check)
hofs += offset_change
if not (c == '.' or c == ' '): # do not do the dot or spaces
self.im.paste(self.digits[c], (hofs + 10 * i, 78))
def draw_hash(self):
"""
Draw first and second half of hash.
"""
hofs = 301
for c in self.gh.hexdig[0:16]:
self.im.paste(self.digits[c], (hofs, 82))
self.im.paste(self.digits[c], (hofs - 9, 129))
hofs += self.digits[c].size[0]
hofs += 14
hofs2 = 466
for c in self.gh.hexdig[16:32]:
self.im.paste(self.digits[c], (hofs, 82))
self.im.paste(self.digits[c], (hofs2, 129))
hofs += self.digits[c].size[0]
hofs2 += self.digits[c].size[0]
def offset_latitude(self, i, c):
"""
Calculate offset for latitude drawing.
"""
checks_raw = [
(True, 10),
(c == '1' and i > 3, -5),
(c == '.', 2),
(c == '-', -1),
(c == '+', -1),
(c == ' ', -2),
]
offset_change = sum(change for check, change in checks_raw if check)
return offset_change
def draw_latitude_top(self):
"""
Draw latitude top.
"""
hofs = 25
for i, c in enumerate("{:+10.6f}".format(self.gh.lat)):
if c not in ' +.':
self.im.paste(self.digits[c], (hofs, 168))
hofs += self.offset_latitude(i, c)
def draw_latitude_bottom(self):
"""
Draw latitude bottom.
"""
hofs = 25 + 110
for i, c in enumerate("{:+10.6f}".format(self.gh.lat)):
if c not in ' +.' and i < 3:
self.im.paste(self.digits[c], (hofs, 266))
hofs += self.offset_latitude(i, c)
def offset_longitude(self, i, c):
"""
Calculate offset for longitude drawing.
"""
checks_raw = [
(True, 10),
(c == '1' and i > 4, -5),
(c == '.', 3),
(c == '-', -1),
(c == '+', -2),
(c == ' ', -2),
]
offset_change = sum(change for check, change in checks_raw if check)
return offset_change
def draw_longitude_top(self):
"""
Draw longitude.
"""
hofs = 143
for i, c in enumerate("{:+11.6f}".format(self.gh.lon)):
if c not in ' +.':
self.im.paste(self.digits[c], (hofs, 169))
hofs += self.offset_longitude(i, c)
def draw_longitude_bottom(self):
"""
Draw longitude.
"""
hofs = 143 + 138
for i, c in enumerate("{:+11.6f}".format(self.gh.lon)):
if c not in ' +.' and i < 4:
self.im.paste(self.digits[c], (hofs, 269))
hofs += self.offset_longitude(i, c)
def draw_coordinate_decimals(self):
"""
Draw decimals of lat/lon in final coordinates.
"""
for i, (c1, c2) in enumerate(zip(str(self.gh.lato)[2:8], str(self.gh.lono)[2:8])):
self.im.paste(self.digits[c1], (300 + 10 * i, 174))
self.im.paste(self.digits[c1], (176 + 10 * i, 267))
self.im.paste(self.digits[c2], (450 + 10 * i, 174))
self.im.paste(self.digits[c2], (335 + 10 * i, 269))
def make(self):
"""Creating the image"""
self.im = Image.open("geohashingclean.png")
self.draw_date()
self.draw_dowjones()
self.draw_hash()
self.draw_latitude_top()
self.draw_longitude_top()
self.draw_latitude_bottom()
self.draw_longitude_bottom()
self.draw_coordinate_decimals()
def show(self):
"""
Show the comic on the screen.
"""
if not self.im:
self.make()
self.im.show(command='display')
def cgi(self, format='png'):
"""
Print out the comic as a cgi-bin output.
:param format: The format of the image.
:type format: str
"""
if not self.im:
self.make()
print "Content-Type: image/png\r\n\r\n",
# there should be a better way to do this
# self.im.save('/dev/stdout',format)
fn = "comics/{:d}-{:d}-{:d}_{:f}_{:f}_{:f}.png".format(
self.gh.date.year, self.gh.date.month, self.gh.date.day, self.gh.dowjones, self.gh.lat, self.gh.lon)
self.im.save(fn, format)
oo = open(fn)
d = oo.read()
oo.close()
print d
def parse_arguments():
"""
Parse arguments.
:return: Arguments dictionary.
:type: dict
"""
args = {
'year': 2005,
'month': 5,
'day': 26,
'dowjones': 0.0,
'lat': 37.421542,
'lon': -122.085589,
'mode': 'cmd',
}
arg = ''
if 'QUERY_STRING' in os.environ:
arg = urllib.unquote(os.environ['QUERY_STRING'])
args['mode'] = 'cgi'
elif len(sys.argv) > 1:
arg = urllib.unquote(sys.argv[-1])
args['mode'] = 'cmd'
args2 = dict(a.split('=') for a in arg.split('&') if '=' in a)
args.update(args2)
return args
def main():
"""
Main method, either in CGI or command line mode.
"""
args = parse_arguments()
date = datetime.date(int(args['year']), int(args['month']), int(args['day']))
gc = GeohashingComic(date, float(args['dowjones']), (float(args['lat']), float(args['lon'])))
if args['mode'] == 'cgi':
gc.cgi()
else:
gc.show()
if __name__ == '__main__':
main()