I'm working with some small area parcel data. When zoomed in it is important to see the actual polygon shape. But when zoomed out, something like a simple point marker that scales would be better, otherwise the small polygon isn't really visible.

Is there a way to dictate this with CartoCSS?


Use the magic of PostGIS and the elegance of CartoCSS!

First, style your polygon layer to include a scale threshold - I'll choose scales greater than 12 for the polygons (ie. zoomed in beyond 12):


  [zoom > 12]{

  polygon-fill: #FF6600;
  polygon-opacity: 0.7;
  line-color: #FFF;
  line-width: 0.5;
  line-opacity: 1;


Now click the + Icon in the CartoDB panel to add another layer

Choose the same polygon layer you styled above, but go into the SQL panel and use the ST_Centroid function to change the geometry of the polygons to points:

ST_Centroid(points.the_geom_webmercator) as the_geom_webmercator
 , cartodb_ID
FROM census_neighborhood_demographics_2010 as points

Note that I've only grabbed the_geom_webmercator from the attribute columns of the polygons, and the cartodb_id column as well.

Now the CartoCSS for the new 'point' layer should have a similar scale definition, but I'll choose scales LESS THAN OR EQUAL to 12 (ie. zoomed out to 12 and beyond):

   [zoom <= 12]{

  marker-fill-opacity: 0.9;
  marker-line-color: #FFF;
  marker-line-width: 1;
  marker-line-opacity: 1;
  marker-placement: point;
  marker-type: ellipse;
  marker-width: 10;
  marker-fill: #FF6600;
  marker-allow-overlap: true;

So now at zoom levels above 12, I see polygons: enter image description here And zoom levels less than or equal to 12, I see points!

enter image description here

One dataset - multiple visualizations and geometry representations!

| improve this answer | |
  • Great! I couldn't see past the single layer ... Now, does this mean that info windows would need to be styled the same way to get consistent interaction? – Adam C Oct 23 '15 at 17:42
  • Most likely... but you can make some choices about what scales to show those infowindows and what the content needs to be... but you can always copy and paste the HTML from one layer to the other (as long as the point layer was selecting the same fields as the polygon layer, so that the HTML tags would have the columns available...) does that make sense? – DPSSpatial Oct 23 '15 at 17:44
  • Yes, once I actually, went through your workflow I saw that of course, one can now decide what info-window is most appropriate for the scale. And yes, the HTML tags / CSS needs to be able to reference the appropriate columns ... so the SQL you provided can be amended to make available the desired columns. – Adam C Oct 23 '15 at 18:17
  • @AdamC right on! Have fun! – DPSSpatial Oct 23 '15 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.