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