2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-01-10 00:43:23 +08:00
|
|
|
require('../../helper');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var server = require('../../../lib/server')();
|
2015-05-13 17:53:14 +08:00
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
var _ = require('underscore');
|
2013-01-10 00:43:23 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('export.topojson', function () {
|
2013-01-10 00:43:23 +08:00
|
|
|
// TOPOJSON tests
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function getRequest (query, extraParams) {
|
2014-11-12 18:36:59 +08:00
|
|
|
var params = {
|
|
|
|
q: query,
|
|
|
|
format: 'topojson'
|
|
|
|
};
|
|
|
|
|
|
|
|
params = _.extend(params, extraParams || {});
|
|
|
|
|
|
|
|
return {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify(params),
|
2019-12-24 01:19:08 +08:00
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
2014-11-12 18:36:59 +08:00
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('GET two polygons sharing an edge as topojson', function (done) {
|
|
|
|
assert.response(server,
|
|
|
|
getRequest(
|
|
|
|
"SELECT 1 as gid, 'U' as name, 'POLYGON((-5 0,5 0,0 5,-5 0))'::geometry as the_geom " +
|
|
|
|
' UNION ALL ' +
|
2014-11-12 18:36:59 +08:00
|
|
|
"SELECT 2, 'D', 'POLYGON((0 -5,0 5,-5 0,0 -5))'::geometry as the_geom "
|
2019-12-24 01:19:08 +08:00
|
|
|
),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'TOPOJSON is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.topojson/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var topojson = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(topojson.type, 'Topology');
|
2013-01-11 18:10:02 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// Check transform
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(topojson, 'transform'));
|
2019-12-24 01:19:08 +08:00
|
|
|
var trans = topojson.transform;
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(trans).length, 2); // only scale and translate
|
|
|
|
assert.strictEqual(trans.scale.length, 2); // scalex, scaley
|
|
|
|
assert.strictEqual(Math.round(trans.scale[0] * 1e6), 1000);
|
|
|
|
assert.strictEqual(Math.round(trans.scale[1] * 1e6), 1000);
|
|
|
|
assert.strictEqual(trans.translate.length, 2); // translatex, translatey
|
|
|
|
assert.strictEqual(trans.translate[0], -5);
|
|
|
|
assert.strictEqual(trans.translate[1], -5);
|
2013-01-10 00:43:23 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// Check objects
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(topojson, 'objects'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(topojson.objects).length, 2);
|
2013-01-11 18:10:02 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
var obj = topojson.objects[0];
|
|
|
|
// console.dir(obj);
|
|
|
|
// Expected:
|
|
|
|
// { type: 'Polygon',
|
|
|
|
// arcs: [ [ 0, 1 ] ],
|
|
|
|
// properties: { gid: 1, nam: 'U' } }
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(obj).length, 3); // type, arcs, properties
|
|
|
|
assert.strictEqual(obj.type, 'Polygon');
|
|
|
|
assert.strictEqual(obj.arcs.length, 1); /* only shell, no holes */
|
2019-12-24 01:19:08 +08:00
|
|
|
var shell = obj.arcs[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(shell.length, 2); /* one shared arc, one non-shared */
|
|
|
|
assert.strictEqual(shell[0], 0); /* shared arc */
|
|
|
|
assert.strictEqual(shell[1], 1); /* non-shared arc */
|
2019-12-24 01:19:08 +08:00
|
|
|
var props = obj.properties;
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(props).length, 2); // gid, name
|
|
|
|
assert.strictEqual(props.gid, 1);
|
|
|
|
assert.strictEqual(props.name, 'U');
|
2013-01-11 18:10:02 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
obj = topojson.objects[1];
|
|
|
|
// console.dir(obj);
|
|
|
|
// Expected:
|
|
|
|
// { type: 'Polygon',
|
|
|
|
// arcs: [ [ 0, 2 ] ],
|
|
|
|
// properties: { gid: 2, nam: 'D' } }
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(obj).length, 3); // type, arcs, properties
|
|
|
|
assert.strictEqual(obj.type, 'Polygon');
|
|
|
|
assert.strictEqual(obj.arcs.length, 1); /* only shell, no holes */
|
2019-12-24 01:19:08 +08:00
|
|
|
shell = obj.arcs[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(shell.length, 2); /* one shared arc, one non-shared */
|
|
|
|
assert.strictEqual(shell[0], 0); /* shared arc */
|
|
|
|
assert.strictEqual(shell[1], 2); /* non-shared arc */
|
2019-12-24 01:19:08 +08:00
|
|
|
props = obj.properties;
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(props).length, 2); // gid, name
|
|
|
|
assert.strictEqual(props.gid, 2);
|
|
|
|
assert.strictEqual(props.name, 'D');
|
2013-01-10 00:43:23 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// Check arcs
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(topojson, 'arcs'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(topojson.arcs.length, 3); // one shared, two non-shared
|
2019-12-24 01:19:08 +08:00
|
|
|
var arc = topojson.arcs[0]; // shared arc
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(arc.length, 2); // shared arc has two vertices
|
2019-12-24 01:19:08 +08:00
|
|
|
var p = arc[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), 0);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), 5);
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[1];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), 5);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), 5);
|
2019-12-24 01:19:08 +08:00
|
|
|
arc = topojson.arcs[1]; // non shared arc
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(arc.length, 3); // non shared arcs have three vertices
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), 5);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), 10);
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[1];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), 5);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), -5);
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[2];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), -10);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), 0);
|
2019-12-24 01:19:08 +08:00
|
|
|
arc = topojson.arcs[2]; // non shared arc
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(arc.length, 3); // non shared arcs have three vertices
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), 5);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), 10);
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[1];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), 0);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), -10);
|
2019-12-24 01:19:08 +08:00
|
|
|
p = arc[2];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(Math.round(p[0] * trans.scale[0]), -5);
|
|
|
|
assert.strictEqual(Math.round(p[1] * trans.scale[1]), 5);
|
2013-01-10 00:43:23 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-01-10 00:43:23 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('null geometries', function (done) {
|
|
|
|
assert.response(server, getRequest(
|
2014-11-12 18:36:59 +08:00
|
|
|
"SELECT 1 as gid, 'U' as name, 'POLYGON((-5 0,5 0,0 5,-5 0))'::geometry as the_geom " +
|
2019-12-24 01:19:08 +08:00
|
|
|
' UNION ALL ' +
|
2014-11-12 18:36:59 +08:00
|
|
|
"SELECT 2, 'D', null::geometry as the_geom "
|
|
|
|
),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-24 01:19:08 +08:00
|
|
|
var cd = res.headers['content-disposition'];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(true, /^attachment/.test(cd), 'TOPOJSON is not disposed as attachment: ' + cd);
|
|
|
|
assert.strictEqual(true, /filename=cartodb-query.topojson/gi.test(cd));
|
2019-12-24 01:19:08 +08:00
|
|
|
var topojson = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(topojson.type, 'Topology');
|
2013-01-12 01:20:25 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// Check transform
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(topojson, 'transform'));
|
2019-12-24 01:19:08 +08:00
|
|
|
var trans = topojson.transform;
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(trans).length, 2); // only scale and translate
|
|
|
|
assert.strictEqual(trans.scale.length, 2); // scalex, scaley
|
|
|
|
assert.strictEqual(Math.round(trans.scale[0] * 1e6), 1000);
|
|
|
|
assert.strictEqual(Math.round(trans.scale[1] * 1e6), 500);
|
|
|
|
assert.strictEqual(trans.translate.length, 2); // translatex, translatey
|
|
|
|
assert.strictEqual(trans.translate[0], -5);
|
|
|
|
assert.strictEqual(trans.translate[1], 0);
|
2013-01-12 01:20:25 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// Check objects
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(topojson, 'objects'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(topojson.objects).length, 1);
|
2013-01-12 01:20:25 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
var obj = topojson.objects[0];
|
|
|
|
// console.dir(obj);
|
|
|
|
// Expected:
|
|
|
|
// { type: 'Polygon',
|
|
|
|
// arcs: [ [ 0, 1 ] ],
|
|
|
|
// properties: { gid: 1, nam: 'U' } }
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(obj).length, 3); // type, arcs, properties
|
|
|
|
assert.strictEqual(obj.type, 'Polygon');
|
|
|
|
assert.strictEqual(obj.arcs.length, 1); /* only shell, no holes */
|
2019-12-24 01:19:08 +08:00
|
|
|
var shell = obj.arcs[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(shell.length, 1); /* one non shared arc */
|
|
|
|
assert.strictEqual(shell[0], 0); /* non-shared arc */
|
2019-12-24 01:19:08 +08:00
|
|
|
var props = obj.properties;
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(_.keys(props).length, 2); // gid, name
|
|
|
|
assert.strictEqual(props.gid, 1);
|
|
|
|
assert.strictEqual(props.name, 'U');
|
2013-01-12 01:20:25 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
// Check arcs
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ok(Object.prototype.hasOwnProperty.call(topojson, 'arcs'));
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(topojson.arcs.length, 1);
|
2019-12-24 01:19:08 +08:00
|
|
|
var arc = topojson.arcs[0];
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(arc, [[0, 0], [4999, 9999], [5000, -9999], [-9999, 0]]);
|
2013-01-12 01:20:25 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-01-12 01:20:25 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('skipped fields are not returned', function (done) {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server,
|
2014-11-12 18:36:59 +08:00
|
|
|
getRequest(
|
|
|
|
"SELECT 1 as gid, 'U' as name, 'POLYGON((-5 0,5 0,0 5,-5 0))'::geometry as the_geom",
|
|
|
|
{
|
|
|
|
skipfields: 'name'
|
|
|
|
}
|
|
|
|
),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2014-11-12 18:36:59 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(parsedBody.objects[0].properties.gid, 1, 'gid was expected property');
|
2014-11-12 18:36:59 +08:00
|
|
|
assert.ok(!parsedBody.objects[0].properties.name);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('jsonp callback is invoked', function (done) {
|
2014-11-12 18:36:59 +08:00
|
|
|
assert.response(
|
2016-09-15 02:54:24 +08:00
|
|
|
server,
|
2014-11-12 18:36:59 +08:00
|
|
|
getRequest(
|
|
|
|
"SELECT 1 as gid, 'U' as name, 'POLYGON((-5 0,5 0,0 5,-5 0))'::geometry as the_geom",
|
|
|
|
{
|
2019-12-26 23:10:41 +08:00
|
|
|
callback: 'fooJsonp'
|
2014-11-12 18:36:59 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200, res.statusCode + ': ' + res.body);
|
2014-11-12 18:36:59 +08:00
|
|
|
var didRunJsonCallback = false;
|
2019-12-26 23:10:41 +08:00
|
|
|
/* eslint-disable */
|
|
|
|
function fooJsonp (body) {
|
2014-11-12 18:36:59 +08:00
|
|
|
didRunJsonCallback = true;
|
|
|
|
}
|
|
|
|
eval(res.body);
|
2019-12-26 23:10:41 +08:00
|
|
|
/* eslint-enable */
|
2014-11-12 18:36:59 +08:00
|
|
|
assert.ok(didRunJsonCallback);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should close on error and error must be the only key in the body', function (done) {
|
2015-05-14 00:15:38 +08:00
|
|
|
assert.response(
|
2016-09-15 02:54:24 +08:00
|
|
|
server,
|
2015-05-14 00:15:38 +08:00
|
|
|
{
|
2019-12-24 01:19:08 +08:00
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT the_geom, 100/(cartodb_id - 3) cdb_ratio FROM untitle_table_4',
|
2015-05-14 00:15:38 +08:00
|
|
|
format: 'topojson'
|
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400
|
|
|
|
},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-26 23:10:41 +08:00
|
|
|
assert.ifError(err);
|
2015-05-14 00:15:38 +08:00
|
|
|
var parsedBody = JSON.parse(res.body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(Object.keys(parsedBody), ['error']);
|
|
|
|
assert.deepStrictEqual(parsedBody.error, ['division by zero']);
|
2015-05-14 00:15:38 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2013-01-10 00:43:23 +08:00
|
|
|
});
|