Download Summary - All IT eBooks

Transcript
Chapter 6
rule.filter = mapnik.Filter("[tally] = 0")
rule.symbols.append(line_symbol)
style = mapnik.Style()
style.rules.append(rule)
map.append_style("unused_road_style", style)
Notice that we use a mapnik.PostGIS() datasource so that the map layer takes its
data directly from our PostGIS database.
Next, we need to define a map layer for the roads which were used (that is, the
roads which have a tally value of 1 or more). This map layer, which we will call
used_roads, will have a separate mapnik.Rule() for each unique tally value. This
allows us to assign a different color to each unique tally value so that the color used
for each road segment varies according to that segment's tally. To implement this,
we will need a function that calculates the mapnik.Stroke() to use for a given tally
value. Here is that function, which you should place near the top of your program:
def calc_stroke(value, max_value):
fraction = float(value) / float(max_value)
def interpolate(start_value, end_value, fraction):
return start_value + (end_value - start_value) * fraction
r = interpolate(0.7, 0.0, fraction)
g = interpolate(0.7, 0.0, fraction)
b = interpolate(1.0, 0.4, fraction)
color = mapnik.Color(int(r*255), int(g*255), int(b*255))
width = max(4.0 * fraction, 1.5)
return mapnik.Stroke(color, width)
The interpolate() helper function is used to calculate a color range from pale
blue to dark blue. We also adjust the width of the displayed road segment according
to the tally so that more frequently used roads are drawn with a wider stroke on
the map.
If you want, you can change the starting and ending colors to make the
heatmap more colorful. As mentioned earlier, we are just using shades of
blue so that the heatmap makes sense when printed in black and white.
[ 167 ]