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