Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mapnik geojson dstype #67

Merged
merged 1 commit into from
Jan 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions lib/datasource-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,15 @@ function processOgrDatasource(file, filesize, proj, filetype, callback) {
"fields": fields
});
});

return callback(null, {
extent: results.extent,
center: results.center,
json: json,
minzoom: min,
maxzoom: max,
layers: layers,
dstype: 'ogr'
dstype: ds.dstype
});
});
});
Expand All @@ -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 <layer>') != -1) {
var layers = err.message.split("are: ")[1];
Expand All @@ -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'));
}

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/metadata_DC_polygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"layers": [
"OGRGeoJSON"
],
"dstype": "ogr",
"dstype": "geojson",
"filetype": ".geojson"
}
}