minor grid rendering test touchups

This commit is contained in:
Dane Springmeyer 2012-08-13 16:52:31 -07:00
parent 9f450bd986
commit d8bb4050c5

View file

@ -15,6 +15,51 @@ def setup():
# from another directory we need to chdir() # from another directory we need to chdir()
os.chdir(execution_path('.')) os.chdir(execution_path('.'))
def show_grids(name,g1,g2):
g1_file = '/tmp/mapnik-%s-actual.json' % name
open(g1_file,'w').write(json.dumps(g1,sort_keys=True))
g2_file = '/tmp/mapnik-%s-expected.json' % name
open(g2_file,'w').write(json.dumps(g2,sort_keys=True))
val = 'JSON does not match ->\n'
if g1['grid'] != g2['grid']:
val += ' X grid does not match\n'
else:
val += ' ✓ grid matches\n'
if g1['data'].keys() != g2['data'].keys():
val += ' X data does not match\n'
else:
val += ' ✓ data matches\n'
if g1['keys'] != g2['keys']:
val += ' X keys do not\n'
else:
val += ' ✓ keys match\n'
val += '\n\t%s\n\t%s' % (g1_file,g2_file)
return val
def show_grids2(name,g1,g2):
g2_expected = '../data/grids/mapnik-%s-actual.json' % name
if not os.path.exists(g2_expected):
# create test fixture based on actual results
open(g2_expected,'a+').write(json.dumps(g1,sort_keys=True))
return
g1_file = '/tmp/mapnik-%s-actual.json' % name
open(g1_file,'w').write(json.dumps(g1,sort_keys=True))
val = 'JSON does not match ->\n'
if g1['grid'] != g2['grid']:
val += ' X grid does not match\n'
else:
val += ' ✓ grid matches\n'
if g1['data'].keys() != g2['data'].keys():
val += ' X data does not match\n'
else:
val += ' ✓ data matches\n'
if g1['keys'] != g2['keys']:
val += ' X keys do not\n'
else:
val += ' ✓ keys match\n'
val += '\n\t%s\n\t%s' % (g1_file,g2_expected)
return val
# first pass impl where resolution is passed as render # first pass impl where resolution is passed as render
# time rather than encoding time, likely will be deprecated soon # time rather than encoding time, likely will be deprecated soon
grid_correct_old = {"keys": ["", "North West", "North East", "South West", "South East"], "data": {"South East": {"Name": "South East"}, "North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!!!! ##### ", " !!!!! ##### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$$ %%%%% ", " $$$$$ %%%%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]} grid_correct_old = {"keys": ["", "North West", "North East", "South West", "South East"], "data": {"South East": {"Name": "South East"}, "North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!!!! ##### ", " !!!!! ##### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$$ %%%%% ", " $$$$$ %%%%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]}
@ -44,7 +89,7 @@ def resolve(grid,row,col):
return grid['data'].get(key) return grid['data'].get(key)
def create_grid_map(width,height,marker=True): def create_grid_map(width,height,sym):
ds = mapnik.MemoryDatasource() ds = mapnik.MemoryDatasource()
context = mapnik.Context() context = mapnik.Context()
context.push('Name') context.push('Name')
@ -69,15 +114,8 @@ def create_grid_map(width,height,marker=True):
ds.add_feature(f) ds.add_feature(f)
s = mapnik.Style() s = mapnik.Style()
r = mapnik.Rule() r = mapnik.Rule()
if marker: sym.allow_overlap = True
symb = mapnik.MarkersSymbolizer() r.symbols.append(sym)
symb.width = mapnik.Expression('10')
symb.height = mapnik.Expression('10')
else:
symb = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png'))
symb.allow_overlap = True
r.symbols.append(symb)
s.rules.append(r) s.rules.append(r)
lyr = mapnik.Layer('Places') lyr = mapnik.Layer('Places')
lyr.datasource = ds lyr.datasource = ds
@ -87,31 +125,14 @@ def create_grid_map(width,height,marker=True):
m.layers.append(lyr) m.layers.append(lyr)
return m return m
def show_grids(name,g1,g2):
g1_file = '/tmp/mapnik-%s-actual.json' % name
open(g1_file,'w').write(json.dumps(g1,sort_keys=True))
g2_file = '/tmp/mapnik-%s-expected.json' % name
open(g2_file,'w').write(json.dumps(g2,sort_keys=True))
val = 'JSON does not match ->\n'
if g1['grid'] != g2['grid']:
val += ' X grid does not match\n'
else:
val += ' ✓ grid matches\n'
if g1['data'].keys() != g2['data'].keys():
val += ' X data does not match\n'
else:
val += ' ✓ data matches\n'
if g1['keys'] != g2['keys']:
val += ' X keys do not\n'
else:
val += ' ✓ keys match\n'
val += '\n\t%s\n\t%s' % (g1_file,g2_file)
return val
def test_render_grid_old(): def test_render_grid_old():
""" test old method """ """ test old method """
width,height = 256,256 width,height = 256,256
m = create_grid_map(width,height) symb = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png'))
sym = mapnik.MarkersSymbolizer()
sym.width = mapnik.Expression('10')
sym.height = mapnik.Expression('10')
m = create_grid_map(width,height,sym)
#print mapnik.save_map_to_string(m) #print mapnik.save_map_to_string(m)
ul_lonlat = mapnik.Coord(142.30,-38.20) ul_lonlat = mapnik.Coord(142.30,-38.20)
lr_lonlat = mapnik.Coord(143.40,-38.80) lr_lonlat = mapnik.Coord(143.40,-38.80)
@ -131,7 +152,10 @@ def test_render_grid_old():
def test_render_grid_new(): def test_render_grid_new():
""" test old against new""" """ test old against new"""
width,height = 256,256 width,height = 256,256
m = create_grid_map(width,height) sym = mapnik.MarkersSymbolizer()
sym.width = mapnik.Expression('10')
sym.height = mapnik.Expression('10')
m = create_grid_map(width,height,sym)
ul_lonlat = mapnik.Coord(142.30,-38.20) ul_lonlat = mapnik.Coord(142.30,-38.20)
lr_lonlat = mapnik.Coord(143.40,-38.80) lr_lonlat = mapnik.Coord(143.40,-38.80)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
@ -169,7 +193,10 @@ grid_feat_id2 = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West
def test_render_grid3(): def test_render_grid3():
""" test using feature id""" """ test using feature id"""
width,height = 256,256 width,height = 256,256
m = create_grid_map(width,height) sym = mapnik.MarkersSymbolizer()
sym.width = mapnik.Expression('10')
sym.height = mapnik.Expression('10')
m = create_grid_map(width,height,sym)
ul_lonlat = mapnik.Coord(142.30,-38.20) ul_lonlat = mapnik.Coord(142.30,-38.20)
lr_lonlat = mapnik.Coord(143.40,-38.80) lr_lonlat = mapnik.Coord(143.40,-38.80)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
@ -280,22 +307,19 @@ def test_line_rendering():
mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name']) mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name'])
utf1 = grid.encode() utf1 = grid.encode()
eq_(utf1,line_expected,show_grids('line',utf1,line_expected)) eq_(utf1,line_expected,show_grids('line',utf1,line_expected))
#open('test.json','w').write(json.dumps(grid.encode()))
point_expected = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]} point_expected = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
def test_point_symbolizer_grid(): def test_point_symbolizer_grid():
width,height = 256,256 width,height = 256,256
m = create_grid_map(width,height,marker=False) sym = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png'))
m = create_grid_map(width,height,sym)
ul_lonlat = mapnik.Coord(142.30,-38.20) ul_lonlat = mapnik.Coord(142.30,-38.20)
lr_lonlat = mapnik.Coord(143.40,-38.80) lr_lonlat = mapnik.Coord(143.40,-38.80)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
#mapnik.render_to_file(m,'test.png')
#print mapnik.save_map_to_string(m)
grid = mapnik.Grid(m.width,m.height) grid = mapnik.Grid(m.width,m.height)
mapnik.render_layer(m,grid,layer=0,fields=['Name']) mapnik.render_layer(m,grid,layer=0,fields=['Name'])
utf1 = grid.encode() utf1 = grid.encode()
#open('test.json','w').write(json.dumps(grid.encode()))
eq_(utf1,point_expected,show_grids('point-sym',utf1,point_expected)) eq_(utf1,point_expected,show_grids('point-sym',utf1,point_expected))