2010-03-12 14:34:13 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2015-02-02 19:31:16 +01:00
|
|
|
from nose.tools import eq_
|
|
|
|
from utilities import execution_path, run_all, get_unique_colors
|
2010-03-12 14:34:13 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
import os, mapnik
|
2010-03-12 14:34:13 +01:00
|
|
|
|
|
|
|
def setup():
|
|
|
|
# All of the paths used are relative, if we run the tests
|
|
|
|
# from another directory we need to chdir()
|
|
|
|
os.chdir(execution_path('.'))
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2010-03-12 14:34:13 +01:00
|
|
|
|
|
|
|
def test_dataraster_coloring():
|
|
|
|
srs = '+init=epsg:32630'
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr = mapnik.Layer('dataraster')
|
2012-09-05 13:53:37 +02:00
|
|
|
if 'gdal' in mapnik.DatasourceCache.plugin_names():
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr.datasource = mapnik.Gdal(
|
2011-10-29 02:06:23 +02:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
|
|
|
lyr.srs = srs
|
2011-11-23 12:33:58 +01:00
|
|
|
_map = mapnik.Map(256,256, srs)
|
|
|
|
style = mapnik.Style()
|
|
|
|
rule = mapnik.Rule()
|
|
|
|
sym = mapnik.RasterSymbolizer()
|
2011-10-29 02:06:23 +02:00
|
|
|
# Assigning a colorizer to the RasterSymbolizer tells the later
|
|
|
|
# that it should use it to colorize the raw data raster
|
2014-04-25 07:01:58 +02:00
|
|
|
colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color("transparent"))
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-10-29 02:06:23 +02:00
|
|
|
for value, color in [
|
|
|
|
( 0, "#0044cc"),
|
|
|
|
( 10, "#00cc00"),
|
|
|
|
( 20, "#ffff00"),
|
|
|
|
( 30, "#ff7f00"),
|
|
|
|
( 40, "#ff0000"),
|
|
|
|
( 50, "#ff007f"),
|
|
|
|
( 60, "#ff00ff"),
|
|
|
|
( 70, "#cc00cc"),
|
|
|
|
( 80, "#990099"),
|
|
|
|
( 90, "#660066"),
|
|
|
|
( 200, "transparent"),
|
|
|
|
]:
|
2014-04-25 07:01:58 +02:00
|
|
|
colorizer.add_stop(value, mapnik.Color(color))
|
|
|
|
sym.colorizer = colorizer;
|
2011-10-29 02:06:23 +02:00
|
|
|
rule.symbols.append(sym)
|
|
|
|
style.rules.append(rule)
|
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
|
|
|
_map.zoom_to_box(lyr.envelope())
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2014-12-03 19:53:04 +01:00
|
|
|
expected_file = './images/support/dataraster_coloring.png'
|
|
|
|
actual_file = '/tmp/' + os.path.basename(expected_file)
|
|
|
|
im.save(actual_file,'png32')
|
|
|
|
if not os.path.exists(expected_file) or os.environ.get('UPDATE'):
|
|
|
|
im.save(expected_file,'png32')
|
|
|
|
actual = mapnik.Image.open(actual_file)
|
|
|
|
expected = mapnik.Image.open(expected_file)
|
|
|
|
eq_(actual.tostring('png32'),expected.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual_file,expected_file))
|
2010-03-12 14:34:13 +01:00
|
|
|
|
|
|
|
def test_dataraster_query_point():
|
|
|
|
srs = '+init=epsg:32630'
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr = mapnik.Layer('dataraster')
|
2012-09-05 13:53:37 +02:00
|
|
|
if 'gdal' in mapnik.DatasourceCache.plugin_names():
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr.datasource = mapnik.Gdal(
|
2011-10-29 02:06:23 +02:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
|
|
|
lyr.srs = srs
|
2011-11-23 12:33:58 +01:00
|
|
|
_map = mapnik.Map(256,256, srs)
|
2011-10-29 02:06:23 +02:00
|
|
|
_map.layers.append(lyr)
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2012-04-13 20:28:30 +02:00
|
|
|
x, y = 556113.0,4381428.0 # center of extent of raster
|
|
|
|
_map.zoom_all()
|
2011-10-29 02:06:23 +02:00
|
|
|
features = _map.query_point(0,x,y).features
|
|
|
|
assert len(features) == 1
|
|
|
|
feat = features[0]
|
|
|
|
center = feat.envelope().center()
|
|
|
|
assert center.x==x and center.y==y, center
|
|
|
|
value = feat['value']
|
2012-04-13 20:28:30 +02:00
|
|
|
assert value == 18.0, value
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2012-04-13 20:28:30 +02:00
|
|
|
# point inside map extent but outside raster extent
|
|
|
|
current_box = _map.envelope()
|
|
|
|
current_box.expand_to_include(-427417,4477517)
|
|
|
|
_map.zoom_to_box(current_box)
|
2011-10-29 02:06:23 +02:00
|
|
|
features = _map.query_point(0,-427417,4477517).features
|
|
|
|
assert len(features) == 0
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-10-29 02:06:23 +02:00
|
|
|
# point inside raster extent with nodata
|
|
|
|
features = _map.query_point(0,126850,4596050).features
|
|
|
|
assert len(features) == 0
|
2010-03-12 15:49:50 +01:00
|
|
|
|
|
|
|
def test_load_save_map():
|
2011-11-23 12:33:58 +01:00
|
|
|
map = mapnik.Map(256,256)
|
2013-09-25 23:08:47 +02:00
|
|
|
in_map = "../visual_tests/styles/raster_symbolizer.xml"
|
2011-10-29 02:06:23 +02:00
|
|
|
try:
|
2011-11-23 12:33:58 +01:00
|
|
|
mapnik.load_map(map, in_map)
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
out_map = mapnik.save_map_to_string(map)
|
2011-10-29 02:06:23 +02:00
|
|
|
assert 'RasterSymbolizer' in out_map
|
|
|
|
assert 'RasterColorizer' in out_map
|
|
|
|
assert 'stop' in out_map
|
|
|
|
except RuntimeError, e:
|
|
|
|
# only test datasources that we have installed
|
|
|
|
if not 'Could not create datasource' in str(e):
|
|
|
|
raise RuntimeError(str(e))
|
2011-05-12 20:09:03 +02:00
|
|
|
|
|
|
|
def test_raster_with_alpha_blends_correctly_with_background():
|
|
|
|
WIDTH = 500
|
|
|
|
HEIGHT = 500
|
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
map = mapnik.Map(WIDTH, HEIGHT)
|
|
|
|
WHITE = mapnik.Color(255, 255, 255)
|
2011-05-12 20:09:03 +02:00
|
|
|
map.background = WHITE
|
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
style = mapnik.Style()
|
|
|
|
rule = mapnik.Rule()
|
|
|
|
symbolizer = mapnik.RasterSymbolizer()
|
2012-07-07 01:48:37 +02:00
|
|
|
symbolizer.scaling = mapnik.scaling_method.BILINEAR
|
2011-05-12 20:09:03 +02:00
|
|
|
|
|
|
|
rule.symbols.append(symbolizer)
|
|
|
|
style.rules.append(rule)
|
|
|
|
|
|
|
|
map.append_style('raster_style', style)
|
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
map_layer = mapnik.Layer('test_layer')
|
2011-05-12 20:09:03 +02:00
|
|
|
filepath = '../data/raster/white-alpha.png'
|
2012-09-05 13:53:37 +02:00
|
|
|
if 'gdal' in mapnik.DatasourceCache.plugin_names():
|
2011-11-23 12:33:58 +01:00
|
|
|
map_layer.datasource = mapnik.Gdal(file=filepath)
|
2011-10-29 02:06:23 +02:00
|
|
|
map_layer.styles.append('raster_style')
|
|
|
|
map.layers.append(map_layer)
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-10-29 02:06:23 +02:00
|
|
|
map.zoom_all()
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
mim = mapnik.Image(WIDTH, HEIGHT)
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
mapnik.render(map, mim)
|
2015-02-02 19:31:16 +01:00
|
|
|
mim.tostring()
|
2011-10-29 02:06:23 +02:00
|
|
|
# All white is expected
|
2012-10-04 03:53:23 +02:00
|
|
|
eq_(get_unique_colors(mim),['rgba(254,254,254,255)'])
|
2011-09-12 20:41:44 +02:00
|
|
|
|
|
|
|
def test_raster_warping():
|
|
|
|
lyrSrs = "+init=epsg:32630"
|
|
|
|
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr = mapnik.Layer('dataraster', lyrSrs)
|
2012-09-05 13:53:37 +02:00
|
|
|
if 'gdal' in mapnik.DatasourceCache.plugin_names():
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr.datasource = mapnik.Gdal(
|
2011-10-29 02:06:23 +02:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
2011-11-23 12:33:58 +01:00
|
|
|
sym = mapnik.RasterSymbolizer()
|
|
|
|
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255,255,0))
|
|
|
|
rule = mapnik.Rule()
|
2011-10-29 02:06:23 +02:00
|
|
|
rule.symbols.append(sym)
|
2011-11-23 12:33:58 +01:00
|
|
|
style = mapnik.Style()
|
2011-10-29 02:06:23 +02:00
|
|
|
style.rules.append(rule)
|
2011-11-23 12:33:58 +01:00
|
|
|
_map = mapnik.Map(256,256, mapSrs)
|
2011-10-29 02:06:23 +02:00
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
2013-01-28 02:27:24 +01:00
|
|
|
map_proj = mapnik.Projection(mapSrs)
|
|
|
|
layer_proj = mapnik.Projection(lyrSrs)
|
|
|
|
prj_trans = mapnik.ProjTransform(map_proj,
|
|
|
|
layer_proj)
|
2011-10-29 02:06:23 +02:00
|
|
|
_map.zoom_to_box(prj_trans.backward(lyr.envelope()))
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2014-12-03 19:53:04 +01:00
|
|
|
expected_file = './images/support/raster_warping.png'
|
|
|
|
actual_file = '/tmp/' + os.path.basename(expected_file)
|
|
|
|
im.save(actual_file,'png32')
|
|
|
|
if not os.path.exists(expected_file) or os.environ.get('UPDATE'):
|
|
|
|
im.save(expected_file,'png32')
|
|
|
|
actual = mapnik.Image.open(actual_file)
|
|
|
|
expected = mapnik.Image.open(expected_file)
|
|
|
|
eq_(actual.tostring('png32'),expected.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual_file,expected_file))
|
2011-09-14 18:17:47 +02:00
|
|
|
|
|
|
|
def test_raster_warping_does_not_overclip_source():
|
|
|
|
lyrSrs = "+init=epsg:32630"
|
|
|
|
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr = mapnik.Layer('dataraster', lyrSrs)
|
2012-09-05 13:53:37 +02:00
|
|
|
if 'gdal' in mapnik.DatasourceCache.plugin_names():
|
2011-11-23 12:33:58 +01:00
|
|
|
lyr.datasource = mapnik.Gdal(
|
2011-10-29 02:06:23 +02:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
2011-11-23 12:33:58 +01:00
|
|
|
sym = mapnik.RasterSymbolizer()
|
|
|
|
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255,255,0))
|
|
|
|
rule = mapnik.Rule()
|
2011-10-29 02:06:23 +02:00
|
|
|
rule.symbols.append(sym)
|
2011-11-23 12:33:58 +01:00
|
|
|
style = mapnik.Style()
|
2011-10-29 02:06:23 +02:00
|
|
|
style.rules.append(rule)
|
2011-11-23 12:33:58 +01:00
|
|
|
_map = mapnik.Map(256,256, mapSrs)
|
|
|
|
_map.background=mapnik.Color('white')
|
2011-10-29 02:06:23 +02:00
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
2011-11-23 12:33:58 +01:00
|
|
|
_map.zoom_to_box(mapnik.Box2d(3,42,4,43))
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-11-23 12:33:58 +01:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2014-12-03 19:53:04 +01:00
|
|
|
expected_file = './images/support/raster_warping_does_not_overclip_source.png'
|
|
|
|
actual_file = '/tmp/' + os.path.basename(expected_file)
|
|
|
|
im.save(actual_file,'png32')
|
|
|
|
if not os.path.exists(expected_file) or os.environ.get('UPDATE'):
|
|
|
|
im.save(expected_file,'png32')
|
|
|
|
actual = mapnik.Image.open(actual_file)
|
|
|
|
expected = mapnik.Image.open(expected_file)
|
|
|
|
eq_(actual.tostring('png32'),expected.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual_file,expected_file))
|
2012-02-24 22:13:56 +01:00
|
|
|
|
2011-08-31 00:51:42 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
2014-07-14 18:34:20 +02:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|