Skip to content

Commit

Permalink
Merge 67f0cfd into 3b5a576
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-esch committed Oct 11, 2017
2 parents 3b5a576 + 67f0cfd commit 5866f56
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 78 deletions.
5 changes: 5 additions & 0 deletions cartoframes/assets/cartoframes.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
zoom: @@ZOOM@@,
center: [@@LAT@@, @@LNG@@],
});
if (config.type === 'torque') {
L.tileLayer(config.named_map.params.basemap_url, {
attribution: "&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>"
}).addTo(map);
};
const updateMapInfo = () => {
$('#zoom').text(map.getZoom());
$('#lat').text(map.getCenter().lat.toFixed(4));
Expand Down
54 changes: 43 additions & 11 deletions cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,16 +666,16 @@ def map(self, layers=None, interactive=True,
layers = list(layers)

if len(layers) > 8:
raise ValueError('map can have at most 8 layers')
raise ValueError('Map can have at most 8 layers')

nullity = [zoom is None, lat is None, lng is None]
if any(nullity) and not all(nullity):
raise ValueError('zoom, lat, and lng must all or none be provided')
raise ValueError('Zoom, lat, and lng must all or none be provided')

# When no layers are passed, set default zoom
if ((len(layers) == 0 and zoom is None) or
(len(layers) == 1 and layers[0].is_basemap)):
[zoom, lat, lng] = [3, 38, -99]
[zoom, lat, lng] = [1, 0, 0]
has_zoom = zoom is not None

# Check for a time layer, if it exists move it to the front
Expand All @@ -686,10 +686,9 @@ def map(self, layers=None, interactive=True,
raise ValueError('Map can at most take 1 Layer with time '
'column/field')
if time_layer:
raise NotImplementedError('Animated maps are not yet supported')
if not interactive:
raise ValueError('map cannot display a static image with a '
'time_column')
raise ValueError('Map cannot display a static image with a '
'time column')
layers.append(layers.pop(time_layers[0]))

base_layers = [idx for idx, layer in enumerate(layers)
Expand All @@ -700,6 +699,14 @@ def map(self, layers=None, interactive=True,
raise ValueError('Map can at most take one BaseMap layer')
elif len(base_layers) == 1:
layers.insert(0, layers.pop(base_layers[0]))
if layers[0].is_basic() and layers[0].labels == 'front':
if time_layers:
warn('Basemap labels on top are not currently supported '
'for animated maps')
else:
layers.append(BaseMap(layers[0].source,
labels=layers[0].labels,
only_labels=True))
elif not base_layers:
# default basemap is dark with labels in back
# labels will be changed if all geoms are non-point
Expand All @@ -715,12 +722,13 @@ def map(self, layers=None, interactive=True,
FROM ({query}) AS _wrap
LIMIT 0
'''.format(cols=','.join(layer.style_cols),
comma=',' if layer.style_cols else '',
query=layer.query))
self._debug_print(layer_fields=resp)
for k, v in dict_items(resp['fields']):
layer.style_cols[k] = v['type']
layer.geom_type = self._geom_type(layer)
if not base_layers:
layer.geom_type = self._geom_type(layer)
geoms.add(layer.geom_type)
# update local style schema to help build proper defaults
layer._setup(layers, idx)
Expand All @@ -738,6 +746,10 @@ def map(self, layers=None, interactive=True,
only_labels=True))

nb_layers = non_basemap_layers(layers)
if time_layer and len(nb_layers) > 1:
raise ValueError('Maps with a time element can only consist of a '
'time layer and a basemap. This constraint will '
'be removed in the future.')
options = {'basemap_url': basemap.url}

for idx, layer in enumerate(nb_layers):
Expand Down Expand Up @@ -807,12 +819,24 @@ def safe_quotes(text, escape_single_quotes=False):
}

if time_layer:
# get turbo-carto processed cartocss
params.update(dict(callback='cartoframes'))
resp = requests.get(
os.path.join(self.creds.base_url(),
'api/v1/map/named', map_name, 'jsonp'),
params=params,
headers={'Content-Type': 'application/json'})

# replace previous cartocss with turbo-carto processed version
layer.cartocss = json.loads(
resp.text.split('&& cartoframes(')[1]
.strip(');'))['metadata']['layers'][1]['meta']['cartocss']
config.update({
'order': 1,
'options': {
'query': time_layer.query,
'user_name': self.creds.username(),
'tile_style': time_layer.torque_cartocss,
'tile_style': layer.cartocss
}
})
config['named_map'].update({
Expand Down Expand Up @@ -1022,9 +1046,17 @@ def _check_query(self, query, style_cols=None):
def _send_map_template(self, layers, has_zoom):
map_name = get_map_name(layers, has_zoom=has_zoom)
if map_name not in self._map_templates:
self._auth_send('api/v1/map/named', 'POST',
headers={'Content-Type': 'application/json'},
data=get_map_template(layers, has_zoom=has_zoom))
resp = self._auth_send(
'api/v1/map/named', 'POST',
headers={'Content-Type': 'application/json'},
data=get_map_template(layers, has_zoom=has_zoom))
# TODO: remove this after testing
if 'errors' in resp:
resp = self._auth_send(
'api/v1/map/named/{}'.format(map_name),
'PUT',
headers={'Content-Type': 'application/json'},
data=get_map_template(layers, has_zoom=has_zoom))

self._map_templates[map_name] = True
return map_name
Expand Down

0 comments on commit 5866f56

Please sign in to comment.