Browse files

Merge pull request #7 from JoeGermuska/grid

add grid rendering
  • Loading branch information...
2 parents fa8e356 + 7eda11d commit dbc5491f21422dda523eb6f7382cbdc93dfe31da @onyxfish committed Aug 23, 2012
Showing with 45 additions and 3 deletions.
  1. +33 −0 invar/renderer.py
  2. +12 −3 ivtile
View
33 invar/renderer.py
@@ -9,6 +9,9 @@
import constants
import projections
+import json
+from os.path import splitext
+
class Renderer(multiprocessing.Process):
"""
A Mapnik renderer process.
@@ -69,6 +72,12 @@ class TileRenderer(Renderer):
"""
Renderer for tiles.
"""
+ def __init__(self, tile_queues, config, width=constants.DEFAULT_WIDTH, height=constants.DEFAULT_HEIGHT, filetype=constants.DEFAULT_FILE_TYPE, buffer_size=None, skip_existing=False, **kwargs):
+ super(TileRenderer, self).__init__(tile_queues, config, width, height, filetype, buffer_size, skip_existing)
+ self.grid = kwargs.get('grid',False)
+ self.key = kwargs.get('key',None)
+ self.fields = kwargs.get('fields',None)
+
def render(self, filename, tile_x, tile_y, zoom):
"""
Render a single tile to a given filename.
@@ -99,6 +108,30 @@ def render(self, filename, tile_x, tile_y, zoom):
image = mapnik.Image(self.width, self.height)
mapnik.render(self.mapnik_map, image)
image.save(filename, self.filetype)
+
+ if self.grid:
+ if self.key:
+ grid = mapnik.Grid(self.width, self.height)
+ else:
+ grid = mapnik.Grid(self.width, self.height, key=self.key)
+
+ fields = []
+ if self.fields:
+ fields.extend(self.fields)
+
+ mapnik.render_layer(self.mapnik_map,grid,layer=0,fields=fields)
+ # then encode the grid array as utf, resample to 1/4 the size, and dump features
+ # this comes from https://github.com/springmeyer/gridsforkids/blob/master/generate_tiles.py
+ # with little consideration
+ grid_utf = grid.encode('utf', resolution=4, features=True)
+
+ # client code uses jsonp, so fake by wrapping in grid() callback
+ base, ext = splitext(filename)
+ grid_filename = "%s.grid.json" % base
+ print 'Rendering %s' % (grid_filename)
+ open(grid_filename,'wb').write('grid(' + json.dumps(grid_utf) + ')')
+
+
class FrameRenderer(Renderer):
"""
View
15 ivtile
@@ -23,7 +23,11 @@ class IVTile(invar.InvarUtility):
self.argparser.add_argument('lon_2', type=float, help="Most eastern longitude")
self.argparser.add_argument('min_zoom', help="Minimum zoom level to render", type=int, default=DEFAULT_MIN_ZOOM)
self.argparser.add_argument('max_zoom', help="Maximum zoom level to render", type=int, default=DEFAULT_MAX_ZOOM)
-
+
+ self.argparser.add_argument('-g', '--grid', action="store_true", help="Force grid JSON to be rendered alongside each tile. If -k or -f options are provided, this is inferred and need not be specified.")
+ self.argparser.add_argument('-k', '--key', help="For grid rendering, the column in the associated dataset which is a unique ID for a feature. If not specified, will use mapnik default ('__id__')")
+ self.argparser.add_argument('-f', '--fields', help="For grid rendering, a comma separated list of fields associated with each feature which will be included in grid JSON.")
+
def main(self):
if not os.path.isdir(self.args.output_dir):
os.mkdir(self.args.output_dir)
@@ -87,9 +91,14 @@ class IVTile(invar.InvarUtility):
print 'Using %i processes to render %i tiles' % (self.args.process_count, tile_count)
processes = []
-
+ try:
+ fields = self.args.fields.split(",")
+ except:
+ fields = None
+
for i in range(self.args.process_count):
- renderer = invar.TileRenderer(tile_queues, self.args.config, self.args.width, self.args.height, buffer_size=self.args.buffer, skip_existing=self.args.skip_existing)
+ grid = (self.args.grid or self.args.fields or self.args.key)
+ renderer = invar.TileRenderer(tile_queues, self.args.config, self.args.width, self.args.height, buffer_size=self.args.buffer, skip_existing=self.args.skip_existing, grid=grid, key=self.args.key, fields=fields)
renderer.start()
processes.append(renderer)

0 comments on commit dbc5491

Please sign in to comment.