Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
amend cdf3215 - allow rendering without stylesheet - refs #440, #367, #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Apr 10, 2016
1 parent af92b76 commit 050e11f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
5 changes: 1 addition & 4 deletions bin/carto
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,8 @@ if (ext == '.mml') {
}
return { id: x, data: fs.readFileSync(path.join(path.dirname(input), x), 'utf8') }
});
compileMML(null,data);
}
else {
console.error("Expecting a Stylesheet property containing an (array of) stylesheet object(s) of the form { id: 'x', 'data': 'y' }.");
}
compileMML(null,data);
}
} else if (ext == '.mss') {
compileMSS(null,data);
Expand Down
47 changes: 23 additions & 24 deletions lib/carto/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ carto.Renderer.prototype.render = function render(m) {
carto.tree.Reference.setVersion(this.options.mapnik_version);

var output = [];
var definitions = [];

// Transform stylesheets into definitions.
if (_.has(m, 'Stylesheet') && !_.isNil(m.Stylesheet)) {
m.Stylesheet = _.castArray(m.Stylesheet);
var definitions = _(m.Stylesheet).chain()
definitions = _(m.Stylesheet).chain()
.map(function(s) {
if (_.isString(s) || !_.has(s, 'id') || !_.has(s, 'data') || _.isNil(s.id) || _.isNil(s.data)) {
var e = new Error("Expecting a stylesheet object of the form { id: 'x', 'data': 'y' } for the Stylesheet property.\n");
Expand All @@ -102,11 +103,6 @@ carto.Renderer.prototype.render = function render(m) {
.flatten()
.value();
}
else {
var e = new Error("Expecting a Stylesheet property containing an (array of) stylesheet object(s) of the form { id: 'x', 'data': 'y' }.\n");
e.stack = null; // do not show stack trace
throw e;
}

function appliesTo(name, classIndex) {
return function(definition) {
Expand All @@ -120,28 +116,31 @@ carto.Renderer.prototype.render = function render(m) {
for (var i = 0; i < m.Layer.length; i++) {
l = m.Layer[i];
styles = [];
classIndex = {};

if (env.benchmark) console.warn('processing layer: ' + l.id);
// Classes are given as space-separated alphanumeric strings.
var classes = (l['class'] || '').split(/\s+/g);
for (var j = 0; j < classes.length; j++) {
classIndex[classes[j]] = true;
}
matching = definitions.filter(appliesTo(l.name, classIndex));
rules = inheritDefinitions(matching, env);
sorted = sortStyles(rules, env);
if (definitions.length > 0) {
classIndex = {};

for (var k = 0, rule, style_name; k < sorted.length; k++) {
rule = foldStyle(sorted[k]);
style_name = l.name + (rule.attachment !== '__default__' ? '-' + rule.attachment : '');
if (env.benchmark) console.warn('processing layer: ' + l.id);
// Classes are given as space-separated alphanumeric strings.
var classes = (l['class'] || '').split(/\s+/g);
for (var j = 0; j < classes.length; j++) {
classIndex[classes[j]] = true;
}
matching = definitions.filter(appliesTo(l.name, classIndex));
rules = inheritDefinitions(matching, env);
sorted = sortStyles(rules, env);

// env.effects can be modified by this call
var styleXML = carto.tree.StyleXML(style_name, rule.attachment, rule, env);
for (var k = 0, rule, style_name; k < sorted.length; k++) {
rule = foldStyle(sorted[k]);
style_name = l.name + (rule.attachment !== '__default__' ? '-' + rule.attachment : '');

if (styleXML) {
output.push(styleXML);
styles.push(style_name);
// env.effects can be modified by this call
var styleXML = carto.tree.StyleXML(style_name, rule.attachment, rule, env);

if (styleXML) {
output.push(styleXML);
styles.push(style_name);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion test/errorhandling/no_stylesheet.result

This file was deleted.

21 changes: 21 additions & 0 deletions test/rendering.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
var path = require('path'),
fs = require('fs'),
assert = require('assert'),
semver = require('semver');

var carto = require('../lib/carto');
var helper = require('./support/helper');

describe('Rendering', function() {

it('should support rendering without Stylesheet (for non-styling/vector tile usage)', function(done) {

// note: this intentionally does not have a `"Stylesheet":[]` property
// so we should skip trying to validate it
var opts = {"name":"","description":"",
"attribution":"",
"center":[0,0,3],
"format":"pbf",
"minzoom":0,
"maxzoom":6,
"srs":"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
"Layer":[],
"json":"{\"vector_layers\":[]}"};
var renderer = new carto.Renderer(null).render(opts);
assert.ok(renderer);
done();
});


helper.files('rendering', 'mml', function(file) {
var api = null,
filename = path.basename(file);
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions test/rendering/no_stylesheet.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map[]>
<Map>


<Layer name="a"
>
</Layer>

</Map>

0 comments on commit 050e11f

Please sign in to comment.