2010-03-12 13:34:13 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from nose.tools import *
|
2010-03-18 20:05:08 +00:00
|
|
|
from utilities import execution_path, save_data, contains_word
|
2010-03-12 13:34:13 +00:00
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
import os, mapnik
|
2010-03-12 13:34:13 +00: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('.'))
|
|
|
|
|
|
|
|
|
|
|
|
def test_dataraster_coloring():
|
|
|
|
srs = '+init=epsg:32630'
|
2012-03-14 22:44:48 +00:00
|
|
|
lyr = mapnik.Layer('dataraster')
|
|
|
|
lyr.datasource = mapnik.Gdal(
|
2010-03-12 13:34:13 +00:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
|
|
|
lyr.srs = srs
|
2012-03-14 22:44:48 +00:00
|
|
|
_map = mapnik.Map(256,256, srs)
|
|
|
|
style = mapnik.Style()
|
|
|
|
rule = mapnik.Rule()
|
|
|
|
sym = mapnik.RasterSymbolizer()
|
2010-03-12 13:34:13 +00:00
|
|
|
# Assigning a colorizer to the RasterSymbolizer tells the later
|
|
|
|
# that it should use it to colorize the raw data raster
|
2012-03-14 22:44:48 +00:00
|
|
|
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color("transparent"))
|
2011-05-04 00:20:17 +00:00
|
|
|
|
2010-03-12 13:34:13 +00: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"),
|
|
|
|
]:
|
2012-03-14 22:44:48 +00:00
|
|
|
sym.colorizer.add_stop(value, mapnik.Color(color))
|
2010-03-12 13:34:13 +00: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-03-14 22:44:48 +00:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2010-03-16 17:37:25 +00:00
|
|
|
# save a png somewhere so we can see it
|
|
|
|
save_data('test_dataraster_coloring.png', im.tostring('png'))
|
2010-03-12 13:34:13 +00:00
|
|
|
imdata = im.tostring()
|
|
|
|
# we have some values in the [20,30) interval so check that they're colored
|
2010-03-18 20:05:08 +00:00
|
|
|
assert contains_word('\xff\xff\x00\xff', imdata)
|
2010-03-12 13:34:13 +00:00
|
|
|
|
|
|
|
def test_dataraster_query_point():
|
|
|
|
srs = '+init=epsg:32630'
|
2012-03-14 22:44:48 +00:00
|
|
|
lyr = mapnik.Layer('dataraster')
|
|
|
|
lyr.datasource = mapnik.Gdal(
|
2010-03-12 13:34:13 +00:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
|
|
|
lyr.srs = srs
|
2012-03-14 22:44:48 +00:00
|
|
|
_map = mapnik.Map(256,256, srs)
|
2010-03-12 13:34:13 +00:00
|
|
|
_map.layers.append(lyr)
|
|
|
|
|
|
|
|
# point inside raster extent with valid data
|
|
|
|
x, y = 427417, 4477517
|
|
|
|
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']
|
|
|
|
assert value == 21.0, value
|
|
|
|
|
|
|
|
# point outside raster extent
|
|
|
|
features = _map.query_point(0,-427417,4477517).features
|
|
|
|
assert len(features) == 0
|
|
|
|
|
|
|
|
# point inside raster extent with nodata
|
|
|
|
features = _map.query_point(0,126850,4596050).features
|
|
|
|
assert len(features) == 0
|
2010-03-12 14:49:50 +00:00
|
|
|
|
|
|
|
def test_load_save_map():
|
2012-03-14 22:44:48 +00:00
|
|
|
map = mapnik.Map(256,256)
|
2010-03-12 14:49:50 +00:00
|
|
|
in_map = "../data/good_maps/raster_symbolizer.xml"
|
2012-03-14 22:44:48 +00:00
|
|
|
mapnik.load_map(map, in_map)
|
2010-03-12 14:49:50 +00:00
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
out_map = mapnik.save_map_to_string(map)
|
2010-03-12 14:49:50 +00:00
|
|
|
assert 'RasterSymbolizer' in out_map
|
|
|
|
assert 'RasterColorizer' in out_map
|
2011-05-04 00:20:17 +00:00
|
|
|
assert 'stop' in out_map
|
2011-05-12 18:09:03 +00:00
|
|
|
|
|
|
|
def test_raster_with_alpha_blends_correctly_with_background():
|
|
|
|
WIDTH = 500
|
|
|
|
HEIGHT = 500
|
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
map = mapnik.Map(WIDTH, HEIGHT)
|
|
|
|
WHITE = mapnik.Color(255, 255, 255)
|
2011-05-12 18:09:03 +00:00
|
|
|
map.background = WHITE
|
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
style = mapnik.Style()
|
|
|
|
rule = mapnik.Rule()
|
|
|
|
symbolizer = mapnik.RasterSymbolizer()
|
2011-05-12 18:09:03 +00:00
|
|
|
#XXX: This fixes it, see http://trac.mapnik.org/ticket/759#comment:3
|
|
|
|
# (and remove comment when this test passes)
|
|
|
|
#symbolizer.scaling="bilinear_old"
|
|
|
|
|
|
|
|
rule.symbols.append(symbolizer)
|
|
|
|
style.rules.append(rule)
|
|
|
|
|
|
|
|
map.append_style('raster_style', style)
|
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
map_layer = mapnik.Layer('test_layer')
|
2011-05-12 18:09:03 +00:00
|
|
|
filepath = '../data/raster/white-alpha.png'
|
2012-03-14 22:44:48 +00:00
|
|
|
map_layer.datasource = mapnik.Gdal(file=filepath)
|
2011-05-12 18:09:03 +00:00
|
|
|
map_layer.styles.append('raster_style')
|
|
|
|
map.layers.append(map_layer)
|
|
|
|
|
|
|
|
map.zoom_all()
|
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
mim = mapnik.Image(WIDTH, HEIGHT)
|
2011-05-12 18:09:03 +00:00
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
mapnik.render(map, mim)
|
2011-05-12 18:09:03 +00:00
|
|
|
save_data('test_raster_with_alpha_blends_correctly_with_background.png',
|
|
|
|
mim.tostring('png'))
|
|
|
|
imdata = mim.tostring()
|
|
|
|
# All white is expected
|
|
|
|
assert contains_word('\xff\xff\xff\xff', imdata)
|
2011-09-12 18:41:44 +00:00
|
|
|
|
|
|
|
def test_raster_warping():
|
|
|
|
lyrSrs = "+init=epsg:32630"
|
|
|
|
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
|
2012-03-14 22:44:48 +00:00
|
|
|
lyr = mapnik.Layer('dataraster', lyrSrs)
|
|
|
|
lyr.datasource = mapnik.Gdal(
|
2011-09-12 18:41:44 +00:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
2012-03-14 22:44:48 +00:00
|
|
|
sym = mapnik.RasterSymbolizer()
|
|
|
|
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255,255,0))
|
|
|
|
rule = mapnik.Rule()
|
2011-09-12 18:41:44 +00:00
|
|
|
rule.symbols.append(sym)
|
2012-03-14 22:44:48 +00:00
|
|
|
style = mapnik.Style()
|
2011-09-12 18:41:44 +00:00
|
|
|
style.rules.append(rule)
|
2012-03-14 22:44:48 +00:00
|
|
|
_map = mapnik.Map(256,256, mapSrs)
|
2011-09-12 18:41:44 +00:00
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
2012-03-14 22:44:48 +00:00
|
|
|
prj_trans = mapnik.ProjTransform(mapnik.Projection(mapSrs),
|
|
|
|
mapnik.Projection(lyrSrs))
|
2011-09-12 18:41:44 +00:00
|
|
|
_map.zoom_to_box(prj_trans.backward(lyr.envelope()))
|
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2011-09-12 18:41:44 +00:00
|
|
|
# save a png somewhere so we can see it
|
|
|
|
save_data('test_raster_warping.png', im.tostring('png'))
|
|
|
|
imdata = im.tostring()
|
|
|
|
assert contains_word('\xff\xff\x00\xff', imdata)
|
2011-09-14 16:17:47 +00:00
|
|
|
|
|
|
|
def test_raster_warping_does_not_overclip_source():
|
|
|
|
lyrSrs = "+init=epsg:32630"
|
|
|
|
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
|
2012-03-14 22:44:48 +00:00
|
|
|
lyr = mapnik.Layer('dataraster', lyrSrs)
|
|
|
|
lyr.datasource = mapnik.Gdal(
|
2011-09-14 16:17:47 +00:00
|
|
|
file = '../data/raster/dataraster.tif',
|
|
|
|
band = 1,
|
|
|
|
)
|
2012-03-14 22:44:48 +00:00
|
|
|
sym = mapnik.RasterSymbolizer()
|
|
|
|
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255,255,0))
|
|
|
|
rule = mapnik.Rule()
|
2011-09-14 16:17:47 +00:00
|
|
|
rule.symbols.append(sym)
|
2012-03-14 22:44:48 +00:00
|
|
|
style = mapnik.Style()
|
2011-09-14 16:17:47 +00:00
|
|
|
style.rules.append(rule)
|
2012-03-14 22:44:48 +00:00
|
|
|
_map = mapnik.Map(256,256, mapSrs)
|
|
|
|
_map.background=mapnik.Color('white')
|
2011-09-14 16:17:47 +00:00
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
2012-03-14 22:44:48 +00:00
|
|
|
_map.zoom_to_box(mapnik.Box2d(3,42,4,43))
|
2011-09-14 16:17:47 +00:00
|
|
|
|
2012-03-14 22:44:48 +00:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2011-09-14 16:17:47 +00:00
|
|
|
# save a png somewhere so we can see it
|
|
|
|
save_data('test_raster_warping_does_not_overclip_source.png',
|
|
|
|
im.tostring('png'))
|
|
|
|
assert im.view(0,200,1,1).tostring()=='\xff\xff\x00\xff'
|
2011-05-12 18:09:03 +00:00
|
|
|
|
2011-08-30 22:51:42 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
|
|
|
[eval(run)() for run in dir() if 'test_' in run]
|