File: raster_symbolizer_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 (258 lines) | stat: -rw-r--r-- 9,051 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python

import os

from nose.tools import eq_
from nose.plugins.skip import SkipTest

import mapnik

from .utilities import execution_path, get_unique_colors, run_all


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():
    if not os.path.exists('../data/raster/dataraster.tif'):
        raise SkipTest

    srs = '+init=epsg:32630'
    lyr = mapnik.Layer('dataraster')
    if 'gdal' in mapnik.DatasourceCache.plugin_names():
        lyr.datasource = mapnik.Gdal(
            file='../data/raster/dataraster.tif',
            band=1,
        )
        lyr.srs = srs
        _map = mapnik.Map(256, 256, srs)
        style = mapnik.Style()
        rule = mapnik.Rule()
        sym = mapnik.RasterSymbolizer()
        # Assigning a colorizer to the RasterSymbolizer tells the later
        # that it should use it to colorize the raw data raster
        colorizer = mapnik.RasterColorizer(
            mapnik.COLORIZER_DISCRETE,
            mapnik.Color("transparent"))

        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"),
        ]:
            colorizer.add_stop(value, mapnik.Color(color))
        sym.colorizer = colorizer
        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())

        im = mapnik.Image(_map.width, _map.height)
        mapnik.render(_map, im)
        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))


def test_dataraster_query_point():
    if not os.path.exists('../data/raster/dataraster.tif'):
        raise SkipTest

    srs = '+init=epsg:32630'
    lyr = mapnik.Layer('dataraster')
    if 'gdal' in mapnik.DatasourceCache.plugin_names():
        lyr.datasource = mapnik.Gdal(
            file='../data/raster/dataraster.tif',
            band=1,
        )
        lyr.srs = srs
        _map = mapnik.Map(256, 256, srs)
        _map.layers.append(lyr)

        x, y = 556113.0, 4381428.0  # center of extent of raster
        _map.zoom_all()
        features = list(_map.query_point(0, x, y))
        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 == 18.0, value

        # 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)
        features = _map.query_point(0, -427417, 4477517)
        assert len(list(features)) == 0

        # point inside raster extent with nodata
        features = _map.query_point(0, 126850, 4596050)
        assert len(list(features)) == 0


def test_load_save_map():
    if not os.path.exists('../data/good_maps/raster_symbolizer.xml'):
        raise SkipTest

    map = mapnik.Map(256, 256)
    in_map = "../data/good_maps/raster_symbolizer.xml"
    try:
        mapnik.load_map(map, in_map)

        out_map = mapnik.save_map_to_string(map)
        assert 'RasterSymbolizer' in out_map
        assert 'RasterColorizer' in out_map
        assert 'stop' in out_map
    except RuntimeError as e:
        # only test datasources that we have installed
        if not 'Could not create datasource' in str(e):
            raise RuntimeError(str(e))


def test_raster_with_alpha_blends_correctly_with_background():
    if not os.path.exists('../data/raster/white-alpha.png'):
        raise SkipTest

    WIDTH = 500
    HEIGHT = 500

    map = mapnik.Map(WIDTH, HEIGHT)
    WHITE = mapnik.Color(255, 255, 255)
    map.background = WHITE

    style = mapnik.Style()
    rule = mapnik.Rule()
    symbolizer = mapnik.RasterSymbolizer()
    symbolizer.scaling = mapnik.scaling_method.BILINEAR

    rule.symbols.append(symbolizer)
    style.rules.append(rule)

    map.append_style('raster_style', style)

    map_layer = mapnik.Layer('test_layer')
    filepath = '../data/raster/white-alpha.png'
    if 'gdal' in mapnik.DatasourceCache.plugin_names():
        map_layer.datasource = mapnik.Gdal(file=filepath)
        map_layer.styles.append('raster_style')
        map.layers.append(map_layer)

        map.zoom_all()

        mim = mapnik.Image(WIDTH, HEIGHT)

        mapnik.render(map, mim)
        mim.tostring()
        # All white is expected
        eq_(get_unique_colors(mim), ['rgba(254,254,254,255)'])


def test_raster_warping():
    if not os.path.exists('../data/raster/dataraster.tif'):
        raise SkipTest

    lyrSrs = "+init=epsg:32630"
    mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
    lyr = mapnik.Layer('dataraster', lyrSrs)
    if 'gdal' in mapnik.DatasourceCache.plugin_names():
        lyr.datasource = mapnik.Gdal(
            file='../data/raster/dataraster.tif',
            band=1,
        )
        sym = mapnik.RasterSymbolizer()
        sym.colorizer = mapnik.RasterColorizer(
            mapnik.COLORIZER_DISCRETE, mapnik.Color(255, 255, 0))
        rule = mapnik.Rule()
        rule.symbols.append(sym)
        style = mapnik.Style()
        style.rules.append(rule)
        _map = mapnik.Map(256, 256, mapSrs)
        _map.append_style('foo', style)
        lyr.styles.append('foo')
        _map.layers.append(lyr)
        map_proj = mapnik.Projection(mapSrs)
        layer_proj = mapnik.Projection(lyrSrs)
        prj_trans = mapnik.ProjTransform(map_proj,
                                         layer_proj)
        _map.zoom_to_box(prj_trans.backward(lyr.envelope()))

        im = mapnik.Image(_map.width, _map.height)
        mapnik.render(_map, im)
        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))


def test_raster_warping_does_not_overclip_source():
    if not os.path.exists('../data/raster/dataraster.tif'):
        raise SkipTest

    lyrSrs = "+init=epsg:32630"
    mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
    lyr = mapnik.Layer('dataraster', lyrSrs)
    if 'gdal' in mapnik.DatasourceCache.plugin_names():
        lyr.datasource = mapnik.Gdal(
            file='../data/raster/dataraster.tif',
            band=1,
        )
        sym = mapnik.RasterSymbolizer()
        sym.colorizer = mapnik.RasterColorizer(
            mapnik.COLORIZER_DISCRETE, mapnik.Color(255, 255, 0))
        rule = mapnik.Rule()
        rule.symbols.append(sym)
        style = mapnik.Style()
        style.rules.append(rule)
        _map = mapnik.Map(256, 256, mapSrs)
        _map.background = mapnik.Color('white')
        _map.append_style('foo', style)
        lyr.styles.append('foo')
        _map.layers.append(lyr)
        _map.zoom_to_box(mapnik.Box2d(3, 42, 4, 43))

        im = mapnik.Image(_map.width, _map.height)
        mapnik.render(_map, im)
        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))

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