Go to file
2011-05-17 13:54:44 -04:00
bin Pushing cartox towards osm.xml compatibility. 2011-02-25 15:48:21 -05:00
build Finding system node and mapnik-render.js, too 2011-05-12 15:05:56 -04:00
lib/carto Merging package. Supporting variables in filters 2011-05-17 13:54:44 -04:00
test Merging package. Supporting variables in filters 2011-05-17 13:54:44 -04:00
.gitignore adding test data directory to gitignore 2011-02-08 10:11:53 -05:00
LICENSE moved to apache license 2010-03-04 14:46:41 -05:00
Makefile Using jshint for more up-tightness 2011-05-10 17:20:41 -04:00
package.json Tag 0.1.8 2011-05-14 15:13:19 -04:00
README.md Never call console.log in carto 2011-05-17 13:52:59 -04:00

carto

Is a stylesheet renderer for Mapnik. It's an evolution of the Cascadenik idea and language, with an emphasis on speed and flexibility.

Installation

Follow the directions to install node-zipfile and then:

npm install carto

note: possibly broken on ubuntu

MML

incompatibility

  • MML files are assumed to be JSON, not XML. The files are near-identical to the XML files accepted by Cascadenik, just translated into JSON.
  • Like Cascadenik, you can also include remote stylesheets, by including their URLs as simple strings in the Stylesheet array.

carto.js MML:

{
    "srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
    "Stylesheet": [{"id":"style.mss","data":"Map {\n  background-color: #fff;\n}\n\n#world {\n  line-color: #ccc;\n  line-width: 0.5;\n  polygon-fill: #eee;\n}"}],
    "Layer": [{
        "id": "world",
        "name": "world",
        "srs": "+proj=latlong +ellps=WGS84 +datum=WGS84 +no_defs",
        "Datasource": {
            "file": "world_borders",
            "type": "shape"
        }
    }]
}

Cascadenik MML

<Stylesheet><![CDATA[
    Map
    {
        map-bgcolor: #69f;
    }

    Layer
    {
        line-width: 1;
        line-color: #696;
        polygon-fill: #6f9;
    }
]]></Stylesheet>
<Layer srs="+proj=latlong +ellps=WGS84 +datum=WGS84 +no_defs">
    <Datasource>
        <Parameter name="type">shape</Parameter>
        <Parameter name="file">world_borders</Parameter>
    </Datasource>
</Layer>
</Map>

Attachments

new

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. carto.js 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. carto.js 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: if you don't define them, carto.js does overriding of styles just like Cascadenik.

This brings us to another incompatibility: line-inline and line-outline have been removed from the language, because attachments are capable of the same trick.

text-name

incompatibility

Instead of the name attribute of the TextSymbolizer and ShieldSymbolizer being a part of the selector, it is a property of a rule. Thus the evaluation is less complex and one can use expressions in names.

cascadenik carto.js
#world NAME {
  text-face-name: "Arial";
}
#world {
  text-name: "NAME";
  text-face-name: "Arial";
}

Mapnik2

new

carto.js is only compatible with Mapnik2. Compatibility with Mapnik 0.7.x is not planned.

Rasters and Buildings

new

Rasters are supported in carto.js - it knows how to download .vrt, .tiff, and soon other raster formats, and the properties of the RasterSymbolizer are exposed in the language.

The BuildingSymbolizer is also supported in carto.js. The code stores symbolizer types and properties in a JSON file (in tree/reference.json), so new Mapnik features can be quickly implemented here.

Variables & Expressions

new

carto.js 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

new

carto.js 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

new

By defining multiple fonts in a text-face-name definition, you create FontSets in carto.js. 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>

Usage

Using the binary

carto map_file.json

Using the code

Currently carto.js 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);
        }
    });

TextMate bundle

There's a TextMate bundle that offers syntax highlighting for .mss and .mml files in the build directory. To install, download or clone this repository, then double-click on the carto.tmbundle icon in that folder.

The TextMate bundle requires node-mapnik and Carto installed globally - the versions that are installed locally in TileMill or other tools can't be automatically discovered.

npm install mapnik -g
npm install carto -g

Credits

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

It depends on:

Only for running tests:

Changelog

0.1.6 & 0.1.7

  • Fixed bug caused by jshint testing

0.1.5

  • Using npm devDependencies in place of ndistro
  • Updated package.json format
  • Fixes tests

0.1.4

  • Fix bug in which SRS autodetection broke error handling
  • Update carto

Authors

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