My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
#!/usr/bin/env python

# maketiles.py

shapespath = '../election-data/shapes/detailed'
tilespath = '../election-tiles/tiles'

import magick
import math
import os
import random
import re
import shutil
import stat
import sys
import time

from geo import Geo
import shpUtils
import states

def randomColor():
return hh() + hh() + hh()

def randomGray():
h = hh()
return '#' + h + h + h

def hh():
return '%02X' %( random.random() *128 + 96 )

placesByName = {}
def placeByName( place ):
name = place['name']
if name not in placesByName:
placesByName[name] = {
'place': place,
'color': randomColor()
}
return placesByName[name]

def filterCONUS( places ):
result = []
for place in places:
state = place['name']
if state == 'Alaska': continue
if state == 'Hawaii': continue
if state == 'Puerto Rico': continue
result.append( place )
return result

def placesBounds( places ):
bounds = [ [ None, None ], [None, None ] ]
for place in places:
bounds = geo.extendBounds( bounds, place['bounds'] )
return bounds

def readFile( filename ):
f = open( filename, 'rb' )
data = f.read()
f.close()
return data

def writeFile( filename, data ):
f = open( filename, 'wb' )
f.write( data )
f.close()

def generate( state, zoom ):
global geo, scaleoffset
print '----------------------------------------'
print 'Generating %s zoom %d' %( state, zoom )
scale = 10

geo = Geo( zoom, 256*scale )
pixgeo = Geo( zoom, 256 )

#exec re.sub( '.+\(', 'data = (', readFile( '%s/%s.js' %( shapespath, state ) ) )
json = readFile( '%s/%s.js' %( shapespath, state ) )
exec re.sub( '^.+\(', 'data = (', json )
places = data['places']

#t1 = time.time()

places = filterCONUS( places )

#outer = pixgeo.pixFromGeoBounds( featuresBounds(features) )
bounds = placesBounds( places )
outer = pixgeo.pixFromGeoBounds( bounds )
outer = pixgeo.inflateBounds( outer, 8 )
gridoffset, gridsize = pixgeo.tileBounds( outer )
scaleoffset = pixgeo.scalePoint( gridoffset, scale )
print 'Offset:[%d,%d], Size:[%d,%d]' %( gridoffset[0], gridoffset[1], gridsize[0], gridsize[1] )

draw = [ 'scale .1,.1\n' ]

draw.append( 'stroke-width 10\n' )
drawPlaces( draw, places )

cmdfile = 'draw.tmp'
writeFile( cmdfile, ''.join(draw) )

#t2 = time.time()
#print '%0.3f seconds to generate commands' %( t2 - t1 )

crop = True
if crop:
cropcmd = '-crop 256x256'
else:
cropcmd = ''
blank = magick.blank( gridsize )
base = '%s/%s/%s-%d' %( tilespath, state, state, zoom )
command = ( '%s -draw "@%s" %s ' + base + '.png' )%( blank, cmdfile, cropcmd )
#command = ( '%s -draw "@draw.cmd" %s -depth 8 -type Palette -floodfill 0x0 white -background white -transparent-color white ' + base + '.png' )%( blank, cropcmd )
#command = ( 'null: -resize %dx%d! -floodfill 0x0 white -draw "@draw.cmd" %s -depth 8 -type Palette -background white -transparent white -transparent-color white ' + base + '.png' )%( gridsize[0], gridsize[1], cropcmd )
#command = 'null: -resize %(cx)dx%(cy)d! -draw "@draw.cmd" %(crop)s tile%(zoom)d.png' %({
# 'cx': gridsize[0],
# 'cy': gridsize[1],
# 'crop': crop,
# 'zoom': zoom
#})
magick.convert( command )
if crop:
xyCount = 2 << zoom
n = 0
# TODO: refactor
xMin = gridoffset[0] / 256
xMinEdge = max( xMin - 2, 0 )
yMin = gridoffset[1] / 256
yMinEdge = max( yMin - 2, 0 )
xN = gridsize[0] / 256
yN = gridsize[1] /256
xLim = xMin + xN
xLimEdge = min( xLim + 2, xyCount )
yLim = yMin + yN
yLimEdge = min( yLim + 2, xyCount )
nMoving = xN * yN
nCopying = ( xLimEdge - xMinEdge ) * ( yLimEdge - yMinEdge ) - nMoving
print 'Moving %d tiles, copying %d blank tiles...' %( nMoving, nCopying )
t1 = time.time()
for y in xrange( yMinEdge, yLimEdge ):
for x in xrange( xMinEdge, xLimEdge ):
target = '%s-%d-%d.png' %( base, y, x )
if xMin <= x < xLim and yMin <= y < yLim:
if xN == 1 and yN == 1:
source = '%s.png' %( base )
else:
source = '%s-%d.png' %( base, n )
if os.path.exists( target ): os.remove( target )
if os.stat(source)[stat.ST_SIZE] > 415:
os.rename( source, target )
else:
os.remove( source )
shutil.copy( 'blanktile.png', target )
n += 1
else:
shutil.copy( 'blanktile.png', target )
t2 = time.time()
print '%0.3f seconds to move files' %( t2 - t1 )

def drawPlaces( draw, places ):
global geo, scaleoffset
nPolys = nPoints = 0
for place in places:
placename = place['name']
color = randomGray()
opacity = .12
alpha = '%02X' % int( opacity * 255.0 )
for shape in place['shapes']:
nPolys += 1
draw += '''
fill %s%s
stroke #00000040
polygon''' %( color, alpha )
points = shape['points']
n = len(points) # - 1
nPoints += n
for j in xrange(n):
point = geo.pixFromGeoPoint( points[j] )
draw += ' %d,%d' %( point[0] - scaleoffset[0], point[1] - scaleoffset[1] )
print '%d points in %d polygons' %( nPoints, nPolys )

for z in xrange(0,6):
generate( 'us', z )

for z in xrange(5,9):
for state in states.array:
#if state['name'] != 'Alaska': # temp
generate( state['abbr'].lower(), z )

for z in xrange(9,10):
generate( 'hi', z )
generate( 'nj', z )

for z in xrange(9,11):
generate( 'ct', z )
generate( 'de', z )
generate( 'ma', z )
generate( 'md', z )
generate( 'nh', z )
generate( 'pr', z )
generate( 'ri', z )
generate( 'vt', z )

for z in xrange(9,12):
generate( 'dc', z )

print 'Done!'

Change log

r95 by election.maps.2008 on Apr 5, 2008   Diff
Tile path and opacity updates
Go to: 
Project members, sign in to write a code review

Older revisions

r52 by election.maps.2008 on Mar 5, 2008   Diff
Don't color code state/county tiles,
add some more zoom levels
r24 by election.maps.2008 on Mar 4, 2008   Diff
Add primary dates to state selector,
change wording
r15 by election.maps.2008 on Mar 3, 2008   Diff
Better fix for Aleutian wraparound
error
All revisions of this file

File info

Size: 5427 bytes, 208 lines
Powered by Google Project Hosting