question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Develop a CartoJSS serializer

See original GitHub issue

Writing CartoCSS in ES5 is a bit difficult since it has no backtick strings. So we want to allow CARTO.js users to write JSON CartoCSS or “CartoJSS”:

For example, this CartoJSS:

{
  '#continent_points': {
    'marker-fill-opacity': 0.9,
    'marker-line-color': '#FFF',
    'marker-placement': 'point',
    'marker-allow-overlap': true,
    '[continent="Africa"]': {
      'marker-fill': '#A6CEE3'
    },
    '[continent="Antarctica"]': {
      'marker-fill': '#1F78B4'
    }
  }
}

will be converted to this CartoCSS:

#continent_points {
  marker-fill-opacity: 0.9;
  marker-line-color: #FFF;
  marker-placement: point;
  marker-allow-overlap: true;
  [continent="Africa"] {
    marker-fill: #A6CEE3;
  }
  [continent="Antarctica"] {
    marker-fill: #1F78B4;
  }
}

Sources:

cc @makella

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
makellacommented, Oct 19, 2017

@Jesus89

So I’ve gone through some CartoCSS documentation and pulled out some relevant pieces that I think will be helpful to you and that aren’t covered in the best practices doc.

Since the doc has pretty basic examples, I tried to provide some that are more complex for you to see. If you have any questions please let me know.

Also, this doesn’t necessarily cover everything, but should be a good start. Also, if this is not what you are looking for, let me know that too!

Instances

https://cartocss.readthedocs.io/en/latest/styling_concepts.html#attachments-and-instances

Example:

The one in the doc is good and yes, it can get more complex, but don’t see people doing this too often.

Selectors

By class:

you have two layers that you want to symbolize the same like poi and airports classes: poi.pois and airports.pois with the syntax:

.pois {
 /*style applied to everything with class 'pois'*/
}

Filter Selectors

By Zoom and/or Numeric Value

In addition to the docs, there are some alternatives you might see

#layer {
 [zoom=4][attribute > 1],
 [zoom=5][attribute > 3],
 [zoom=6][attribute > 4][attribute < 7]
 [zoom=6][attribute > 4][attribute < 7],
 [zoom>=7][zoom<=10][attribute > 8]  //combines numeric and zoom level filters
 {
  /*style*/
 }
}

AND/OR

#layer[zoom=4][attribute > 1],
#layer[zoom=5][attribute > 3],
#layer[zoom=6][attribute > 4][attribute < 7],
#layer[zoom>=7][zoom<=10][attribute > 8] {
  /*style*/
}

AND/OR

#layer[zoom>=4][zoom<=10] {
  line-color: red;
  line-width: 2;
  [zoom=8] { line-width: 3; }
  [zoom=9] { line-width: 4; }
  [zoom=10] { line-width: 5; }
}

By Regular Expression

#roads[Type=~".* Highway"] { 
 /* style */ 
}

Color Definitions and Functions

https://cartocss.readthedocs.io/en/latest/language_elements.html#color

Example

//multiple color functions

@seed: #ffec8c;

#layer {
 polygon-fill: mix(spin(@seed,30),#000,80);
}

//color function with alternate color definition

#layer {
 polygon-fill: fadeout(rgba(255,255,255,0.5);
}

Float

https://cartocss.readthedocs.io/en/latest/language_elements.html#float

Example

@z5: 6;

#line {
  line-width: @z5+2; 
}

#line {
 line-width: [attribute]/@z5;
}

String

https://cartocss.readthedocs.io/en/latest/language_elements.html#float

Example

//attributes with a character
#layer {
 text-name: [attribute] + 'million';
 text-face-name: 'Open Sans Regular';
}

//stringing attributes with separating character
#layer {
 text-name: [attribute] + ', ' + [attribute_2];
 text-face-name: 'Open Sans Regular';
}

Numbers

https://cartocss.readthedocs.io/en/latest/language_elements.html#numbers

Functions

https://cartocss.readthedocs.io/en/latest/language_elements.html#numbers

Example

#layer {
 marker-width: 10;
 marker-transform: rotate[degrees], transform(-5,5);
}

#layer {
 marker-width: 10;
 marker-transform: translate([degrees],[symbol_size]/2*-1);
}

Marker, polygon, line image URL

https://cartocss.readthedocs.io/en/latest/language_elements.html#uri

0reactions
Jesus89commented, Oct 25, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

CartoJSS - Write CartoCSS using a Javascript object - GitHub
Write CartoCSS using a Javascript object. Contribute to CartoDB/CartoJSS development by creating an account on GitHub.
Read more >
Tutorial 1: Serialization - Django REST framework
Creating a Serializer class. The first thing we need to get started on our Web API is to provide a way of serializing...
Read more >
Creating and Using Serializers - Django REST Framework
To create a basic serializer one needs to import serializers class from rest_framework and define fields for a serializer just like creating ......
Read more >
How to use Serializers in the Django Python web framework
Serialization is the process of transforming data into a format that can be stored or transmitted and then reconstructing it.
Read more >
DRF Serializer How do I serialize my data and display
You could create a seperate serializer to use with the nested serializer. class SubjectNestedSerializer(serializers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found