File: raster_colorizer_test.py

package info (click to toggle)
python-mapnik 1%3A0.0~20200224-7da019cf9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,784 kB
  • sloc: python: 12,085; cpp: 5,717; sh: 101; makefile: 18
file content (107 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# coding=utf8
import os
import sys

from nose.tools import eq_

import mapnik

from .utilities import execution_path, run_all

PYTHON3 = sys.version_info[0] == 3


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('.'))

# test discrete colorizer mode


def test_get_color_discrete():
    # setup
    colorizer = mapnik.RasterColorizer()
    colorizer.default_color = mapnik.Color(0, 0, 0, 0)
    colorizer.default_mode = mapnik.COLORIZER_DISCRETE

    colorizer.add_stop(10, mapnik.Color(100, 100, 100, 100))
    colorizer.add_stop(20, mapnik.Color(200, 200, 200, 200))

    # should be default colour
    eq_(colorizer.get_color(-50), mapnik.Color(0, 0, 0, 0))
    eq_(colorizer.get_color(0), mapnik.Color(0, 0, 0, 0))

    # now in stop 1
    eq_(colorizer.get_color(10), mapnik.Color(100, 100, 100, 100))
    eq_(colorizer.get_color(19), mapnik.Color(100, 100, 100, 100))

    # now in stop 2
    eq_(colorizer.get_color(20), mapnik.Color(200, 200, 200, 200))
    eq_(colorizer.get_color(1000), mapnik.Color(200, 200, 200, 200))

# test exact colorizer mode


def test_get_color_exact():
    # setup
    colorizer = mapnik.RasterColorizer()
    colorizer.default_color = mapnik.Color(0, 0, 0, 0)
    colorizer.default_mode = mapnik.COLORIZER_EXACT

    colorizer.add_stop(10, mapnik.Color(100, 100, 100, 100))
    colorizer.add_stop(20, mapnik.Color(200, 200, 200, 200))

    # should be default colour
    eq_(colorizer.get_color(-50), mapnik.Color(0, 0, 0, 0))
    eq_(colorizer.get_color(11), mapnik.Color(0, 0, 0, 0))
    eq_(colorizer.get_color(20.001), mapnik.Color(0, 0, 0, 0))

    # should be stop 1
    eq_(colorizer.get_color(10), mapnik.Color(100, 100, 100, 100))

    # should be stop 2
    eq_(colorizer.get_color(20), mapnik.Color(200, 200, 200, 200))

# test linear colorizer mode


def test_get_color_linear():
    # setup
    colorizer = mapnik.RasterColorizer()
    colorizer.default_color = mapnik.Color(0, 0, 0, 0)
    colorizer.default_mode = mapnik.COLORIZER_LINEAR

    colorizer.add_stop(10, mapnik.Color(100, 100, 100, 100))
    colorizer.add_stop(20, mapnik.Color(200, 200, 200, 200))

    # should be default colour
    eq_(colorizer.get_color(-50), mapnik.Color(0, 0, 0, 0))
    eq_(colorizer.get_color(9.9), mapnik.Color(0, 0, 0, 0))

    # should be stop 1
    eq_(colorizer.get_color(10), mapnik.Color(100, 100, 100, 100))

    # should be stop 2
    eq_(colorizer.get_color(20), mapnik.Color(200, 200, 200, 200))

    # half way between stops 1 and 2
    eq_(colorizer.get_color(15), mapnik.Color(150, 150, 150, 150))

    # after stop 2
    eq_(colorizer.get_color(100), mapnik.Color(200, 200, 200, 200))


def test_stop_label():
    stop = mapnik.ColorizerStop(
        1, mapnik.COLORIZER_LINEAR, mapnik.Color('red'))
    assert not stop.label
    label = u"32ยบ C"
    if not PYTHON3:
        label = label.encode('utf8')
    stop.label = label
    assert stop.label == label, stop.label

if __name__ == "__main__":
    setup()
    exit(run_all(eval(x) for x in dir() if x.startswith("test_")))