Skip to content

Commit

Permalink
allow for multiple filetypes to share the same module
Browse files Browse the repository at this point in the history
  • Loading branch information
gretacb committed Feb 4, 2015
1 parent a0a0c01 commit 5d9e3a4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 57 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ module.exports.digest = function(file, callback) {
*/
function getMetadata(file, filetype, callback) {
var type = modules.filter(function(module) {
return module.validFileType === filetype;
var done = module.validFileType.some(function(t) {
return t === filetype;
});
if (done) return module;
}),
metadata = {
filename: path.basename(file, path.extname(file))
Expand Down
2 changes: 1 addition & 1 deletion lib/geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function GeoJSON(filepath) {
this.layers = ['OGRGeoJSON'];
}

GeoJSON.validFileType = 'geojson';
GeoJSON.validFileType = ['geojson'];
GeoJSON.prototype.detailsName = 'json';
GeoJSON.prototype.dstype = 'geojson';

Expand Down
2 changes: 1 addition & 1 deletion lib/kml.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Kml(filepath) {
}
}

Kml.validFileType = 'kml';
Kml.validFileType = ['kml','gpx'];
Kml.prototype.detailsName = 'json';
Kml.prototype.dstype = 'ogr';

Expand Down
102 changes: 53 additions & 49 deletions lib/raster.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var gdal = require('gdal'),
invalid = require('./invalid'),
path = require('path');


mapnik.register_default_input_plugins();

module.exports = Raster;
Expand All @@ -23,6 +24,7 @@ function Raster(filepath) {
}
catch (err) {
throw invalid('Invalid raster: could not open the file');

}

try {
Expand All @@ -35,8 +37,8 @@ function Raster(filepath) {

try {
var geotransform = this.gdalDatasource.geoTransform;
this.details.pixelSize = [geotransform[1], geotransform[5]];
this.details.origin = [geotransform[0], geotransform[3]];
this.details.pixelSize = [ geotransform[1], geotransform[5] ];
this.details.origin = [ geotransform[0], geotransform[3] ];
}
catch (err) {
throw invalid('Invalid raster: could not read georeferencing information');
Expand All @@ -46,9 +48,11 @@ function Raster(filepath) {
catch (err) {
throw invalid('Invalid raster: could not read spatial reference information');
}


}

Raster.validFileType = 'tif';
Raster.validFileType = ['tif', 'vrt'];
Raster.prototype.detailsName = 'raster';
Raster.prototype.dstype = 'gdal';

Expand All @@ -68,32 +72,32 @@ Raster.prototype.getCenter = function(callback) {
};

Raster.prototype.getExtent = function(callback) {
var current = new mapnik.Projection(this.projection),
wgs84 = new mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'),
extent, transform;

var extent;
try {
extent = this.mapnikDatasource.extent();
}
catch (err) {
catch(err) {
return callback(
invalid('Invalid raster: could not read extent')
);
}

var current = new mapnik.Projection(this.projection);
var wgs84 = new mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs');

if (current !== wgs84) {
transform = new mapnik.ProjTransform(current, wgs84);
var transform = new mapnik.ProjTransform(current, wgs84);
extent = transform.forward(extent);
}

return callback(null, extent);
};

Raster.prototype.getBands = function() {
var numBands = this.gdalDatasource.bands.count(),
band, bandInfo, i, bands = [];
var numBands = this.gdalDatasource.bands.count();
var band, bandInfo, bands = [];

for (i = numBands; i <= numBands; i++) {
for (var i = numBands; i <= numBands; i++) {
band = this.gdalDatasource.bands.get(i);

try {
Expand Down Expand Up @@ -130,46 +134,47 @@ Raster.prototype.getLayers = function(callback) {
* @param srs
* @returns unit type (String)
*/
function getUnitType(srs) {
var possibleUnits = ['m', 'ft', 'mi', 'km', 'us-ft', 'us-mi'],
i;

for (i = 0; i < possibleUnits.length; i++) {
if (srs.indexOf('+units=' + possibleUnits[i]) !== -1) return possibleUnits[i];
}
if (srs.indexOf('+units=') === -1 && srs.indexOf('+proj=longlat') !== -1) return 'decimal degrees';
//Default to meters for now, if nothing matches
else return 'm';
function getUnitType(srs){
var possibleUnits = ['m','ft','mi','km','us-ft','us-mi'];
for(var i = 0; i < possibleUnits.length; i++){
if(srs.indexOf("+units=" + possibleUnits[i]) !== -1) return possibleUnits[i];
}
if(srs.indexOf("+units=") === -1 && srs.indexOf("+proj=longlat") !== -1) return 'decimal degrees';
//Default to meters for now, if nothing matches
else return 'm';
}

function convertToMeters(pixelSize, unit) {
var circumference = 40075000,
conversions = {
m: function(x) { return x; },
ft: function(x) { return x / 0.3048; },
mi: function(x) { return x / 1609.34; },
km: function(x) { return x / 1000; },
'us-ft': function(x) { return x / 0.3048; },
'us-mi': function(x) { return x / 1609.34; },
'decimal degrees': function(x) { return x / 360 * circumference; }
},

x = conversions[unit](pixelSize[0]),
y = conversions[unit](pixelSize[1]);
var circumference = 40075000;

var conversions = {
'm': function(x) { return x; },
'ft': function(x) { return x / 0.3048; },
'mi': function(x) { return x / 1609.34; },
'km': function(x) { return x / 1000; },
'us-ft': function(x) { return x / 0.3048; },
'us-mi': function(x) { return x / 1609.34; },
'decimal degrees': function(x) { return x / 360 * circumference; }
};

var x = conversions[unit](pixelSize[0]);
var y = conversions[unit](pixelSize[1]);

return [x, y];
}

Raster.prototype.getZooms = function(callback) {
var pixelSize = convertToMeters(this.details.pixelSize, getUnitType(this.projection)),
circumference = 40075000,
zoomLevels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
spatialResolutions = zoomLevels.map(function(z) {
return circumference * Math.cos(0) / Math.pow(2, (z + 8));
}),
validSpatialResolutions = spatialResolutions.filter(function(res) {
return res > pixelSize[0];
});
circumference = 40075000,
zoomLevels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
spatialResolutions = zoomLevels.map(function(z) {
return circumference * Math.cos(0) / Math.pow(2, (z + 8));
});

var _this = this;
var validSpatialResolutions = spatialResolutions.filter(function(res) {
return res > _this.details.pixelSize[0];
});

this.details.pixelSize = pixelSize;
return callback(
Expand All @@ -180,14 +185,13 @@ Raster.prototype.getZooms = function(callback) {
};

Raster.prototype.getDetails = function(callback) {
var details = this.details,
ref;
var details = this.details;

try {
details.width = this.gdalDatasource.rasterSize.x;
details.height = this.gdalDatasource.rasterSize.y;
}
catch (err) {
catch(err) {
return callback(
invalid('Invalid raster: could not read image dimensions', err)
);
Expand All @@ -198,20 +202,20 @@ Raster.prototype.getDetails = function(callback) {
details.bands = this.getBands();
details.nodata = this.details.bands[0].nodata;
}
catch (err) {
catch(err) {
return callback(
invalid('Invalid raster: could not get band information', err)
);
}

try {
ref = gdal.SpatialReference.fromProj4(this.projection);
var ref = gdal.SpatialReference.fromProj4(this.projection);
details.units = {
linear: ref.getLinearUnits(),
angular: ref.getAngularUnits()
};
}
catch (err) {
catch(err) {
return callback(
invalid('Invalid raster: could not read spatial reference information', err)
);
Expand Down
2 changes: 1 addition & 1 deletion lib/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Shape(filepath) {
});
}

Shape.validFileType = 'shp';
Shape.validFileType = ['shp'];
Shape.prototype.detailsName = 'json';
Shape.prototype.dstype = 'shape';

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"mapnik": "~3.1.2",
"sphericalmercator": "~1.0.2",
"gdal": "0.3.1",
"mapbox-file-sniff": "0.0.9",
"mapbox-file-sniff": "0.1.0",
"queue-async": "1.0.7"
},
"repository": {
Expand Down
15 changes: 12 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ var UPDATE = process.env.UPDATE;
tape('[SHAPE] Getting datasources: should return expected metadata', function(assert) {
var file = testData + '/data/shp/world_merc/world_merc.shp';
mapnik_omnivore.digest(file, function(err, metadata) {
if (err) return done(err);
if (err) {
assert.ifError(err, 'should not error');
return assert.end();
}
assert.ok(err === null);
try {
assert.deepEqual(metadata, expectedMetadata_world_merc);
Expand Down Expand Up @@ -66,7 +69,10 @@ var UPDATE = process.env.UPDATE;
if (UPDATE) expectedMetadata_DC_polygon = JSON.parse(fs.readFileSync(path.resolve('test/fixtures/metadata_DC_polygon.json')));
var file = testData + '/data/geojson/DC_polygon.geo.json';
mapnik_omnivore.digest(file, function(err, metadata) {
if (err) return done(err);
if (err) {
assert.ifError(err, 'should not error');
return assert.end();
}
assert.ok(err === null);
try {
assert.deepEqual(metadata, expectedMetadata_DC_polygon);
Expand All @@ -88,7 +94,10 @@ var UPDATE = process.env.UPDATE;
};

mapnik_omnivore.digest(file, function(err, metadata) {
if (err) return done(err);
if (err) {
assert.ifError(err, 'should not error');
return assert.end();
}
assert.ok(err === null);

//Round extent values to avoid floating point discrepancies in Travis
Expand Down

2 comments on commit 5d9e3a4

@rclark
Copy link
Contributor

@rclark rclark commented on 5d9e3a4 Feb 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a bunch of style-ish changes here to what I had done in previous commits. Admittedly I was trying out being a airbnb style-guide nazi. Were you consciously reverting some of these changes (like chained variable declarations in the convertToMeters function), or were there some merge conflicts that got resolved in a way that dropped my adjustments?

I am totally open to not being a styleguide nazi; part of it was me wanting to see how many of these kinds of problems come up.

@GretaCB
Copy link
Contributor

@GretaCB GretaCB commented on 5d9e3a4 Feb 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rclark oh this is a definite mistake on my part. I stashed some of my chagnes before merging your changes from last night, and the merge mustve caused some conflicts that I didnt catch. I will fix.

Please sign in to comment.