예제 #1
0
    def test_querylayer_time_default(self):
        """layer.QueryLayer time defaults"""
        time_ans = {
            'column': 'time_col',
            'method': 'count',
            'cumulative': False,
            'frames': 256,
            'duration': 30
        }
        # pass a valid column name
        qlayer = QueryLayer(self.query, time='time_col')
        self.assertEqual(qlayer.time, time_ans)

        # pass a valid dict style
        qlayer = QueryLayer(self.query,
                            time={
                                'column': 'time_col',
                                'method': 'avg',
                                'duration': 10
                            })
        time_ans = {
            'column': 'time_col',
            'method': 'avg',
            'frames': 256,
            'duration': 10,
            'cumulative': False
        }

        self.assertEqual(qlayer.time, time_ans)
예제 #2
0
    def test_querylayer_size_defaults(self):
        """layer.QueryLayer gets defaults for options not passed"""
        qlayer = QueryLayer(self.query, size='cold_brew')
        size_col_ans = {
            'column': 'cold_brew',
            'range': [5, 25],
            'bins': 10,
            'bin_method': 'quantiles'
        }
        self.assertEqual(qlayer.size,
                         size_col_ans,
                         msg='size column should receive defaults')

        qlayer = QueryLayer(self.query,
                            size={
                                'column': 'cold_brew',
                                'range': [4, 15],
                                'bin_method': 'equal'
                            })
        ans = {
            'column': 'cold_brew',
            'range': [4, 15],
            'bins': 10,
            'bin_method': 'equal'
        }
        self.assertEqual(qlayer.size,
                         ans,
                         msg=('size dict should receive defaults if not '
                              'provided'))
예제 #3
0
 def test_querylayer_get_cartocss(self):
     """layer.QueryLayer._get_cartocss"""
     qlayer = QueryLayer(self.query,
                         size=dict(column='cold_brew', min=10, max=20))
     self.assertRegexpMatches(
         qlayer._get_cartocss(BaseMap()),
         ('.*marker-width:\sramp\(\[cold_brew\],\srange\(10,20\),\s'
          'quantiles\(5\)\).*'))
예제 #4
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\).*')
예제 #5
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\).*')
예제 #6
0
    def test_querylayer_size_column_key(self):
        """layer.QueryLayer size dict has to have a column key"""

        # size dict must have a 'column' key
        with self.assertRaises(ValueError,
                               msg='size dict must have a `column` key'):
            QueryLayer(self.query, size={'scheme': styling.temps(10)})
예제 #7
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)
예제 #8
0
 def test_querylayer_size_and_time(self):
     """layer.QueryLayer size and time cannot be used together"""
     # size and time cannot be specified at the same time if size is
     #  styled by value
     with self.assertRaises(ValueError, msg='time key should not be present'):
         QueryLayer(self.query,
                    size={'column': 'mag',
                          'scheme': styling.temps(10)},
                    time='date_col')
예제 #9
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']
        str_colors_ans = ['#FF0000', 'aliceblue', 'cookie_monster']
        str_scheme_ans = [None, None, styling.mint(5)]

        for idx, color in enumerate(str_colors):
            qlayer = QueryLayer(self.query, color=color)
            print(qlayer.color)
            self.assertEqual(qlayer.color, str_colors_ans[idx])
            self.assertEqual(qlayer.scheme, str_scheme_ans[idx])

        # 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)})
예제 #10
0
    def setUp(self):
        self.layers = [
            BaseMap('dark'),
            Layer('cb_2013_puma10_500k', color='grey'),
            Layer('tweets_obama', color='yellow', size='favoritescount')
        ]

        self.layers_w_time = [
            BaseMap('dark', labels='front'),
            Layer('acadia'),
            QueryLayer('select * from acadia limit 10', time='foo'),
            Layer('biodiversity'),
            BaseMap('dark', labels='front', only_labels=True)
        ]
예제 #11
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\).*')
예제 #12
0
 def test_querylayer_size_default(self):
     """layer.QueryLayer size to have default value"""
     qlayer = QueryLayer(self.query)
     self.assertEqual(qlayer.size,
                      10,
                      msg='should return default styling of 10')
예제 #13
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)
예제 #14
0
    def test_querylayer_get_cartocss(self):
        """layer.QueryLayer._get_cartocss"""
        qlayer = QueryLayer(self.query,
                            size=dict(column='cold_brew', min=10, max=20))
        qlayer.geom_type = 'point'
        self.assertRegexpMatches(
            qlayer._get_cartocss(BaseMap()),  # pylint: disable=protected-access
            (r'.*marker-width:\sramp\(\[cold_brew\],\srange\(10,20\),\s'
             r'quantiles\(5\)\).*'))

        # test line cartocss
        qlayer = QueryLayer(self.query)
        qlayer.geom_type = 'line'
        self.assertRegexpMatches(
            qlayer._get_cartocss(BaseMap()),  # pylint: disable=protected-access
            r'^\#layer.*line\-width.*$')
        # test point, line, polygon
        for geom in (
                'point',
                'line',
                'polygon',
        ):
            styles = {
                'point': r'marker\-fill',
                'line': r'line\-color',
                'polygon': r'polygon\-fill'
            }
            qlayer = QueryLayer(self.query, color='colname')
            qlayer.geom_type = geom
            self.assertRegexpMatches(
                qlayer._get_cartocss(BaseMap()),  # pylint: disable=protected-access
                r'^\#layer.*{}.*\}}$'.format(styles[geom]))

        # geometry type should be defined
        with self.assertRaises(ValueError, msg='invalid geometry type'):
            querylayer = QueryLayer(self.query, color='red')
            querylayer.geom_type = 'notvalid'
            querylayer._get_cartocss(BaseMap())  # pylint: disable=protected-access
예제 #15
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
예제 #16
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)})