Skip to content

Commit

Permalink
Fixes + more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed May 13, 2015
1 parent 3a638a5 commit 736de96
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.11"
- "0.10"
script:
- npm install tilestrata
- make test && (make test-ci-coverage || true)
2 changes: 1 addition & 1 deletion LICENSE
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright 2015 Natural Atlas, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
41 changes: 41 additions & 0 deletions Makefile
@@ -0,0 +1,41 @@
.PHONY: test test-ci-coverage release

_MOCHA=node_modules/.bin/_mocha
ISTANBUL=node_modules/.bin/istanbul
COVERALLS=node_modules/.bin/coveralls

test:
npm test

test-ci-coverage:
npm install coveralls
npm install istanbul
@rm -rf coverage
npm run clean
$(ISTANBUL) cover $(_MOCHA) --report lcovonly -- -R tap

@echo
@echo Sending report to coveralls.io...
@cat ./coverage/lcov.info | $(COVERALLS)
@rm -rf ./coverage
@echo Done

release:
ifeq ($(strip $(version)),)
@echo "\033[31mERROR:\033[0;39m No version provided."
@echo "\033[1;30mmake release version=1.0.0\033[0;39m"
else
rm -rf node_modules
npm install
npm install tilestrata
make test
sed -i.bak 's/"version": "[^"]*"/"version": "$(version)"/' package.json
rm *.bak
git add .
git commit -a -m "Released $(version)."
git tag v$(version)
git push origin master
git push origin --tags
npm publish
@echo "\033[32mv${version} released\033[0;39m"
endif
13 changes: 11 additions & 2 deletions README.md
@@ -1,5 +1,6 @@
# tilestrata-utfmerge
A TileStrata plugin for merging utfgrids

A TileStrata plugin for merging JSON [utfgrids](https://github.com/mapbox/utfgrid-spec), usually provided by the [tilestrata-mapnik](https://github.com/naturalatlas/tilestrata-mapnik) plugin.

### Sample Usage

Expand All @@ -11,4 +12,12 @@ server.layer('mylayer').route('combined.json')
['utfgrid-poi','t.json'],
['utfgrid-labels','t.json']
]));
```
```

## License

