Permalink
Browse files

Tool for visualizing the output.

  • Loading branch information...
1 parent 4a55dca commit e351307eec5fd662103b6edbd60ad36d708c71b5 Schuyler Erle committed Aug 3, 2011
Showing with 65 additions and 0 deletions.
  1. +65 −0 mapnik_render.py
View
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+
+from mapnik import *
+import sys, random
+
+width, height = 4096, 4096
+rgbs = ["80", "a2", "ab"]
+base = "data/results"
+city = sys.argv[1]
+
+woe_id = None
+for line in file(base+"/intl_cities.txt"):
+ woe_id, name = line.strip().split(None,1)
+ if city == name: break
+if woe_id is None: raise Exception("Couldn't find the city '%s'" % city)
+
+m = Map(width, height, "+proj=latlong +datum=WGS84")
+m.background = Color('white')
+
+if city == "Tokyo":
+ register_fonts("/usr/share/fonts/truetype/takao")
+ font = "TakaoMincho Regular"
+else:
+ font = "DejaVu Sans"
+
+def append_style(name, *symbols):
+ s = Style()
+ r = Rule()
+ for symbol in symbols:
+ r.symbols.append(symbol)
+ s.rules.append(r)
+ m.append_style(name,s)
+
+random.shuffle(rgbs)
+fill = Color('#%s%s%s' % tuple(rgbs))
+hood = Layer('hood', "+proj=latlong +datum=WGS84")
+hood.datasource = Ogr(base=base,file=city+".json",layer="OGRGeoJSON")
+append_style("hood", PolygonSymbolizer(fill))
+hood.styles.append("hood")
+m.layers.append(hood)
+
+blocks = Layer('blocks',"+proj=latlong +datum=WGS84")
+blocks.datasource = Ogr(base=base,file="blocks_"+woe_id+".json",layer='OGRGeoJSON')
+append_style('blocks', LineSymbolizer(Color('rgb(50%,50%,50%)'),1.0))
+blocks.styles.append('blocks')
+m.layers.append(blocks)
+
+bounds = Layer('bounds', "+proj=latlong +datum=WGS84")
+bounds.datasource = Ogr(base=base,file=city+".json",layer="OGRGeoJSON")
+append_style("bounds", LineSymbolizer(Color('#222222'), 2.0))
+text = TextSymbolizer("name", font, 10, Color("black"))
+text.allow_overlap = False
+text.avoid_edges = True
+text.wrap_width = 15
+halo_fill = [min(x+32, 255) for x in (fill.r, fill.g, fill.b)]
+text.halo_fill = Color(*halo_fill)
+text.halo_radius = 1
+append_style("bounds_label", text)
+# <TextSymbolizer name="NAME" face_name="DejaVu Sans Bold" size="7" fill="black" halo_fill= "#DFDBE3" halo_radius="1" wrap_width="20" spacing="5" allow_overlap="false" avoid_edges="false" min_distance="10"/>
+bounds.styles.append("bounds")
+bounds.styles.append("bounds_label")
+m.layers.append(bounds)
+
+m.zoom_to_box(hood.envelope())
+render_to_file(m,city+'.png', 'png')

0 comments on commit e351307

Please sign in to comment.