Skip to content

How spatial aggregation works

javi santana edited this page Dec 2, 2013 · 9 revisions

When Torque library render points it does not render exactly the same points you have in the database, it aggregates the points in clusters in order to speedup rendering and data transfer.

So imagine you have this cartocss

Map {
-torque-aggregation-function:"count(cartodb_id)";
-torque-resolution: 2;
}

This means that for the current zoom torque will fetch points in clusters of 2x2 pixels. Every cluster has a value, that value is calculated using -torque-aggregation-function, so in this case the value will be the number of points inside that cluster. That value can be accessed from CartoCSS using the variable value.

Every cluster is renderer as a point.

Given that you can do:

#layer {
[value > 1] { marker-fill: #000; }
[value > 4] { marker-fill: #400; }
[value > 16] { marker-fill: #800; }
[value > 32] { marker-fill: #F00; }
}

and this would render the point with different color depending on the number of points inside it.

Can I use strings with torque?

In general you can not do:

[column = 'mytext'] { marker-fill: red; }

Because of two things:

  • cluster does not contain values for all the columns, you can only use value variable
  • you would need to use an aggregation function for strings

So, how could I use strings column with torque?

Imagine you have a string column (team) with two values, "team A" and "team B" and you want to render "team A" points with red and "team B" with blue, you could do:

torqueLayer.setSQL("select *, (CASE WHEN team='team A' THEN 1 ELSE 2 END) as team_n from table");

with this css

Map {
 ...
 '-torque-aggregation-function: "avg(team_n)";
 ...
}

#layer {
...
  marker-fill: #FF0000;
  // avg of 1 and 2
  [value > 1.5] { marker-fill: #0000FF; }
...
}

Clone this wiki locally