forked from sbma44/uber-cities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-geojson.js
42 lines (39 loc) · 1.56 KB
/
extract-geojson.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
var fs = require('fs');
fs.readdir('./cities', function(err, files) {
files.forEach(function(filename, i) {
fs.readFile('./cities/' + filename, function(err, data){
var basename = filename.replace(/\.json/, '');
var data = JSON.parse(data);
if (data.geojson !== null) {
fs.writeFile('geojson/' + basename + '.geojson', data.geojson);
}
else {
console.log('- bad geojson for ' + filename + ', creating from bounding box instead');
var x1 = data.gbounds.northeast.lng,
y1 = data.gbounds.northeast.lat,
x2 = data.gbounds.southwest.lng,
y2 = data.gbounds.southwest.lat;
var geojson = {
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates:[[
[x1, y1],
[x1, y2],
[x2, y2],
[x2, y1],
[x1, y1]
]]
}
}
]
};
fs.writeFile('geojson/' + basename + '.geojson', JSON.stringify(geojson));
}
});
});
});