Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 1 | var exec = require('child_process').exec |
| 2 | var fs = require('fs') |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 3 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 4 | var area = require('@mapbox/geojson-area') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 5 | var geojsonhint = require('@mapbox/geojsonhint') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 6 | var bbox = require('@turf/bbox').default |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 7 | var helpers = require('@turf/helpers') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 8 | var multiPolygon = helpers.multiPolygon |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 9 | var polygon = helpers.polygon |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 10 | var asynclib = require('async') |
| 11 | var jsts = require('jsts') |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 12 | var rimraf = require('rimraf') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 13 | var overpass = require('query-overpass') |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 14 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 15 | var osmBoundarySources = require('./osmBoundarySources.json') |
| 16 | var zoneCfg = require('./timezones.json') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 17 | var expectedZoneOverlaps = require('./expectedZoneOverlaps.json') |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 18 | |
daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 19 | const ProgressStats = require('./progressStats') |
| 20 | var progressStats = new ProgressStats(Object.keys(osmBoundarySources).length) |
| 21 | |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 22 | // allow building of only a specified zones |
| 23 | var filteredIndex = process.argv.indexOf('--filtered-zones') |
| 24 | if (filteredIndex > -1 && process.argv[filteredIndex + 1]) { |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 25 | const filteredZones = process.argv[filteredIndex + 1].split(',') |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 26 | var newZoneCfg = {} |
| 27 | filteredZones.forEach((zoneName) => { |
| 28 | newZoneCfg[zoneName] = zoneCfg[zoneName] |
| 29 | }) |
| 30 | zoneCfg = newZoneCfg |
| 31 | |
| 32 | // filter out unneccessary downloads |
| 33 | var newOsmBoundarySources = {} |
| 34 | Object.keys(zoneCfg).forEach((zoneName) => { |
| 35 | zoneCfg[zoneName].forEach((op) => { |
| 36 | if (op.source === 'overpass') { |
| 37 | newOsmBoundarySources[op.id] = osmBoundarySources[op.id] |
| 38 | } |
| 39 | }) |
| 40 | }) |
| 41 | |
| 42 | osmBoundarySources = newOsmBoundarySources |
| 43 | } |
| 44 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 45 | var geoJsonReader = new jsts.io.GeoJSONReader() |
| 46 | var geoJsonWriter = new jsts.io.GeoJSONWriter() |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 47 | var precisionModel = new jsts.geom.PrecisionModel(1000000) |
| 48 | var precisionReducer = new jsts.precision.GeometryPrecisionReducer(precisionModel) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 49 | var distZones = {} |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 50 | var minRequestGap = 4 |
| 51 | var curRequestGap = 4 |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 52 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 53 | var safeMkdir = function (dirname, callback) { |
| 54 | fs.mkdir(dirname, function (err) { |
| 55 | if (err && err.code === 'EEXIST') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 56 | callback() |
| 57 | } else { |
| 58 | callback(err) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 63 | var debugGeo = function (op, a, b, reducePrecision) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 64 | var result |
| 65 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 66 | if (reducePrecision) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 67 | a = precisionReducer.reduce(a) |
| 68 | b = precisionReducer.reduce(b) |
| 69 | } |
| 70 | |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 71 | try { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 72 | switch (op) { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 73 | case 'union': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 74 | result = a.union(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 75 | break |
| 76 | case 'intersection': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 77 | result = a.intersection(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 78 | break |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 79 | case 'intersects': |
| 80 | result = a.intersects(b) |
| 81 | break |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 82 | case 'diff': |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 83 | result = a.difference(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 84 | break |
| 85 | default: |
| 86 | var err = new Error('invalid op: ' + op) |
| 87 | throw err |
| 88 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 89 | } catch (e) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 90 | if (e.name === 'TopologyException') { |
| 91 | console.log('Encountered TopologyException, retry with GeometryPrecisionReducer') |
| 92 | return debugGeo(op, a, b, true) |
| 93 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 94 | console.log('op err') |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 95 | console.log(e) |
| 96 | console.log(e.stack) |
| 97 | fs.writeFileSync('debug_' + op + '_a.json', JSON.stringify(geoJsonWriter.write(a))) |
| 98 | fs.writeFileSync('debug_' + op + '_b.json', JSON.stringify(geoJsonWriter.write(b))) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 99 | throw e |
| 100 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 101 | |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 102 | return result |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 105 | var fetchIfNeeded = function (file, superCallback, downloadCallback, fetchFn) { |
| 106 | // check for file that got downloaded |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 107 | fs.stat(file, function (err) { |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 108 | if (!err) { |
| 109 | // file found, skip download steps |
| 110 | return superCallback() |
| 111 | } |
| 112 | // check for manual file that got fixed and needs validation |
| 113 | var fixedFile = file.replace('.json', '_fixed.json') |
| 114 | fs.stat(fixedFile, function (err) { |
| 115 | if (!err) { |
| 116 | // file found, return fixed file |
| 117 | return downloadCallback(null, require(fixedFile)) |
| 118 | } |
| 119 | // no manual fixed file found, download from overpass |
| 120 | fetchFn() |
| 121 | }) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 122 | }) |
| 123 | } |
| 124 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 125 | var geoJsonToGeom = function (geoJson) { |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 126 | try { |
| 127 | return geoJsonReader.read(JSON.stringify(geoJson)) |
| 128 | } catch (e) { |
| 129 | console.error('error converting geojson to geometry') |
| 130 | fs.writeFileSync('debug_geojson_read_error.json', JSON.stringify(geoJson)) |
| 131 | throw e |
| 132 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 135 | var geomToGeoJson = function (geom) { |
| 136 | return geoJsonWriter.write(geom) |
| 137 | } |
| 138 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 139 | var geomToGeoJsonString = function (geom) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 140 | return JSON.stringify(geoJsonWriter.write(geom)) |
| 141 | } |
| 142 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 143 | var downloadOsmBoundary = function (boundaryId, boundaryCallback) { |
| 144 | var cfg = osmBoundarySources[boundaryId] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 145 | var query = '[out:json][timeout:60];(' |
| 146 | if (cfg.way) { |
| 147 | query += 'way' |
| 148 | } else { |
| 149 | query += 'relation' |
| 150 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 151 | var boundaryFilename = './downloads/' + boundaryId + '.json' |
| 152 | var debug = 'getting data for ' + boundaryId |
| 153 | var queryKeys = Object.keys(cfg) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 154 | |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 155 | for (var i = queryKeys.length - 1; i >= 0; i--) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 156 | var k = queryKeys[i] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 157 | if (k === 'way') continue |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 158 | var v = cfg[k] |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 159 | |
| 160 | query += '["' + k + '"="' + v + '"]' |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
evansiroky | 283ebbc | 2018-07-16 15:13:07 -0700 | [diff] [blame] | 163 | query += ';);out body;>;out meta qt;' |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 164 | |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 165 | console.log(debug) |
| 166 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 167 | asynclib.auto({ |
| 168 | downloadFromOverpass: function (cb) { |
daniel.teske | 70445a7 | 2018-10-05 09:15:42 +0200 | [diff] [blame] | 169 | progressStats.logNext() |
| 170 | console.log('downloading from overpass; overall progress ' + progressStats.getPercentage() + '% done - ' + progressStats.getTimeLeft(5) + ' left') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 171 | fetchIfNeeded(boundaryFilename, boundaryCallback, cb, function () { |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 172 | var overpassResponseHandler = function (err, data) { |
| 173 | if (err) { |
| 174 | console.log(err) |
| 175 | console.log('Increasing overpass request gap') |
| 176 | curRequestGap *= 2 |
| 177 | makeQuery() |
| 178 | } else { |
| 179 | console.log('Success, decreasing overpass request gap') |
| 180 | curRequestGap = Math.max(minRequestGap, curRequestGap / 2) |
| 181 | cb(null, data) |
| 182 | } |
| 183 | } |
| 184 | var makeQuery = function () { |
| 185 | console.log('waiting ' + curRequestGap + ' seconds') |
| 186 | setTimeout(function () { |
| 187 | overpass(query, overpassResponseHandler, { flatProperties: true }) |
| 188 | }, curRequestGap * 1000) |
| 189 | } |
| 190 | makeQuery() |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 191 | }) |
| 192 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 193 | validateOverpassResult: ['downloadFromOverpass', function (results, cb) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 194 | var data = results.downloadFromOverpass |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 195 | if (!data.features) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 196 | var err = new Error('Invalid geojson for boundary: ' + boundaryId) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 197 | return cb(err) |
| 198 | } |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 199 | if (data.features.length === 0) { |
| 200 | console.error('No data for the following query:') |
| 201 | console.error(query) |
| 202 | console.error('To read more about this error, please visit https://git.io/vxKQL') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 203 | return cb(new Error('No data found for from overpass query')) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 204 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 205 | cb() |
| 206 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 207 | saveSingleMultiPolygon: ['validateOverpassResult', function (results, cb) { |
| 208 | var data = results.downloadFromOverpass |
| 209 | var combined |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 210 | |
| 211 | // union all multi-polygons / polygons into one |
| 212 | for (var i = data.features.length - 1; i >= 0; i--) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 213 | var curOsmGeom = data.features[i].geometry |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 214 | const curOsmProps = data.features[i].properties |
| 215 | if ( |
| 216 | (curOsmGeom.type === 'Polygon' || curOsmGeom.type === 'MultiPolygon') && |
| 217 | curOsmProps.type === 'boundary' // need to make sure enclaves aren't unioned |
| 218 | ) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 219 | console.log('combining border') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 220 | let errors = geojsonhint.hint(curOsmGeom) |
| 221 | if (errors && errors.length > 0) { |
| 222 | const stringifiedGeojson = JSON.stringify(curOsmGeom, null, 2) |
| 223 | errors = geojsonhint.hint(stringifiedGeojson) |
| 224 | console.error('Invalid geojson received in Overpass Result') |
| 225 | console.error('Overpass query: ' + query) |
| 226 | const problemFilename = boundaryId + '_convert_to_geom_error.json' |
| 227 | fs.writeFileSync(problemFilename, stringifiedGeojson) |
| 228 | console.error('saved problem file to ' + problemFilename) |
| 229 | console.error('To read more about this error, please visit https://git.io/vxKQq') |
| 230 | return cb(errors) |
| 231 | } |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 232 | try { |
| 233 | var curGeom = geoJsonToGeom(curOsmGeom) |
| 234 | } catch (e) { |
| 235 | console.error('error converting overpass result to geojson') |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 236 | console.error(e) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 237 | |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 238 | fs.writeFileSync(boundaryId + '_convert_to_geom_error-all-features.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 239 | return cb(e) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 240 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 241 | if (!combined) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 242 | combined = curGeom |
| 243 | } else { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 244 | combined = debugGeo('union', curGeom, combined) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 248 | try { |
| 249 | fs.writeFile(boundaryFilename, geomToGeoJsonString(combined), cb) |
| 250 | } catch (e) { |
| 251 | console.error('error writing combined border to geojson') |
| 252 | fs.writeFileSync(boundaryId + '_combined_border_convert_to_geom_error.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 253 | return cb(e) |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 254 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 255 | }] |
| 256 | }, boundaryCallback) |
| 257 | } |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 258 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 259 | var getTzDistFilename = function (tzid) { |
| 260 | return './dist/' + tzid.replace(/\//g, '__') + '.json' |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Get the geometry of the requested source data |
| 265 | * |
| 266 | * @return {Object} geom The geometry of the source |
| 267 | * @param {Object} source An object representing the data source |
| 268 | * must have `source` key and then either: |
| 269 | * - `id` if from a file |
| 270 | * - `id` if from a file |
| 271 | */ |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 272 | var getDataSource = function (source) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 273 | var geoJson |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 274 | if (source.source === 'overpass') { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 275 | geoJson = require('./downloads/' + source.id + '.json') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 276 | } else if (source.source === 'manual-polygon') { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 277 | geoJson = polygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 278 | } else if (source.source === 'manual-multipolygon') { |
Evan Siroky | 8e30a2e | 2016-08-06 19:55:35 -0700 | [diff] [blame] | 279 | geoJson = multiPolygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 280 | } else if (source.source === 'dist') { |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 281 | geoJson = require(getTzDistFilename(source.id)) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 282 | } else { |
| 283 | var err = new Error('unknown source: ' + source.source) |
| 284 | throw err |
| 285 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 286 | return geoJsonToGeom(geoJson) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 289 | /** |
| 290 | * Post process created timezone boundary. |
| 291 | * - remove small holes and exclaves |
| 292 | * - reduce geometry precision |
| 293 | * |
| 294 | * @param {Geometry} geom The jsts geometry of the timezone |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 295 | * @param {boolean} returnAsObject if true, return as object, otherwise return stringified |
| 296 | * @return {Object|String} geojson as object or stringified |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 297 | */ |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 298 | var postProcessZone = function (geom, returnAsObject) { |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 299 | // reduce precision of geometry |
| 300 | const geojson = geomToGeoJson(precisionReducer.reduce(geom)) |
| 301 | |
| 302 | // iterate through all polygons |
| 303 | const filteredPolygons = [] |
| 304 | let allPolygons = geojson.coordinates |
| 305 | if (geojson.type === 'Polygon') { |
| 306 | allPolygons = [geojson.coordinates] |
| 307 | } |
| 308 | |
| 309 | allPolygons.forEach((curPolygon, idx) => { |
| 310 | // remove any polygon with very small area |
| 311 | const polygonFeature = polygon(curPolygon) |
| 312 | const polygonArea = area.geometry(polygonFeature.geometry) |
| 313 | |
| 314 | if (polygonArea < 1) return |
| 315 | |
| 316 | // find all holes |
| 317 | const filteredLinearRings = [] |
| 318 | |
| 319 | curPolygon.forEach((curLinearRing, lrIdx) => { |
| 320 | if (lrIdx === 0) { |
| 321 | // always keep first linearRing |
| 322 | filteredLinearRings.push(curLinearRing) |
| 323 | } else { |
| 324 | const polygonFromLinearRing = polygon([curLinearRing]) |
| 325 | const linearRingArea = area.geometry(polygonFromLinearRing.geometry) |
| 326 | |
| 327 | // only include holes with relevant area |
| 328 | if (linearRingArea > 1) { |
| 329 | filteredLinearRings.push(curLinearRing) |
| 330 | } |
| 331 | } |
| 332 | }) |
| 333 | |
| 334 | filteredPolygons.push(filteredLinearRings) |
| 335 | }) |
| 336 | |
| 337 | // recompile to geojson string |
| 338 | const newGeojson = { |
| 339 | type: geojson.type |
| 340 | } |
| 341 | |
| 342 | if (geojson.type === 'Polygon') { |
| 343 | newGeojson.coordinates = filteredPolygons[0] |
| 344 | } else { |
| 345 | newGeojson.coordinates = filteredPolygons |
| 346 | } |
| 347 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 348 | return returnAsObject ? newGeojson : JSON.stringify(newGeojson) |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 351 | var makeTimezoneBoundary = function (tzid, callback) { |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 352 | console.log('makeTimezoneBoundary for', tzid) |
| 353 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 354 | var ops = zoneCfg[tzid] |
| 355 | var geom |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 356 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 357 | asynclib.eachSeries(ops, function (task, cb) { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 358 | var taskData = getDataSource(task) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 359 | console.log('-', task.op, task.id) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 360 | if (task.op === 'init') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 361 | geom = taskData |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 362 | } else if (task.op === 'intersect') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 363 | geom = debugGeo('intersection', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 364 | } else if (task.op === 'difference') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 365 | geom = debugGeo('diff', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 366 | } else if (task.op === 'difference-reverse-order') { |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 367 | geom = debugGeo('diff', taskData, geom) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 368 | } else if (task.op === 'union') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 369 | geom = debugGeo('union', geom, taskData) |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 370 | } else { |
| 371 | var err = new Error('unknown op: ' + task.op) |
| 372 | return cb(err) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 373 | } |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 374 | cb() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 375 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 376 | function (err) { |
| 377 | if (err) { return callback(err) } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 378 | fs.writeFile(getTzDistFilename(tzid), |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 379 | postProcessZone(geom), |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 380 | callback) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 381 | }) |
| 382 | } |
| 383 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 384 | var loadDistZonesIntoMemory = function () { |
| 385 | console.log('load zones into memory') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 386 | var zones = Object.keys(zoneCfg) |
| 387 | var tzid |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 388 | |
| 389 | for (var i = 0; i < zones.length; i++) { |
| 390 | tzid = zones[i] |
| 391 | distZones[tzid] = getDataSource({ source: 'dist', id: tzid }) |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | var getDistZoneGeom = function (tzid) { |
| 396 | return distZones[tzid] |
| 397 | } |
| 398 | |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 399 | var roundDownToTenth = function (n) { |
| 400 | return Math.floor(n * 10) / 10 |
| 401 | } |
| 402 | |
| 403 | var roundUpToTenth = function (n) { |
| 404 | return Math.ceil(n * 10) / 10 |
| 405 | } |
| 406 | |
| 407 | var formatBounds = function (bounds) { |
| 408 | let boundsStr = '[' |
| 409 | boundsStr += roundDownToTenth(bounds[0]) + ', ' |
| 410 | boundsStr += roundDownToTenth(bounds[1]) + ', ' |
| 411 | boundsStr += roundUpToTenth(bounds[2]) + ', ' |
| 412 | boundsStr += roundUpToTenth(bounds[3]) + ']' |
| 413 | return boundsStr |
| 414 | } |
| 415 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 416 | var validateTimezoneBoundaries = function () { |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 417 | console.log('do validation... this may take a few minutes') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 418 | var allZonesOk = true |
| 419 | var zones = Object.keys(zoneCfg) |
| 420 | var compareTzid, tzid, zoneGeom |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 421 | |
| 422 | for (var i = 0; i < zones.length; i++) { |
| 423 | tzid = zones[i] |
| 424 | zoneGeom = getDistZoneGeom(tzid) |
| 425 | |
| 426 | for (var j = i + 1; j < zones.length; j++) { |
| 427 | compareTzid = zones[j] |
| 428 | |
| 429 | var compareZoneGeom = getDistZoneGeom(compareTzid) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 430 | |
| 431 | var intersects = false |
| 432 | try { |
| 433 | intersects = debugGeo('intersects', zoneGeom, compareZoneGeom) |
| 434 | } catch (e) { |
| 435 | console.warn('warning, encountered intersection error with zone ' + tzid + ' and ' + compareTzid) |
| 436 | } |
| 437 | if (intersects) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 438 | var intersectedGeom = debugGeo('intersection', zoneGeom, compareZoneGeom) |
| 439 | var intersectedArea = intersectedGeom.getArea() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 440 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 441 | if (intersectedArea > 0.0001) { |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 442 | // check if the intersected area(s) are one of the expected areas of overlap |
| 443 | const allowedOverlapBounds = expectedZoneOverlaps[`${tzid}-${compareTzid}`] || expectedZoneOverlaps[`${compareTzid}-${tzid}`] |
| 444 | const overlapsGeoJson = geoJsonWriter.write(intersectedGeom) |
| 445 | |
| 446 | // these zones are allowed to overlap in certain places, make sure the |
| 447 | // found overlap(s) all fit within the expected areas of overlap |
| 448 | if (allowedOverlapBounds) { |
| 449 | // if the overlaps are a multipolygon, make sure each individual |
| 450 | // polygon of overlap fits within at least one of the expected |
| 451 | // overlaps |
| 452 | let overlapsPolygons |
| 453 | switch (overlapsGeoJson.type) { |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 454 | case 'MultiPolygon': |
| 455 | overlapsPolygons = overlapsGeoJson.coordinates.map( |
| 456 | polygonCoords => ({ |
| 457 | coordinates: polygonCoords, |
| 458 | type: 'Polygon' |
| 459 | }) |
| 460 | ) |
| 461 | break |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 462 | case 'Polygon': |
| 463 | overlapsPolygons = [overlapsGeoJson] |
| 464 | break |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 465 | case 'GeometryCollection': |
| 466 | overlapsPolygons = [] |
| 467 | overlapsGeoJson.geometries.forEach(geom => { |
| 468 | if (geom.type === 'Polygon') { |
| 469 | overlapsPolygons.push(geom) |
| 470 | } else if (geom.type === 'MultiPolygon') { |
| 471 | geom.coordinates.forEach(polygonCoords => { |
| 472 | overlapsPolygons.push({ |
| 473 | coordinates: polygonCoords, |
| 474 | type: 'Polygon' |
| 475 | }) |
| 476 | }) |
| 477 | } |
| 478 | }) |
| 479 | break |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 480 | default: |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 481 | console.error('unexpected geojson overlap type') |
| 482 | console.log(overlapsGeoJson) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 483 | break |
| 484 | } |
| 485 | |
| 486 | let allOverlapsOk = true |
| 487 | overlapsPolygons.forEach((polygon, idx) => { |
| 488 | const bounds = bbox(polygon) |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 489 | const polygonArea = area.geometry(polygon) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 490 | if ( |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 491 | polygonArea > 10 && // ignore small polygons |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 492 | !allowedOverlapBounds.some(allowedBounds => |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 493 | allowedBounds.bounds[0] <= bounds[0] && // minX |
| 494 | allowedBounds.bounds[1] <= bounds[1] && // minY |
| 495 | allowedBounds.bounds[2] >= bounds[2] && // maxX |
| 496 | allowedBounds.bounds[3] >= bounds[3] // maxY |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 497 | ) |
| 498 | ) { |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 499 | console.error(`Unexpected intersection (${polygonArea} area) with bounds: ${formatBounds(bounds)}`) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 500 | allOverlapsOk = false |
| 501 | } |
| 502 | }) |
| 503 | |
| 504 | if (allOverlapsOk) continue |
| 505 | } |
| 506 | |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 507 | // at least one unexpected overlap found, output an error and write debug file |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 508 | console.error('Validation error: ' + tzid + ' intersects ' + compareTzid + ' area: ' + intersectedArea) |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 509 | const debugFilename = tzid.replace(/\//g, '-') + '-' + compareTzid.replace(/\//g, '-') + '-overlap.json' |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 510 | fs.writeFileSync( |
| 511 | debugFilename, |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 512 | JSON.stringify(overlapsGeoJson) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 513 | ) |
| 514 | console.error('wrote overlap area as file ' + debugFilename) |
| 515 | console.error('To read more about this error, please visit https://git.io/vx6nx') |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 516 | allZonesOk = false |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return allZonesOk ? null : 'Zone validation unsuccessful' |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 523 | } |
| 524 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 525 | let oceanZoneBoundaries |
| 526 | |
| 527 | var addOceans = function (callback) { |
| 528 | console.log('adding ocean boundaries') |
| 529 | const oceanZones = [ |
| 530 | { tzid: 'Etc/GMT-12', left: 172.5, right: 180 }, |
| 531 | { tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 }, |
| 532 | { tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 }, |
| 533 | { tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 }, |
| 534 | { tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 }, |
| 535 | { tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 }, |
| 536 | { tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 }, |
| 537 | { tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 }, |
| 538 | { tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 }, |
| 539 | { tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 }, |
| 540 | { tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 }, |
| 541 | { tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 }, |
| 542 | { tzid: 'Etc/GMT', left: -7.5, right: 7.5 }, |
| 543 | { tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 }, |
| 544 | { tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 }, |
| 545 | { tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 }, |
| 546 | { tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 }, |
| 547 | { tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 }, |
| 548 | { tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 }, |
| 549 | { tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 }, |
| 550 | { tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 }, |
| 551 | { tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 }, |
| 552 | { tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 }, |
| 553 | { tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 }, |
| 554 | { tzid: 'Etc/GMT+12', left: -180, right: -172.5 } |
| 555 | ] |
| 556 | |
| 557 | const zones = Object.keys(zoneCfg) |
| 558 | |
| 559 | oceanZoneBoundaries = oceanZones.map(zone => { |
| 560 | console.log(zone.tzid) |
| 561 | const geoJson = polygon([[ |
| 562 | [zone.left, 90], |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 563 | [zone.left, -90], |
| 564 | [zone.right, -90], |
| 565 | [zone.right, 90], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 566 | [zone.left, 90] |
| 567 | ]]).geometry |
| 568 | |
| 569 | let geom = geoJsonToGeom(geoJson) |
| 570 | |
| 571 | // diff against every zone |
| 572 | zones.forEach(distZone => { |
| 573 | geom = debugGeo('diff', geom, getDistZoneGeom(distZone)) |
| 574 | }) |
| 575 | |
| 576 | return { |
| 577 | geom: postProcessZone(geom, true), |
| 578 | tzid: zone.tzid |
| 579 | } |
| 580 | }) |
| 581 | |
| 582 | callback() |
| 583 | } |
| 584 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 585 | var combineAndWriteZones = function (callback) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 586 | var stream = fs.createWriteStream('./dist/combined.json') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 587 | var streamWithOceans = fs.createWriteStream('./dist/combined-with-oceans.json') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 588 | var zones = Object.keys(zoneCfg) |
| 589 | |
| 590 | stream.write('{"type":"FeatureCollection","features":[') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 591 | streamWithOceans.write('{"type":"FeatureCollection","features":[') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 592 | |
| 593 | for (var i = 0; i < zones.length; i++) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 594 | if (i > 0) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 595 | stream.write(',') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 596 | streamWithOceans.write(',') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 597 | } |
| 598 | var feature = { |
| 599 | type: 'Feature', |
| 600 | properties: { tzid: zones[i] }, |
| 601 | geometry: geomToGeoJson(getDistZoneGeom(zones[i])) |
| 602 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 603 | const stringified = JSON.stringify(feature) |
| 604 | stream.write(stringified) |
| 605 | streamWithOceans.write(stringified) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 606 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 607 | oceanZoneBoundaries.forEach(boundary => { |
| 608 | streamWithOceans.write(',') |
| 609 | var feature = { |
| 610 | type: 'Feature', |
| 611 | properties: { tzid: boundary.tzid }, |
| 612 | geometry: boundary.geom |
| 613 | } |
| 614 | streamWithOceans.write(JSON.stringify(feature)) |
| 615 | }) |
| 616 | asynclib.parallel([ |
| 617 | cb => { |
| 618 | stream.end(']}', cb) |
| 619 | }, |
| 620 | cb => { |
| 621 | streamWithOceans.end(']}', cb) |
| 622 | } |
| 623 | ], callback) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 626 | asynclib.auto({ |
| 627 | makeDownloadsDir: function (cb) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 628 | console.log('creating downloads dir') |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 629 | safeMkdir('./downloads', cb) |
| 630 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 631 | makeDistDir: function (cb) { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 632 | console.log('createing dist dir') |
| 633 | safeMkdir('./dist', cb) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 634 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 635 | getOsmBoundaries: ['makeDownloadsDir', function (results, cb) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 636 | console.log('downloading osm boundaries') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 637 | asynclib.eachSeries(Object.keys(osmBoundarySources), downloadOsmBoundary, cb) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 638 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 639 | createZones: ['makeDistDir', 'getOsmBoundaries', function (results, cb) { |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 640 | console.log('createZones') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 641 | asynclib.each(Object.keys(zoneCfg), makeTimezoneBoundary, cb) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 642 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 643 | validateZones: ['createZones', function (results, cb) { |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 644 | console.log('validating zones') |
| 645 | loadDistZonesIntoMemory() |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 646 | if (process.argv.indexOf('no-validation') > -1) { |
| 647 | console.warn('WARNING: Skipping validation!') |
| 648 | cb() |
| 649 | } else { |
| 650 | cb(validateTimezoneBoundaries()) |
| 651 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 652 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 653 | addOceans: ['validateZones', function (results, cb) { |
| 654 | addOceans(cb) |
| 655 | }], |
| 656 | mergeZones: ['addOceans', function (results, cb) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 657 | console.log('merge zones') |
| 658 | combineAndWriteZones(cb) |
| 659 | }], |
| 660 | zipGeoJson: ['mergeZones', function (results, cb) { |
| 661 | console.log('zip geojson') |
| 662 | exec('zip dist/timezones.geojson.zip dist/combined.json', cb) |
| 663 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 664 | zipGeoJsonWithOceans: ['mergeZones', function (results, cb) { |
| 665 | console.log('zip geojson with oceans') |
| 666 | exec('zip dist/timezones-with-oceans.geojson.zip dist/combined-with-oceans.json', cb) |
| 667 | }], |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 668 | makeShapefile: ['mergeZones', function (results, cb) { |
| 669 | console.log('convert from geojson to shapefile') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 670 | rimraf.sync('dist/combined-shapefile.*') |
| 671 | exec( |
evansiroky | e3360f7 | 2018-11-16 09:24:26 -0800 | [diff] [blame] | 672 | 'ogr2ogr -f "ESRI Shapefile" dist/combined-shapefile.shp dist/combined.json', |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 673 | function (err, stdout, stderr) { |
| 674 | if (err) { return cb(err) } |
| 675 | exec('zip dist/timezones.shapefile.zip dist/combined-shapefile.*', cb) |
| 676 | } |
| 677 | ) |
| 678 | }], |
| 679 | makeShapefileWithOceans: ['mergeZones', function (results, cb) { |
| 680 | console.log('convert from geojson with oceans to shapefile') |
| 681 | rimraf.sync('dist/combined-shapefile-with-oceans.*') |
| 682 | exec( |
evansiroky | e3360f7 | 2018-11-16 09:24:26 -0800 | [diff] [blame] | 683 | 'ogr2ogr -f "ESRI Shapefile" dist/combined-shapefile-with-oceans.shp dist/combined-with-oceans.json', |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 684 | function (err, stdout, stderr) { |
| 685 | if (err) { return cb(err) } |
| 686 | exec('zip dist/timezones-with-oceans.shapefile.zip dist/combined-shapefile-with-oceans.*', cb) |
| 687 | } |
| 688 | ) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 689 | }] |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 690 | }, function (err, results) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 691 | console.log('done') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 692 | if (err) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 693 | console.log('error!', err) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 694 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 695 | }) |