aboutsummaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorevansiroky <evan.siroky@yahoo.com>2016-06-16 00:05:14 -0700
committerevansiroky <evan.siroky@yahoo.com>2016-06-16 00:05:14 -0700
commitd401c89727f1fc22cb2e718599bb1d76aa6e968b (patch)
tree7c1e4ea1f8ea4905248d309d2bc6d8a52d170e1e /index.js
parentb1d4404cf2c62f069ba434a73fbbce53e17db0b5 (diff)
downloadtimezone-boundary-builder-d401c89727f1fc22cb2e718599bb1d76aa6e968b.tar.gz
Download scripts
- downloading efele.net shapefile - querying overpass api for boundaries
Diffstat (limited to 'index.js')
-rw-r--r--index.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..6d6196e
--- /dev/null
+++ b/index.js
@@ -0,0 +1,80 @@
+var fs = require('fs'),
+ http = require('http')
+
+var async = require('async'),
+ overpass = require('query-overpass'),
+ shp = require('shpjs')
+
+
+var toArrayBuffer = function(buffer) {
+ var ab = new ArrayBuffer(buffer.length)
+ var view = new Uint8Array(ab)
+ for (var i = 0; i < buffer.length; ++i) {
+ view[i] = buffer[i]
+ }
+ return view
+}
+
+var extractToGeoJson = function(callback) {
+ shp(toArrayBuffer(fs.readFileSync('./downloads/tz_world_mp.zip')))
+ .then(function(geojson) { console.log('extract success'); callback(null, geojson) })
+ .catch(function(e){ console.log('extract err', e); callback(e) })
+}
+
+var osmBoundarySources = require('./osmBoundarySources.json')
+
+async.auto({
+ makeDownloadsDir: function(cb) {
+ console.log('creating downloads dir')
+ fs.mkdir('./downloads', function(err) {
+ if(err && err.code === 'EEXIST') {
+ cb()
+ } else {
+ cb(err)
+ }
+ })
+ },
+ getEfeleShapefile: ['makeDownloadsDir', function(results, cb) {
+ console.log('downloads efele.net shapefile')
+ return cb()
+ var file = fs.createWriteStream('./downloads/tz_world_mp.zip')
+ http.get('http://efele.net/maps/tz/world/tz_world_mp.zip', function(response) {
+ response.pipe(file)
+ file
+ .on('finish', function() {
+ file.close(cb)
+ })
+ .on('error', cb)
+ })
+ }],
+ extractShapefile: ['getEfeleShapefile', function(results, cb) {
+ console.log('extracting efele.net shapefile')
+ extractToGeoJson(cb)
+ }],
+ getOsmBoundaries: ['makeDownloadsDir', function(results, cb) {
+ console.log('downloading osm boundaries')
+ var boundaryIds = Object.keys(osmBoundarySources)
+ async.eachSeries(boundaryIds,
+ function(boundaryId, boundaryCallback) {
+ var cfg = osmBoundarySources[boundaryId]
+ console.log('osm boundary for:', boundaryId, cfg.query)
+ overpass(cfg.query, function(err, data) {
+ if(err) { return boundaryCallback(err) }
+ if(!data.features || data.features.length == 0) {
+ err = new Error('Invalid geojson for boundary: ' + boundaryId)
+ return boundaryCallback(err)
+ }
+ // union all multi-polygons / polygons into one
+ fs.writeFile('./downloads/' + boundaryId, JSON.stringify(data, null, 2), boundaryCallback)
+ }, { flatProperties: true })
+ }, cb)
+ }]
+}, function(err) {
+ console.log('done')
+ if(err) {
+ console.log('error!', err)
+ return
+ }
+
+ //console.log(osmBoundarySources)
+}) \ No newline at end of file