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