blob: 6d6196e08dca7b49d2e31f6fcbae9b6e361a2da3 [file] [log] [blame]
evansirokyd401c892016-06-16 00:05:14 -07001var fs = require('fs'),
2 http = require('http')
3
4var async = require('async'),
5 overpass = require('query-overpass'),
6 shp = require('shpjs')
7
8
9var toArrayBuffer = function(buffer) {
10 var ab = new ArrayBuffer(buffer.length)
11 var view = new Uint8Array(ab)
12 for (var i = 0; i < buffer.length; ++i) {
13 view[i] = buffer[i]
14 }
15 return view
16}
17
18var extractToGeoJson = function(callback) {
19 shp(toArrayBuffer(fs.readFileSync('./downloads/tz_world_mp.zip')))
20 .then(function(geojson) { console.log('extract success'); callback(null, geojson) })
21 .catch(function(e){ console.log('extract err', e); callback(e) })
22}
23
24var osmBoundarySources = require('./osmBoundarySources.json')
25
26async.auto({
27 makeDownloadsDir: function(cb) {
28 console.log('creating downloads dir')
29 fs.mkdir('./downloads', function(err) {
30 if(err && err.code === 'EEXIST') {
31 cb()
32 } else {
33 cb(err)
34 }
35 })
36 },
37 getEfeleShapefile: ['makeDownloadsDir', function(results, cb) {
38 console.log('downloads efele.net shapefile')
39 return cb()
40 var file = fs.createWriteStream('./downloads/tz_world_mp.zip')
41 http.get('http://efele.net/maps/tz/world/tz_world_mp.zip', function(response) {
42 response.pipe(file)
43 file
44 .on('finish', function() {
45 file.close(cb)
46 })
47 .on('error', cb)
48 })
49 }],
50 extractShapefile: ['getEfeleShapefile', function(results, cb) {
51 console.log('extracting efele.net shapefile')
52 extractToGeoJson(cb)
53 }],
54 getOsmBoundaries: ['makeDownloadsDir', function(results, cb) {
55 console.log('downloading osm boundaries')
56 var boundaryIds = Object.keys(osmBoundarySources)
57 async.eachSeries(boundaryIds,
58 function(boundaryId, boundaryCallback) {
59 var cfg = osmBoundarySources[boundaryId]
60 console.log('osm boundary for:', boundaryId, cfg.query)
61 overpass(cfg.query, function(err, data) {
62 if(err) { return boundaryCallback(err) }
63 if(!data.features || data.features.length == 0) {
64 err = new Error('Invalid geojson for boundary: ' + boundaryId)
65 return boundaryCallback(err)
66 }
67 // union all multi-polygons / polygons into one
68 fs.writeFile('./downloads/' + boundaryId, JSON.stringify(data, null, 2), boundaryCallback)
69 }, { flatProperties: true })
70 }, cb)
71 }]
72}, function(err) {
73 console.log('done')
74 if(err) {
75 console.log('error!', err)
76 return
77 }
78
79 //console.log(osmBoundarySources)
80})