Exemplo n.º 1
0
    def test_querylayer_time_errors(self):
        """layer.QueryLayer time option exceptions"""

        # time str column cannot be the_geom
        with self.assertRaises(ValueError,
                               msg='time column cannot be `the_geom`'):
            QueryLayer(self.query, time='the_geom')

        # time dict must have a 'column' key
        with self.assertRaises(ValueError,
                               msg='time dict must have a `column` key'):
            QueryLayer(self.query, time={'scheme': styling.armyRose(10)})

        # pass an int as the time column
        with self.assertRaises(ValueError,
                               msg='`time` key has to be a str or dict'):
            QueryLayer(self.query, time=7)

        with self.assertRaises(ValueError):
            querylayer = QueryLayer('select * from watermelon', time='seeds')
            querylayer.style_cols['seeds'] = 'string'
            querylayer.geom_type = 'point'
            querylayer._setup([BaseMap(), querylayer], 1)  # pylint: disable=protected-access

        with self.assertRaises(ValueError):
            querylayer = QueryLayer('select * from watermelon', time='seeds')
            querylayer.style_cols['seeds'] = 'date'
            querylayer.geom_type = 'polygon'
            querylayer._setup([BaseMap(), querylayer], 1)  # pylint: disable=protected-access
Exemplo n.º 2
0
    def test_querylayer_time_category(self):
        """layer.QueryLayer time with categories"""
        querylayer = QueryLayer(self.query, time='timecol', color='colorcol')
        # category type
        querylayer.style_cols['colorcol'] = 'string'
        querylayer.style_cols['timecol'] = 'date'

        # if non-point geoms are present (or None), raise an error
        with self.assertRaises(
                ValueError,
                msg='cannot make torque map with non-point geometries'):
            querylayer._setup([BaseMap(), querylayer], 1)  # pylint: disable=protected-access

        querylayer.geom_type = 'point'
        # normal behavior for point geometries
        querylayer._setup([BaseMap(), querylayer], 1)  # pylint: disable=protected-access
        self.assertDictEqual(
            querylayer.scheme,
            dict(name='Antique',
                 bin_method='',
                 bins=[str(i) for i in range(1, 11)]))
        # expect category maps query
        self.assertRegexpMatches(
            querylayer.query, r'(?s)^SELECT\norig\.\*,\s__wrap\.'
            r'cf_value_colorcol\n.*GROUP\sBY.*orig\.'
            r'colorcol$')
        # cartocss should have cdb math mode
        self.assertRegexpMatches(querylayer.cartocss,
                                 r'.*CDB_Math_Mode\(cf_value_colorcol\).*')
Exemplo n.º 3
0
    def test_querylayer_time_category(self):
        """layer.QueryLayer time with categories"""
        ql = QueryLayer(self.query, time='timecol', color='colorcol')
        # category type
        ql.style_cols['colorcol'] = 'string'
        ql.style_cols['timecol'] = 'date'

        # if non-point geoms are present (or None), raise an error
        with self.assertRaises(
                ValueError,
                msg='cannot make torque map with non-point geometries'):
            ql._setup([BaseMap(), ql], 1)

        ql.geom_type = 'point'
        # normal behavior for point geometries
        ql._setup([BaseMap(), ql], 1)
        self.assertDictEqual(
            ql.scheme,
            dict(name='Antique',
                 bin_method='',
                 bins=','.join(str(i) for i in range(1, 11))))
        # expect category maps query
        self.assertRegexpMatches(
            ql.query, '^SELECT orig\.\*, '
            '__wrap.cf_value_colorcol.* '
            'GROUP BY.*orig\.colorcol$')
        # cartocss should have cdb math mode
        self.assertRegexpMatches(ql.cartocss,
                                 '.*CDB_Math_Mode\(cf_value_colorcol\).*')
Exemplo n.º 4
0
    def test_querylayer_time_numeric(self):
        """layer.QueryLayer time with quantitative classification"""
        querylayer = QueryLayer(self.query, time='timecol', color='colorcol')
        # category type
        querylayer.style_cols['colorcol'] = 'number'
        querylayer.style_cols['timecol'] = 'date'
        querylayer.geom_type = 'point'

        # normal behavior for point geometries
        querylayer._setup([BaseMap(), querylayer], 1)  # pylint: disable=protected-access
        self.assertDictEqual(querylayer.scheme, styling.mint(5))
        # expect category maps query
        self.assertRegexpMatches(querylayer.query.strip(),
                                 r'^SELECT \*, colorcol as value '
                                 r'.*_wrap$')
        # cartocss should have cdb math mode
        self.assertRegexpMatches(querylayer.cartocss, r'.*avg\(colorcol\).*')
