Пример #1
0
    def test_palette_1(self):
        # One color
        colors = ColorPalette({0: self.red})
        self.assertEqual(colors._clauses(band='r'), [('n >= 0', self.red.r)])
        self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)])
        self.assertEqual(
            colors._expression(band='r'),
            'where(n >= 0, {true}, {false})'.format(
                true=self.red.r, false=ColorPalette.BACKGROUND.r))
        self.assertEqual(
            colors._expression(band='a'),
            'where(n >= 0, {true}, {false})'.format(
                true=self.red.a, false=ColorPalette.BACKGROUND.a))

        # One color, with nodata value before it
        colors = ColorPalette({0: self.red})
        self.assertEqual(colors._clauses(band='r', nodata=float('-inf')),
                         [('n >= 0', self.red.r)])
        self.assertEqual(colors._clauses(band='a', nodata=float('-inf')),
                         [('n >= 0', self.red.a)])
        self.assertEqual(
            colors._expression(band='r', nodata=float('-inf')),
            'where(n >= 0, {true}, {false})'.format(
                true=self.red.r, false=ColorPalette.BACKGROUND.r))
        self.assertEqual(
            colors._expression(band='a', nodata=float('-inf')),
            'where(n >= 0, {true}, {false})'.format(
                true=self.red.a, false=ColorPalette.BACKGROUND.a))

        # One color, with nodata value after it
        colors = ColorPalette({0: self.red})
        self.assertEqual(colors._clauses(band='r', nodata=float('inf')),
                         [('n >= 0', self.red.r)])
        self.assertEqual(colors._clauses(band='a', nodata=float('inf')),
                         [('n >= 0', self.red.a),
                          ('n == inf', ColorPalette.BACKGROUND.a)])
        self.assertEqual(
            colors._expression(band='r', nodata=float('inf')),
            'where(n >= 0, {true}, {false})'.format(
                true=self.red.r, false=ColorPalette.BACKGROUND.r))
        self.assertEqual(
            colors._expression(band='a', nodata=float('inf')),
            'where(n == inf, {false}, where(n >= 0, {true}, {false}))'.format(
                true=self.red.a, false=ColorPalette.BACKGROUND.a))
Пример #2
0
    def test_palette_0(self):
        # Empty
        colors = ColorPalette()
        self.assertEqual(colors._clauses(band='r'), [])
        self.assertEqual(colors._clauses(band='a'), [])
        self.assertEqual(colors._expression(band='r'), None)
        self.assertEqual(colors._expression(band='a'), None)

        # Empty, with nodata - no-op
        self.assertEqual(colors._clauses(band='r', nodata=0), [])
        self.assertEqual(colors._clauses(band='a', nodata=0), [])
        self.assertEqual(colors._expression(band='r', nodata=0), None)
        self.assertEqual(colors._expression(band='a', nodata=0), None)
Пример #3
0
    def test_palette_2(self):
        # Two colors
        colors = ColorPalette({0: self.red, 2: self.green})
        self.assertEqual(colors._clauses(band='r'), [('n >= 0', self.red.r),
                                                     ('n >= 2', self.green.r)])
        self.assertEqual(colors._clauses(band='g'), [('n >= 2', self.green.g)])
        self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)])
        self.assertEqual(
            colors._expression(band='r'),
            'where(n >= 2, {green}, where(n >= 0, {red}, {false}))'.format(
                red=self.red.r,
                green=self.green.r,
                false=ColorPalette.BACKGROUND.r))
        self.assertEqual(
            colors._expression(band='g'),
            'where(n >= 2, {green}, {false})'.format(
                green=self.green.g, false=ColorPalette.BACKGROUND.g))
        self.assertEqual(
            colors._expression(band='a'),
            'where(n >= 0, {red}, {false})'.format(
                red=self.red.a, false=ColorPalette.BACKGROUND.a))

        # Two colors, with a nodata value in between them
        colors = ColorPalette({0: self.red, 2: self.green})
        self.assertEqual(colors._clauses(band='r', nodata=1),
                         [('n >= 0', self.red.r), ('n >= 2', self.green.r)])
        self.assertEqual(colors._clauses(band='g', nodata=1),
                         [('n >= 2', self.green.g)])
        self.assertEqual(colors._clauses(band='a', nodata=1),
                         [('n >= 0', self.red.a),
                          ('n == 1', ColorPalette.BACKGROUND.a)])
        self.assertEqual(
            colors._expression(band='r', nodata=1),
            'where(n >= 2, {green}, where(n >= 0, {red}, {false}))'.format(
                red=self.red.r,
                green=self.green.r,
                false=ColorPalette.BACKGROUND.r))
        self.assertEqual(
            colors._expression(band='g', nodata=1),
            'where(n >= 2, {green}, {false})'.format(
                green=self.green.g, false=ColorPalette.BACKGROUND.g))
        self.assertEqual(
            colors._expression(band='a', nodata=1),
            'where(n == 1, {false}, where(n >= 0, {red}, {false}))'.format(
                red=self.red.a, false=ColorPalette.BACKGROUND.a))

        # Two colors, with nodata value replacing one of them
        colors = ColorPalette({0: self.red, 2: self.green})
        self.assertEqual(colors._clauses(band='r', nodata=0),
                         [('n >= 0', self.red.r), ('n >= 2', self.green.r)])
        self.assertEqual(colors._clauses(band='g', nodata=0),
                         [('n >= 2', self.green.g)])
        self.assertEqual(colors._clauses(band='a', nodata=0),
                         [('n >= 0', self.red.a),
                          ('n == 0', ColorPalette.BACKGROUND.a)])
        self.assertEqual(
            colors._expression(band='r', nodata=0),
            'where(n >= 2, {green}, where(n >= 0, {red}, {false}))'.format(
                red=self.red.r,
                green=self.green.r,
                false=ColorPalette.BACKGROUND.r))
        self.assertEqual(
            colors._expression(band='g', nodata=0),
            'where(n >= 2, {green}, {false})'.format(
                green=self.green.g, false=ColorPalette.BACKGROUND.g))
        self.assertEqual(
            colors._expression(band='a', nodata=0),
            'where(n == 0, {false}, where(n >= 0, {red}, {false}))'.format(
                red=self.red.a, false=ColorPalette.BACKGROUND.a))