Allow for filters which compare fields.

This commit is contained in:
Tom MacWright 2012-12-19 18:59:16 -05:00
parent 89f8edbddc
commit f693f062ec
9 changed files with 79 additions and 11 deletions

View File

@ -4,7 +4,7 @@ var path = require('path'),
fs = require('fs'),
carto = require('carto'),
url = require('url'),
_ = require('underscore');
_ = require('lodash');
var existsSync = require('fs').existsSync || require('path').existsSync
@ -131,7 +131,7 @@ if (ext == '.mml') {
nosymlink: options.nosymlink
}, compile);
} else if (ext == '.mss') {
var env = _({}).defaults({
var env = _.defaults({}, {
benchmark: options.benchmark,
validation_data: false,
effects: []

View File

@ -640,7 +640,7 @@ carto.Parser = function Parser(env) {
if (! $('[')) return;
if (key = $(/^[a-zA-Z0-9\-_]+/) || $(this.entities.quoted) || $(this.entities.variable) || $(this.entities.field)) {
if ((op = $(this.entities.comparison)) &&
(val = $(this.entities.quoted) || $(this.entities.variable) || $(this.entities.dimension))) {
(val = $(this.entities.quoted) || $(this.entities.variable) || $(this.entities.dimension) || $(this.entities.field))) {
if (! $(']')) return;
if (!key.is) key = new tree.Field(key);
return new tree.Filter(key, op, val, memo, env.filename);

View File

@ -1,4 +1,4 @@
var _ = require('underscore');
var _ = require('lodash');
var carto = require('./index');
var tree = require('./tree');
@ -16,7 +16,7 @@ carto.Renderer = function Renderer(env, options) {
carto.Renderer.prototype.render = function render(m, callback) {
// effects is a container for side-effects, which currently
// are limited to FontSets.
var env = _(this.env).defaults({
var env = _.defaults(this.env, {
benchmark: false,
validation_data: false,
effects: []
@ -29,14 +29,14 @@ carto.Renderer.prototype.render = function render(m, callback) {
var output = [];
// Transform stylesheets into rulesets.
var rulesets = _(m.Stylesheet).chain()
var rulesets = _(m.Stylesheet)
.map(function(s) {
if (typeof s == 'string') {
throw new Error("Stylesheet object is expected not a string: '" + s + "'");
}
// Passing the environment from stylesheet to stylesheet,
// allows frames and effects to be maintained.
env = _(env).extend({filename:s.id});
env = _.extend(env, {filename:s.id});
// @TODO try/catch?
var time = +new Date(),

View File

@ -1,6 +1,4 @@
(function(tree) {
var _ = require('underscore');
tree.Call = function Call(name, args, index) {
this.name = name;
this.args = args;

View File

@ -33,7 +33,7 @@ tree.Filter.prototype.toXML = function(env) {
var val = this.val.toString(this.val.is == 'string');
if (
(ops[this.op][1] == 'numeric' && isNaN(val)) ||
(ops[this.op][1] == 'numeric' && isNaN(val) && this.val.is !== 'field') ||
(ops[this.op][1] == 'string' && (val)[0] != "'")
) {
env.error({

View File

@ -4,7 +4,7 @@
// combinations.
(function(tree) {
var _ = require('underscore');
var _ = require('lodash');
var mapnik_reference = require('mapnik-reference');
tree.Reference = {

View File

@ -0,0 +1,15 @@
{
"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",
"Stylesheet": [
"filter_comparing_fields.mss"
],
"Layer": [{
"name": "world",
"class": "world",
"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",
"Datasource": {
"file": "http://tilemill-data.s3.amazonaws.com/test_data/shape_demo.zip",
"type": "shape"
}
}]
}

View File

@ -0,0 +1,23 @@
#world[[FOO] = [BAR]] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 0.5;
}
#world[[BAR] = [HI]] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 0.5;
}
#world[[BAR] = [HI]] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 5;
}
#world[[BAR] > [HI]] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 5;
}

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map[]>
<Map 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" maximum-extent="-20037508.34,-20037508.34,20037508.34,20037508.34">
<Style name="world" filter-mode="first" >
<Rule>
<Filter>([BAR] &gt; [HI])</Filter>
<PolygonSymbolizer fill="#ffffff" />
<LineSymbolizer stroke="#ff0000" stroke-width="5" />
</Rule>
<Rule>
<Filter>([BAR] = [HI])</Filter>
<PolygonSymbolizer fill="#ffffff" />
<LineSymbolizer stroke="#ff0000" stroke-width="5" />
</Rule>
<Rule>
<Filter>([FOO] = [BAR])</Filter>
<PolygonSymbolizer fill="#ffffff" />
<LineSymbolizer stroke="#ff0000" stroke-width="0.5" />
</Rule>
</Style>
<Layer name="world"
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">
<StyleName>world</StyleName>
<Datasource>
<Parameter name="file"><![CDATA[[absolute path]]]></Parameter>
<Parameter name="type"><![CDATA[shape]]></Parameter>
</Datasource>
</Layer>
</Map>