carto/README.md

289 lines
8.3 KiB
Markdown
Raw Normal View History

2011-02-08 00:22:14 +08:00
# carto
2011-02-01 06:34:10 +08:00
Is a stylesheet renderer for Mapnik. It's an evolution of the [Cascadenik](https://github.com/mapnik/Cascadenik) idea and language, with an emphasis on speed and flexibility.
2011-02-03 06:26:38 +08:00
## Installation
2011-02-08 00:22:14 +08:00
npm install carto
2011-02-03 06:26:38 +08:00
2011-02-01 06:34:10 +08:00
## MML
_incompatibility_
2011-02-01 23:04:41 +08:00
* 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.
2011-07-25 15:36:15 +08:00
* Carto will not embed files or download URLs for you. Stylesheets should be embedded directly into your MML JSON and any datasources should be paths (relative or absolute) that would be acceptable in Mapnik XML.
2011-02-01 06:34:10 +08:00
2011-02-08 00:22:14 +08:00
carto.js MML:
2011-02-03 05:58:33 +08:00
{
"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",
2011-02-03 05:58:33 +08:00
"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
2011-02-01 23:04:41 +08:00
<pre>&lt;Stylesheet&gt;&lt;![CDATA[
2011-02-03 05:58:33 +08:00
Map
{
map-bgcolor: #69f;
}
2011-02-01 23:04:41 +08:00
2011-02-03 05:58:33 +08:00
Layer
{
line-width: 1;
line-color: #696;
polygon-fill: #6f9;
}
]]&gt;&lt;/Stylesheet&gt;
&lt;Layer srs=&quot;+proj=latlong +ellps=WGS84 +datum=WGS84 +no_defs&quot;&gt;
&lt;Datasource&gt;
&lt;Parameter name=&quot;type&quot;&gt;shape&lt;/Parameter&gt;
&lt;Parameter name=&quot;file&quot;&gt;world_borders&lt;/Parameter&gt;
&lt;/Datasource&gt;
&lt;/Layer&gt;
2011-02-01 23:04:41 +08:00
&lt;/Map&gt;</pre>
2011-02-01 06:34:10 +08:00
## Attachments
_new_
2011-02-08 00:22:14 +08:00
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.
2011-02-01 06:34:10 +08:00
2011-02-08 00:22:14 +08:00
Layers in Mapnik can have multiple [borders](http://trac.mapnik.org/wiki/LineSymbolizer) 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:
2011-02-01 06:34:10 +08:00
#world {
line-color: #fff;
line-width: 3;
}
#world::outline {
line-color: #000;
line-width: 6;
}
2011-02-08 00:22:14 +08:00
Attachments are optional: if you don't define them, carto.js does overriding of styles just like Cascadenik.
2011-02-01 06:34:10 +08:00
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_
2011-02-01 23:04:41 +08:00
Instead of the name attribute of the [TextSymbolizer](http://trac.mapnik.org/wiki/TextSymbolizer) and [ShieldSymbolizer](http://trac.mapnik.org/wiki/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.
2011-02-01 06:34:10 +08:00
<table>
<tr>
<th>cascadenik</th>
2011-02-08 00:22:14 +08:00
<th>carto.js</th>
2011-02-01 06:34:10 +08:00
</tr>
<tr>
2011-02-03 05:58:33 +08:00
<td valign='top'>
2011-02-01 06:34:10 +08:00
<pre>
#world NAME {
text-face-name: "Arial";
}</pre>
</td>
2011-02-03 05:58:33 +08:00
<td valign='top'>
2011-02-01 06:34:10 +08:00
<pre>
#world {
text-name: "NAME";
text-face-name: "Arial";
}</pre>
</td>
</tr>
</table>
## Mapnik2
2011-02-01 06:41:48 +08:00
_new_
2011-02-01 06:34:10 +08:00
2011-02-08 00:22:14 +08:00
`carto.js` is only compatible with [Mapnik2](http://trac.mapnik.org/wiki/Mapnik2). Compatibility with Mapnik 0.7.x is not planned.
2011-02-01 06:34:10 +08:00
## Rasters and Buildings
2011-02-01 06:41:48 +08:00
_new_
2011-02-01 06:34:10 +08:00
2011-02-08 00:22:14 +08:00
Rasters are supported in carto.js - it knows how to download `.vrt`, `.tiff`, and soon other raster formats, and the properties of the [RasterSymbolizer](http://trac.mapnik.org/wiki/RasterSymbolizer) are exposed in the language.
2011-02-01 06:34:10 +08:00
2011-02-08 04:26:28 +08:00
The [BuildingSymbolizer](http://trac.mapnik.org/wiki/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.
2011-02-01 06:34:10 +08:00
## Variables & Expressions
2011-02-01 06:41:48 +08:00
_new_
2011-02-01 06:34:10 +08:00
2011-02-08 00:22:14 +08:00
`carto.js` inherits from its basis in [less.js](http://lesscss.org/) some new features in CSS. One can define variables in stylesheets, and use expressions to modify them.
2011-02-01 06:34:10 +08:00
@mybackground: #2B4D2D;
Map {
background-color: @mybackground
}
#world {
polygon-fill: @mybackground + #222;
line-color: darken(@mybackground, 10%);
}
2011-02-01 23:04:41 +08:00
## Nested Styles
_new_
2011-02-08 00:22:14 +08:00
`carto.js` also inherits nesting of rules from less.js.
2011-02-01 23:04:41 +08:00
/* 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;
}
}
2011-02-01 06:34:10 +08:00
## FontSets
2011-02-01 06:41:48 +08:00
_new_
2011-02-01 06:34:10 +08:00
2011-02-08 00:22:14 +08:00
By defining multiple fonts in a `text-face-name` definition, you create [FontSets](http://trac.mapnik.org/wiki/FontSet) in `carto.js`. These are useful for supporting multiple character sets and fallback fonts for distributed styles.
2011-02-01 06:34:10 +08:00
2011-02-01 06:41:48 +08:00
<table>
<tr>
2011-02-08 00:22:14 +08:00
<th>carto</th><th>XML</th>
2011-02-01 06:41:48 +08:00
</tr>
<tr>
2011-02-03 05:58:33 +08:00
<td valign='top'>
2011-02-01 06:41:48 +08:00
<pre>#world {
text-name: "[NAME]";
text-size: 11;
text-face-name: "Georgia Regular", "Arial Italic";
}</pre>
2011-02-03 05:58:33 +08:00
</td>
<td valign='top'>
2011-02-01 06:41:48 +08:00
<pre>&lt;FontSet name=&quot;fontset-0&quot;&gt;
2011-02-08 01:50:41 +08:00
&lt;Font face-name=&quot;Georgia Regular&quot;/&gt;
&lt;Font face-name=&quot;Arial Italic&quot;/&gt;
2011-02-01 06:41:48 +08:00
&lt;/FontSet&gt;
&lt;Style name=&quot;world-text&quot;&gt;
&lt;Rule&gt;
2011-02-08 01:50:41 +08:00
&lt;TextSymbolizer fontset-name=&quot;fontset-0&quot;
2011-02-01 06:41:48 +08:00
size=&quot;11&quot;
name=&quot;[NAME]&quot;/&gt;
&lt;/Rule&gt;
&lt;/Style&gt;</pre>
</td>
<tr>
</table>
2011-02-01 06:34:10 +08:00
## Usage
2011-02-03 06:08:05 +08:00
#### Using the binary
2011-02-01 06:34:10 +08:00
2011-02-08 23:09:47 +08:00
carto map_file.json
2011-02-01 06:34:10 +08:00
2011-02-03 06:08:05 +08:00
#### Using the code
2011-02-08 00:22:14 +08:00
Currently `carto.js` is designed to be invoked from [node.js](http://nodejs.org/).
2011-02-03 06:08:05 +08:00
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)
2011-02-08 00:22:14 +08:00
var carto = require('carto');
2011-02-03 06:08:05 +08:00
2011-02-08 00:22:14 +08:00
new carto.Renderer({
2011-02-03 06:08:05 +08:00
filename: input,
local_data_dir: path.dirname(input),
}).render(data, function(err, output) {
if (err) {
if (Array.isArray(err)) {
err.forEach(function(e) {
2011-02-08 00:22:14 +08:00
carto.writeError(e, options);
2011-02-03 06:08:05 +08:00
});
} else { throw err; }
} else {
sys.puts(output);
}
});
2011-02-08 22:51:43 +08:00
## 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](https://github.com/mapnik/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
2011-02-03 06:29:04 +08:00
## Credits
2011-02-08 00:22:14 +08:00
`carto.js` is based on [less.js](https://github.com/cloudhead/less.js), a CSS compiler written by Alexis Sellier.
2011-02-03 06:29:04 +08:00
2011-02-07 23:07:40 +08:00
It depends on:
2011-02-03 06:29:04 +08:00
* [underscore.js](https://github.com/documentcloud/underscore/)
2011-02-07 23:07:40 +08:00
Only for running tests:
* [expresso](https://github.com/visionmedia/expresso)
* [sax-js](https://github.com/isaacs/sax-js/)
2011-02-03 06:29:04 +08:00
2011-04-30 04:09:24 +08:00
## Changelog
2011-07-29 02:43:35 +08:00
### 0.2.1
* Accept valid Map properties directly from input mml object.
2011-07-26 03:40:34 +08:00
### 0.2.0
2011-07-25 15:36:15 +08:00
2011-08-03 23:33:30 +08:00
* Removed all external handling - see http://github.com/mapbox/millstone for localizing/caching MML objects with external references.
2011-07-26 03:40:34 +08:00
* All errors are now handled as Error objects.
2011-07-25 15:36:15 +08:00
2011-06-01 22:19:42 +08:00
### 0.1.14
2011-06-01 22:17:30 +08:00
* Optional-file datasources - allows string argument to OGR
2011-05-18 23:33:17 +08:00
### 0.1.9
* Variables in filters.
### 0.1.6 & 0.1.8
2011-05-13 02:45:36 +08:00
* Fixed bug caused by jshint testing
2011-05-03 23:12:36 +08:00
### 0.1.5
* Using npm devDependencies in place of ndistro
* Updated package.json format
* Fixes tests
2011-04-30 04:09:24 +08:00
### 0.1.4
* Fix bug in which SRS autodetection broke error handling
* Update carto
2011-02-03 06:29:04 +08:00
2011-02-01 06:34:10 +08:00
## Authors
* Tom MacWright (tmcw)
* Konstantin Käfer (kkaefer)
2011-02-01 23:04:41 +08:00
* AJ Ashton (ajashton)
2011-02-07 07:08:55 +08:00
* Dane Springmeyer (springmeyer)