From 0ae2ff5cbdb6584b3a3bd452dc9225bff19ca97c Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Wed, 21 Jan 2015 17:07:50 -0700 Subject: [PATCH] use mapnik geojson dstype --- lib/datasource-processor.js | 71 ++++++++++++++++++-------- test/fixtures/metadata_DC_polygon.json | 4 +- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/lib/datasource-processor.js b/lib/datasource-processor.js index 14a2148..0ccda53 100644 --- a/lib/datasource-processor.js +++ b/lib/datasource-processor.js @@ -514,6 +514,7 @@ function processOgrDatasource(file, filesize, proj, filetype, callback) { "fields": fields }); }); + return callback(null, { extent: results.extent, center: results.center, @@ -521,7 +522,7 @@ function processOgrDatasource(file, filesize, proj, filetype, callback) { minzoom: min, maxzoom: max, layers: layers, - dstype: 'ogr' + dstype: ds.dstype }); }); }); @@ -536,44 +537,69 @@ function processOgrDatasource(file, filesize, proj, filetype, callback) { * ds (mapnik Datasource) */ function getDatasourceProperties(file, filetype, callback) { - var dstype = 'ogr'; - var options = {}; + var options = { + type: filetype === '.geojson' ? 'geojson' : 'ogr', + file: file + }; + + if (filetype === '.geojson') { + var ds; + try { ds = new mapnik.Datasource(options); } + catch(err) { + if (/Failed parse GeoJSON file/.test(err)) + // If it couldn't be parsed, try again as topojson + return getDatasourceProperties(file, '.topojson', callback); + } - //Get layers for kml or geojson/topojson - if (filetype === '.kml' || filetype === '.geojson') { - options.type = dstype; - options.file = file; + var features = []; + var featureset = ds.featureset(); + var feature = featureset.next(); + if (feature === undefined) { + return callback(invalid('Source appears to have no features data.')); + } - //Get KML/geojson layer names from the OGR error message...for now + ds.dstype = 'geojson'; + return callback(null, [ 'OGRGeoJSON' ], ds); + } + + // Get layers for kml or topojson + else if (filetype === '.kml' || filetype === '.topojson') { + // Get KML layer names from the OGR error message...for now getLayers(options, function(err, layers) { if (err) return callback(err); getDatasource(options, layers, function(err, ds) { if (err) return callback(err); - else return callback(null, layers, ds); + else { + ds.dstype = 'ogr'; + return callback(null, layers, ds); + } }); }); + } - //Get layers for .gpx - } else if (filetype === '.gpx') { - options.type = dstype; - options.file = file; - - //GPX files can only have these layers + // Get layers for .gpx + else if (filetype === '.gpx') { + // GPX files can only have these layers var layers = ['waypoints', 'routes', 'tracks', 'route_points', 'track_points']; getDatasource(options, layers, function(err, ds) { if (err) return callback(err); + ds.dstype = 'ogr'; return callback(null, layers, ds); }); } + + else return callback(new Error('Unexpected filetype passed to getDatasourceProperties')); } -//Sole purpose is to obtain layer names from OGR error message +// Sole purpose is to obtain layer names from OGR error message function getLayers(options, callback) { var layers_array; var error; - //Expect an error in order to obtain layer names from the OGR Error string...for now + + // Expect an error in order to obtain layer names from the OGR Error string...for now + var ds; try { - var ds = new mapnik.Datasource(options); + ds = new mapnik.Datasource(options); } catch (err) { if (err.message && err.message.indexOf('OGR Plugin: missing ') != -1) { var layers = err.message.split("are: ")[1]; @@ -583,9 +609,14 @@ function getLayers(options, callback) { var layer_names = layers.split('\'').join(''); //designate each index of the array by splitting by commas layers_array = layer_names.split(','); - } else error = err; + } + + else error = err; + } + if (error === undefined) { + // console.log(ds.layers.count()); + return callback(null, layers_array); } - if (error === undefined) return callback(null, layers_array); else return callback(invalid('Error obtaining layer names')); } diff --git a/test/fixtures/metadata_DC_polygon.json b/test/fixtures/metadata_DC_polygon.json index 25c223e..c051739 100644 --- a/test/fixtures/metadata_DC_polygon.json +++ b/test/fixtures/metadata_DC_polygon.json @@ -32,6 +32,6 @@ "layers": [ "OGRGeoJSON" ], - "dstype": "ogr", + "dstype": "geojson", "filetype": ".geojson" -} \ No newline at end of file +}