def test_gradient_0(self): # Empty colors = ColorGradient() 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)
def test_gradient_3(self): # Three colors - one gradient split in half dark_red = rgba(127, 0, 0, 255) colors = ColorGradient({0: self.red, 128: dark_red, 255: self.black}) self.assertEqual( colors._clauses(band='r'), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.black.r)]) self.assertEqual(colors._clauses(band='g'), []) self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)]) self.assertEqual( colors._expression(band='r'), 'where(n >= 255, {black}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( black=self.black.r, red=float(self.red.r), false=ColorGradient.BACKGROUND.r)) self.assertEqual(colors._expression(band='g'), None) self.assertEqual( colors._expression(band='a'), 'where(n >= 0, {red}, {false})'.format( red=self.red.a, false=ColorGradient.BACKGROUND.a)) # Three colors - one gradient split in half dark_red = rgba(127, 0, 0, 255) colors = ColorGradient({0: self.red, 64: dark_red, 255: self.black}) self.assertEqual( colors._clauses(band='r'), [('n >= 0', '-0.5 * n + {0}'.format(float(self.red.r))), ('n >= 64', '-1.5039370078740157 * n + 223.251968503937'), ('n >= 255', self.black.r)]) self.assertEqual(colors._clauses(band='g'), []) self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)]) self.assertEqual( colors._expression(band='r'), 'where(n >= 255, {black}, ' 'where(n >= 64, -1.5039370078740157 * n + 223.251968503937, ' 'where(n >= 0, -0.5 * n + {red}, {false})))'.format( black=self.black.r, red=float(self.red.r), false=ColorGradient.BACKGROUND.r)) self.assertEqual(colors._expression(band='g'), None) self.assertEqual( colors._expression(band='a'), 'where(n >= 0, {red}, {false})'.format( red=self.red.a, false=ColorGradient.BACKGROUND.a))
def test_gradient_2(self): # Two colors colors = ColorGradient({0: self.red, 255: self.green}) self.assertEqual( colors._clauses(band='r'), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.green.r)]) self.assertEqual( colors._clauses(band='g'), [('n >= 0', '1.0 * n + {0}'.format(float(self.red.g))), ('n >= 255', self.green.g)]) self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)]) self.assertEqual( colors._expression(band='r'), 'where(n >= 255, {green}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( red=float(self.red.r), green=self.green.r, false=ColorGradient.BACKGROUND.r)) self.assertEqual( colors._expression(band='g'), 'where(n >= 255, {green}, ' 'where(n >= 0, 1.0 * n + {red}, {false}))'.format( red=float(self.red.g), green=self.green.g, false=ColorGradient.BACKGROUND.g)) self.assertEqual( colors._expression(band='a'), 'where(n >= 0, {red}, {false})'.format( red=self.red.a, false=ColorGradient.BACKGROUND.a)) # Two colors, with a nodata value in between them colors = ColorGradient({0: self.red, 255: self.green}) self.assertEqual( colors._clauses(band='r', nodata=1), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.green.r)]) self.assertEqual( colors._clauses(band='g', nodata=1), [('n >= 0', '1.0 * n + {0}'.format(float(self.red.g))), ('n >= 255', self.green.g)]) self.assertEqual(colors._clauses(band='a', nodata=1), [('n >= 0', self.red.a), ('n == 1', ColorGradient.BACKGROUND.a)]) self.assertEqual( colors._expression(band='r', nodata=1), 'where(n >= 255, {green}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( red=float(self.red.r), green=self.green.r, false=ColorGradient.BACKGROUND.r)) self.assertEqual( colors._expression(band='g', nodata=1), 'where(n >= 255, {green}, ' 'where(n >= 0, 1.0 * n + {red}, {false}))'.format( red=float(self.red.g), green=self.green.g, false=ColorGradient.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=ColorGradient.BACKGROUND.a)) # Two colors, with nodata value replacing one of them colors = ColorGradient({0: self.red, 255: self.green}) self.assertEqual( colors._clauses(band='r', nodata=0), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.green.r)]) self.assertEqual( colors._clauses(band='g', nodata=0), [('n >= 0', '1.0 * n + {0}'.format(float(self.red.g))), ('n >= 255', self.green.g)]) self.assertEqual(colors._clauses(band='a', nodata=0), [('n >= 0', self.red.a), ('n == 0', ColorGradient.BACKGROUND.a)]) self.assertEqual( colors._expression(band='r', nodata=0), 'where(n >= 255, {green}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( red=float(self.red.r), green=self.green.r, false=ColorGradient.BACKGROUND.r)) self.assertEqual( colors._expression(band='g', nodata=0), 'where(n >= 255, {green}, ' 'where(n >= 0, 1.0 * n + {red}, {false}))'.format( red=float(self.red.g), green=self.green.g, false=ColorGradient.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=ColorGradient.BACKGROUND.a))
def test_gradient_1(self): # One color colors = ColorGradient({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=ColorGradient.BACKGROUND.r)) self.assertEqual( colors._expression(band='a'), 'where(n >= 0, {true}, {false})'.format( true=self.red.a, false=ColorGradient.BACKGROUND.a)) # One color, with nodata value before it colors = ColorGradient({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=ColorGradient.BACKGROUND.r)) self.assertEqual( colors._expression(band='a', nodata=float('-inf')), 'where(n >= 0, {true}, {false})'.format( true=self.red.a, false=ColorGradient.BACKGROUND.a)) # One color, with nodata value after it colors = ColorGradient({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', ColorGradient.BACKGROUND.a)]) self.assertEqual( colors._expression(band='r', nodata=float('inf')), 'where(n >= 0, {true}, {false})'.format( true=self.red.r, false=ColorGradient.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=ColorGradient.BACKGROUND.a))
def test_gradient_3(self): # Three colors - one gradient split in half dark_red = rgba(127, 0, 0, 255) colors = ColorGradient({0: self.red, 128: dark_red, 255: self.black}) self.assertEqual( colors._clauses(band='r'), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.black.r)] ) self.assertEqual(colors._clauses(band='g'), []) self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)]) self.assertEqual(colors._expression(band='r'), 'where(n >= 255, {black}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( black=self.black.r, red=float(self.red.r), false=ColorGradient.BACKGROUND.r )) self.assertEqual(colors._expression(band='g'), None) self.assertEqual(colors._expression(band='a'), 'where(n >= 0, {red}, {false})'.format( red=self.red.a, false=ColorGradient.BACKGROUND.a )) # Three colors - one gradient split in half dark_red = rgba(127, 0, 0, 255) colors = ColorGradient({0: self.red, 64: dark_red, 255: self.black}) self.assertEqual( colors._clauses(band='r'), [('n >= 0', '-0.5 * n + {0}'.format(float(self.red.r))), ('n >= 64', '-1.5039370078740157 * n + 223.251968503937'), ('n >= 255', self.black.r)] ) self.assertEqual(colors._clauses(band='g'), []) self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)]) self.assertEqual( colors._expression(band='r'), 'where(n >= 255, {black}, ' 'where(n >= 64, -1.5039370078740157 * n + 223.251968503937, ' 'where(n >= 0, -0.5 * n + {red}, {false})))'.format( black=self.black.r, red=float(self.red.r), false=ColorGradient.BACKGROUND.r )) self.assertEqual(colors._expression(band='g'), None) self.assertEqual(colors._expression(band='a'), 'where(n >= 0, {red}, {false})'.format( red=self.red.a, false=ColorGradient.BACKGROUND.a ))
def test_gradient_2(self): # Two colors colors = ColorGradient({0: self.red, 255: self.green}) self.assertEqual( colors._clauses(band='r'), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.green.r)] ) self.assertEqual( colors._clauses(band='g'), [('n >= 0', '1.0 * n + {0}'.format(float(self.red.g))), ('n >= 255', self.green.g)] ) self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)]) self.assertEqual( colors._expression(band='r'), 'where(n >= 255, {green}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( red=float(self.red.r), green=self.green.r, false=ColorGradient.BACKGROUND.r ) ) self.assertEqual( colors._expression(band='g'), 'where(n >= 255, {green}, ' 'where(n >= 0, 1.0 * n + {red}, {false}))'.format( red=float(self.red.g), green=self.green.g, false=ColorGradient.BACKGROUND.g ) ) self.assertEqual( colors._expression(band='a'), 'where(n >= 0, {red}, {false})'.format( red=self.red.a, false=ColorGradient.BACKGROUND.a ) ) # Two colors, with a nodata value in between them colors = ColorGradient({0: self.red, 255: self.green}) self.assertEqual( colors._clauses(band='r', nodata=1), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.green.r)] ) self.assertEqual( colors._clauses(band='g', nodata=1), [('n >= 0', '1.0 * n + {0}'.format(float(self.red.g))), ('n >= 255', self.green.g)] ) self.assertEqual(colors._clauses(band='a', nodata=1), [('n >= 0', self.red.a), ('n == 1', ColorGradient.BACKGROUND.a)]) self.assertEqual( colors._expression(band='r', nodata=1), 'where(n >= 255, {green}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( red=float(self.red.r), green=self.green.r, false=ColorGradient.BACKGROUND.r ) ) self.assertEqual( colors._expression(band='g', nodata=1), 'where(n >= 255, {green}, ' 'where(n >= 0, 1.0 * n + {red}, {false}))'.format( red=float(self.red.g), green=self.green.g, false=ColorGradient.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=ColorGradient.BACKGROUND.a ) ) # Two colors, with nodata value replacing one of them colors = ColorGradient({0: self.red, 255: self.green}) self.assertEqual( colors._clauses(band='r', nodata=0), [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))), ('n >= 255', self.green.r)] ) self.assertEqual( colors._clauses(band='g', nodata=0), [('n >= 0', '1.0 * n + {0}'.format(float(self.red.g))), ('n >= 255', self.green.g)] ) self.assertEqual(colors._clauses(band='a', nodata=0), [('n >= 0', self.red.a), ('n == 0', ColorGradient.BACKGROUND.a)]) self.assertEqual( colors._expression(band='r', nodata=0), 'where(n >= 255, {green}, ' 'where(n >= 0, -1.0 * n + {red}, {false}))'.format( red=float(self.red.r), green=self.green.r, false=ColorGradient.BACKGROUND.r ) ) self.assertEqual( colors._expression(band='g', nodata=0), 'where(n >= 255, {green}, ' 'where(n >= 0, 1.0 * n + {red}, {false}))'.format( red=float(self.red.g), green=self.green.g, false=ColorGradient.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=ColorGradient.BACKGROUND.a ) )
def test_gradient_1(self): # One color colors = ColorGradient({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=ColorGradient.BACKGROUND.r )) self.assertEqual(colors._expression(band='a'), 'where(n >= 0, {true}, {false})'.format( true=self.red.a, false=ColorGradient.BACKGROUND.a )) # One color, with nodata value before it colors = ColorGradient({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=ColorGradient.BACKGROUND.r )) self.assertEqual(colors._expression(band='a', nodata=float('-inf')), 'where(n >= 0, {true}, {false})'.format( true=self.red.a, false=ColorGradient.BACKGROUND.a )) # One color, with nodata value after it colors = ColorGradient({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', ColorGradient.BACKGROUND.a)]) self.assertEqual(colors._expression(band='r', nodata=float('inf')), 'where(n >= 0, {true}, {false})'.format( true=self.red.r, false=ColorGradient.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=ColorGradient.BACKGROUND.a ))