Layers vs. Sublayers

903 views
Skip to first unread message

Brandon Christensen

unread,
Aug 7, 2013, 5:50:57 PM8/7/13
to car...@googlegroups.com
I'm a bit confused about layers versus sublayers in CartoDB 2.1.  I've looked around the website and I don't see a conceptual explanation of the two, just the API definitions.  

Here's a little information about how I've been using CartoDB:  I have my own installation running on an Amazon server and I'm pointing CartoDB at existing tables in my database.  I use a hand-customized viz.json file to initialize my map and then use the cartodb.js API to dynamically change the map as users interact with it.  Of my three mapping tables, one is very large (~4 million rows) and the other two have only a few hundred rows each.

Prior to 2.1 my custom viz.json had a base map and three "carto" layers.  Each carto layer represented a particular table in my database.  I used the cartodb.js API to modify the query/tile_style combo for each layer to allow the user to change how the data was shown.  With this setup each layer was loaded as an individual tile image so modifying the query or tile_style resulted in just the one layer reloading.  Or similarly, showing/hiding a layer only modified that one layer.

With 2.1 I've modified my viz.json to use a base map and one "layergroup" layer that contains three "CartoDB" layers (First complaint - the API calls these "sublayers" but that terminology is not used in the viz.json format).  I based my new viz.json on a visualization I created through the CartoDB web interface - adding multiple tables to a viz there results in the layer organization I've described.   I've noticed that the three sublayers are composited on the server into one tile image.  This means that when I change the "sql", "cartocss" or visibility options on any of my sublayers the whole tile image for all three layers has to be redrawn.  This is very inefficient when I hide one of my smaller layers and the tiler has to redraw the large layer as well.  Of course as the tiles are cached the performance gets better, but I have a number of styles for each layer so there are a lot of potential combinations.

So I'm unclear on the recommended usage of the new sublayers.  It seems like sublayers lend themselves more to displaying related information in a single tile (like if I wanted to display a set of points and a circular radius around each point I could do that with two sublayers in a layer group).  But if the data is less related then it seems like the old layers are more appropriate.  Can a viz contain a top level "CartoDB" layer anymore?  Is it possible mix "carto" layers with "layergroup" layers?  The API reference makes it seem like sublayers are where the actual query and cartoCSS is expected to be defined and doesn't mention those at all for CartoDBLayers.

I guess part of my problem is that there's not a clear definition of the options in the viz.json format, I'm not even sure what the valid layer types are.  I've found the 5 month old documentation on the format (https://github.com/CartoDB/cartodb.js/blob/develop/doc/vizjson_format.md) but it doesn't seem complete or up to date.  That file also references a layergroup definition in the Windshaft repo (https://github.com/CartoDB/Windshaft/wiki/Multilayer-API) but I'm not sure how to map that documentation to the viz.json format.  It would be really helpful if this documentation was cleaned up.

Thanks for your help!

- Brandon

Javier de la Torre

unread,
Aug 8, 2013, 4:42:57 AM8/8/13
to car...@googlegroups.com
Hi Brandon,

Thanks for your message. This is really valuable feedback. First of all we are on the process of creating a dedicated mailing list for CartoDB developers where we will discuss most technical things. But in this case your message is really in between. Also some of the core developers are right now on holidays so expect some delay in some issues.

I'm a bit confused about layers versus sublayers in CartoDB 2.1.  I've looked around the website and I don't see a conceptual explanation of the two, just the API definitions.  

Clearly we need to have better documentation about it.

Let me explain broadly the different options you have between layers and sublayers and the motivations behind them. And then we can probably discuss changes on dicumentation and libraries to make it more clear or propose changes to it.


Before 2.1 in CartoDB when you wanted to overlay different layers on a map it was only possible to do it via adding different "tilesets" that loaded separately. This will work well in some scenarios, but it really works bad on others. For example if you had 8 different layers, the performance on the client will degrade very quickly and the number of HTTP requests made will be too many, so the user experience was not good enough. Additionally it was hard to work aroundOn mobile devices you could forget about it, it just did not work. Finally handling interactivity was challenging.

In CartoDB 2.1 we introduced a new Maps API based on the concept that you could have sublayers, which basically are layers that get "fusion" prior to sending them to the client. That way you could say here are my 8 layers, each one with its own SQL and CSS, and you will only get back one single tileset. This work much better for performance, cachability and many other things we have been building.

The reason we called them sublayers is to not confused them with not "fusioned" layers. And you can still keep adding layers separately like you were doing before. In some cases like yours it might make more sense to keep treating the layers separately, and you can keep doing that without problems if you think it works better for your case.


Hope this helps, and I will keep this email also within developers to discuss how to better document the API and the usage, indusing the vizjson documentation.

Thanks.






--
 
---
You received this message because you are subscribed to the Google Groups "cartodb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cartodb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mike Scopino

unread,
Aug 21, 2013, 11:57:14 AM8/21/13
to car...@googlegroups.com
Javier,

This is a great explanation. I also felt the same confusion that Brandon expressed. Can you please elaborate on one detail of layers vs. sublayers.

Is there any difference in ability to add hover/click events to polygons within layers vs. sublayers? For example if I wanted to create a map where one set of polygons linked to "foo" and another set to "bar", can I create them as sublayers of the same layer? Or do I need to break out events into entirely different layers? 

Mike

Javier Santana

unread,
Aug 21, 2013, 12:02:28 PM8/21/13
to car...@googlegroups.com
Hi Mike

you can listen for events per sublayer:

layer.getSubLayer(0).on('featureClick', function() { do stuff });

--
www.cartodb.com
Map, analyze and build applications with your data
Reply all
Reply to author
Forward
0 new messages