From 736de96c0ea643d97d0fb45dc97e920b5fccdf20 Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Wed, 13 May 2015 15:29:12 -0600 Subject: [PATCH] Fixes + more tests. --- .travis.yml | 7 +++++++ LICENSE | 2 +- Makefile | 41 +++++++++++++++++++++++++++++++++++++++++ README.md | 13 +++++++++++-- blend.js | 26 +++++++++++++------------- index.js | 9 ++++++--- package.json | 4 ++-- test/blend.test.js | 13 +++---------- test/index.js | 36 ++++++++++++++++++++++++++++++++++++ test/utils.js | 9 +++++++++ 10 files changed, 129 insertions(+), 31 deletions(-) create mode 100644 .travis.yml create mode 100644 Makefile create mode 100644 test/index.js create mode 100644 test/utils.js diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..78782a0 --- /dev/null +++ b/.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) diff --git a/LICENSE b/LICENSE index 8f71f43..31fac8d 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c836ada --- /dev/null +++ b/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 diff --git a/README.md b/README.md index 6a1ea3c..c9da573 100644 --- a/README.md +++ b/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 @@ -11,4 +12,12 @@ server.layer('mylayer').route('combined.json') ['utfgrid-poi','t.json'], ['utfgrid-labels','t.json'] ])); -``` \ No newline at end of file +``` + +## 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. diff --git a/blend.js b/blend.js index 1eb365e..c84dc73 100644 --- a/blend.js +++ b/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= 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); -} \ No newline at end of file +} diff --git a/index.js b/index.js index 0459f23..7831e45 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +var async = require('async'); var dependency = require('tilestrata-dependency'); var blend = require('./blend.js'); @@ -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; @@ -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'}); }); } }; -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 13819a7..8f63354 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/blend.test.js b/test/blend.test.js index 47549e2..060ac73 100644 --- a/test/blend.test.js +++ b/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); }); -}); \ No newline at end of file +}); diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..fa80eca --- /dev/null +++ b/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(); + }); + }); +}); diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..ed4717d --- /dev/null +++ b/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'); +};