Skip to content

Commit

Permalink
Allow specifying a column type for each layer
Browse files Browse the repository at this point in the history
Closes #93
Includes testcases
Produces Mapnik XML using the "pgraster" input driver:
https://github.com/strk/mapnik.plugin.input.pgraster
  • Loading branch information
Sandro Santilli committed Jun 24, 2014
1 parent 888080f commit 4d560ab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Allow specifying a column type for each layer (#93)

Version 0.19.0
2014-MM-DD

Expand Down
12 changes: 12 additions & 0 deletions lib/grainstore/mml_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ var MMLBuilder = function(redis_pool, opts, optional_args, init_callback){
var tostore = {};
tostore.sql = opts.sql;
if ( opts.gcols ) tostore.gcols = opts.gcols;
if ( opts.gcoltypes ) tostore.gcoltypes = opts.gcoltypes;
tostore.style = style;
tostore.version = style_version;
tostore.interactivity = interactivity;
Expand Down Expand Up @@ -603,6 +604,17 @@ var MMLBuilder = function(redis_pool, opts, optional_args, init_callback){
if ( opts.gcols && opts.gcols[i] ) {
datasource.geometry_field = opts.gcols[i];
}
if ( opts.gcoltypes && opts.gcoltypes[i] ) {
if ( opts.gcoltypes[i] == 'raster' ) {
datasource.type = 'pgraster';
datasource.raster_field = datasource.geometry_field;
delete datasource.geometry_field;
}
else if ( opts.gcoltypes[i] != 'geometry' ) {
throw new Error("Unsupported geometry column type for layer "
+ i + ": " + opts.gcoltypes[i]);
}
}

var layer = {};
if ( tables.length == 1 && opts.table ) {
Expand Down
46 changes: 46 additions & 0 deletions test/mml_builder_multilayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ suite('mml_builder multilayer', function() {
);
});

// See http://github.com/CartoDB/grainstore/issues/93
test('accept gcoltypes array', function(done) {
var mml_store = new grainstore.MMLStore(redis_opts, {mapnik_version: '2.1.0'});
var mml_builder;

Step(
function initBuilder() {
mml_builder = mml_store.mml_builder({
dbname: 'my_database',
sql:['SELECT ST_MakePoint(0,0) g',
'SELECT ST_AsRaster(ST_MakePoint(0,0),1.0,1.0) r'],
gcols: ['g','r'],
gcoltypes: [,'raster'], // first intentionally blank
style_version:'2.1.0',
}, this);
},
function getXML0(err) {
if ( err ) { done(err); return; }
mml_builder.toXML(this);
},
function checkXML0(err, xml) {
if ( err ) { done(err); return; }
var xmlDoc = libxmljs.parseXmlString(xml);

var layer0 = xmlDoc.get("Layer[@name='layer0']");
assert.ok(layer0, "Layer0 not found in XML");
var ds0 = layer0.get("Datasource");
assert.ok(ds0, "Datasource for layer0 not found in XML");
var gf0 = ds0.get("Parameter[@name='geometry_field']");
assert.equal(gf0.text(), "g", xmlDoc);

var layer1 = xmlDoc.get("Layer[@name='layer1']");
assert.ok(layer1, "Layer1 not found in XML");
var ds1 = layer1.get("Datasource");
assert.ok(ds1, "Datasource for layer1 not found in XML: " + xmlDoc);
var gf1 = ds1.get("Parameter[@name='raster_field']");
assert.ok(gf1, "raster_field for layer1 not found in: " + ds1);
assert.equal(gf1.text(), "r");
var typ1 = ds1.get("Parameter[@name='type']");
assert.equal(typ1.text(), "pgraster");

mml_builder.delStyle(done);
}
);
});

test('error out on blank CartoCSS in a style array', function(done) {
var style0 = "#layer0 { marker-width:3; }";
var style1 = "";
Expand Down

0 comments on commit 4d560ab

Please sign in to comment.