Copyright © 2015 [Natural Atlas, Inc.](https://github.com/naturalatlas) & [Contributors](https://github.com/naturalatlas/tilestrata-utfmerge/graphs/contributors)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
26 changes: 13 additions & 13 deletions blend.js
@@ -1,19 +1,19 @@
module.exports = function(grids, options){
module.exports = function(grids, options) {
var result = grids[0];
for(var i = 1; i<grids.length; i++){
for (var i = 1; i < grids.length; i++) {
result = merge(result, grids[i]);
}
return result;
}
};

/**
* Merges two utfgrid objects
*
*
* @param {object} a - original utfgrid
* @param {object} b - utfgrid to put on top
* @return {object}
*/
function merge(a, b){
function merge(a, b) {
var grid_c = [];
var data_c = {};
var keys_c = [""];
Expand All @@ -22,20 +22,20 @@ function merge(a, b){
var key_mapping_b = {};

var dim = a.grid.length;
for(var y = 0; y < dim; y++){
for (var y = 0; y < dim; y++) {
var row_a = a.grid[y];
var row_b = b.grid[y];
var row_c = [];
for(var x = 0; x < dim; x++){
for (var x = 0; x < dim; x++) {
var id_a = decode(row_a.charCodeAt(x));
var id_b = decode(row_b.charCodeAt(x));
var id_c = 0;
var key_a = a.keys[id_a];
var key_b = b.keys[id_b];
var key_c = 0;
if(b.data[key_b]){
if (b.data[key_b]) {
key_c = key_mapping_b[key_b];
if(!key_c) {
if (!key_c) {
key_c = (key++).toString();
key_mapping_b[key_b] = key_c;
keys_c.push(key_c);
Expand All @@ -44,7 +44,7 @@ function merge(a, b){
id_c = keys_c.indexOf(key_c);
} else if (a.data[key_a]) {
key_c = key_mapping_a[key_a];
if(!key_c) {
if (!key_c) {
key_c = (key++).toString();
key_mapping_a[key_a] = key_c;
keys_c.push(key_c);
Expand All @@ -64,14 +64,14 @@ function merge(a, b){
};
}

function decode(x){
function decode(x) {
if(x >= 93) x--;
if(x >= 35) x--;
return x - 32;
}
function encode(x){
function encode(x) {
x += 32;
if(x >= 34) x++;
if(x >= 92) x++;
return String.fromCharCode(x);
}
}
9 changes: 6 additions & 3 deletions index.js
@@ -1,3 +1,4 @@
var async = require('async');
var dependency = require('tilestrata-dependency');
var blend = require('./blend.js');

Expand All @@ -18,7 +19,8 @@ module.exports = function(layers, options) {
function loadTiles(callback) {
async.map(layers, function(layer, callback) {
layer.serve(server, req, function(err, buffer, headers) {
callback(err, buffer._utfgrid);
var data = buffer._utfgrid || JSON.parse(buffer.toString('utf8'));
callback(err, data);
});
}, function(err, result) {
grids = result;
Expand All @@ -30,11 +32,12 @@ module.exports = function(layers, options) {
result = new Buffer(JSON.stringify(merged), 'utf8');
result._utfgrid = merged;
grids = null;
callback();
}
], function(err) {
if (err) return callback(err);
callback(null, result, {'Content-Type': 'application/json')});
callback(null, result, {'Content-Type': 'application/json'});
});
}
};
};
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -24,8 +24,8 @@
},
"homepage": "https://github.com/naturalatlas/tilestrata-utfmerge",
"dependencies": {
"tilestrata": "^0.7.1",
"tilestrata-dependency": "^0.2.1"
"async": "^0.9.0",
"tilestrata-dependency": "^0.2.2"
},
"devDependencies": {
"chai": "^1.10.0",
Expand Down
13 changes: 3 additions & 10 deletions test/blend.test.js
@@ -1,18 +1,11 @@
var blend = require('../blend.js');
var assert = require('chai').assert;

function assertGridEqual(result, expected){
assert.deepEqual(result.keys, expected.keys, 'keys should match');
for(var i = 0; i < expected.grid.length; i++){
assert.equal(result.grid[i], expected.grid[i], 'row '+i+' should match');
};
assert.deepEqual(result.data, expected.data, 'data property should match');
}
var utils = require('./utils.js');

describe('blend()', function() {
it('should merge utfgrids', function() {
var fixture = require('./fixtures/a.js');
var output = blend(fixture.inputs);
assertGridEqual(output, fixture.result);
utils.assertGridEqual(output, fixture.result);
});
});
});
36 changes: 36 additions & 0 deletions test/index.js
@@ -0,0 +1,36 @@
var tilestrata = require('tilestrata');
var TileRequest = tilestrata.TileRequest;
var utfmerge = require('../index.js');
var assert = require('chai').assert;
var utils = require('./utils.js');
var a = require('./fixtures/a.js');

describe('Plugin', function() {
it('should operate normally', function(done) {
var server = tilestrata.createServer();
server.layer('mylayer').route('a.json').use({
serve: function(server, tile, callback) {
var data = a.inputs[0];
var buffer = new Buffer(JSON.stringify(data), 'utf8');
buffer._utfgrid = data;
return callback(null, buffer, {'Content-Type':'application/json'});
}
});
server.layer('mylayer').route('b.json').use({
serve: function(server, tile, callback) {
var data = a.inputs[1];
var buffer = new Buffer(JSON.stringify(data), 'utf8');
return callback(null, buffer, {'Content-Type':'application/json'});
}
});

var tile = TileRequest.parse('/mylayer/0/0/0/c.json');
var plugin = utfmerge([['mylayer','a.json'],['mylayer','b.json']]);
plugin.serve(server, tile, function(err, buffer, headers) {
assert.isFalse(!!err, err);
utils.assertGridEqual(JSON.parse(buffer.toString('utf8')), a.result);
utils.assertGridEqual(buffer._utfgrid, a.result);
done();
});
});
});
9 changes: 9 additions & 0 deletions test/utils.js
@@ -0,0 +1,9 @@
var assert = require('chai').assert;

module.exports.assertGridEqual = function(result, expected){
assert.deepEqual(result.keys, expected.keys, 'keys should match');
for(var i = 0; i < expected.grid.length; i++){
assert.equal(result.grid[i], expected.grid[i], 'row '+i+' should match');
};
assert.deepEqual(result.data, expected.data, 'data property should match');
};

0 comments on commit 736de96

Please sign in to comment.