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