Skip to content

Commit

Permalink
solve #101
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Aug 9, 2013
1 parent b6eb41f commit 489b651
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/millstone.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function unzip(file, callback) {
}

var remain = zf.names.length;
var ds_file = _(zf.names).chain()
var ds_files = _(zf.names).chain()
.reject(function(name) {
return (name && (name[0] === '.' || path.basename(name)[0] === '.'));
})
Expand All @@ -364,10 +364,27 @@ function unzip(file, callback) {
path.extname(name).toLowerCase()
);
})
.uniq()
.compact()
.first()
.value();

var ds_file = ds_files[0];

if (ds_files.length > 1) {
// prefer first .shp
for (var i=0;i<ds_files.length;++i) {
var fname = ds_files[i];
if (path.extname(fname) == '.shp') {
ds_file = fname;
break;
}
}
if (env == 'development') {
console.error('[millstone] warning: detected more than one file type in zip (by ext) that may be valid: ' + ds_files);
console.error('[millstone] warning: chose: ' + ds_file);
}
}

if (!ds_file) return callback(new Error("Valid datasource not detected (by extension) in zip: '" + file + "'"));

zf.names.forEach(function(name) {
Expand Down
Binary file not shown.
38 changes: 38 additions & 0 deletions test/zip-with-shapefile-and-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var fs = require('fs');
var path = require('path');
var assert = require('assert');

// switch to 'development' for more verbose logging
process.env.NODE_ENV = 'production'
var utils = require('../lib/util.js');
var millstone = require('../lib/millstone');
var tests = module.exports = {};
var rm = require('./support.js').rm;

var existsSync = require('fs').existsSync || require('path').existsSync;

beforeEach(function(){
rm(path.join(__dirname, '/tmp/millstone-test'));
})

// https://github.com/mapbox/millstone/issues/101
it('correctly prefers (for back compatibility) shapefiles over .txt files in zip archive', function(done) {
var mml = JSON.parse(fs.readFileSync(path.join(__dirname, 'zip-with-shapefile-and-txt/project.mml')));

var cache = '/tmp/millstone-test';
var options = {
mml: mml,
base: path.join(__dirname, 'zip-with-shapefile-and-txt'),
cache: cache
};

try {
fs.mkdirSync(options.cache, 0777);
} catch (e) {}

millstone.resolve(options, function(err, resolved) {
assert.equal(err,undefined,err);
assert.deepEqual(resolved.Layer[0].Datasource.type, 'shape');
done();
});
});
11 changes: 11 additions & 0 deletions test/zip-with-shapefile-and-txt/project.mml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Stylesheet": [ ],
"Layer": [
{
"name": "zip-with-shapefile-and-txt",
"Datasource": {
"file": "../data/ne_10m_admin_0_boundary_lines_disputed_areas.zip"
}
}
]
}

0 comments on commit 489b651

Please sign in to comment.