Exemplo n.º 5
0
    def test_line_styling(self):  # pylint: disable=too-many-statements
        """layer.QueryLayer line styling"""
        linelayer = QueryLayer('select * from lines', size=5)

        linelayer.geom_type = 'line'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue('line-width: 5' in linelayer.cartocss)

        size = 'size_col'
        color = 'mag'
        linelayer = QueryLayer('select * from lines', size=size, color=color)

        linelayer.geom_type = 'line'
        linelayer.style_cols['mag'] = 'number'
        linelayer.style_cols['size_col'] = 'number'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue(
            'line-width: ramp([size_col], range(1,5), quantiles(5))' in
            linelayer.cartocss)

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Mint), quantiles(5), >)' in
            linelayer.cartocss)

        size = {'column': 'size_col'}
        color = 'mag'
        linelayer = QueryLayer('select * from lines', size=size, color=color)

        linelayer.geom_type = 'line'
        linelayer.style_cols['mag'] = 'number'
        linelayer.style_cols['size_col'] = 'number'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue(
            'line-width: ramp([size_col], range(1,5), quantiles(5))' in
            linelayer.cartocss)

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Mint), quantiles(5), >)' in
            linelayer.cartocss)

        size = {'column': 'size_col', 'range': (5, 10)}
        color = 'mag'
        linelayer = QueryLayer('select * from lines', size=size, color=color)
        linelayer.geom_type = 'line'
        linelayer.style_cols['mag'] = 'number'
        linelayer.style_cols['size_col'] = 'number'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue(
            'line-width: ramp([size_col], range(5,10), quantiles(5))' in
            linelayer.cartocss)

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Mint), quantiles(5), >)' in
            linelayer.cartocss)

        size = 1.5
        color = 'mag'

        linelayer = QueryLayer('select * from lines', size=size, color=color)

        linelayer.geom_type = 'line'
        linelayer.style_cols['mag'] = 'number'
        linelayer.style_cols['size_col'] = 'number'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue('line-width: 1.5' in linelayer.cartocss)

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Mint), quantiles(5), >)' in
            linelayer.cartocss)

        size = {'column': 'size_col', 'range': [2, 6]}

        color = {'column': 'mag', 'scheme': styling.sunset(7)}

        linelayer = QueryLayer('select * from lines', size=size, color=color)

        linelayer.geom_type = 'line'
        linelayer.style_cols['mag'] = 'number'
        linelayer.style_cols['size_col'] = 'number'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue(
            'line-width: ramp([size_col], range(2,6), quantiles(5))' in
            linelayer.cartocss)

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Sunset), quantiles(7), >)' in
            linelayer.cartocss)

        # size and color
        size = {
            'column': 'size_col',
            'range': [2, 6],
            'bin_method': BinMethod.jenks
        }
        color = {'column': 'mag', 'scheme': styling.sunset(7)}

        linelayer = QueryLayer('select * from lines', size=size, color=color)

        linelayer.geom_type = 'line'
        linelayer.style_cols['mag'] = 'number'
        linelayer.style_cols['size_col'] = 'number'
        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue('line-width: ramp([size_col], range(2,6), jenks(5))' in
                        linelayer.cartocss)

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Sunset), quantiles(7), >)' in
            linelayer.cartocss)

        # category lines

        linelayer = QueryLayer('select * from lines',
                               color={
                                   'column': 'mag',
                                   'scheme': styling.antique(7)
                               })

        linelayer.geom_type = 'line'

        linelayer._setup([BaseMap(), linelayer], 1)  # pylint: disable=protected-access

        self.assertTrue(
            'line-color: ramp([mag], cartocolor(Antique), category(7), =)' in
            linelayer.cartocss)
Exemplo n.º 6
0
    def test_querylayer_colors(self):
        """layer.QueryLayer color options tests"""

        # no color options passed
        basic = QueryLayer(self.query)
        self.assertEqual(basic.color, None)

        # check valid dict color options
        dict_colors = [{
            'column': 'mandrill',
            'scheme': styling.armyRose(7)
        }, {
            'column': 'mercxx',
            'scheme': {
                'bin_method': 'equal',
                'bins': 7,
                'name': 'Temps'
            }
        }, {
            'column': 'elephant',
            'scheme': styling.redOr(10, bin_method='jenks')
        }]
        dict_colors_ans = ['mandrill', 'mercxx', 'elephant']
        dict_colors_scheme = [{
            'name': 'ArmyRose',
            'bins': 7,
            'bin_method': 'quantiles'
        }, {
            'name': 'Temps',
            'bins': 7,
            'bin_method': 'equal'
        }, {
            'name': 'RedOr',
            'bins': 10,
            'bin_method': 'jenks'
        }]
        for idx, val in enumerate(dict_colors):
            qlayer = QueryLayer(self.query, color=val)
            self.assertEqual(qlayer.color, dict_colors_ans[idx])
            self.assertEqual(qlayer.scheme, dict_colors_scheme[idx])

        # check valid string color options
        str_colors = ('#FF0000', 'aliceblue', 'cookie_monster', 'big_bird')
        str_colors_ans = ('#FF0000', 'aliceblue', 'cookie_monster', 'big_bird')
        str_scheme_ans = (None, None, styling.mint(5), styling.antique(10))

        for idx, color in enumerate(str_colors):
            qlayer = QueryLayer(self.query, color=color)
            qlayer.geom_type = 'point'
            if color == 'cookie_monster':
                qlayer.style_cols[color] = 'number'
                qlayer._setup([BaseMap(), qlayer], 1)  # pylint: disable=protected-access
            elif color == 'big_bird':
                qlayer.style_cols[color] = 'string'
                qlayer._setup([BaseMap(), qlayer], 1)  # pylint: disable=protected-access
            self.assertEqual(qlayer.color, str_colors_ans[idx])
            self.assertEqual(qlayer.scheme, str_scheme_ans[idx])

        with self.assertRaises(ValueError,
                               msg='styling value cannot be a date'):
            qlayer = QueryLayer(self.query, color='datetime_column')
            qlayer.style_cols['datetime_column'] = 'date'
            qlayer._setup([BaseMap(), qlayer], 1)  # pylint: disable=protected-access

        # Exception testing
        # color column cannot be a geometry column
        with self.assertRaises(ValueError,
                               msg='color clumn cannot be a geometry column'):
            QueryLayer(self.query, color='the_geom')

        # color dict must have a 'column' key
        with self.assertRaises(ValueError,
                               msg='color dict must have a `column` key'):
            QueryLayer(self.query, color={'scheme': styling.vivid(10)})