diff --git a/lib/datasource-processor.js b/lib/datasource-processor.js index 25c766e..241c2ea 100644 --- a/lib/datasource-processor.js +++ b/lib/datasource-processor.js @@ -113,7 +113,12 @@ function getProjection(file, filetype, callback) { if (err) return callback(err); return callback(null, proj); }); - //else kml and gpx + } else if(filetype === '.gdb') { + projectionViaOGR(file, function(err, proj) { + if (err) return callback(err); + return callback(null, proj); + }); + //else kml, gpx, geojson, and csv we assume is WGS84 (aka +init=epsg:4326 or +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs) } else return callback(null, '+init=epsg:4326'); } @@ -438,6 +443,37 @@ function projectionFromRaster(filepath, callback) { callback(null, info); } + +/** + * Obtains projection from any ogr vector format by using node-gdal lib + * @param file (filepath) + * @returns result.proj4 + */ +function projectionViaOGR(filepath, callback) { + + var ds; + try { ds = gdal.open(filepath); } + catch(err) { + return callback(invalid('could not open the file ' + filepath)); + } + + if (!ds || !ds.layers || ds.layers.count() < 1) { + return callback(invalid('file has no valid layers: ' + filepath)); + } + // FIXME: https://github.com/mapbox/mapnik-omnivore/issues/72 + if (ds.layers.count() > 1) { + return callback(invalid('No support yet for multilayer files inside ' + filepath)); + } else { + try { + var proj4 = srs.parse(ds.layers.get(0).srs.toWKT()).proj4; + if (proj4) return callback(null,proj4); + } catch(err) { + console.error(err.stack); + } + } + return callback(invalid('could not read spatial reference information for ' + filepath)); +} + /** * Iterates through source's bands and obtains band properties * @param ds (Datasource) @@ -565,7 +601,7 @@ function getDatasourceProperties(file, filetype, callback) { } // Get layers for kml or topojson - else if (filetype === '.kml' || filetype === '.topojson') { + else if (filetype === '.kml' || filetype === '.topojson' || filetype === '.gdb') { // Get KML layer names from the OGR error message...for now getLayers(options, function(err, layers) { if (err) return callback(err); @@ -657,5 +693,6 @@ module.exports = { getMinMaxZoomGDAL: getMinMaxZoomGDAL, getProjection: getProjection, projectionFromRaster: projectionFromRaster, - projectionFromShape: projectionFromShape + projectionFromShape: projectionFromShape, + projectionViaOGR: projectionViaOGR }; diff --git a/package.json b/package.json index 024cca9..12592b1 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "tape": "3.0.x", "coveralls": "~2.11.1", "istanbul": "~0.3.0", - "mapnik-test-data": "http://mapbox-npm.s3.amazonaws.com/package/mapnik-test-data-2.0.3-fece0a9c546074a1479e9eae2333184029bb2279.tgz" + "mapnik-test-data": "https://github.com/mapbox/mapnik-test-data/tarball/moar" }, "scripts": { "test": "tape test/*.js", diff --git a/test/datasource-processor.test.js b/test/datasource-processor.test.js index 34e7544..0f81e7f 100644 --- a/test/datasource-processor.test.js +++ b/test/datasource-processor.test.js @@ -14,6 +14,7 @@ var expectedMetadata_1week_earthquake = JSON.parse(fs.readFileSync(path.resolve( var expectedMetadata_sample_tif = JSON.parse(fs.readFileSync(path.resolve('test/fixtures/metadata_sample_tif.json'))); var expectedMetadata_sample_vrt = JSON.parse(fs.readFileSync(path.resolve('test/fixtures/metadata_sample_vrt.json'))); var expectedMetadata_topo = JSON.parse(fs.readFileSync(path.resolve('test/fixtures/metadata_topo.json'))); +var expectedMetadata_filegdb = JSON.parse(fs.readFileSync(path.resolve('test/fixtures/metadata_filegdb.json'))); var UPDATE = process.env.UPDATE; @@ -201,6 +202,23 @@ var UPDATE = process.env.UPDATE; /** * Testing datasourceProcessor.init */ + tape('[FILEGDB] Setup', function(assert) { + var gdbFile = testData + '/data/filegdb/multipoint.gdb'; + var filesize = 428328; // N/A since it is a directory! + var type = '.gdb'; + //Overwrites metadata json file if output does not match + datasourceProcessor.init(gdbFile, filesize, type, function(err, metadata) { + if (err) { + assert.ifError(err, 'should not error'); + return assert.end(); + } + if (UPDATE) fs.writeFileSync(path.resolve('test/fixtures/metadata_filegdb.json'), JSON.stringify(metadata, null, 2)); + assert.deepEqual(metadata, expectedMetadata_filegdb); + + assert.end(); + }); + }); + tape('[SHAPE] Setup', function(assert) { var shpFile = testData + '/data/shp/world_merc/world_merc.shp'; var filesize = 428328; diff --git a/test/fixtures/metadata_filegdb.json b/test/fixtures/metadata_filegdb.json new file mode 100644 index 0000000..5e43690 --- /dev/null +++ b/test/fixtures/metadata_filegdb.json @@ -0,0 +1,33 @@ +{ + "filesize": 428328, + "projection": "+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs", + "filename": "multipoint", + "center": [ + -75.16024141355962, + 39.95769202781635 + ], + "extent": [ + -75.16026089315217, + 39.9576802349116, + -75.16022193396708, + 39.9577038207211 + ], + "json": { + "vector_layers": [ + { + "id": "multipoint", + "description": "", + "minzoom": 0, + "maxzoom": 22, + "fields": {} + } + ] + }, + "minzoom": 0, + "maxzoom": 22, + "layers": [ + "multipoint" + ], + "dstype": "ogr", + "filetype": ".gdb" +} \ No newline at end of file