-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
141 lines (123 loc) · 3.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var fs = require('fs');
var path = require('path');
var invalid = require('./lib/invalid');
var mapnik = require('mapnik');
var sniffer = require('@mapbox/mapbox-file-sniff');
var queue = require('queue-async');
var Csv = require('./lib/csv');
// Register datasource plugins
mapnik.register_default_input_plugins();
var modules = [
require('./lib/geojson'),
require('./lib/raster'),
require('./lib/shape'),
require('./lib/ogr'),
require('./lib/topojson'),
Csv
];
// silence mapnik logs
mapnik.Logger.setSeverity(mapnik.Logger.NONE);
/**
* Initializes the module
* @param file (filepath)
* @returns metadata {filesize, projection, filename, center, extent, json, minzoom, maxzoom, layers, dstype, filetype}
*/
module.exports.digest = function(file, callback) {
sniffer.fromFile(file, function(err, fileinfo) {
if (err && err.code === 'EINVALID') {
try {
new Csv(file);
fileinfo.type = 'csv';
}
catch (error) { return callback(err); }
} else if (err) return callback(err);
getMetadata(file, fileinfo.type, function(err, metadata) {
if (err) return callback(err);
return callback(null, metadata);
});
});
};
/**
* Validates size of file and processes the file
* @param file (filepath)
* @returns metadata {filesize, projection, filename, center, extent, json, minzoom, maxzoom, layers, dstype, filetype}
*/
function getMetadata(file, filetype, callback) {
var type = modules.filter(function(module) {
return module.validFileType.some(function(t) {
return t === filetype;
});
});
var metadata = { filename: path.basename(file, path.extname(file)) };
var q = queue(1);
var source;
// Instantiate new object, based on datatype
try {
source = new type[0](file);
} catch (err) {
if (err.message.indexOf('could not parse') !== -1) {
// Sanitize error message
var filepath = err.message.split('could not parse')[1];
err.message = err.message.replace(filepath, ' source');
}
return callback(invalid('Error creating Mapnik Datasource: ' + err.message));
}
// Build metadata object for source asynchronously
metadata.filetype = '.' + filetype;
metadata.dstype = source.dstype;
q.defer(function(next) {
fs.stat(file, function(err, stats) {
if (err) return callback(invalid(err));
metadata.filesize = stats.size;
next();
});
});
q.defer(function(next) {
source.getCenter(function(err, center) {
if (err) return next(err);
metadata.center = center;
next();
});
});
q.defer(function(next) {
source.getExtent(function(err, extent) {
if (err) return next(err);
metadata.extent = extent;
next();
});
});
q.defer(function(next) {
source.getZooms(function(err, minzoom, maxzoom) {
if (err) return next(err);
metadata.minzoom = minzoom;
metadata.maxzoom = maxzoom;
next();
});
});
q.defer(function(next) {
source.getProjection(function(err, projection) {
if (err) return next(err);
metadata.projection = projection;
next();
});
});
q.defer(function(next) {
source.getDetails(function(err, details) {
if (err) return next(err);
var detailsName = source.detailsName;
metadata[detailsName] = details;
next();
});
});
q.defer(function(next) {
source.getLayers(function(err, layers) {
if (err) return next(err);
metadata.layers = layers;
next();
});
});
q.await(function(err) {
if (err) return callback(invalid(err));
callback(err, metadata);
});
}