2012-01-08 15:01:54 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import mapnik
|
2012-09-28 03:58:50 +02:00
|
|
|
mapnik.logger.set_severity(mapnik.severity_type.None)
|
|
|
|
|
2012-01-08 15:01:54 +01:00
|
|
|
import sys
|
|
|
|
import os.path
|
2012-11-21 00:08:53 +01:00
|
|
|
from compare import compare, compare_grids, summary, fail
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
2012-01-08 15:01:54 +01:00
|
|
|
|
2012-10-03 06:18:45 +02:00
|
|
|
visual_output_dir = "/tmp/mapnik-visual-images"
|
|
|
|
|
2012-03-20 22:06:22 +01:00
|
|
|
defaults = {
|
2013-03-20 03:47:20 +01:00
|
|
|
'sizes': [(500, 100)],
|
|
|
|
'scales':[1.0,2.0]
|
2012-03-20 22:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sizes_many_in_big_range = [(800, 100), (600, 100), (400, 100),
|
|
|
|
(300, 100), (250, 100), (150, 100), (100, 100)]
|
|
|
|
|
|
|
|
sizes_few_square = [(800, 800), (600, 600), (400, 400), (200, 200)]
|
|
|
|
sizes_many_in_small_range = [(490, 100), (495, 100), (497, 100), (498, 100),
|
|
|
|
(499, 100), (500, 100), (501, 100), (502, 100), (505, 100), (510, 100)]
|
|
|
|
|
2012-09-26 20:28:23 +02:00
|
|
|
default_text_box = mapnik.Box2d(-0.05, -0.01, 0.95, 0.01)
|
|
|
|
|
2012-03-09 02:01:51 +01:00
|
|
|
dirname = os.path.dirname(__file__)
|
2012-03-23 20:42:11 +01:00
|
|
|
|
2012-02-20 17:42:12 +01:00
|
|
|
files = [
|
2012-09-26 20:28:23 +02:00
|
|
|
{'name': "list", 'sizes': sizes_many_in_big_range,'bbox':default_text_box},
|
|
|
|
{'name': "simple", 'sizes': sizes_many_in_big_range,'bbox':default_text_box},
|
|
|
|
{'name': "lines-1", 'sizes': sizes_few_square,'bbox':default_text_box},
|
|
|
|
{'name': "lines-2", 'sizes': sizes_few_square,'bbox':default_text_box},
|
|
|
|
{'name': "lines-3", 'sizes': sizes_few_square,'bbox':default_text_box},
|
2013-03-20 03:50:57 +01:00
|
|
|
{'name': "lines-4", 'sizes': sizes_few_square,'bbox':default_text_box},
|
|
|
|
{'name': "lines-5", 'sizes': sizes_few_square,'bbox':default_text_box},
|
|
|
|
{'name': "lines-6", 'sizes': sizes_few_square,'bbox':default_text_box},
|
|
|
|
{'name': "formatting", 'bbox':default_text_box},
|
2013-03-06 20:33:20 +01:00
|
|
|
# https://github.com/mapnik/mapnik/issues/1696
|
|
|
|
# https://github.com/mapnik/mapnik/issues/1521
|
|
|
|
# fails with clang++ on os x
|
|
|
|
#{'name': "lines-shield", 'sizes': sizes_few_square,'bbox':default_text_box},
|
2012-12-21 02:27:09 +01:00
|
|
|
{'name': "collision", 'sizes':[(600,400)]},
|
2013-03-20 03:47:20 +01:00
|
|
|
{'name': "marker-svg-opacity"},
|
2012-11-21 02:58:39 +01:00
|
|
|
{'name': "marker-multi-policy", 'sizes':[(600,400)]},
|
2012-12-11 00:55:13 +01:00
|
|
|
{'name': "marker-on-line", 'sizes':[(600,400)],
|
|
|
|
'bbox': mapnik.Box2d(-10, 0, 15, 20)},
|
2013-01-04 22:42:13 +01:00
|
|
|
{'name': "marker-on-line-spacing-eq-width", 'sizes':[(600,400)]},
|
|
|
|
{'name': "marker-on-line-spacing-eq-width-overlap", 'sizes':[(600,400)]},
|
2012-12-03 04:53:44 +01:00
|
|
|
{'name': "marker_line_placement_on_points"},
|
2013-02-21 02:55:53 +01:00
|
|
|
{'name': "marker-with-background-image", 'sizes':[(600,400),(400,600),(257,256)]},
|
2013-02-28 18:19:40 +01:00
|
|
|
{'name': "marker-with-background-image-and-hsla-transform", 'sizes':[(600,400),(400,600),(257,256)]},
|
2013-03-06 20:32:27 +01:00
|
|
|
{'name': "marker-on-hex-grid", 'sizes':[(600,400),(400,600),(257,256)]},
|
2012-11-29 17:54:47 +01:00
|
|
|
{'name': "whole-centroid", 'sizes':[(600,400)],
|
|
|
|
'bbox': mapnik.Box2d(736908, 4390316, 2060771, 5942346)},
|
2013-03-09 05:32:39 +01:00
|
|
|
{'name': "text-halo-rasterizer", 'sizes':[(600,400)]},
|
2012-09-26 20:28:23 +02:00
|
|
|
{'name': "simple-E", 'bbox':mapnik.Box2d(-0.05, -0.01, 0.95, 0.01)},
|
|
|
|
{'name': "simple-NE",'bbox':default_text_box},
|
|
|
|
{'name': "simple-NW",'bbox':default_text_box},
|
|
|
|
{'name': "simple-N",'bbox':default_text_box},
|
|
|
|
{'name': "simple-SE",'bbox':default_text_box},
|
|
|
|
{'name': "simple-SW",'bbox':default_text_box},
|
|
|
|
{'name': "simple-S",'bbox':default_text_box},
|
|
|
|
{'name': "simple-W",'bbox':default_text_box},
|
|
|
|
{'name': "formatting-1",'bbox':default_text_box},
|
|
|
|
{'name': "formatting-2",'bbox':default_text_box},
|
|
|
|
{'name': "formatting-3",'bbox':default_text_box},
|
|
|
|
{'name': "formatting-4",'bbox':default_text_box},
|
|
|
|
{'name': "expressionformat",'bbox':default_text_box},
|
|
|
|
{'name': "shieldsymbolizer-1", 'sizes': sizes_many_in_small_range,'bbox':default_text_box},
|
2013-03-20 03:50:57 +01:00
|
|
|
{'name': "shieldsymbolizer-2", 'bbox':default_text_box},
|
|
|
|
{'name': "shieldsymbolizer-3", 'bbox':default_text_box},
|
|
|
|
{'name': "shieldsymbolizer-4", 'bbox':default_text_box},
|
|
|
|
{'name': "orientation", 'sizes': [(800, 200)]},
|
|
|
|
{'name': "hb-fontsets", 'sizes': [(800, 200)]},
|
|
|
|
{'name': "charspacing", 'sizes': [(200, 400)]},
|
|
|
|
{'name': "line_break", 'sizes': [(800, 800)]},
|
2012-09-26 20:28:23 +02:00
|
|
|
{'name': "rtl-point", 'sizes': [(200, 200)],'bbox':default_text_box},
|
|
|
|
{'name': "jalign-auto", 'sizes': [(200, 200)],'bbox':default_text_box},
|
|
|
|
{'name': "line-offset", 'sizes':[(900, 250)],'bbox': mapnik.Box2d(-5.192, 50.189, -5.174, 50.195)},
|
|
|
|
{'name': "tiff-alpha-gdal", 'sizes':[(600,400)]},
|
2012-09-29 00:07:33 +02:00
|
|
|
{'name': "tiff-alpha-broken-assoc-alpha-gdal", 'sizes':[(600,400)]},
|
2012-09-26 21:01:28 +02:00
|
|
|
{'name': "tiff-alpha-gradient-gdal", 'sizes':[(600,400)]},
|
2013-03-07 02:41:20 +01:00
|
|
|
{'name': "tiff-nodata-edge-gdal", 'sizes':[(600,400),(969,793)]},
|
|
|
|
{'name': "tiff-opaque-edge-gdal", 'sizes':[(256,256),(969,793)]},
|
|
|
|
{'name': "tiff-opaque-edge-gdal2", 'sizes':[(600,400),(969,793)]},
|
|
|
|
{'name': "tiff-opaque-edge-raster2", 'sizes':[(600,400),(969,793)]},
|
2013-02-01 00:37:01 +01:00
|
|
|
{'name': "tiff-resampling", 'sizes':[(600,400)]},
|
2013-03-07 02:41:20 +01:00
|
|
|
# https://github.com/mapnik/mapnik/issues/1622
|
|
|
|
{'name': "tiff-edge-alignment-gdal1", 'sizes':[(256,256),(255,257)],
|
|
|
|
'bbox':mapnik.Box2d(-13267022.12540147,4618019.500877209,-13247454.246160466,4637587.380118214)
|
|
|
|
},
|
|
|
|
{'name': "tiff-edge-alignment-gdal2", 'sizes':[(256,256),(255,257)],
|
|
|
|
'bbox':mapnik.Box2d(-13267022.12540147,4598451.621636203,-13247454.246160466,4618019.500877209)
|
|
|
|
},
|
2012-11-21 00:08:53 +01:00
|
|
|
# https://github.com/mapnik/mapnik/issues/1520
|
|
|
|
# commented because these are not critical failures
|
|
|
|
#{'name': "tiff-alpha-raster", 'sizes':[(600,400)]},
|
|
|
|
#{'name': "tiff-alpha-broken-assoc-alpha-raster", 'sizes':[(600,400)]},
|
|
|
|
#{'name': "tiff-nodata-edge-raster", 'sizes':[(600,400)]},
|
|
|
|
#{'name': "tiff-opaque-edge-raster", 'sizes':[(256,256)]},
|
2012-03-16 00:38:03 +01:00
|
|
|
]
|
2012-02-17 23:19:07 +01:00
|
|
|
|
2013-03-08 02:19:00 +01:00
|
|
|
def report(diff,threshold,quiet=False):
|
2012-12-07 05:14:14 +01:00
|
|
|
if diff > threshold:
|
|
|
|
if quiet:
|
|
|
|
sys.stderr.write('\x1b[31m.\x1b[0m')
|
|
|
|
else:
|
|
|
|
print '\x1b[31m✘\x1b[0m (\x1b[34m%u different pixels\x1b[0m)' % diff
|
|
|
|
else:
|
|
|
|
if quiet:
|
|
|
|
sys.stderr.write('\x1b[32m.\x1b[0m')
|
|
|
|
else:
|
|
|
|
print '\x1b[32m✓\x1b[0m'
|
|
|
|
|
2013-03-20 03:47:20 +01:00
|
|
|
def render(config, width, height, bbox, scale_factor, quiet=False, overwrite_failures=False):
|
2013-03-06 20:32:27 +01:00
|
|
|
filename = config['name']
|
2012-03-02 01:45:44 +01:00
|
|
|
m = mapnik.Map(width, height)
|
2013-03-20 03:47:20 +01:00
|
|
|
postfix = "%s-%d-%d-%s" % (filename,width,height,scale_factor)
|
2012-12-07 05:14:14 +01:00
|
|
|
|
2013-03-06 20:32:27 +01:00
|
|
|
## AGG rendering
|
|
|
|
if config.get('agg',True):
|
2013-03-20 03:47:20 +01:00
|
|
|
expected_agg = os.path.join(dirname, "images", postfix + '-agg-reference.png')
|
|
|
|
actual_agg = os.path.join(visual_output_dir, '%s-agg.png' % postfix)
|
2013-03-06 20:32:27 +01:00
|
|
|
try:
|
|
|
|
mapnik.load_map(m, os.path.join(dirname, "styles", "%s.xml" % filename), False)
|
|
|
|
if bbox is not None:
|
|
|
|
m.zoom_to_box(bbox)
|
|
|
|
else:
|
|
|
|
m.zoom_all()
|
|
|
|
except Exception, e:
|
|
|
|
sys.stderr.write(e.message + '\n')
|
2013-03-20 03:47:20 +01:00
|
|
|
fail(actual_agg,expected_agg,str(e.message))
|
2013-03-20 03:50:57 +01:00
|
|
|
return m
|
2013-03-06 20:32:27 +01:00
|
|
|
if not quiet:
|
2013-03-20 03:47:20 +01:00
|
|
|
print "\"%s\" with agg..." % (postfix),
|
2013-03-06 20:32:27 +01:00
|
|
|
try:
|
2013-03-20 03:47:20 +01:00
|
|
|
mapnik.render_to_file(m, actual_agg, 'png8:m=h', scale_factor)
|
|
|
|
if not os.path.exists(expected_agg):
|
2013-03-06 20:32:27 +01:00
|
|
|
# generate it on the fly
|
2013-03-20 03:47:20 +01:00
|
|
|
fail(actual_agg,expected_agg,None)
|
2013-03-06 20:32:27 +01:00
|
|
|
else:
|
2013-03-08 02:19:00 +01:00
|
|
|
threshold = 0
|
2013-03-20 03:47:20 +01:00
|
|
|
diff = compare(actual_agg, expected_agg, threshold=0, alpha=True)
|
2013-03-08 02:19:00 +01:00
|
|
|
if overwrite_failures and diff > threshold:
|
2013-03-20 03:47:20 +01:00
|
|
|
fail(actual_agg,expected_agg,None)
|
2013-03-08 02:19:00 +01:00
|
|
|
else:
|
|
|
|
report(diff,threshold,quiet)
|
2013-03-06 20:32:27 +01:00
|
|
|
except Exception, e:
|
|
|
|
sys.stderr.write(e.message + '\n')
|
2013-03-20 03:47:20 +01:00
|
|
|
fail(actual_agg,expected_agg,str(e.message))
|
2013-03-06 20:32:27 +01:00
|
|
|
|
|
|
|
### TODO - set up tests to compare agg and cairo png output
|
|
|
|
|
|
|
|
### Cairo rendering
|
|
|
|
if config.get('cairo',True):
|
2013-03-20 03:47:20 +01:00
|
|
|
expected_cairo = os.path.join(dirname, "images", postfix + '-cairo-reference.png')
|
|
|
|
actual_cairo = os.path.join(visual_output_dir, '%s-cairo.png' % postfix)
|
2012-10-03 06:18:45 +02:00
|
|
|
if not quiet:
|
2013-03-20 03:47:20 +01:00
|
|
|
print "\"%s\" with cairo..." % (postfix),
|
2012-10-03 06:18:45 +02:00
|
|
|
try:
|
2013-03-20 03:47:20 +01:00
|
|
|
mapnik.render_to_file(m, actual_cairo,'ARGB32', scale_factor)
|
|
|
|
# open and re-save as png8 to save space
|
|
|
|
new_im = mapnik.Image.open(actual_cairo)
|
|
|
|
new_im.save(actual_cairo,'png8:m=h')
|
2013-03-06 20:32:27 +01:00
|
|
|
if not os.path.exists(expected_cairo):
|
|
|
|
# generate it on the fly
|
|
|
|
fail(actual_cairo,expected_cairo,None)
|
2012-11-21 00:08:53 +01:00
|
|
|
else:
|
|
|
|
# cairo and agg differ in alpha for reasons unknown, so don't test it for now
|
2013-03-08 02:19:00 +01:00
|
|
|
threshold = 0
|
|
|
|
diff = compare(actual_cairo, expected_cairo, threshold=threshold, alpha=False)
|
|
|
|
if overwrite_failures and diff > threshold:
|
|
|
|
fail(actual_cairo,expected_cairo,None)
|
|
|
|
else:
|
|
|
|
report(diff,threshold,quiet)
|
2012-10-03 06:18:45 +02:00
|
|
|
except Exception, e:
|
|
|
|
sys.stderr.write(e.message + '\n')
|
2013-03-06 20:32:27 +01:00
|
|
|
fail(actual_cairo,expected_cairo,str(e.message))
|
|
|
|
|
|
|
|
## Grid rendering
|
2013-03-20 03:47:20 +01:00
|
|
|
# TODO - grid renderer does not support scale_factor yet via python
|
2013-03-20 03:50:57 +01:00
|
|
|
if 0 and scale_factor == 1.0 and config.get('grid',True):
|
2013-03-20 03:47:20 +01:00
|
|
|
expected_grid = os.path.join(dirname, "grids", postfix + '-grid-reference.json')
|
|
|
|
actual_grid = os.path.join(visual_output_dir, '%s-grid.json' % postfix)
|
2012-11-21 00:08:53 +01:00
|
|
|
if not quiet:
|
2013-03-20 03:47:20 +01:00
|
|
|
print "\"%s\" with grid..." % (postfix),
|
2012-11-21 00:08:53 +01:00
|
|
|
try:
|
|
|
|
grid = mapnik.Grid(m.width,m.height)
|
|
|
|
mapnik.render_layer(m,grid,layer=0)
|
|
|
|
utf1 = grid.encode('utf',resolution=4)
|
2013-03-20 03:47:20 +01:00
|
|
|
open(actual_grid,'wb').write(json.dumps(utf1,indent=1))
|
2012-11-21 00:08:53 +01:00
|
|
|
if not os.path.exists(expected_grid):
|
|
|
|
# generate it on the fly
|
|
|
|
fail(actual_grid,expected_grid,None)
|
|
|
|
else:
|
2013-03-09 05:32:39 +01:00
|
|
|
threshold = 0
|
2013-03-08 02:19:00 +01:00
|
|
|
diff = compare_grids(actual_grid, expected_grid, threshold=threshold, alpha=False)
|
|
|
|
if overwrite_failures and diff > threshold:
|
|
|
|
fail(actual_grid,expected_grid,None)
|
|
|
|
else:
|
|
|
|
report(diff,threshold,quiet)
|
2012-11-21 00:08:53 +01:00
|
|
|
except Exception, e:
|
|
|
|
sys.stderr.write(e.message + '\n')
|
2013-03-20 03:47:20 +01:00
|
|
|
fail(actual_grid,expected_grid,str(e.message))
|
2012-01-08 15:01:54 +01:00
|
|
|
return m
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2012-04-04 23:54:07 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if '-q' in sys.argv:
|
|
|
|
quiet = True
|
|
|
|
sys.argv.remove('-q')
|
|
|
|
else:
|
|
|
|
quiet = False
|
|
|
|
|
2013-03-08 02:19:00 +01:00
|
|
|
if '--overwrite' in sys.argv:
|
|
|
|
overwrite_failures = True
|
|
|
|
sys.argv.remove('--overwrite')
|
|
|
|
else:
|
|
|
|
overwrite_failures = False
|
|
|
|
|
2012-04-04 23:54:07 +02:00
|
|
|
if len(sys.argv) == 2:
|
2012-07-03 23:39:34 +02:00
|
|
|
files = [{"name": sys.argv[1], "sizes": sizes_few_square}]
|
2012-04-04 23:54:07 +02:00
|
|
|
elif len(sys.argv) > 2:
|
2012-07-03 23:39:34 +02:00
|
|
|
files = []
|
2012-07-30 01:25:21 +02:00
|
|
|
for name in sys.argv[1:]:
|
2012-07-03 23:39:34 +02:00
|
|
|
files.append({"name": name})
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2012-10-03 06:18:45 +02:00
|
|
|
if not os.path.exists(visual_output_dir):
|
|
|
|
os.makedirs(visual_output_dir)
|
|
|
|
|
2012-09-08 00:43:32 +02:00
|
|
|
if 'osm' in mapnik.DatasourceCache.plugin_names():
|
2012-08-16 04:22:47 +02:00
|
|
|
for f in files:
|
|
|
|
config = dict(defaults)
|
|
|
|
config.update(f)
|
|
|
|
for size in config['sizes']:
|
2013-03-20 03:47:20 +01:00
|
|
|
for scale_factor in config['scales']:
|
|
|
|
m = render(config,
|
|
|
|
size[0],
|
|
|
|
size[1],
|
|
|
|
config.get('bbox'),
|
|
|
|
scale_factor,
|
|
|
|
quiet=quiet,
|
|
|
|
overwrite_failures=overwrite_failures)
|
2012-08-16 04:22:47 +02:00
|
|
|
mapnik.save_map(m, os.path.join(dirname, 'xml_output', "%s-out.xml" % config['name']))
|
2012-01-25 00:00:09 +01:00
|
|
|
|
2012-11-21 00:08:53 +01:00
|
|
|
summary(generate=True)
|