Go to file
2014-04-10 21:07:47 -04:00
bin checks for millstone.drainpool only if -l flag is passed 2013-09-24 15:16:17 -05:00
build remove unused sys require 2012-08-02 14:48:04 -07:00
lib/carto add support for image-filters-inflate - refs mapnik/mapnik#2165 2014-02-27 10:41:43 -08:00
man Add a man page for carto 2013-03-03 19:05:47 +00:00
test add support for image-filters-inflate - refs mapnik/mapnik#2165 2014-02-27 10:41:43 -08:00
.gitignore Add test/rendering-mss/npm-debug.log to .gitignore 2013-08-09 15:37:06 +02:00
.npmignore Getting ignores right, tagging 0.4.4 2011-11-29 09:50:05 -05:00
.travis.yml travis: stop testing on node v0.6.x as per isaacs/npm#4379 2014-01-07 16:04:10 -08:00
CHANGELOG.md Bump to v0.9.5 with many fixes and optimizations 2013-10-25 13:23:25 -07:00
DEVELOPING.md revert rename 2013-12-09 20:39:54 -05:00
LICENSE moved to apache license 2010-03-04 14:46:41 -05:00
Makefile Simplify filter system, support numeric variables in filters 2012-12-19 17:01:35 -05:00
package.json use mapnik-reference@5.0.8 2014-04-10 21:07:47 -04:00
README.md more readme modernization tweaks 2014-04-07 16:56:25 -04:00

CartoCSS

Build Status

Is the language for map design used by TileMill. It is similiar in syntax to CSS, but builds upon it with specific abilities to filter map data and by providing things like variables.

Carto, aka CartoCSS, targets the Mapnik renderer and is able to general Mapnik XML>

Carto is an evolution of the Cascadenik idea and language, with an emphasis on speed and flexibility. If you are a previous user of Cascadenik, see the key differences wiki.

Documentation

For users looking to learn how to use TileMill the best places to start are to 1) Download TileMill and review the Carto reference documentation.

Tutorials like the TileMill Crashcourse are a great place to start to learn the basics of CartoCSS.

For more advanced topics see:

Developers

For details about how to install Carto from source and use on the command line see the Installation section.

Styling Concepts

Attachments and Instances

In CSS, a certain object can only have one instance of a property. A <div> has a specific border width and color, rules that match better than others (#id instead of .class) override previous definitions. CartoCSS acts the same way normally for the sake of familiarity and organization, but Mapnik itself is more powerful.

Layers in Mapnik can have multiple borders and multiple copies of other attributes. This ability is useful in drawing line outlines, like in the case of road borders or 'glow' effects around coasts. CartoCSS makes this accessible by allowing attachments to styles:

#world {
  line-color: #fff;
  line-width: 3;
}

#world::outline {
  line-color: #000;
  line-width: 6;
}

Attachments are optional.

While attachments allow creating implicit "layers" with the same data, using instances allows you to create multiple symbolizers in the same style/layer:

#roads {
  casing/line-width: 6;
  casing/line-color: #333;
  line-width: 4;
  line-color: #666;
}

This makes Mapnik first draw the line of color #333 with a width of 6, and then immediately afterwards, it draws the same line again with width 4 and color #666. Contrast that to attachments: Mapnik would first draw all casings before proceeding to the actual lines.

Variables & Expressions

CartoCSS inherits from its basis in less.js some new features in CSS. One can define variables in stylesheets, and use expressions to modify them.

@mybackground: #2B4D2D;

Map {
  background-color: @mybackground
}

#world {
  polygon-fill: @mybackground + #222;
  line-color: darken(@mybackground, 10%);
}

Nested Styles

CartoCSS also inherits nesting of rules from less.js.

/* Applies to all layers with .land class */
.land {
  line-color: #ccc;
  line-width: 0.5;
  polygon-fill: #eee;
  /* Applies to #lakes.land */
  #lakes {
    polygon-fill: #000;
  }
}

This can be a convenient way to group style changes by zoom level:

[zoom > 1] {
  /* Applies to all layers at zoom > 1 */
  polygon-gamma: 0.3;
  #world {
    polygon-fill: #323;
  }
  #lakes {
    polygon-fill: #144;
  }
}

FontSets

By defining multiple fonts in a text-face-name definition, you create FontSets in CartoCSS. These are useful for supporting multiple character sets and fallback fonts for distributed styles.

cartoXML
<pre>#world {

text-name: "[NAME]"; text-size: 11; text-face-name: "Georgia Regular", "Arial Italic"; }

<FontSet name="fontset-0">
  <Font face-name="Georgia Regular"/>
  <Font face-name="Arial Italic"/>
</FontSet>
<Style name="world-text">
  <Rule>
    <TextSymbolizer fontset-name="fontset-0"
      size="11"
      name="[NAME]"/>
  </Rule>
</Style>

Filters

CartoCSS supports a variety of filter styles:

Numeric comparisons:

#world[population > 100]
#world[population < 100]
#world[population >= 100]
#world[population <= 100]

General comparisons:

#world[population = 100]
#world[population != 100]

String comparisons:

/* a regular expression over name */
#world[name =~ "A.*"]

Installation

If you're using TileMill, you're already using CartoCSS and don't need to do a thing.

If you're a developer-type and want to use the carto binary with node.js (and you have npm installed),

npm install -g carto

Optionally you may also want to install millstone which is required for resolving data in the same way as TileMill does:

npm install -g millstone

Having millstone installed specifically enable support for localizing external resources (URLs and local files) referenced in your mml file, and detecting projections (using node-srs)

Now that Carto is installed you should have a carto command line tool available that can be run on a TileMill project:

carto project.mml > mapnik.xml

From code

Currently CartoCSS is designed to be invoked from node.js. The Renderer interface is the main API for developers, and it takes an MML file as a string as input.

// defined variables:
// - input (the name or identifier of the file being parsed)
// - data (a string containing the MML or an object of MML)
var carto = require('carto');

new carto.Renderer({
        filename: input,
        local_data_dir: path.dirname(input),
    }).render(data, function(err, output) {
        if (err) {
            if (Array.isArray(err)) {
                err.forEach(function(e) {
                    carto.writeError(e, options);
                });
            } else { throw err; }
        } else {
            sys.puts(output);
        }
    });

Vim

To install, download or clone this repository, then add the vim-carto directory located at build/vim-carto to your ~/.vim file.

Credits

CartoCSS is based on less.js, a CSS compiler written by Alexis Sellier.

It depends on:

Only for running tests:

Authors

  • Tom MacWright (tmcw)
  • Konstantin Käfer (kkaefer)
  • AJ Ashton (ajashton)
  • Dane Springmeyer (springmeyer)