Simplify filter system, support numeric variables in filters

This commit is contained in:
Tom MacWright 2012-12-19 17:01:35 -05:00
parent 8603799fa7
commit 9eee907467
8 changed files with 59 additions and 33 deletions

View File

@ -3,7 +3,6 @@
#
expresso = ./node_modules/.bin/mocha
docco = ./node_modules/.bin/docco
lint:
./node_modules/.bin/jshint lib/carto/*.js lib/carto/tree/*.js
@ -18,7 +17,4 @@ endif
check: test
doc:
$(docco) lib/carto/*.js lib/carto/tree/*.js
.PHONY: test

View File

@ -662,10 +662,11 @@ carto.Parser = function Parser(env) {
save();
var key, op, val;
if (! $('[')) return;
if (key = $(/^[a-zA-Z0-9\-_]+/) || $(this.entities.quoted) || $(this.entities.variable)) {
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) || $(/^[\w\-\.]+/))) {
(val = $(this.entities.quoted) || $(this.entities.variable) || $(this.entities.dimension))) {
if (! $(']')) return;
if (!key.is) key = new tree.Field(key);
return new tree.Filter(key, op, val, memo, env.filename);
}
}

View File

@ -1,28 +1,12 @@
(function(tree) {
tree.Filter = function Filter(key, op, val, index, filename) {
if (key.is) {
this.key = key.value;
this._key = key;
} else {
this.key = key;
}
this.key = key;
this.op = op;
this.val = val;
this.index = index;
this.filename = filename;
if (val.is) {
this.val = val.value;
this._val = val;
} else {
this.val = val;
}
if (ops[this.op][1] == 'numeric') {
this.val = 1 * this.val;
}
this.id = this.key + this.op + this.val;
};
@ -39,15 +23,14 @@ var ops = {
};
tree.Filter.prototype.toXML = function(env) {
var val, key;
if (this.val['eval']) this._val = this.val['eval'](env);
if (this.key['eval']) this._key = this.key['eval'](env);
if (this._key) key = this._key.toString(false);
if (this._val) val = this._val.toString(this._val.is == 'string');
var key = this.key['eval'](env);
var val = this.val['eval'](env);
key = key.toString(false);
val = val.toString(val.is == 'string');
if (
(ops[this.op][1] == 'numeric' && isNaN(this.val)) ||
(ops[this.op][1] == 'string' && (val || this.val)[0] != "'")
(ops[this.op][1] == 'numeric' && isNaN(val)) ||
(ops[this.op][1] == 'string' && (val)[0] != "'")
) {
env.error({
message: 'Cannot use operator "' + this.op + '" with value ' + this.val,
@ -56,7 +39,7 @@ tree.Filter.prototype.toXML = function(env) {
});
}
return '[' + (key || this.key) + ']' + ops[this.op][0] + '' + (val || this.val) + (ops[this.op][2] || '');
return key + ops[this.op][0] + '' + val + (ops[this.op][2] || '');
};
tree.Filter.prototype.toString = function() {

View File

@ -7,6 +7,10 @@ tree.Variable = function Variable(name, index, filename) {
};
tree.Variable.prototype = {
is: 'variable',
toString: function() {
return this.name;
},
eval: function(env) {
var variable,
v,

View File

@ -42,7 +42,6 @@
},
"devDependencies": {
"mocha": "1.3.x",
"docco": "0.3.x",
"jshint": "0.2.x",
"sax": "0.1.x"
},

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": [
"support4504.mss"
],
"Layer": [{
"class": "new",
"name": "countries",
"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,7 @@
@z13: 19;
@num_pixels: 50;
@z13area: @z13 * @z13 * @num_pixels;
#countries[way_area > @z13area] {
polygon-opacity:0.2;
}

View File

@ -0,0 +1,21 @@
<?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="countries" filter-mode="first" >
<Rule>
<Filter>([way_area] &gt; 18050)</Filter>
<PolygonSymbolizer fill-opacity="0.2" />
</Rule>
</Style>
<Layer name="countries"
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>countries</StyleName>
<Datasource>
<Parameter name="file"><![CDATA[[absolute path]]]></Parameter>
<Parameter name="type"><![CDATA[shape]]></Parameter>
</Datasource>
</Layer>
</Map>