2012-01-08 14:01:54 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-07-25 01:58:03 +00:00
|
|
|
import sys
|
2014-07-25 18:49:05 +00:00
|
|
|
import mapnik
|
2014-08-05 02:18:38 +00:00
|
|
|
#mapnik.logger.set_severity(mapnik.severity_type.None)
|
2014-08-03 19:27:55 +00:00
|
|
|
#mapnik.logger.set_severity(mapnik.severity_type.Debug)
|
2013-05-20 22:07:20 +00:00
|
|
|
import shutil
|
2012-01-08 14:01:54 +00:00
|
|
|
import os.path
|
2013-03-25 02:56:14 +00:00
|
|
|
from compare import compare, compare_grids
|
2014-09-06 23:04:01 +00:00
|
|
|
import platform
|
2014-12-09 15:31:07 +00:00
|
|
|
import glob
|
2012-11-20 23:08:53 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
2012-01-08 14:01:54 +00:00
|
|
|
|
2012-10-03 04:18:45 +00:00
|
|
|
visual_output_dir = "/tmp/mapnik-visual-images"
|
|
|
|
|
2012-03-20 21:06:22 +00:00
|
|
|
defaults = {
|
2014-12-09 15:31:07 +00:00
|
|
|
'status': True,
|
2013-03-11 20:36:31 +00:00
|
|
|
'sizes': [(500, 100)],
|
2013-03-25 02:56:14 +00:00
|
|
|
'scales':[1.0,2.0],
|
2013-05-20 19:45:06 +00:00
|
|
|
'agg': True,
|
2013-05-22 22:42:28 +00:00
|
|
|
'cairo': mapnik.has_cairo(),
|
2013-07-23 23:37:25 +00:00
|
|
|
'grid': mapnik.has_grid_renderer()
|
2012-03-20 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 21:03:53 +00:00
|
|
|
cairo_threshold = 10
|
2014-07-22 21:55:06 +00:00
|
|
|
agg_threshold = 0
|
2014-10-06 21:31:38 +00:00
|
|
|
grid_threshold = 5
|
2014-09-06 23:04:01 +00:00
|
|
|
if 'Linux' == platform.uname()[0]:
|
2013-05-31 21:03:53 +00:00
|
|
|
# we assume if linux then you are running packaged cairo
|
|
|
|
# which is older than the 1.12.14 version we used on OS X
|
|
|
|
# to generate the expected images, so we'll rachet back the threshold
|
|
|
|
# https://github.com/mapnik/mapnik/issues/1868
|
2014-07-22 21:55:06 +00:00
|
|
|
cairo_threshold = 230
|
|
|
|
agg_threshold = 12
|
2014-10-01 00:41:55 +00:00
|
|
|
grid_threshold = 6
|
2013-05-31 21:03:53 +00:00
|
|
|
|
|
|
|
def render_cairo(m, output, scale_factor):
|
|
|
|
mapnik.render_to_file(m, output, 'ARGB32', scale_factor)
|
|
|
|
# open and re-save as png8 to save space
|
|
|
|
new_im = mapnik.Image.open(output)
|
|
|
|
new_im.save(output, 'png8:m=h')
|
|
|
|
|
|
|
|
def render_grid(m, output, scale_factor):
|
|
|
|
grid = mapnik.Grid(m.width, m.height)
|
2014-09-18 22:26:50 +00:00
|
|
|
mapnik.render_layer(m, grid, layer=0, scale_factor=scale_factor)
|
2013-05-31 21:03:53 +00:00
|
|
|
utf1 = grid.encode('utf', resolution=4)
|
|
|
|
open(output,'wb').write(json.dumps(utf1, indent=1))
|
|
|
|
|
2014-09-18 22:26:50 +00:00
|
|
|
def render_agg(m, output, scale_factor):
|
|
|
|
mapnik.render_to_file(m, output, 'png8:m=h', scale_factor),
|
|
|
|
|
2013-05-31 21:03:53 +00:00
|
|
|
renderers = [
|
|
|
|
{ 'name': 'agg',
|
2014-09-18 22:26:50 +00:00
|
|
|
'render': render_agg,
|
2013-05-31 21:03:53 +00:00
|
|
|
'compare': lambda actual, reference: compare(actual, reference, alpha=True),
|
2014-07-22 21:55:06 +00:00
|
|
|
'threshold': agg_threshold,
|
2013-05-31 21:03:53 +00:00
|
|
|
'filetype': 'png',
|
|
|
|
'dir': 'images'
|
|
|
|
},
|
|
|
|
{ 'name': 'cairo',
|
|
|
|
'render': render_cairo,
|
|
|
|
'compare': lambda actual, reference: compare(actual, reference, alpha=False),
|
|
|
|
'threshold': cairo_threshold,
|
|
|
|
'filetype': 'png',
|
|
|
|
'dir': 'images'
|
|
|
|
},
|
|
|
|
{ 'name': 'grid',
|
|
|
|
'render': render_grid,
|
|
|
|
'compare': lambda actual, reference: compare_grids(actual, reference, alpha=False),
|
2014-10-01 00:41:55 +00:00
|
|
|
'threshold': grid_threshold,
|
2013-05-31 21:03:53 +00:00
|
|
|
'filetype': 'json',
|
|
|
|
'dir': 'grids'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2012-03-09 01:01:51 +00:00
|
|
|
dirname = os.path.dirname(__file__)
|
2012-03-23 19:42:11 +00:00
|
|
|
|
2013-03-25 02:56:14 +00:00
|
|
|
class Reporting:
|
|
|
|
DIFF = 1
|
|
|
|
NOT_FOUND = 2
|
|
|
|
OTHER = 3
|
|
|
|
REPLACE = 4
|
2013-05-16 20:17:48 +00:00
|
|
|
def __init__(self, quiet, overwrite_failures = False):
|
2014-09-25 00:48:25 +00:00
|
|
|
self.quiet = quiet
|
|
|
|
self.passed = 0
|
|
|
|
self.failed = 0
|
|
|
|
self.overwrite_failures = overwrite_failures
|
|
|
|
self.errors = [ #(type, actual, expected, diff, message)
|
|
|
|
]
|
2013-09-19 02:55:02 +00:00
|
|
|
|
2013-03-25 02:56:14 +00:00
|
|
|
def result_fail(self, actual, expected, diff):
|
2014-09-25 00:48:25 +00:00
|
|
|
self.failed += 1
|
|
|
|
if self.quiet:
|
|
|
|
if platform.uname()[0] == 'Windows':
|
|
|
|
sys.stderr.write('.')
|
|
|
|
else:
|
|
|
|
sys.stderr.write('\x1b[31m.\x1b[0m')
|
|
|
|
else:
|
|
|
|
print '\x1b[31m✘\x1b[0m (\x1b[34m%u different pixels\x1b[0m)' % diff
|
|
|
|
|
|
|
|
if self.overwrite_failures:
|
|
|
|
self.errors.append((self.REPLACE, actual, expected, diff, None))
|
|
|
|
contents = open(actual, 'r').read()
|
|
|
|
open(expected, 'wb').write(contents)
|
|
|
|
else:
|
|
|
|
self.errors.append((self.DIFF, actual, expected, diff, None))
|
2013-09-19 02:55:02 +00:00
|
|
|
|
2013-03-25 02:56:14 +00:00
|
|
|
def result_pass(self, actual, expected, diff):
|
2014-09-25 00:48:25 +00:00
|
|
|
self.passed += 1
|
|
|
|
if self.quiet:
|
|
|
|
if platform.uname()[0] == 'Windows':
|
|
|
|
sys.stderr.write('.')
|
|
|
|
else:
|
|
|
|
sys.stderr.write('\x1b[32m.\x1b[0m')
|
|
|
|
else:
|
|
|
|
if platform.uname()[0] == 'Windows':
|
|
|
|
print '\x1b[32m✓\x1b[0m'
|
|
|
|
else:
|
|
|
|
print '✓'
|
2012-12-07 04:14:14 +00:00
|
|
|
|
2013-03-25 02:56:14 +00:00
|
|
|
def not_found(self, actual, expected):
|
2014-09-25 00:48:25 +00:00
|
|
|
self.failed += 1
|
|
|
|
self.errors.append((self.NOT_FOUND, actual, expected, 0, None))
|
|
|
|
if self.quiet:
|
|
|
|
sys.stderr.write('\x1b[33m.\x1b[0m')
|
|
|
|
else:
|
|
|
|
print '\x1b[33m?\x1b[0m (\x1b[34mReference file not found, creating\x1b[0m)'
|
|
|
|
contents = open(actual, 'r').read()
|
|
|
|
open(expected, 'wb').write(contents)
|
2013-03-25 02:56:14 +00:00
|
|
|
|
|
|
|
def other_error(self, expected, message):
|
2014-09-25 00:48:25 +00:00
|
|
|
self.failed += 1
|
|
|
|
self.errors.append((self.OTHER, None, expected, 0, message))
|
|
|
|
if self.quiet:
|
|
|
|
sys.stderr.write('\x1b[31m.\x1b[0m')
|
|
|
|
else:
|
|
|
|
print '\x1b[31m✘\x1b[0m (\x1b[34m%s\x1b[0m)' % message
|
2013-03-25 02:56:14 +00:00
|
|
|
|
2013-05-20 22:07:20 +00:00
|
|
|
def make_html_item(self,actual,expected,diff):
|
2014-09-25 00:48:25 +00:00
|
|
|
item = '''
|
|
|
|
<div class="expected">
|
|
|
|
<a href="%s">
|
|
|
|
<img src="%s" width="100%s">
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
''' % (expected,expected,'%')
|
|
|
|
item += '<div class="text">%s</div>' % (diff)
|
|
|
|
item += '''
|
|
|
|
<div class="actual">
|
|
|
|
<a href="%s">
|
|
|
|
<img src="%s" width="100%s">
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
''' % (actual,actual,'%')
|
|
|
|
return item
|
2013-05-17 23:46:42 +00:00
|
|
|
|
2013-05-16 20:17:48 +00:00
|
|
|
def summary(self):
|
2014-09-25 00:48:25 +00:00
|
|
|
if len(self.errors) == 0:
|
|
|
|
print '\nAll %s visual tests passed: \x1b[1;32m✓ \x1b[0m' % self.passed
|
|
|
|
return 0
|
|
|
|
sortable_errors = []
|
|
|
|
print "\nVisual rendering: %s failed / %s passed" % (len(self.errors), self.passed)
|
|
|
|
for idx, error in enumerate(self.errors):
|
|
|
|
if error[0] == self.OTHER:
|
|
|
|
print str(idx+1) + ") \x1b[31mfailure to run test:\x1b[0m %s (\x1b[34m%s\x1b[0m)" % (error[2],error[4])
|
|
|
|
elif error[0] == self.NOT_FOUND:
|
|
|
|
print str(idx+1) + ") Generating reference image: '%s'" % error[2]
|
|
|
|
continue
|
|
|
|
elif error[0] == self.DIFF:
|
|
|
|
print str(idx+1) + ") \x1b[34m%s different pixels\x1b[0m:\n\t%s (\x1b[31mactual\x1b[0m)\n\t%s (\x1b[32mexpected\x1b[0m)" % (error[3], error[1], error[2])
|
|
|
|
if '.png' in error[1]: # ignore grids
|
|
|
|
sortable_errors.append((error[3],error))
|
|
|
|
elif error[0] == self.REPLACE:
|
|
|
|
print str(idx+1) + ") \x1b[31mreplaced reference with new version:\x1b[0m %s" % error[2]
|
|
|
|
if len(sortable_errors):
|
|
|
|
# drop failure results in folder
|
|
|
|
vdir = os.path.join(visual_output_dir,'visual-test-results')
|
|
|
|
if not os.path.exists(vdir):
|
|
|
|
os.makedirs(vdir)
|
|
|
|
html_template = open(os.path.join(dirname,'html_report_template.html'),'r').read()
|
|
|
|
name = 'comparison.html'
|
|
|
|
failures_realpath = os.path.join(vdir,name)
|
|
|
|
html_out = open(failures_realpath,'w+')
|
|
|
|
sortable_errors.sort(reverse=True)
|
|
|
|
html_body = ''
|
|
|
|
for item in sortable_errors:
|
|
|
|
# copy images into single directory
|
|
|
|
actual = item[1][1]
|
|
|
|
expected = item[1][2]
|
|
|
|
diff = item[0]
|
|
|
|
actual_new = os.path.join(vdir,os.path.basename(actual))
|
|
|
|
shutil.copy(actual,actual_new)
|
|
|
|
expected_new = os.path.join(vdir,os.path.basename(expected))
|
|
|
|
shutil.copy(expected,expected_new)
|
|
|
|
html_body += self.make_html_item(os.path.relpath(actual_new,vdir),os.path.relpath(expected_new,vdir),diff)
|
|
|
|
html_out.write(html_template.replace('{{RESULTS}}',html_body))
|
|
|
|
print 'View failures by opening %s' % failures_realpath
|
|
|
|
return 1
|
2013-03-25 02:56:14 +00:00
|
|
|
|
2014-12-09 15:31:07 +00:00
|
|
|
def render(filename, config, scale_factor, reporting):
|
|
|
|
m = mapnik.Map(*config['sizes'][0])
|
2013-03-25 01:28:54 +00:00
|
|
|
|
|
|
|
try:
|
2014-12-09 15:31:07 +00:00
|
|
|
mapnik.load_map(m, os.path.join(dirname, "styles", filename), True)
|
|
|
|
|
|
|
|
if not (m.parameters['status'] if ('status' in m.parameters) else config['status']):
|
|
|
|
return
|
2013-03-25 01:28:54 +00:00
|
|
|
except Exception, e:
|
2014-09-25 00:43:48 +00:00
|
|
|
if 'Could not create datasource' in str(e) \
|
|
|
|
or 'Bad connection' in str(e):
|
|
|
|
return m
|
2014-09-25 00:48:25 +00:00
|
|
|
reporting.other_error(filename, repr(e))
|
|
|
|
return m
|
2013-09-19 02:55:02 +00:00
|
|
|
|
2014-12-09 15:31:07 +00:00
|
|
|
sizes = config['sizes'];
|
|
|
|
if 'sizes' in m.parameters:
|
|
|
|
sizes = [[int(i) for i in size.split(',')] for size in m.parameters['sizes'].split(';')]
|
|
|
|
|
|
|
|
for size in sizes:
|
|
|
|
m.width, m.height = size
|
|
|
|
|
|
|
|
if 'bbox' in m.parameters:
|
|
|
|
bbox = mapnik.Box2d.from_string(str(m.parameters['bbox']))
|
|
|
|
m.zoom_to_box(bbox)
|
|
|
|
else:
|
|
|
|
m.zoom_all()
|
|
|
|
|
|
|
|
name = filename[0:-4]
|
|
|
|
postfix = "%s-%d-%d-%s" % (name, m.width, m.height, scale_factor)
|
|
|
|
for renderer in renderers:
|
|
|
|
if config.get(renderer['name'], True):
|
|
|
|
expected = os.path.join(dirname, renderer['dir'], '%s-%s-reference.%s' %
|
|
|
|
(postfix, renderer['name'], renderer['filetype']))
|
|
|
|
actual = os.path.join(visual_output_dir, '%s-%s.%s' %
|
|
|
|
(postfix, renderer['name'], renderer['filetype']))
|
|
|
|
if not quiet:
|
|
|
|
print "\"%s\" with %s..." % (postfix, renderer['name']),
|
|
|
|
try:
|
|
|
|
renderer['render'](m, actual, scale_factor)
|
|
|
|
if not os.path.exists(expected):
|
|
|
|
reporting.not_found(actual, expected)
|
2014-09-25 00:48:25 +00:00
|
|
|
else:
|
2014-12-09 15:31:07 +00:00
|
|
|
diff = renderer['compare'](actual, expected)
|
|
|
|
if diff > renderer['threshold']:
|
|
|
|
reporting.result_fail(actual, expected, diff)
|
|
|
|
else:
|
|
|
|
reporting.result_pass(actual, expected, diff)
|
|
|
|
except Exception, e:
|
|
|
|
reporting.other_error(expected, repr(e))
|
2012-01-08 14:01:54 +00:00
|
|
|
return m
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2012-04-04 21:54:07 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if '-q' in sys.argv:
|
|
|
|
quiet = True
|
|
|
|
sys.argv.remove('-q')
|
|
|
|
else:
|
|
|
|
quiet = False
|
|
|
|
|
2013-03-08 01:19:00 +00:00
|
|
|
if '--overwrite' in sys.argv:
|
|
|
|
overwrite_failures = True
|
|
|
|
sys.argv.remove('--overwrite')
|
|
|
|
else:
|
|
|
|
overwrite_failures = False
|
|
|
|
|
2014-12-09 15:31:07 +00:00
|
|
|
files = None
|
2013-05-20 19:45:06 +00:00
|
|
|
if len(sys.argv) > 1:
|
2014-12-09 15:31:07 +00:00
|
|
|
files = [name + ".xml" for name in sys.argv[1:]]
|
|
|
|
else:
|
|
|
|
files = [os.path.basename(file) for file in glob.glob(os.path.join(dirname, "styles/*.xml"))]
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2012-10-03 04:18:45 +00:00
|
|
|
if not os.path.exists(visual_output_dir):
|
2014-09-25 00:48:25 +00:00
|
|
|
os.makedirs(visual_output_dir)
|
2012-10-03 04:18:45 +00:00
|
|
|
|
2013-05-21 05:23:04 +00:00
|
|
|
reporting = Reporting(quiet, overwrite_failures)
|
2014-07-30 00:23:09 +00:00
|
|
|
try:
|
2014-09-25 00:48:25 +00:00
|
|
|
for filename in files:
|
|
|
|
config = dict(defaults)
|
2014-12-09 15:31:07 +00:00
|
|
|
for scale_factor in config['scales']:
|
|
|
|
m = render(filename, config, scale_factor, reporting)
|
|
|
|
#mapnik.save_map(m, os.path.join(dirname, 'xml_output', "%s-out.xml" % filename))
|
2014-07-30 00:23:09 +00:00
|
|
|
except KeyboardInterrupt:
|
2014-09-25 00:48:25 +00:00
|
|
|
pass
|
2013-05-21 05:23:04 +00:00
|
|
|
sys.exit(reporting.summary())
|