-
Notifications
You must be signed in to change notification settings - Fork 1
/
boroughs.js
51 lines (44 loc) · 1.19 KB
/
boroughs.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
var fs = require('fs'),
xml2js = require('xml2js'),
async = require('async');
var parser = new xml2js.Parser({mergeAttrs:true,explicitArray:false});
this.getBoroughs = function(directory, callback) {
var map = function(arr,func){
var target = [];
arr.forEach(function(item){
target.push(func(item));
});
return target;
};
var fullPath = function(file){
return __dirname + directory + "/" + file;
};
var name = function(file){
return file.split('.')[0];
};
var parseGPX = function(xml){
var points = [];
xml.gpx.rte.rtept.forEach(function(pt){
points.push({x:parseFloat(pt.lat),y:parseFloat(pt.lon)});
});
return points;
};
var parseFiles = function(files,names){
async.map(files,parser.parseString,function(err,xml){
if (err) return callback(err);
var target = {};
names.forEach(function (borough, index){
target[borough] = parseGPX(xml[index]);
});
return callback(null, target);
});
};
fs.readdir(__dirname + directory, function(err,files){
if (err) return callback(err);
var pathed = map(files,fullPath);
async.map(pathed,fs.readFile,function(err, data){
if (err) return callback(err);
parseFiles(data,map(files,name));
});
});
};