Skip to content

Commit

Permalink
Merge 29eb853 into 140393e
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-esch committed Dec 14, 2017
2 parents 140393e + 29eb853 commit 92c6995
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
29 changes: 15 additions & 14 deletions cartoframes/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def __init__(self, query, time=None, color=None, size=None,
# style columns as keys, data types as values
self.style_cols = dict()
self.geom_type = None
self.cartocss = None
self.torque_cartocss = None

# TODO: move these if/else branches to individual methods
# color, scheme = self._get_colorscheme()
Expand Down Expand Up @@ -407,11 +409,10 @@ def _setup(self, layers, layer_idx):
]).format(col=self.color, query=self.orig_query)
agg_func = '\'CDB_Math_Mode(cf_value_{})\''.format(self.color)
self.scheme = {
'bins': ','.join(str(i) for i in range(1, 11)),
'name': (self.scheme.get('name') if self.scheme
else 'Bold'),
'bin_method': '',
}
'bins': ','.join(str(i) for i in range(1, 11)),
'name': (self.scheme.get('name') if self.scheme
else 'Bold'),
'bin_method': '', }
elif (self.color in self.style_cols and
self.style_cols[self.color] in ('number', )):
self.query = ' '.join([
Expand All @@ -421,7 +422,7 @@ def _setup(self, layers, layer_idx):
agg_func = '\'avg({})\''.format(self.color)
else:
agg_func = "'{method}(cartodb_id)'".format(
method=method)
method=method)
self.torque_cartocss = cssify({
'Map': {
'-torque-frame-count': frames,
Expand Down Expand Up @@ -456,8 +457,8 @@ def _get_cartocss(self, basemap, has_time=False):

if self.scheme:
color_style = get_scheme_cartocss(
'value' if has_time else self.color,
self.scheme)
'value' if has_time else self.color,
self.scheme)
else:
color_style = self.color

Expand All @@ -481,14 +482,14 @@ def _get_cartocss(self, basemap, has_time=False):
'#layer[{} = null]'.format(self.color): {
'marker-fill': '#666'}
})
for t in range(1, self.time['trails'] + 1):
for trail_num in range(1, self.time['trails'] + 1):
# Trails decay as 1/2^n, and grow 30% at each step
trail_temp = cssify({
'#layer[frame-offset={}]'.format(t): {
'marker-width': size_style * (1.0 + t * 0.3),
'marker-opacity': 0.9 / 2.0**t,
}
})
'#layer[frame-offset={}]'.format(trail_num): {
'marker-width': size_style * (1.0 + trail_num * 0.3),
'marker-opacity': 0.9 / 2.0**trail_num,
}
})
css += trail_temp
return css
else:
Expand Down
18 changes: 12 additions & 6 deletions cartoframes/styling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:width: 700px
:alt: CARTOColors
""" # noqa

import collections

class BinMethod:
"""Data classification methods used for the styling of data on maps.
Expand All @@ -31,14 +31,19 @@ def get_scheme_cartocss(column, scheme_info):
else:
color_scheme = 'cartocolor({})'.format(scheme_info['name'])
bin_method = scheme_info['bin_method']
return ('ramp([{column}], {color_scheme}, '
if not isinstance(scheme_info['bins'], int):
bins = ','.join(str(i) for i in scheme_info['bins'])
else:
bins = scheme_info['bins']
ramp = ('ramp([{column}], {color_scheme}, '
'{bin_method}({bins}){comparison})').format(
column=column,
color_scheme=color_scheme,
bin_method=bin_method,
bins=scheme_info['bins'],
comparison=('' if bin_method == 'category' else ', <=')
)
bins=bins,
comparison=('' if bin_method == 'category' else ', <='))
print(ramp)
return ramp


def custom(colors, bins=None, bin_method=BinMethod.quantiles):
Expand Down Expand Up @@ -78,7 +83,8 @@ def scheme(name, bins, bin_method):
return {
'name': name,
'bins': bins,
'bin_method': bin_method,
'bin_method': (bin_method if not isinstance(bins, collections.Iterable)
else ''),
}


Expand Down
16 changes: 11 additions & 5 deletions test/test_styling.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ def test_get_scheme_cartocss(self):
'ramp([acadia], cartocolor(Purp), quantiles(4), <=)')
# test on custom
self.assertEqual(
get_scheme_cartocss('acadia',
styling.custom(('#FFF', '#888', '#000'),
bins=3,
bin_method='equal')),
'ramp([acadia], (#FFF,#888,#000), equal(3), <=)')
get_scheme_cartocss('acadia',
styling.custom(('#FFF', '#888', '#000'),
bins=3,
bin_method='equal')),
'ramp([acadia], (#FFF,#888,#000), equal(3), <=)')

# test with non-int quantification
self.assertEqual(
get_scheme_cartocss('acadia',
styling.sunset([1, 2, 3])),
'ramp([acadia], cartocolor(Sunset), (1,2,3), <=)')

def test_scheme(self):
"""styling.scheme"""
Expand Down

0 comments on commit 92c6995

Please sign in to comment.