Skip to content

Instantly share code, notes, and snippets.

@mikejs
Created April 10, 2010 07:41
Show Gist options
  • Save mikejs/361900 to your computer and use it in GitHub Desktop.
Save mikejs/361900 to your computer and use it in GitHub Desktop.
import sys
import mapnik2
import cairo
prj = mapnik2.Projection("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
m = mapnik2.Map(1280, 768, prj.params())
m.background = mapnik2.Color('white')
district_lyr = mapnik2.Layer('Districts')
district_lyr.datasource = mapnik2.PostGIS(
host='localhost', user='postgres', dbname='big_map',
table="big_map_district")
district_style = mapnik2.Style()
rule = mapnik2.Rule()
rule.filter = mapnik2.Expression("[state_abbrev] = 'ca'")
rule.symbols.append(mapnik2.PolygonSymbolizer(mapnik2.Color('rgb(60%, 60%, 60%)')))
district_style.rules.append(rule)
for i in xrange(0, 41):
rule = mapnik2.Rule()
rule.filter = mapnik2.Expression("[state_abbrev] = 'ca' and [percent_rural] = %d" % i)
sym = mapnik2.PolygonSymbolizer(mapnik2.Color('rgb(6%, 40%, 10%)'))
sym.fill_opacity = i / float(41)
rule.symbols.append(sym)
district_style.rules.append(rule)
text_style = mapnik2.Style()
rule = mapnik2.Rule()
rule.filter = mapnik2.Expression("[state_abbrev] = 'ca' and [percent_rural] >= 5 and [name] != '40'")
rule.symbols.append(mapnik2.TextSymbolizer(mapnik2.Expression("'District ' + [sld].replace('^00?', '') + ': ' + [percent_rural] + '%'"), "DejaVu Sans Book", 14, mapnik2.Color('black')))
text_style.rules.append(rule)
m.append_style('district', district_style)
m.append_style('text', text_style)
district_lyr.styles.append('district')
district_lyr.styles.append('text')
m.layers.append(district_lyr)
bbox = mapnik2.Box2d(-124.4095, 32.53, -114.13, 42)
m.zoom_to_box(bbox)
mapnik2.render_to_file(m, 'rural.png', 'png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment