npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

alacarto

v0.18.0

Published

Mapnik Stylesheet Compiler

Downloads

2

Readme

Alacarto - CartoCSS implementation

Build Status Build status codecov Package Version Dependencies

CartoCSS (short: Carto) is a language for map design. It is similar in syntax to CSS, but builds upon it with specific abilities to filter map data and by providing things like variables. It targets the Mapnik renderer and is able to generate Mapnik XML. It can run from the command line or in the browser.

Carto is an evolution of the Cascadenik idea and language, with an emphasis on speed and flexibility.

This project is a fork of Mapbox' original CartoCSS implementation.

Documentation

The best place to start is to review the Carto reference documentation.

Tutorials like the Mapbox Getting started with CartoCSS guide are a great place to start to learn the basics of CartoCSS.

For more advanced topics see the Studio style quickstart guide and Studio style manual. The links below reference the Tilemill application, which preceded Mapbox Studio Classic, but still contain useful and relevant information.

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.

<pre>#world {

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

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 Mapbox Studio Classic, 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 @mapbox/carto

Optionally you may also want to install millstone which is required for resolving data in the same way as Mapbox Studio Classic 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 Mapbox Studio Classic project:

carto project.mml > mapnik.xml

Available parameters:

  • -h / --help - Display help message
  • -v / --version - Display version information
  • -b / --benchmark - Outputs total compile time
  • -l / --localize - Use millstone to localize resources when loading an MML (default: off)
  • -n / --nosymlink - Use absolute paths instead of symlinking files
  • -a / --api VERSION - Specify Mapnik API version (e.g. --api 3.0.10) (default: 2.3.0)
  • -ppi RESOLUTION - Pixels per inch used to convert m, mm, cm, in, pt, pc to pixels (default: 90.714)

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)
var carto = require('@mapbox/carto');

try {
    var data = fs.readFileSync(input, 'utf-8');
    var mml = new carto.MML();
    mml.load(path.dirname(input), data, function (err, data) {
        if (err) throw err;
        var output = new carto.Renderer({
            filename: input,
            local_data_dir: path.dirname(input),
        }).render(data);
        console.log(output);
    });
} catch(err) {
    if (Array.isArray(err)) {
        err.forEach(function(e) {
            carto.writeError(e, options);
        });
    } else { throw err; }
}

If you want to use CartoCSS within the browser you should not use MML loading via carto.MML.load. Instead you should supply the JSON of the MML including the Stylesheet strings directly to carto.Renderer.render.

Credits

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

See also a list of dependencies.

Similar projects

  • Mapbox Carto (the original, where this project was forked from)
  • Magnacarto (Go implementation of CartoCSS for Mapnik and Mapserver)

Authors

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